Skip to content

Instantly share code, notes, and snippets.

View cameronp98's full-sized avatar

Cameron Phillips cameronp98

  • Nottingham / Leeds
View GitHub Profile
@cameronp98
cameronp98 / ActionButton.js
Created December 27, 2021 15:35
Action button (for use with e.g tailwindcss)
import React, { useState } from 'react'
const ActionButton = ({ action }) => {
const [btnState, setBtnState] = useState('idle')
const doLoad = () => {
if (btnState === 'idle') {
setBtnState('loading')
action().then(() => {
setBtnState('done')
import React from 'react'
import Button from './components/Button'
const App = () => {
const subscribe = () => {
// ...
}
return <Button onClick={() => subscribe}>Subscribe</Button>
}
@cameronp98
cameronp98 / remake_venv.sh
Created December 19, 2021 19:38
Remake a venv after rebuilding python
#!/bin/bash
pip freeze > requirements.txt
deactivate
rm -rf ./venv
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
@cameronp98
cameronp98 / collage.py
Last active December 16, 2021 16:11
Create a collage of 4 random images from the unsplash api
import aiohttp
import asyncio
import aiofiles
import io
from PIL import Image
# put your access key here or change "us_api_url" to omit it
ACCESS_KEY = "_"
@cameronp98
cameronp98 / loader.rs
Last active April 7, 2021 11:43
Rust console loading percentage for iterator
use std::io::{self, prelude::*};
use std::thread;
use std::time::Duration;
struct Loader<I> {
iter: I,
len: usize,
progress: usize,
}
@cameronp98
cameronp98 / CopyInput.js
Last active May 29, 2020 18:15
Copy Input, requires semantic-ui-react semantic-ui-css
import React from 'react'
import { Button, Input } from 'semantic-ui-react'
function CopyInput({ value, label }) {
const input = React.createRef()
function handleClick() {
input.current.select()
// input.current.setSelectionRange(0, 99999)
document.execCommand('copy')
@cameronp98
cameronp98 / asyncget.js
Last active May 31, 2020 12:38
Async get for component data using axios and React Hooks
import axios from 'axios'
import { useEffect, useState } from 'react'
export default function(url) {
const [state, setState] = useState({ status: 'pending' })
useEffect(() => {
axios
.get(url)
.then((res) => {
@cameronp98
cameronp98 / copy-button.js
Created May 24, 2020 20:07
React button to copy text to clipboard using a hidden input field
import React from 'react'
function CopyButton({ value }) {
const hiddenInput = React.createRef()
function handleClick() {
hiddenInput.current.select()
hiddenInput.current.setSelectionRange(0, 99999)
document.execCommand('copy')
console.log(`copied '${value}'`)
@cameronp98
cameronp98 / promise-loader-button.js
Last active May 24, 2020 17:34
Semantic UI promise loader button
import React, { useState } from 'react'
import { Button } from 'semantic-ui-react'
// action returns a promise after the resolution of which `loading` is set to false
const PromiseButton = ({ action, children, ...props }) => {
const [loading, setLoading] = useState(false)
const handleClick = () => {
setLoading(true)
action().then(() => {
@cameronp98
cameronp98 / permutations.rs
Last active May 4, 2020 08:31
not permutations but combinations, cba to change
fn perms<T: Clone>(values: &[T], perm_len: usize) -> Vec<Vec<T>> {
let mut out = Vec::new();
perms_inner(&mut out, &values, Vec::with_capacity(perm_len), perm_len);
return out;
}
fn perms_inner<T: Clone>(out: &mut Vec<Vec<T>>, values: &[T], perm: Vec<T>, perm_len: usize) {
if perm.len() == perm_len {
out.push(perm);
return;