Skip to content

Instantly share code, notes, and snippets.

View HalCanary's full-sized avatar

Hal Canary HalCanary

View GitHub Profile
@HalCanary
HalCanary / makesvg.py
Last active June 7, 2023 12:59
makesvg.py
#! /usr/bin/env python3
'''
Making SVGs by hand is a pain. This is a better way:
It's functional, rather than using the XML "markup language" directly.
No closing tags, just balanced parentheses!
#xml #svg #python #python3 #graphicsprogramming
https://twitter.com/halcanary/status/1458802118136111107
Updated to use `xml.dom.minidom` instead of `lxml`.
@HalCanary
HalCanary / iterator_example.cpp
Created November 5, 2021 14:58
Example of c++17ish Iterator pattern.
#include <cstdio>
using Bar = int;
class Foo {
public:
struct Iter {
Iter& operator++() { ++ptr; return *this; }
bool operator!=(Iter const& other) const { return ptr != other.ptr; }
Bar& operator*() { return *ptr; }
@HalCanary
HalCanary / disc_useage_darwin.sh
Created September 24, 2021 14:07
disc_useage_darwin.sh
#!/bin/sh
INFO="$(diskutil ap list disk1)"
printf 'size: %14d\n' "$(printf %s "$INFO" | sed -n 's/ *Size (Capacity Ceiling): *\([0-9]*\).*/\1/p')"
printf 'use: %14d\n' "$(printf %s "$INFO" | sed -n 's/ *Capacity In Use By Volumes: *\([0-9]*\).*/\1/p')"
@HalCanary
HalCanary / cpufrequency.c
Last active August 20, 2021 16:39
cpufrequency.c
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysctl.h>
int main() {
static const char* NAMES[3] = {
"hw.cpufrequency",
from __future__ import division
def division(n, d):
'''
find integers q, r such that
d * q + r = n
0 <= r < d
'''
assert n >= 0
assert d > 0
q, r = 0, n
@HalCanary
HalCanary / p.md
Created July 16, 2021 13:42
C++ Protobuff proposal:

C++ Protobuff proposal:

Given the following proto file:

enum Key {
    Unknown = 0;
    KeyOne = 1;
    KeyTwo = 2;
}

message KeyValue {

@HalCanary
HalCanary / xcode_command_line_remote_update.md
Last active June 29, 2021 22:05
Update Xcode from command line remotely:

Update Xcode from the command line remotely

Local:

download Xcode_XX.XX.XX.xip from https://developer.apple.com/download/all/.

scp "$(ls -t ~/Downloads/Xcode_*.xip | head -1)" USER@REMOTE:
ssh USER@REMOTE
@HalCanary
HalCanary / resize_mac_vertical.osascript
Last active June 7, 2021 13:12
MacOS: Vertically Maximize All Open Windows
#! /usr/bin/env osascript
## Vertically maximize all open windows.
tell application "Finder" to set BNDS to (bounds of window of desktop)
set DESKTOP_HEIGHT to item 4 of BNDS
tell application "System Events" to repeat with P in application processes whose background only is false
tell application "System Events" to tell P
repeat with WIN in windows of P
@HalCanary
HalCanary / do_or_fail.h
Created June 1, 2021 13:44
do_or_fail.h
#ifndef DO_OR_FAIL_H
#define DO_OR_FAIL_H
extern void fail_now(const char* file, int line, const char* msg, int err);
#define DO_OR_FAIL(SUCCESS, MSG) \
do { if (!(SUCCESS)) { fail_now(__FILE__, __LINE__, (MSG), errno); } } while (false)
#endif // DO_OR_FAIL_H
@HalCanary
HalCanary / wide_ruled_paper.ps
Last active May 17, 2021 14:06
wide_ruled_paper.ps
%!PS
% wide ruled letter paper
/inch { 72 mul } def
/page_width 8.5 inch def
/page_height 11 inch def
/line_space 0.34375 inch def
/margin 1.25 inch def
<< /PageSize [page_width page_height] >> setpagedevice