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 27, 2014
2 parents d550c88 + 4b815db commit 6417956
Show file tree
Hide file tree
Showing 31 changed files with 920 additions and 193 deletions.
18 changes: 18 additions & 0 deletions NOTICE.md
@@ -0,0 +1,18 @@
Betty
=====

For more info, visit the Betty homepage at https://github.com/pickhardt/betty

Copyright 2014 Betty

Betty hereby licenses this project to you under the Apache License,
version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
6 changes: 3 additions & 3 deletions README.md
@@ -1,10 +1,9 @@
Betty (version 0.1.5)
Betty (version 0.1.6)
=====================

Betty is a friendly English-like interface for your command line.

She translates English-like phrases into commands in case you every run into
situations [like this][xkcd].
She translates English-like phrases into commands in case you ever run into situations [like this][xkcd].

[xkcd]:http://xkcd.com/1168/

Expand Down Expand Up @@ -171,4 +170,5 @@ For more information on SemVer, visit [http://semver.org/](http://semver.org/).

License
-------

Released under the Apache License 2.0. Related link: www.apache.org/licenses/LICENSE-2.0.html
32 changes: 32 additions & 0 deletions autocomplete.rb
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby

`complete -o bashdefault -o default -o filenames -o nospace -C ~/betty/autocomplete.rb betty`
# This should be called only once upon installation, but it doesn't hurt if it gets called multiple times.

if ARGV.length==0
puts "Betty: Autocomplete installed."
exit
end

words = ARGV[1..-1].reverse.join(" ")
last = ARGV[2]
current = ARGV[1]

if last == "betty"
puts "count" if "count".start_with? current
puts "find" if "find".start_with? current
puts "show" if "show".start_with? current
puts "what" if "what".start_with? current
puts "convert" if "convert".start_with? current
end

if last == "count"
puts "words" if "words".start_with? current
puts "lines" if "lines".start_with? current
puts "chars" if "chars".start_with? current
end

if last == "convert"
`COMPREPLY=()` # default filename completion
# `compgen ...`
end
16 changes: 16 additions & 0 deletions changelog.txt
@@ -1,3 +1,19 @@
0.1.6 (May 26, 2014)
--------------------

Added calculations: "betty what is 3 plus 5"

Added autocomplete support.

Added pod bay doors to fun module.

Added unit conversion: "betty what is 10 grams in kg"

Added support for zip, tar bzip, and tar gzip decompression.

Added support for sizes: "betty whats the size of this folder"


0.1.5 (May 13, 2014)
--------------------

Expand Down
34 changes: 25 additions & 9 deletions install.rb
@@ -1,35 +1,51 @@
#!/usr/bin/env ruby
require 'readline'


def ask(prompt="", newline=false)
prompt += "\n" if newline
Readline.readline(prompt, true).squeeze(" ").strip
end



INSTALL_LOC = Dir.home + '/betty/'

if Dir.exist? INSTALL_LOC
raise "~/betty already exists! Please manually remove if you want to proceed"
puts "Warning: ~/betty already exists!"
end

puts "We will install to ~/betty and put an alias in your .<shell>rc. Hit <enter> or 'y' if this is okay."
STDOUT.flush
CONTINUE = ask "> "
if CONTINUE == "" || CONTINUE == "y"
# copy to ~/betty/
COPY_COMMAND = 'cp -rf '+ Dir.pwd + ' ' + INSTALL_LOC
print "Running `" + COPY_COMMAND + "`\n"
system COPY_COMMAND

# get current shell
*junk, SHELL = `echo $SHELL`.split('/')
if Dir.exist? INSTALL_LOC
# raise "~/betty already exists! Please manually remove if you want to proceed"
else
COPY_COMMAND = 'cp -rf ' + Dir.pwd + ' ' + INSTALL_LOC
print "Running `" + COPY_COMMAND + "`\n"
system COPY_COMMAND
end

# writing the alias
SHELLRC = (Dir.home + '/.' + SHELL.chomp + 'rc').chomp
begin
*junk, SHELL = `echo $SHELL`.split('/')
rescue Exception
SHELL="bash" #ruby 1.8 (parsing exceptions are not rescued by default)
end
bash_config = '.' + SHELL.chomp + 'rc'
bash_config = '.bash_profile' if RUBY_PLATFORM.match /darwin/ #and ... ?

SHELLRC = (Dir.home + '/' + bash_config ).chomp
print "Writing an alias called `betty` to " + SHELLRC + "\n"
open(SHELLRC, 'a') do |f|
f.puts "\n"
f.puts "######## Generated by Betty's install script"
f.puts "alias betty=#{ Dir.home }/betty/main.rb"
end

puts "add auto-complete by typing"
puts "complete -C #{INSTALL_LOC}autocomplete.rb betty"
`#{INSTALL_LOC}autocomplete.rb`
end

52 changes: 52 additions & 0 deletions lib/calc.rb
@@ -0,0 +1,52 @@
class String
def numeric?
Float(self) != nil rescue false
end
end

module Calculate
def self.interpret(command)
responses = []
matches = command.match(/^what\s+is\s+(.+)\s+(.+)\s(.+)$/)

if matches
if (matches[1].numeric? && matches[3].numeric?)
arg1 = matches[1]
arg2 = matches[3]
else
return []
end

oper = matches[2]
if ["plus", "+"].include?(oper)
op = "+"
elsif ["minus", "-"].include?(oper)
op = "-"
elsif ["times", "*"].include?(oper)
op = "*"
elsif ["by", "divided", "divied by", "/"].include?(oper)
op = "/"
else
return []
end

responses << {
command: "bc <<< #{arg1}#{op}#{arg2}",
explanation: "Calculates #{arg1}#{op}#{arg2}"
}
end
responses
end

def self.help
commands = []
commands << {
category: "Calculate",
description: '\033[34mCalculate\033[0m',
usage: ["- betty what is 40 plus 2"]
}
commands
end
end

$executors << Calculate
40 changes: 40 additions & 0 deletions lib/commands.rb
@@ -0,0 +1,40 @@
require File.expand_path('../os.rb', __FILE__)

module Command

@@platform_error = "echo \"I don\'t know how to do that on #{OS.platform_name} yet, sorry !\""

#todo: complete for missing OSes
def self.browser(link)
browser = ""
case OS.platform_name
when 'OS X'
browser = 'open'
when 'Linux'
browser = 'xdg-open'
when 'Windows'
browser = 'start'
else
return @@platform_error
end
return "#{ browser } #{ link }"
end

def self.bus(msg= {})
case OS.platform_name
when 'OS X'
if ! msg[:osx]
return @@platform_error
end
return "osascript -e '#{ msg[:osx] }'"
when 'Linux'
if ! msg[:linux]
return @@platform_error
end
return "dbus-send #{ msg[:linux] }"
else
return @@platform_error
end
end
end

6 changes: 3 additions & 3 deletions lib/config.rb
Expand Up @@ -126,9 +126,9 @@ def self.help
commands = []
commands << {
:category => "Config",
:usage => ["- betty change your name to Joe",
"- betty speak to me",
"- betty stop speaking to me"]
:usage => ["change your name to Joe",
"speak to me",
"stop speaking to me"]
}
commands
end
Expand Down
86 changes: 86 additions & 0 deletions lib/convert.rb
@@ -0,0 +1,86 @@
module Conversion

def self.detect_in_format f
# todo : mapping latin1 -> ISO-8859-1 etc etc
return f.gsub(/.*\./,"")
end

def self.detect_out_format f
return f.gsub(/.*\./,"")
end


def self.interpret(command)
responses = []

image_formats=%w[jpg bmp gif tif png]
sound_formats=%w[wav mp3 flac au ogg]
encoding_formats=%w[utf8 latin1 ascii ISO-8859-1 UTF-8]
doc_formats=%w[doc pdf]
# etc, todo

convert_pattern=%r{
(convert|save|transform)\s
(?<all>all\s)?
(?<my>my\s)?
(?<files>.*?)\s
(to|as)\s
(?<out>.*)
}imx

match=convert_pattern.match command
return responses if not match

in_files=match[:files]
in_format=detect_in_format in_files
out_format=detect_out_format match[:out]
out_file=match[:out] if match[:out].length>3
out_file||=in_files.sub(/\.#{in_format}/,".#{out_format}")
args = ""

if match and encoding_formats.index out_format
command="iconv "
args="-f ISO-8859-1 -t UTF-8"
args="-f #{in_format} -t #{out_format}"
end

if match and image_formats.index out_format
command="sips -s format"
args = out_format + " "
args += in_files + " "
args += " --out " + out_file
# OR: alias resize-image='/opt/local/bin/convert in.jpg -resize 231x231 out.jpg'
end

if match and sound_formats.index out_format
command="ffmpeg"
command="sox" if in_format=="raw"|| in_format=="pcm"
args="-i #{in_files} #{out_file}"
args="-t raw -r 8000 -e signed -b 16 -c 1" if in_format=="raw"
args="-t raw -r 16k -e signed -b 8 -c 1" if in_format=="pcm"
# function m4a_TO_mp3(){
# open "http://media.io/"
# }
end

responses << {
:command => "#{command} #{args}",
:explanation => "Convert #{in_files} to #{out_format}"
}

return responses
end

def self.help
commands = []
commands << {
:category => "Conversion",
:description => '\033[34mConvert\033[0m all kinds of files',
:usage => ["convert img.jpg to bmp",
"convert song.wav to mp3"]
}
commands
end
end

$executors << Conversion
6 changes: 3 additions & 3 deletions lib/count.rb
Expand Up @@ -41,9 +41,9 @@ def self.help
commands << {
:category => "Count",
:description => '\033[34mCount\033[0m',
:usage => ["- betty how many words are in this directory",
"- betty how many characters are in myfile.py",
"- betty count lines in this folder",
:usage => ["how many words are in this directory",
"how many characters are in myfile.py",
"count lines in this folder",
"(Note that there're many ways to say more or less the same thing.)"]
}
commands
Expand Down

0 comments on commit 6417956

Please sign in to comment.