Skip to content

Instantly share code, notes, and snippets.

@acook
acook / keypress.rb
Created December 2, 2012 18:42
Read keypresses from user in terminal, including arrow keys using pure Ruby. This has since been folded into a much more robust gem called Remedy. https://rubygems.org/gems/remedy & https://github.com/acook/remedy
require 'io/console'
# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
STDIN.echo = false
STDIN.raw!
input = STDIN.getc.chr
if input == "\e" then
input << STDIN.read_nonblock(3) rescue nil
@acook
acook / _Solus Workarounds.md
Last active October 16, 2023 01:23
Solus Linux workarounds and tutorials. Please let me know if they break or you found a better way.

Solus Workarounds

Solus Linux looks good out of the box and has some good features but lacks the integrations and support of other distros.

Here are some workarounds. Please let me know if they break or you found a better way.

@acook
acook / 0_about.markdown
Last active December 23, 2021 05:34
Startup speeds of various interpreters, source: https://github.com/acook/impel/blob/master/spec/benchmark_spec.rb#L44

What is this?

This benchmark establishes baseline the cost-of-doing-business for using a given interpreted language. It gauges the initialization/teardown overhead before/after the interpreter can actually do any work. Each program is the nearest equivalent of a single exit(0) command as possible. This is not a general-purpose benchmark, but it's interesting to see how different interpreters behave. The below tests were all run on the same CPU running the noop programs for 200 iterations.

Why?

The original impetus was for a project called impel which was going to be executed in the $PS1, $PROMPT_COMMAND, precmd, or equivalent where I wanted to avoid adding latency to the command prompt but was ambivalent about the language it was written in. There are certainly other concerns to be had beyond this overhead, it's just one of many things to take into consideration.

@acook
acook / termsize.rb
Created December 2, 2012 17:31
Getting the terminal size in Ruby
# via http://www.megasolutions.net/ruby/Getting-the-size-of-the-terminal-in-a-portable-way-26006.aspx
TIOCGWINSZ = 0x40087468
def get_winsize
str = [0, 0, 0, 0].pack('SSSS')
if STDIN.ioctl(TIOCGWINSZ, str) >= 0
rows, cols, xpixels, ypixels = str.unpack("SSSS")
p rows, cols, xpixels, ypixels
else
puts "Unable to get window size"
@acook
acook / master_to_main
Created February 25, 2021 05:34
Rename local branch from master to main using git commandline
git branch -m master main
git fetch origin
git branch -u origin/main main
package main
import (
"fmt"
"os"
)
func main() {
// go complains: "use of builtin recover not in function call"
//defer cleanup_broken()
@acook
acook / introspection.rb
Created September 2, 2011 22:20
Does a deep inspection of Ruby objects.
#
# You can do `Object.send :include, Introspection`
# Then you can call `.introspect` on anything.
#
# The `introspect` method returns a hash with information about the receiver
# including its Class, the Constants and instance_methods defined inside it.
#
# The "depth" parameter indicates how deeply it should inspect the object.
# By default there is no depth limit. Pass in 1 to just get info on the object itself.
#
class Potato
def initialize weight
@weight = weight
end
end
def self.Potato *weight
Potato.new weight.first || 1
end
#!/usr/bin/env ruby
class Passwerd
CHARSETS = {
lower: (?a..?z).to_a,
upper: (?A..?Z).to_a,
number: (?0..?9).to_a,
space: [' '],
sym1: (?!..?/).to_a,
sym2: (?:..?@).to_a,
firstyear
=> "1885-1886"
lastyear
=> "1884-1885"
firstyear > lastyear
=> false
firstyear.bytes
=> [49, 56, 56, 53, 45, 49, 56, 56, 54]