Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
pickhardt committed May 9, 2014
2 parents 805560f + 9b66ad3 commit 0311b0c
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << {
Expand Down
4 changes: 2 additions & 2 deletions lib/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions lib/sizes.rb
Original file line number Diff line number Diff line change
@@ -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
75 changes: 75 additions & 0 deletions lib/spotify.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions main.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 0311b0c

Please sign in to comment.