diff --git a/README.md b/README.md index ba0c5c6..9941761 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Betty (version 0.1.3) +Betty (version 0.1.4) ===================== Betty is a friendly English-like interface for your command line. @@ -134,6 +134,15 @@ The following is a non-exhaustive list of things you can do: betty show me all processes by root containing grep betty show me all my processes containing netbio + Sizes + betty show size for myfile.txt + + Spotify + betty play spotify + betty pause spotify + betty next spotify + betty previous spotify + User betty whats my username betty whats my real name diff --git a/lib/config.rb b/lib/config.rb index e019bb0..b9af424 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -49,6 +49,28 @@ def self.interpret(command) } end + if command.match(/^(list\s(your\s)?voices)/i) + responses << { + :command => 'say -v "?"', + :explanation => 'List the availables voices for text-to-speech.' + } + end + + if command.match(/^(?:set|change|make)\s+(?:your|betty\'?s?)\s+voice\s+to\s+(.+)$/i) + new_voice = $1.strip + responses << { + :call_before => lambda { self.set("voice", new_voice) }, + :say => "OK. My new voice is #{ new_voice } from now on." + } + end + + if command.match(/^what\'?s?(?:\s+is)?\s+your\s+voice\??$/i) + my_voice = self.get("voice") + responses << { + :say => "My voice is setted as #{ my_voice }." + } + end + if command.match(/^(?:set|change|make)\s+(?:your|betty\'?s?)\s+name\s+to\s+(.+)$/i) || command.match(/^stop\s+speak(ing)?\s+to\s+me$/) new_name = $1.strip responses << { diff --git a/lib/internet.rb b/lib/internet.rb index 9072d1a..6e693ec 100755 --- a/lib/internet.rb +++ b/lib/internet.rb @@ -39,8 +39,8 @@ def self.compress(command) what_file = match[1].strip { - :command => "cd #{what_file}; tar -czvf #{what_file}.tar.gz *" - :explanation => "Compress the contents of #{what_file} directory, outputting the compressed file to parent directory" + :command => "cd #{ what_file }; tar -czvf #{ what_file }.tar.gz *", + :explanation => "Compress the contents of #{ what_file } directory, outputting the compressed file to parent directory" } end end diff --git a/lib/sizes.rb b/lib/sizes.rb new file mode 100644 index 0000000..d181754 --- /dev/null +++ b/lib/sizes.rb @@ -0,0 +1,61 @@ +module Sizes + GIGA_SIZE = 1073741824.0 + MEGA_SIZE = 1048576.0 + KILO_SIZE = 1024.0 + @command_string = "" + + # Return the file size with a readable style. + def Sizes.readable_file_size(size, precision) + case + when size == 1 + return "1 B" + when size < KILO_SIZE + return "%d B" % size + when size < MEGA_SIZE + return "%.#{precision}f KB" % (size / KILO_SIZE) + when size < GIGA_SIZE + return "%.#{precision}f MB" % (size / MEGA_SIZE) + else + return "%.#{precision}f GB" % (size / GIGA_SIZE) + end + end + + def self.show_sizes(where) + #puts "your command: #{@command_string}" + pipe = IO.popen("du -s #{ where } | sort -n") + while (line = pipe.gets) + match = line.match(/^(\d+)\s+.+?$/) + if match + puts "#{ Sizes.readable_file_size(match[1].to_i, 2) } #{ line }" + else + puts line + end + end + end + + def self.size_stuff(command) + @command_string = command + match = command.match(/^show\s+(?:size|disk|usage)\s+for\s+(.+?)$/i) || command.match(/^what[^\s]*\s+(?:the|)\s*(?:size|space)\s+(?:for|in|of)\s+(.+?)$/i) + + if match + where = match[1].strip + is_this_directory = where == '.' || where.downcase.match(/^(this\s+)?(?:dir(?:ectory)|folder|path)?$/) + + { + :call => lambda {self.show_sizes(is_this_directory ? '.' : where )}, + :explanation => "Shows the size of files in #{ is_this_directory ? 'all the files in the current directory, including subdirectories' : where }." + } + end + end + + def self.interpret(command) + responses = [] + + sizes_command = self.size_stuff(command) + responses << sizes_command if sizes_command + + responses + end +end + +$executors << Sizes diff --git a/lib/spotify.rb b/lib/spotify.rb new file mode 100755 index 0000000..b2600c7 --- /dev/null +++ b/lib/spotify.rb @@ -0,0 +1,75 @@ +module Spotify + + def self.start(command) + matching = command.match(/^(start|resume|play)\s+(spotify|((?:the|my)\s+)?music)$/i) + + if matching + { + :command => "osascript -e 'tell application \"spotify\" to play'", + :explanation => "Starts playing spotify." + } + else + nil + end + end + + def self.pause(command) + matching = command.match(/^pause\s+(spotify|((?:the|my)\s+)?music)$/i) + + if matching + { + :command => "osascript -e 'tell application \"spotify\" to pause'", + :explanation => "Pauses spotify." + } + else + nil + end + end + + def self.next(command) + matching = command.match(/^(next|advance)\s+(song|music|track|spotify)$/i) + + if matching + { + :command => "osascript -e 'tell application \"spotify\" to next track'", + :explanation => "Makes spotify play the next track." + } + else + nil + end + end + + def self.prev(command) + matching = command.match(/^prev(?:ious)?\s+(song|music|track|spotify)$/i) + + if matching + { + :command => "osascript -e 'tell application \"spotify\" to previous track'", + :explanation => "Makes spotify play the previous track." + } + else + nil + end + end + + + def self.interpret(command) + responses = [] + + start_command = self.start(command) + responses << start_command if start_command + + pause_command = self.pause(command) + responses << pause_command if pause_command + + next_command = self.next(command) + responses << next_command if next_command + + prev_command = self.prev(command) + responses << prev_command if prev_command + + responses + end +end + +$executors << Spotify diff --git a/lib/user.rb b/lib/user.rb index 3d34836..17c8a6d 100755 --- a/lib/user.rb +++ b/lib/user.rb @@ -48,6 +48,8 @@ def self.interpret(command) command_to_use = "" case program + when "go" + command_to_use = "go version" when "mysql" command_to_use = "mysql -u root -p -e ' SELECT VERSION(); '" else diff --git a/main.rb b/main.rb index b84e47f..9a702be 100755 --- a/main.rb +++ b/main.rb @@ -1,8 +1,11 @@ #!/usr/bin/env ruby +require 'logger' $URL = 'https://github.com/pickhardt/betty' -$VERSION = '0.1.3' +$VERSION = '0.1.4' $executors = [] +$LOG = Logger.new(File.open(ENV['HOME'] + '/.betty_history', 'a+')) + Dir[File.dirname(__FILE__) + '/lib/*.rb'].each {|file| require file } BettyConfig.initialize @@ -42,6 +45,9 @@ def interpret(command) $executors.each do |executor| executors_responses = executor.interpret(command) responses = responses.concat(executors_responses) + if executors_responses.length == 1 and executors_responses[0][:command] + $LOG.info('main_interpret') {"#{command} ==> #{executor.name} ==> #{executors_responses[0][:command]}"} + end end responses end @@ -71,7 +77,9 @@ def run(response) def speak(text) if User.has_command?('say') - system("say \"#{ text }\"") # formerly mpg123 -q + say = 'say' + say += " -v '#{BettyConfig.get("voice")}'" if BettyConfig.get("voice") + system("#{say} \"#{ text }\"") # formerly mpg123 -q else has_afplay = User.has_command?('afplay') has_mpg123 = User.has_command?('mpg123')