Skip to content

Instantly share code, notes, and snippets.

@eric-wood
Created March 28, 2012 18:45
Show Gist options
  • Save eric-wood/2229326 to your computer and use it in GitHub Desktop.
Save eric-wood/2229326 to your computer and use it in GitHub Desktop.
iTunes track information in your terminal!
# Show iTunes information in real-time in the terminal
def status
cmd = "osascript -e 'tell application \"iTunes\" to player state'"
`#{cmd}`
end
def track_name
cmd = "osascript -e 'tell application \"iTunes\" to name of current track'"
`#{cmd}`
end
def track_artist
cmd = "osascript -e 'tell application \"iTunes\" to artist of current track'"
`#{cmd}`
end
def track_album
cmd = "osascript -e 'tell application \"iTunes\" to album of current track'"
`#{cmd}`
end
# Return how far we are into the current track
def position
cmd = "osascript -e 'tell application \"iTunes\" to player position'"
pos = `#{cmd}`
pos.to_i
end
def position_string(pos)
frac = (pos.to_i)/(60.to_f)
min = frac.to_i
sec = ((frac - min)*60).ceil
min = min.to_s
sec = sec.to_s
min = ' ' + min if min.size == 1
sec = '0' + sec if sec.size == 1
"#{min}:#{sec}"
end
def track_time
cmd = "osascript -e 'tell application \"iTunes\" to time of current track'"
`#{cmd}`.to_f
end
def track_length
cmd = "osascript -e 'tell application \"iTunes\" to duration of current track'"
len = `#{cmd}`
len.to_i
end
progress_width = 30
tick_size = 0
len = 0
pos = 0
tick_char = "="
prev_track = ""
loop do
st = status.strip
if st == 'playing'
name = track_name
if name != prev_track
# Get info
artist = track_artist
album = track_album
len = track_length
pos = position
rem = (len - pos).abs
rem_string = position_string(rem)
pos_string = position_string(pos)
tick_size = len / progress_width
# Display info
print "\e[1A\e[2K"*4
puts name, artist, album
puts "#{pos_string} [#{' '*progress_width}] #{rem_string}"
else
pos = position
rem = (len - pos)
pos_string = position_string(pos)
rem_string = position_string(rem)
ticks = (pos / tick_size).floor
spaces = progress_width - ticks
print "\e[1A\e[2K"
puts "#{pos_string} [#{tick_char*ticks}#{' '*spaces}] #{rem_string}"
end
prev_track = track_name
else
prev_track = ""
end
sleep 0.1
end
@willcosgrove
Copy link

What does print "\e[1A\e[2K"*4 do? I assume it's allowing you to do the overwriting of the previous info, but what exactly does the \e[1A\e[2K voodoo mean?

@eric-wood
Copy link
Author

Those are terminal control characters; there's a whole bunch of different ones for doing all sorts of things.
"\e[1A" moves the cursor up one line (the command is "\e[nA", where n is the number of lines).
"\e[2K" clears the current line.
That's essentially how I update the progress bar without having to redraw everything.

More information on terminal control characters: http://www.termsys.demon.co.uk/vtansi.htm (note that "\e" is the same as ESC)

@willcosgrove
Copy link

Huh, that's nifty. Thanks, I didn't know about those!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment