Skip to content

Instantly share code, notes, and snippets.

@jackwillis
jackwillis / app.rb
Last active April 18, 2024 01:07
Sinatra scripts
require 'sinatra'
get '/' do
'Hello from root'
end
get '/foo' do
'Hello from GET /foo'
end
if status is-interactive
# Commands to run in interactive sessions can go here
end
status --is-login; and status --is-interactive; and exec byobu-launcher
function rainbowify
# Define a set of RGB colors for the rainbow
set colors '255;0;0' '255;69;0' '255;140;0' '255;165;0' '255;215;0' '255;255;0' '218;255;0' '140;255;0' '69;255;0' '0;255;0' '0;255;69' '0;255;140' '0;255;215' '0;255;255' '0;215;255' '0;140;255' '0;69;255' '0;0;255' '69;0;255' '140;0;255' '215;0;255' '255;0;255' '255;0;215' '255;0;140' '255;0;69'
set colored_text ''
set char_index 1
@jackwillis
jackwillis / gsm7.rb
Last active October 7, 2018 20:51
GSM 03.38 basic character set (“GSM-7”) in Ruby
require 'set'
module GSM7
# https://en.wikipedia.org/wiki/GSM_03.38#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_/_GSM_03.38
BASIC_CHARACTER_SET = Set.new([
"\n", "\r", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "¡", "£", "¤", "¥", "§", "¿", "Ä", "Å", "Æ", "Ç", "É", "Ñ", "Ö", "Ø", "Ü", "ß", "à", "ä", "å", "æ", "è", "é", "ì", "ñ", "ò", "ö", "ø", "ù", "ü", "Γ", "Δ", "Θ", "Λ", "Ξ", "Π", "Σ", "Φ", "Ψ", "Ω"
]).freeze
def self.basic_characters_only?(string)
Set.new(string.chars).subset?(BASIC_CHARACTER_SET)
@jackwillis
jackwillis / xheight_ratio.rs
Last active June 5, 2018 03:53
Measuring a font's x-height/cap-height ratio using rusttype
extern crate num_rational; // 0.1
extern crate rusttype; // 0.6
use num_rational::Ratio;
use rusttype::{Font, FontCollection, Glyph, Rect};
fn main() {
let filename = "C:\\WINDOWS\\Fonts\\Tahoma.ttf";
let font = read_font_from_filename(filename);
@jackwillis
jackwillis / katex.rb
Last active February 1, 2019 12:41
Server-side KaTeX rendering for Jekyll
require 'execjs'
module Katex
class << self
JS_FILENAME = 'vendor/katex/katex.min.js'
JS_CTX = ::ExecJS.compile(File.read(JS_FILENAME))
INLINE_REGEX = /\\\((.*?)\\\)/m.freeze
DISPLAY_REGEX = /\\\[(.*?)\\\]/m.freeze
@jackwillis
jackwillis / Main.hs
Last active October 19, 2017 18:24
OL-Systems in Haskell
module Main where
import OLSystem (OLSystem, buildSystem, stepSystem, axiom)
import Data.Sequence (iterateN)
main = do
let algae = buildSystem "A" [ ('A', "AB"), ('B', "A") ]
print $ axiom <$> iterateN 7 stepSystem algae
-- ["A", "AB", "ABA", "ABAAB", "ABAABABA", "ABAABABAABAAB", "ABAABABAABAABABAABABA"]
@jackwillis
jackwillis / flower_external_anatomy.hs
Last active September 14, 2017 17:47
External anatomy of a flower using algebraic data types
module Flower where
-- there's probably mistakes in here, also it is not a serious model of the flower
data Pedicel = Pedicel
data Tepal = Tepal
data Sepal = Sepal
data Petal = Petal
data Perigonium = Perigonium [Tepal]
@jackwillis
jackwillis / fold.hs
Last active October 19, 2017 18:33
How to correctly write left fold in Haskell
{-# LANGUAGE NoMonomorphismRestriction #-}
-- pull in the necessary modules
import Control.Cond (if')
import Control.Monad (ap)
import Control.Monad.Fix (fix)
import Data.Foldable (null)
-- fold over a list
fold :: (a -> b -> a) -> a -> [b] -> a
@jackwillis
jackwillis / RPGPrompt.cabal
Last active July 31, 2017 10:33
RPG-style text prompt
name: RPGPrompt
version: 0.1.0.0
synopsis: RPG-style text prompt
author: Jack Willis
maintainer: jack@attac.us
license: MIT
build-type: Simple
cabal-version: >=1.10
executable RPGPrompt
@jackwillis
jackwillis / NaiveTime.hs
Created July 28, 2017 01:52
NaiveTime in Haskell
import Data.Monoid
data NaiveTime = NT Int Int deriving (Show, Eq)
instance Monoid NaiveTime where
mempty = NT 0 0
mappend (NT h m) (NT h' m') = NT h'' m''
where h'' = (h + h' + ((m + m') `quot` 60)) `mod` 24
m'' = (m + m') `mod` 60