Skip to content

Instantly share code, notes, and snippets.

@mschuerig
mschuerig / git-amnesia.rb
Last active January 31, 2022 10:13
What did I do?
#! /usr/bin/ruby -w
require 'date'
require 'find'
# TODO
# - make work with -p option
def main
logopts = "--color=always --since=#{Date.today - 3} #{ARGV.join(' ')}"
function reentrant(f, opts) {
opts = $.extend({catchup: false});
var state = {
running: false,
reentry: false
};
var rf = function() {
if (state.running) {
state.reentry = true;
return;
@mschuerig
mschuerig / flactags.rb
Last active August 29, 2015 14:08
List and apply FLAC file tags. For editing music metadata in a text editor.
#! /usr/bin/ruby2.1 -w
require 'digest/md5'
require 'find'
require 'taglib'
def main
case ARGV[0]
when 'list'
ARGV.shift
@mschuerig
mschuerig / try-to-sleep
Last active August 29, 2015 14:07
Server sleep scripts. Run in the evening as a cron job.
#! /bin/bash
#
# Save as /usr/local/sbin/try-to-sleep
#
# Have cron try it regularly.
# /etc/cron.d/try-to-sleep:
# 12,27,42,57 22-23,0-5 * * * root /usr/local/sbin/try-to-sleep
#
@mschuerig
mschuerig / exclaim-errors
Created July 18, 2014 14:50
Print errors logged by journald. Regularly call from cron.
#! /bin/sh -e
STATE=/var/local/exclaim-errors.state
now=$( date +'%Y-%m-%d %H:%M:%S' )
since="$(head -n1 $STATE 2>/dev/null || echo 2000-01-01)"
echo "$now" > $STATE
journalctl --priority=err --since="$since" | tail -n+2
@mschuerig
mschuerig / yo_completion
Created July 18, 2014 08:14
Yeoman yo bash completion for generators
_yo() {
local cur prev words cword generators
_get_comp_words_by_ref -n : cur prev words cword
generators=$( \
yo --help \
| sed -n '/Please choose a generator below./{:a;n;/-----------------------------------------/b;p;ba}' \
| sed -n '/^ /p' \
)
@mschuerig
mschuerig / docker-enter
Last active February 8, 2017 00:50
Enter a running docker container
#! /bin/sh -e
# See
# http://blog.docker.com/2014/06/why-you-dont-need-to-run-sshd-in-docker/
# https://github.com/jpetazzo/nsenter
case "$1" in
-h|--help)
echo "Usage: docker-enter CONTAINER"
exit 0
@mschuerig
mschuerig / mpd-announce
Last active August 29, 2015 14:02
Announce (speak) the song currently playing in MPD
#! /bin/sh -e
# Requires https://github.com/profh/romanic
# gem install romanic
device="default:CARD=X20"
lock="/run/lock/mpd-announce"
dotlockfile -r 0 -p "$lock" || exit 0
trap "dotlockfile -p -u \"$lock\"" EXIT TERM INT
@mschuerig
mschuerig / snapper completion
Last active August 29, 2015 13:56
snapper bash completion
__snappercomp() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $( compgen -W "$1" -- "$cur" ) )
}
# From http://github.com/jweslley/rails_completion
# @param $1 Name of variable to return result to
# @param $2 Command list
__snappercmd() {
any_command=$(echo $2 | sed -e 's/[[:space:]]/|/g')
@mschuerig
mschuerig / hash_bench.rb
Created December 11, 2013 00:35
Measure the times it takes to access (hit and miss) elements in hashes with up to 2^21 elements.
require 'benchmark'
require 'ascii_charts'
def measure
GC.disable
Benchmark.realtime do
yield
end
ensure
GC.enable