Skip to content

Instantly share code, notes, and snippets.

@vidarh
vidarh / terrain.rb
Created February 25, 2024 18:27
Stupidly basic and ugly terrain generation for DragonRuby
def tick args
args.outputs.background_color = [0,0,0]
args.outputs.labels << {x: 20, y: 700, text: "Landscape Test", r: 255, g: 255, b: 255 }
args.pixel_array(:col).width = 1
args.pixel_array(:col).height = 200
px = args.pixel_array(:col).pixels
200.times do |y|
y = y.abs.floor & 0xff
col = y << 8
@vidarh
vidarh / stateexample.rb
Created January 22, 2024 05:29
Trivial state machine example
module StateMachine
def self.included(base) = base.extend(ClassMethods)
def transition_to(to)
before = self.class.do_before_transition(state)
send(before) if before
return @state = to
end
@vidarh
vidarh / a-hairy-exploration-of-k.md
Last active January 5, 2024 11:39
A hairy exploration of k
@vidarh
vidarh / SIEVE.rb
Last active January 5, 2024 12:09
Ruby port of the SIEVE caching algorithm using a ring instead of a linked list
# See https://cachemon.github.io/SIEVE-website/blog/2023/12/17/sieve-is-simpler-than-lru/
# for a description and the Python version
# *LARGELY UNTESTED*
Node = Struct.new(:value, :visited, :prev, :next)
class SieveCache
def initialize(capacity)
@capacity = capacity
@cache = {}
@vidarh
vidarh / rubywm.rb
Created November 21, 2023 20:16
rubywm.rb - a minimalist WM using pure-x11 and based on TinyWM
# Based on TinyWM by Nick Welch
require 'X11'
dpy = X11::Display.new
root = dpy.screens.first.root
# OK, so pure-x11 *really* badly needs built in keysym lookup,
# but I don't want to add that without doing it properly, so this
@vidarh
vidarh / README.md
Created September 28, 2023 18:37
Identify relevant commands for this directory, and optionally bring up rofi|dmenu to select, and optionally run them

dir-commands

Pick and optionally run relevant commands for a directory

Vidar Hokstad vidar@hokstad.com, 2023. Licensed under the MIT license.

With no options dir-commands looks for Makefile, Rakefile, Gemfile, package.json, .git etc. in

@vidarh
vidarh / sexp.rb
Created June 8, 2023 15:17
Minimal Ruby S-expression parser w/test case showing using %(string syntax) to naturally embed s-expressions in Ruby code.
require 'strscan'
class Sexp
def initialize(str); @s = StringScanner.new(str); end
def s(e); @s.scan(e); end
def r(e); s(e) or raise "Expected #{x}"; end
def list; s("(")&&b=body and r(")") and b; end
def exp; s(/[a-zA-Z][\w_-]*/)&.to_sym||s(/[0-9]+/)&.to_i||s(/"[^"]*"/)&.[](1..-2)||list||r("exp"); end
def body; r=[]; while s(/\s*/) and !@s.eos? and !@s.check(")") and e=exp; r<<e; end; r; end
end
def sexp str; Sexp.new(str).body; end
@vidarh
vidarh / loader.rb
Created November 10, 2021 15:05
Prototype loader for GtkSourceView themes into Rouge
#
# # Theme loader
#
# Tries to load different types of themes
#
require 'nokogiri'
module ThemeLoader
include Rouge::Token::Tokens
@vidarh
vidarh / main.rb
Created March 28, 2020 11:42
Simple demo of gravity in Dragonruby
# Click the Run game! button to execute the code.
def vector_add(v1,v2)
v1.zip(v2).map{|p1,p2| p1+p2}
end
# For demonstration purposes, we just blanket apply a terminal velocity.
# Set this low to see the effect.
def resistance(v, gravity)
# This only applies air resistance downwards, which is lazy
@vidarh
vidarh / shunting.rb
Created March 18, 2018 15:26
Variant of code from article on operator precedence parser from http://hokstad.com/operator-precedence-parser
require 'pp'
Oper = Struct.new(:pri,:sym,:type)
class TreeOutput
def initialize
@vstack = []
end
def oper o