Skip to content

Instantly share code, notes, and snippets.

@kathampy
kathampy / usize_add.rs
Created March 4, 2021 06:13
Checked add signed to usize.
fn add(u: usize, i: i32) -> Option<usize> {
if i.is_negative() {
u.checked_sub(i.wrapping_abs() as u32 as usize)
} else {
u.checked_add(i as usize)
}
}
fn main() {
let u = 7;
@kathampy
kathampy / curl.rb
Created October 25, 2019 08:02
`curl.rb` with `LibreSSL`
# --with-ssl is ignored and depends_on "libressl" doesn't work.
ENV.prepend_path "PKG_CONFIG_PATH", "#{Formula["libressl"].opt_lib}/pkgconfig"
@kathampy
kathampy / saturating_cast.rs
Last active October 23, 2019 17:19
Saturating Casts
/*
From https://github.com/rust-lang/rust/issues/23596
128-bit support
More usize & isize conversions
Lossless conversion between usize & isize <-> integers
Inlined functions
Removed self-casts and primitives with From<T> implementations
Note: usize & isize conversions valid on 32-bit & 64-bit word sizes.
@kathampy
kathampy / cloudflared.sh
Created November 15, 2018 23:09
Reinstalls cloudflared on EdgeOS after firmware upgrade
[ -e /etc/cloudflared ] || {
curl --fail https://raw.githubusercontent.com/yon2004/ubnt_cloudflared/master/cloudflared --output /usr/local/bin/cloudflared || die
chmod +x /usr/local/bin/cloudflared || die
ln -s /config/cloudflared /etc/cloudflared || die
[ -e /etc/cloudflared/config.yml ] || {
curl --fail https://raw.githubusercontent.com/yon2004/ubnt_cloudflared/master/config.yml --output /etc/cloudflared/config.yml || die
}
@kathampy
kathampy / pacproxy.sh
Created January 4, 2018 08:41
Runs an instance of pacproxy in the background which terminates with the current shell.
# Runs an instance of pacproxy in the background which terminates with the current shell.
# Requires pacproxy (https://github.com/williambailey/pacproxy/releases).
# Must be sourced in the current shell with the command ". ./pacproxy.sh".
pacproxy -c http://proxy/proxy.pac -l 127.0.0.1:3128 -v & trap 'pkill -P $$' SIGINT SIGTERM EXIT && export HTTP_PROXY=http://127.0.0.1:3128 && export HTTPS_PROXY=$HTTP_PROXY
@kathampy
kathampy / local.applescript.plist
Created November 2, 2016 11:57
Run an AppleScript from a Launch Agent.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.applescript</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
@kathampy
kathampy / .zshenv
Last active November 2, 2016 11:58
Set $PATH with path_helper for non-login shells on macOS.
if [[ ! -o login && -x /usr/libexec/path_helper ]]; then
eval `/usr/libexec/path_helper -s`
fi
@kathampy
kathampy / local.environment.plist
Last active November 2, 2016 11:51
Set environment variables on macOS.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.environment</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>setenv</string>