Skip to content

Instantly share code, notes, and snippets.

View davidsharp's full-sized avatar

David Sharp davidsharp

View GitHub Profile
{ // must be inside our own scope here so that when we are unloaded everything disappears
// we also define functions using 'let fn = function() {..}' for the same reason. function decls are global
let drawTimeout;
// Actually draw the watch face
let draw = function() {
var x = g.getWidth() / 2;
var y = g.getHeight() / 2;
g.reset().clearRect(Bangle.appRect); // clear whole background (w/o widgets)
g.setColor(0.2,0.2,1);
@davidsharp
davidsharp / leibniz.js
Created December 31, 2023 15:41
pi approximation in JS
const approximate_pi = n => {
let pi = 0
let denom = 1
for(let i = 0;i<n;i++){
pi += (i%2?-4:4)/denom
denom += 2
}
return pi
}
@davidsharp
davidsharp / circDepReplacer.js
Created November 22, 2023 09:58
A quick little little circular dependancy remover for JSON.stringify
function circDepReplacer(k,v){return k&&v==this?null:v}
// used like JSON.stringify(obj,circDepReplacer)
// only replaces references to main object, not sub-objects
@davidsharp
davidsharp / bluetooth.10s.sh
Last active November 15, 2023 15:45 — forked from ieatfood/Connect Airpods.applescript
A xbar/bitbar wrapper around an Applescript to connect bluetooth devices, such as Airpods.
#!/bin/bash
function pair(){
osascript <<'END'
use framework "IOBluetooth"
use scripting additions
set blueToothDevice to "Buds Pro"
on getFirstMatchingDevice(deviceName)
@davidsharp
davidsharp / getBytes.js
Created April 6, 2023 10:36
turns a byte count into an object which can be used to compose KB/MB/GB/TB/PB values
const getBytes = (bytes,{unit=null,binary=false,toFixed=1} = {}) => {
const divisor = binary?1024:1000;
let value = bytes;
let sizeLevel=-1;
const sizeArray=['KB','MB','GB','TB','PB'];
if(unit){
sizeLevel=sizeArray.indexOf(unit);
value=(bytes/Math.pow(divisor,sizeLevel+1)).toFixed(toFixed)
}
else while(value>=divisor){
@davidsharp
davidsharp / kofi-widget.jsx
Created March 20, 2023 12:27
A Preact component for displaying Ko-fi embed
import {useEffect} from 'preact/hooks';
export default function Kofi({name='davidsharp',text='Tip Me',backgroundColor='#fcbf47',textColor='#323842'}){
const widgetScript = (`console.log('Donate @ ko-fi.com/${name}')
kofiWidgetOverlay.draw('${name}', {
'type': 'floating-chat',
'floating-chat.donateButton.text': '${text}',
'floating-chat.donateButton.background-color': '${backgroundColor}',
'floating-chat.donateButton.text-color': '${textColor}'
});`)
@davidsharp
davidsharp / nvm-symlink.sh
Created July 12, 2022 16:14
symlinking the node binary with whatever nvm has set it as
@davidsharp
davidsharp / autolink.jsx
Created June 24, 2022 09:08
Ever so slightly shorter version of the Autolink component found in 30 Seconds of Knowledge
@davidsharp
davidsharp / force-xhr-status-with-proxy.js
Created April 22, 2022 10:27
Proxying a XMLHttpRequest to force the status received
XMLHttpRequest = new Proxy(XMLHttpRequest, {
construct:function(t,a){
const req = new t();
return new Proxy(req, {
get:function(o,p){
if(p=='status')return 9001
return typeof o[p] == 'function'?o[p].bind(o):o[p]
},
set: function(target, prop, value) {
Reflect.set(target, prop, value) // or target[prop] = value
@davidsharp
davidsharp / rot13.js
Created February 19, 2022 17:10
Dirty little ROT13 function
const rot13 = str => str.replace(/[a-z]/gi,x=>{
const c=x.charCodeAt(0)
const a=c>=97?97:65
return String.fromCharCode(
a+((c-a+13)%26)
)
})