diff options
-rwxr-xr-x | omptagger | 134 |
1 files changed, 91 insertions, 43 deletions
@@ -20,6 +20,46 @@ require 'rubygems' require 'TagLib' require 'filemagic' +class String + def colourise(colour) + case colour + when :green + code = '32' + when :yellow + code = '33' + when :cyan + code = '36' + else + return self + end + + return "\e[#{code}m#{self}\e[0m" + end + + def format(type) + case type + when :file + indent = '' + colour = :yellow + when :action + indent = ' ' + colour = :green + when :tag + indent = ' ' + colour = :cyan + when :info + indent = ' ' + colour = nil + end + + if @@colour + return indent + self.colourise(colour) + else + return indent + self + end + end +end + class Hash def longest_key_length self.keys.inject(0) do |longest, key| @@ -30,25 +70,32 @@ end class Metadata def output(tag, val, padding = 0) - puts ' ' + tag + ' ' * (2 + padding) + val + puts tag.format(:tag) + ' ' * (2 + padding) + val end private :output + def initialize(file) + puts (file + ':').format(:file) + + @filename = File.expand_path(file) + @write = false + end + def view - puts ' Viewing tags:' + puts 'Viewing all tags:'.format(:action) if @tags.empty? - puts ' No tags set.' + puts 'No tags set.'.format(:info) throw :next end end def viewtag(tag) - puts ' Viewing ' + tag + ' tag:' + puts ('Viewing ' + tag + ' tag:').format(:action) unless @tags.has_key? tag - puts ' Tag not set.' + puts 'Tag not set.'.format(:info) throw :next end end @@ -76,19 +123,27 @@ end class FLAC < VorbisComment def initialize(file) - @filename = File.expand_path(file) - @write = false + super @file = TagLib::FLAC::File.new(file) @tags = @file.xiphComment.fieldListMap.hash end end +class Vorbis < VorbisComment + def initialize(file) + super + + @file = TagLib::Vorbis::File.new(file) + @tags = @file.tag.fieldListMap.hash + end +end + def help puts <<-end -Usage: omptagger [options] [files] +Usage: omptagger [actions/options] [files] -Options: +Actions: --view -v View all tags --view-tag -t View a tag --add-tag -a Add a tag @@ -96,32 +151,14 @@ Options: --remove -r Remove all tags --remove-tag -d Remove a tag --generate -g Generate tags based on filename - --rename -m Generate filename based on tags - --scheme -n Specify a file naming scheme - --list -l Display available scheme keys - --help -h Display help information - --pretend -p Do not finalise changes + --rename -m Rename file based on tags + --scheme -n Change file naming scheme -Notes: - * Actions are executed in the order they are specified. - * The default file naming scheme is Artist - Title. - end - - exit -end - -def list - puts <<-end -Keys Vorbis Comments ID3v2 Frames ----- --------------- ------------- - %a Artist Artist - %b Album Album - %d Date Year - %n TrackNumber Track - %t Title Title ----- --------------- ------------- - %* Wildcard Wildcard - %% Per cent sign Per cent sign +Options: + --no-colour -c Disable colourisation of output + --pretend -p Disable finalisation of changes + --help -h Display help information + --info -i Display additional information end exit @@ -134,24 +171,26 @@ scheme = '%a - %t' GetoptLong.new( ['--view', '-v', GetoptLong::NO_ARGUMENT], ['--view-tag', '-t', GetoptLong::REQUIRED_ARGUMENT], + ['--add-tag', '-a', GetoptLong::REQUIRED_ARGUMENT], ['--set-tag', '-s', GetoptLong::REQUIRED_ARGUMENT], ['--remove', '-r', GetoptLong::NO_ARGUMENT], ['--remove-tag', '-d', GetoptLong::REQUIRED_ARGUMENT], ['--generate', '-g', GetoptLong::NO_ARGUMENT], ['--rename', '-m', GetoptLong::NO_ARGUMENT], ['--scheme', '-n', GetoptLong::REQUIRED_ARGUMENT], + ['--no-colour', '-c', GetoptLong::NO_ARGUMENT], ['--pretend', '-p', GetoptLong::NO_ARGUMENT], - ['--list', '-l', GetoptLong::NO_ARGUMENT], - ['--help', '-h', GetoptLong::NO_ARGUMENT] + ['--help', '-h', GetoptLong::NO_ARGUMENT], + ['--info', '-i', GetoptLong::NO_ARGUMENT] ).each do |option, argument| option = option.delete('-').intern case option when :scheme scheme = argument - when :help, :list, :pretend + when :nocolour, :pretend, :help, :info options << option - when :viewtag, :settag, :removetag + when :viewtag, :addtag, :settag, :removetag actions << [option, argument] when :generate, :rename actions << [option, scheme] @@ -161,7 +200,7 @@ GetoptLong.new( end help if options.include?(:help) -list if options.include?(:list) +info if options.include?(:info) help if actions.empty? if ARGV.empty? @@ -169,6 +208,16 @@ if ARGV.empty? exit end +if options.include?(:nocolour) + class String + @@colour = false + end +else + class String + @@colour = true + end +end + mime = FileMagic.mime mime.simplified = true @@ -179,19 +228,18 @@ ARGV.each do |file| case mime.file(file) when 'audio/x-flac' track = FLAC.new(file) + when 'application/ogg' + track = Vorbis.new(file) else raise 'File extension not recognised.' end - puts file + ':' - actions.each do |action| catch :next do track.send(*action) end end rescue RuntimeError => message - $stderr.puts file + ':' - $stderr.puts ' ' + message + $stderr.puts $0 + ': ' + file + ': ' + message end end |