Skip to content

Instantly share code, notes, and snippets.

@foca
Created August 5, 2012 20:23
Show Gist options
  • Save foca/3267007 to your computer and use it in GitHub Desktop.
Save foca/3267007 to your computer and use it in GitHub Desktop.
Pretty-print Adium transcripts from the terminal. Nice to pipe to `pbcopy`
#!/usr/bin/env ruby
require "nokogiri"
require "time"
module Purple
DIR = File.expand_path("~/Library/Application\ Support/Adium\ 2.0/Users/Default/Logs")
def self.transcripts(buddy, which=-1)
files = Dir.glob(File.join(DIR, "*/#{buddy}/**/*"))
.select { |file| File.file?(file) }
.sort_by { |file| File.basename(file) }
files = files[which] unless which == :all
Array(files).map { |file| Nokogiri(File.read(file)) / "message" }.flatten
end
class Transcript
def initialize(messages)
@messages = messages
end
def pretty_print(out=$stdout)
@messages.each do |message|
out.print timestamp(message), " "
out.print nickname(message), " "
out.puts message.inner_text
end
end
def max_nickname_size
@max_nickname_size ||= @messages.map {|m| (m["alias"] || m["sender"]).size }.max
end
def nickname(message)
"<#{message["alias"] || message["sender"]}>".rjust(max_nickname_size + 2)
end
def timestamp(message)
"[" + Time.parse(message["time"]).strftime("%H:%M:%S") + "]"
end
end
end
if $0 == __FILE__
banner =<<-HELP
Usage: #{File.basename($0)} <buddy_name>
Display a nicely formatted Adium chat transcript to STDOUT. For example:
#{File.basename($0)} john.doe@gmail.com
#{File.basename($0)} someone@me.com
#{File.basename($0)} some_aim_handle
This will get the latest (or currently active) chat transcript for the selected
buddy.
HELP
abort banner if ARGV.empty?
buddy = ARGV.last
n = if ARGV.include?("--all")
:all
else
ARGV.detect { |k| k =~ /-(\d+)/ } && $1 || -1
end
files = Purple.transcripts(buddy, n)
abort "#{buddy} has no transcripts\n\n#{banner}" if files.nil? || files.empty?
Purple::Transcript.new(files).pretty_print
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment