Skip to content

Instantly share code, notes, and snippets.

View riddochc's full-sized avatar

Chris Riddoch riddochc

View GitHub Profile
(defn truncate-safely-to [s bytelimit]
"Truncate a UTF-8 string to the longest valid UTF-8 string less than bytelimit bytes"
(if (< bytelimit 1) "" ; empty string
(let [utf8 (.getBytes s "UTF-8")
eos (- bytelimit 1)
idx (if (<= (alength utf8) bytelimit)
bytelimit
(if (= 0 (bit-and (aget utf8 eos) 0x80))
eos
(first ;; get the character
@riddochc
riddochc / make-playlist.rb
Created August 1, 2014 21:46
Make playlist file for VLC from all MP4s below current directory, uses tagreader from taglib
#!/usr/bin/env ruby
require 'nokogiri'
require 'open3'
Track = Struct.new(:fullpath, :duration, :seqnum)
tracks = Dir.glob("**/*.mp4").map.with_index {|file, i|
output = Open3.capture2e("/usr/bin/tagreader", file) {|out| out}
if output[1].to_i != 0 # Successful return status?
@riddochc
riddochc / record.sh
Created August 23, 2011 07:15
Recording audio for transcription
#!/bin/sh
outfile=`date +"%Y-%m-%d-%H-%M-%S"`
arecord -t wav -f S16_LE -c 1 -r 22050 > ~/training/$outfile.wav
@riddochc
riddochc / worldclock.rb
Created August 23, 2011 07:11
Tools for a world clock
#!/usr/bin/env ruby
require 'time'
class Time
def convert_zone(to_zone)
original_zone = ENV["TZ"]
utc_time = dup.gmtime
ENV["TZ"] = to_zone
to_zone_time = utc_time.localtime
@riddochc
riddochc / date_string.rb
Created August 23, 2011 01:17
Express one date relative to another
require 'date'
class Date
def human_string()
d = self
today = Date.today
days_apart = (today - d).to_i
# Figure out the appropriate ordinal suffix for the n"th"
if d.mday != 11 and d.mday.to_s =~ /1$/
@riddochc
riddochc / jquery-tests.html
Created April 28, 2011 09:01
Jquery hiding form elements based on radio button selection
<html>
<head>
<title>JQuery tests</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>
<body>
<h1>JQuery tests</h1>
<form>