Skip to content

Instantly share code, notes, and snippets.

View HalCanary's full-sized avatar

Hal Canary HalCanary

View GitHub Profile
@HalCanary
HalCanary / get_battery_health.m
Created March 19, 2023 14:34
get_battery_health.m
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/NSObjCRuntime.h>
#include <IOKit/ps/IOPSKeys.h>
#include <IOKit/ps/IOPowerSources.h>
#include <assert.h>
int main() {
CFTypeRef psInfo = IOPSCopyPowerSourcesInfo();
assert(psInfo != NULL);
CFArrayRef list = IOPSCopyPowerSourcesList(psInfo);
assert(list != NULL);
@HalCanary
HalCanary / animate.py
Created December 23, 2022 19:13
animate.py
#! /usr/bin/env python3
import matplotlib.pyplot as plt
import matplotlib.animation as anim
import numpy as np
x = np.arange(0,2*(np.pi),.01)
y, z = 0 * x, 0 * x
fig = plt.figure()
ax = plt.axes(xlim=(0, 2*np.pi), ylim=(-2, 2))
yline, = ax.plot([], [])
zline, = ax.plot([], [])
@HalCanary
HalCanary / rest.py
Created November 21, 2022 14:44
rest.py
import urllib.request
import urllib.parse
import json
################################################################################
def Get(auth, host, path, query={}):
q = urllib.parse.urlencode(query)
req = urllib.request.Request(
f"https://{host}{path}?{q}",
@HalCanary
HalCanary / gist:cb51e7fc5128f5762491e748c3827efa
Last active November 17, 2022 15:21
Mastodon Status Poster Command Line Program
@HalCanary
HalCanary / example.cpp
Last active October 9, 2022 15:33
enum X Macro
///////////////////////////////////////////////
// header
#include <string_view>
#define RAINBOW(X) \
X(Red) \
X(Orange) \
X(Yellow) \
X(Green) \
@HalCanary
HalCanary / html_generator_example.go
Last active November 14, 2023 13:20
html_generator_example.go
// Go Playground: https://go.dev/play/p/ao7M54vTDRQ
package main
import (
"os"
"strings"
"golang.org/x/net/html"
)
@HalCanary
HalCanary / pre-commit.sh
Created May 20, 2022 13:02
git_hooks/pre-commit for checking gofmt (portable Posix shell version)
#! /bin/sh
if command -v gofmt > /dev/null 2>&1; then
BAD=''
while IFS= read -r f; do
if echo "$f" | grep -q '\.go$'; then
HASH="$(git ls-files --stage -- "$f" | awk '{print $2}')"
if [ "$(git show "$HASH" | gofmt -l 2>&1)" ]; then
if [ -z "$BAD" ]; then
BAD='Y'
@HalCanary
HalCanary / pre-commit.zsh
Created May 20, 2022 11:58
git_hooks/pre-commit for checking gofmt.
#! /bin/zsh
if command -v gofmt > /dev/null 2>&1; then
BAD=()
git diff --cached --name-only --diff-filter=AM | while IFS= read -r f; do
if echo "$f" | grep -q '\.go$'; then
HASH="$(git ls-files --stage -- "$f" | awk '{print $2}')"
if [ "$(git show "$HASH" | gofmt -l 2>&1)" ]; then
BAD+=(${f})
fi
@HalCanary
HalCanary / StrdupNS.m
Last active March 31, 2022 23:26
strdup NSString to C string
#import <Foundation/Foundation.h>
#include <stdlib.h>
static inline char* strdupNS(NSString* nss) {
static const NSStringEncoding enc = NSUTF8StringEncoding;
NSUInteger len = [nss lengthOfBytesUsingEncoding:enc];
if (len <= 0) {
return NULL;
}
char* cstr = malloc((size_t)(1 + len));
if (![nss getCString:cstr maxLength:(1 + len) encoding:enc]) {
@HalCanary
HalCanary / send-email.py
Last active January 13, 2022 15:06
send-email
#!/usr/bin/env python3
'''Usage:
{} TO_ADDR SUBJECT CONTENT_TEXT_FILE_PATH [ATTACHMENTS]
'''
import email.message
import email.utils
import os
import smtplib