Skip to content

Instantly share code, notes, and snippets.

View aasmith's full-sized avatar

Andrew A Smith aasmith

View GitHub Profile
@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 / 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 <[email protected]>
# (C) 2010. Licensed on GPLv3"
# modified by jeffrey paul <[email protected]>
# 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 / 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 / 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 / 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 / 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

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 / 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
irb(main):002:0> Dir.glob("../x/*") { |x| `mv #{x} .`; puts "Added #{x}"; `time derp`; }
Added ../x/actionpack-3.2.20.gemspec
0.07 real 0.05 user 0.01 sys
Added ../x/activemodel-3.2.20.gemspec
0.06 real 0.05 user 0.01 sys
Added ../x/activerecord-3.2.20.gemspec
0.06 real 0.05 user 0.01 sys
Added ../x/activeresource-3.2.20.gemspec
0.07 real 0.05 user 0.01 sys
Added ../x/activesupport-3.2.20.gemspec
irb(main):002:0> Dir.glob("../x/*").shuffle.each { |x| `mv #{x} .`; puts "Added #{x}"; `time foo`; }
Added ../x/sexp_processor-4.4.1.gemspec
0.10 real 0.08 user 0.01 sys
Added ../x/diff-lcs-1.2.4.gemspec
0.10 real 0.08 user 0.01 sys
Added ../x/wirble-0.1.3.gemspec
0.10 real 0.08 user 0.01 sys
Added ../x/byebug-2.7.0.gemspec
0.10 real 0.08 user 0.01 sys
Added ../x/ruby-progressbar-1.2.0.gemspec