Skip to content

Instantly share code, notes, and snippets.

@algal
algal / colorize-emacs.bashsource
Last active March 28, 2025 16:15
Setting up truecolor (24 bit color) in emacs in the terminal, under iTerm2, blink.sh, and others.
# sourcing this file will define a bash functions that
# tries to run subsequent calls to emacs with 24 bit color.
#
# It sets TERM=xterm-emacs-leg if
# - we've created a user-local terminfo record for xterm-emacs-leg, and
# - we're using iTerm2 or something has set COLORTERM=truecolor
#
# This will cause emacs to use 24 bit color only when it will work,
# inside or outside of tmux. I haven't found a way to auto-detect Blink.sh yet.
#
@algal
algal / fixssh-forwarding.el
Created February 20, 2020 01:41
Within emacs, fix ssh agent forwarding, which breaks in long-running tmux sessions
;; The purpose of this file is to define the function `fixssh-in-tmux`,
;; which attempts to fix ssh agent forwarding when it breaks within
;; a tmux session
;; from https://github.com/magnars/s.el/blob/master/s.el
(defun fixssh--s-match (regexp s &optional start)
"When the given expression matches the string, this function returns a list
of the whole matching string and a string for each matched subexpressions.
If it did not match the returned value is an empty list (nil).
@algal
algal / California-time.bash
Created October 7, 2019 05:10
print the time in California, in RFC 2822 format
#!/bin/bash
# prints California time, on macOS or Ubuntu 18
exec env TZ='America/Los_Angeles' date -R
@algal
algal / vmstatistics64.swift
Last active January 6, 2025 12:51
Get virtual memory usage on iOS or macOS
import Darwin
import Foundation
// known good: Swift 5
// runs on macOS, probably works on iOS (but haven't tried)
/// Wraps `host_statistics64`, and provides info on virtual memory
///
/// - Returns: a `vm_statistics64`, or nil if the kernel reported an error
///
//
// processFIle.swift
// LineIteratorProj
//
// Created by Alexis Gallagher on 8/14/19.
// Copyright © 2019 Sculpt Labs. All rights reserved.
//
// known-good: Swift 5
@algal
algal / SwiftLineReader.swift
Created August 14, 2019 21:25
Lazily read a line at a time in Swift
import Foundation
// known good: Swift 5
typealias LineIteratorState = (
// pointer to a C string representing a line
linePtr:UnsafeMutablePointer<CChar>?,
linecap:Int,
filePtr:UnsafeMutablePointer<FILE>?
)
@algal
algal / RangeStrider.swift
Last active April 30, 2019 03:34
Sequence of Range<Int>s
// known-good: Swift 5.0.1
/*
# how to use:
instead of:
for i in 0 ..< (n-1)/bs {
let startIdx = i * bs
@algal
algal / PileView.swift
Last active March 8, 2019 23:19
Like a simple vertical stack view. No animations. But no magical mystery meat, and it works.
//
// Pile.swift
// PileTest
//
// Created by Alexis Gallagher on 3/6/19.
// Copyright © 2019 Bespoke. All rights reserved.
//
// known-good: Swift 4.2, iOS 12
@algal
algal / Zipping.swift
Created February 17, 2019 01:18
Zip files on iOS, without using external libraries and without interoperating with the low-level Compression framework
// Zipping.swift
// known-good: Swift 4.2
// Alexis Gallagher
import Foundation
public extension URL {
/// Creates a zip archive of the file or folder represented by this URL and returns a references to the zipped file
///
@algal
algal / regex.swift
Last active February 6, 2019 09:10
Swift Regular Expression Helpers
// known-good: Swift 4.2
// expected good: Swift 5.0
import Foundation
/*
This file defines three helpers for matching regular expressions against strings, and inspecting the results of capture groups in the regular expressions.
1. `RegexMatches(ofPattern:againstString:)` provides a lazy Sequence of matches, where every match is an array representing the matches capture groups.