eggsh 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,39 @@
1
1
  = eggsh
2
2
 
3
- Description goes here.
4
-
5
- == Contributing to eggsh
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
3
+ An interactive shell with Ruby power
4
+
5
+ == Synopsis
6
+
7
+ If we want to delete all the files except files with *.c extensions, we have to
8
+ do this in base:
9
+
10
+ ls | grep -v *.c | xargs rm -rf
11
+
12
+ The solution is not so obvious , and it requires the knowledge of the tools like
13
+ grep, sed, awk, xargs ... etc. If ruby expressions are available in the command
14
+ line environment, things will be much easy:
15
+
16
+ rm -rf { ls['*'] - ls['*.c'] }
17
+ // ruby expression inside {}
18
+
19
+ == Usage
20
+
21
+ Ruby 1.9 interpreter is required.
22
+
23
+ gem install eggsh
24
+
25
+ Call 'eggsh' to start the shell.
26
+ Settings of the shell are placed under ~/.eggshrc.
27
+
28
+ {} brackets are used to evaluate ruby expressions.
29
+
30
+ == Example
31
+
32
+ Making every files without extension excutable:
33
+
34
+ chmod +x { ls - ls['*.*'] }
14
35
 
15
36
  == Copyright
16
37
 
17
- Copyright (c) 2011 Andrew Liu. See LICENSE.txt for
18
- further details.
38
+ Copyright (c) 2011 Andrew Liu. See LICENSE.txt for further details.
19
39
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{eggsh}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Liu"]
12
- s.date = %q{2011-01-24}
12
+ s.date = %q{2011-01-25}
13
13
  s.default_executable = %q{eggsh}
14
14
  s.description = %q{Use Ruby expressions instead of unfriendly shell script}
15
15
  s.email = %q{andrewliu33@gmail.com}
@@ -3,9 +3,13 @@ require_relative 'util'
3
3
  require 'readline'
4
4
 
5
5
  module Eggsh
6
+ # the path of rc file
7
+ RC_PATH = '~/.eggshrc'
8
+
6
9
  class Runner
7
10
  def initialize
8
11
  @shell = Eggsh::Shell.new
12
+ load_rc
9
13
  end
10
14
 
11
15
  def run!
@@ -17,11 +21,13 @@ module Eggsh
17
21
  if line =~ /^(?<p>[^\{\}]*(\{\g<p>*\})*)*$/
18
22
  @shell.exec line
19
23
  else
20
- puts 'Unbalanced parentheses.'
24
+ puts 'eggsh: Unbalanced parentheses'
21
25
  end
22
26
  end
23
27
  end
24
28
 
29
+ private
30
+ # read a single line by readline
25
31
  def read
26
32
  line = Readline.readline(@shell.prompt, true)
27
33
  return nil if line.nil?
@@ -30,6 +36,14 @@ module Eggsh
30
36
  end
31
37
  line
32
38
  end
39
+
40
+ # load rc file from ~/.eggshrc
41
+ def load_rc
42
+ path = File.expand_path RC_PATH
43
+ if File.exist? path
44
+ eval(IO.read path)
45
+ end
46
+ end
33
47
  end
34
48
  end
35
49
 
@@ -1,33 +1,32 @@
1
1
  require_relative 'translator'
2
2
 
3
- # temprerary put here
4
- $alias_hash = {'ls' => 'ls --color=auto'}
5
-
6
3
  module Eggsh
7
4
  class Shell
5
+ # mapping from shell commands to member function
8
6
  SHELL_CMD = {'cd' => :cd, 'pwd' => :pwd, 'fullpwd' => :full_pwd,
9
7
  'quit' => :quit, 'exit' => :quit}
10
8
 
9
+ # alias hash for command alias
10
+ ALIAS = {'ls' => 'ls --color=auto'}
11
+
11
12
  def initialize
12
13
  @env = ENV.to_hash
13
14
  @pwd = ENV['PWD']
14
15
  @translator = Eggsh::Translator.new
15
16
  end
16
17
 
17
- def env
18
- @env
19
- end
20
-
18
+ # generating prompt
21
19
  def prompt
22
20
  "#{pwd.to_color(:bold_green)}$ "
23
21
  end
24
22
 
23
+ # handling a single line
25
24
  def exec line
26
25
  begin
27
26
  # alias first
28
- if line != '' && $alias_hash.has_key?(line.split(' ')[0])
27
+ if line != '' && ALIAS.has_key?(line.split(' ')[0])
29
28
  splitted = line.split(' ')
30
- splitted[0] = $alias_hash[splitted[0]]
29
+ splitted[0] = ALIAS[splitted[0]]
31
30
  line = splitted.join ' '
32
31
  end
33
32
 
@@ -39,7 +38,7 @@ module Eggsh
39
38
  begin
40
39
  spawn(@env, @translator.translate(line).gsub("\n", ' '), :chdir => @pwd)
41
40
  Process.wait
42
- rescue Errno::ENOENT => e
41
+ rescue Exception => e
43
42
  puts e.display
44
43
  end
45
44
  end
@@ -8,7 +8,7 @@ class Object
8
8
  end
9
9
  end
10
10
 
11
- # define to_color for terminal output
11
+ # color code for colorful terminal output
12
12
  COLOR_CODE = {
13
13
  :black => '30', :red => '31', :green => '32', :yellow => '33', :blue => '34',
14
14
  :purple => '35', :cyan => '36', :white => '37', :bold_black => '1;30',
@@ -17,6 +17,7 @@ class Object
17
17
  :bold_white => '1;37'
18
18
  }
19
19
 
20
+ # define to_color for terminal output
20
21
  def to_color color
21
22
  "\x1b[#{COLOR_CODE[color]}m#{to_s}\x1b[m"
22
23
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Liu
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-24 00:00:00 +08:00
17
+ date: 2011-01-25 00:00:00 +08:00
18
18
  default_executable: eggsh
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
126
  requirements:
127
127
  - - ">="
128
128
  - !ruby/object:Gem::Version
129
- hash: -1614003115125705715
129
+ hash: 4197712127916392614
130
130
  segments:
131
131
  - 0
132
132
  version: "0"