Skip to content

Instantly share code, notes, and snippets.

View aasmith's full-sized avatar

Andrew A Smith aasmith

View GitHub Profile
@aasmith
aasmith / hibernate.sh
Created August 24, 2014 18:13
Power down with a one-time forced hibernate in OS X
# Before using this script, see output of pmset -g and note the value
# of hibernatemode.
pmset -g
# Save to disk, per pmset(1):
#
# hibernatemode = 25 (binary 0001 1001) is only settable via pmset. The
# system will store a copy of memory to persistent storage (the disk), and
# will remove power to memory. The system will restore from disk image.
sudo pmset -a hibernatemode 25

Keybase proof

I hereby claim:

  • I am aasmith on github.
  • I am aasmith (https://keybase.io/aasmith) on keybase.
  • I have a public key whose fingerprint is 6CDC 81AC 6916 8916 C232 DCCE 6F0B 0A1B D764 A6B5

To claim this, I am signing this object:

@aasmith
aasmith / eachdir.rb
Last active August 29, 2015 14:04
eachdir - Run a command in each child directory.
#!/usr/bin/env ruby -w
# eachdir - Run a command in each child directory.
abort "need command" unless ARGV[0]
abort "quote the command" unless ARGV.size == 1
def banner(path)
dirname, basename = File.split path
puts
@aasmith
aasmith / write-image.sh
Last active November 1, 2021 03:20
Writing an image to a USB drive is somewhat convoluted in OS X...
# Insert drive.
# See what drives are available.
diskutil list
# Unmount the target drive.
diskutil unmountDisk /dev/disk1
# OPTIONAL step to convert ISO to usb compatible image (ubuntu ISOs require this)
hdiutil convert -format UDRW -o ~/path/to/target.img ~/path/to/ubuntu.iso
@aasmith
aasmith / hpack-integer.rb
Last active December 21, 2015 09:59
HPACK Integer [de]compression
# encode int i onto n bits
def encode(i, n)
a = []
i = i.to_i
# Max number that can be stored on initial bits
max = 2**n - 1
if i < max
a << i
@aasmith
aasmith / lol.rb
Created April 29, 2013 03:17
An even more "exciting" way to define configuration-style class methods!
def lol(n)
Module.new { define_method(:hoho) { n } }
end
class Bar
extend lol 123
end
Bar.hoho # => 123
@aasmith
aasmith / flac2alac.sh
Last active August 9, 2024 13:47
A minimally-modified version of flac2alac for OS X.
#!/bin/bash
# flac2alac By Arlindo \"Nighto\" Pereira <nighto@nighto.net>
# (C) 2010. Licensed on GPLv3"
# modified by jeffrey paul <sneak@datavibe.net>
# inspiration from http://ca.ubuntuforums.org/member.php?u=6176 (MetalMusicAddict)
# script at http://ca.ubuntuforums.org/showthread.php?t=889700&page=2
@aasmith
aasmith / monitor_power.rb
Created April 4, 2013 04:29
Determine if the screen is currently in power saving mode on OS X. This will probably break sometime after 10.8.
# Get the possible max power state and current power states for the device.
# If they all match (i.e. the current state is the same as the max state),
# then the screen is assumed to be on.
power_management = `ioreg -n IODisplayWrangler |grep -i IOPowerManagement`
power_states = power_management.scan(/(?:"\w+PowerState"=(\d))+/)
abort "Not enough power states!" if power_states.size < 2
screen_on = power_states.uniq.size == 1
@aasmith
aasmith / goof.rb
Last active December 10, 2015 13:08
Goofspiel!
# Generate all possible combinations for a 46-point win in the game of
# Goofspiel.
#
cards = [*1..13]
res = [*4..13].map do |n|
cards.combination(n).
select { |cards| cards.reduce(:+) == 46 }.
map(&:sort).
map(&:reverse)
@aasmith
aasmith / monitor-off.rbw
Last active October 19, 2020 02:58
Turns off all monitors in Windows using the Win32 API.
# Send all monitors to power saving mode (standby) using the WIN32 API.
#
# Documentation for WM_SYSCOMMAND & SC_MONITORPOWER:
# http://msdn.microsoft.com/en-us/library/windows/desktop/ms646360(v=vs.85).aspx
require 'fiddle'
require 'fiddle/import'
require 'fiddle/types'
module User32
extend Fiddle::Importer