rib 0.1.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/.gitignore +0 -4
  2. data/CHANGES.md +6 -0
  3. data/README +107 -113
  4. data/README.md +107 -113
  5. data/Rakefile +5 -3
  6. data/TODO.md +6 -0
  7. data/bin/rib-all +5 -0
  8. data/bin/rib-auto +9 -0
  9. data/bin/rib-min +5 -0
  10. data/bin/rib-rails +9 -0
  11. data/bin/rib-ramaze +9 -0
  12. data/lib/rib.rb +70 -6
  13. data/lib/rib/all.rb +0 -1
  14. data/lib/rib/api.rb +60 -64
  15. data/lib/rib/app/auto.rb +25 -0
  16. data/lib/rib/app/rails.rb +41 -0
  17. data/lib/rib/app/ramaze.rb +25 -0
  18. data/lib/rib/config.rb +3 -0
  19. data/lib/rib/core.rb +13 -1
  20. data/lib/rib/core/completion.rb +15 -3
  21. data/lib/rib/core/history.rb +56 -0
  22. data/lib/rib/core/multiline.rb +104 -0
  23. data/lib/rib/core/readline.rb +3 -1
  24. data/lib/rib/core/squeeze_history.rb +45 -0
  25. data/lib/rib/core/strip_backtrace.rb +45 -0
  26. data/lib/rib/core/underscore.rb +17 -8
  27. data/lib/rib/debug.rb +2 -1
  28. data/lib/rib/dep/hirb.rb +24 -0
  29. data/lib/rib/more.rb +4 -3
  30. data/lib/rib/more/anchor.rb +85 -0
  31. data/lib/rib/more/color.rb +44 -43
  32. data/lib/rib/{zore → more}/edit.rb +3 -3
  33. data/lib/rib/more/multiline_history.rb +24 -12
  34. data/lib/rib/more/multiline_history_file.rb +7 -3
  35. data/lib/rib/plugin.rb +2 -4
  36. data/lib/rib/runner.rb +84 -49
  37. data/lib/rib/shell.rb +4 -2
  38. data/lib/rib/test.rb +55 -2
  39. data/lib/rib/test/multiline.rb +140 -0
  40. data/lib/rib/version.rb +1 -1
  41. data/rib.gemspec +54 -22
  42. data/screenshot.png +0 -0
  43. data/task/gemgem.rb +3 -1
  44. data/test/core/{test_history_file.rb → test_history.rb} +29 -19
  45. data/test/core/test_multiline.rb +22 -0
  46. data/test/core/test_readline.rb +13 -8
  47. data/test/{more → core}/test_squeeze_history.rb +24 -18
  48. data/test/core/test_underscore.rb +32 -21
  49. data/test/more/test_multiline_history.rb +42 -0
  50. data/test/test_shell.rb +13 -8
  51. metadata +72 -27
  52. data/2011-02-28.md +0 -203
  53. data/CHANGES +0 -86
  54. data/TODO +0 -6
  55. data/lib/rib/core/history_file.rb +0 -38
  56. data/lib/rib/more/multiline.rb +0 -77
  57. data/lib/rib/more/squeeze_history.rb +0 -37
  58. data/lib/rib/more/strip_backtrace.rb +0 -43
  59. data/lib/rib/zore.rb +0 -3
  60. data/lib/rib/zore/anchor.rb +0 -69
@@ -5,6 +5,8 @@ module Rib::Color
5
5
  include Rib::Plugin
6
6
  Shell.use(self)
7
7
 
8
+ # --------------- Rib API ---------------
9
+
8
10
  def before_loop
9
11
  config[:color] ||= {
10
12
  Numeric => :red ,
@@ -26,73 +28,72 @@ module Rib::Color
26
28
  config[:result_prompt] + format_color(result)
27
29
  end
28
30
 
29
- private
30
- def colors
31
- config[:color]
32
- end
31
+ # --------------- Plugin API ---------------
32
+
33
+ def colors; config[:color]; end
33
34
 
34
35
  def format_color result, display=result.inspect
35
36
  case result
36
- when String ; P.send(colors[String ]){ display }
37
- when Numeric; P.send(colors[Numeric]){ display }
38
- when Symbol ; P.send(colors[Symbol ]){ display }
37
+ when String ; send(colors[String ]){ display }
38
+ when Numeric; send(colors[Numeric]){ display }
39
+ when Symbol ; send(colors[Symbol ]){ display }
39
40
 
40
- when Array ; P.send(colors[Array ]){ '[' } +
41
+ when Array ; send(colors[Array ]){ '[' } +
41
42
  result.map{ |e | format_color(e) }.
42
- join(P.send(colors[Array ]){ ', ' }) +
43
- P.send(colors[Array ]){ ']' }
43
+ join(send(colors[Array ]){ ', ' }) +
44
+ send(colors[Array ]){ ']' }
44
45
 
45
- when Hash ; P.send(colors[Hash ]){ '{' } +
46
+ when Hash ; send(colors[Hash ]){ '{' } +
46
47
  result.map{ |k, v| format_color(k) +
47
- P.send(colors[Hash ]){ '=>' } +
48
+ send(colors[Hash ]){ '=>' } +
48
49
  format_color(v) }.
49
- join(P.send(colors[Hash ]){ ', ' }) +
50
- P.send(colors[Hash ]){ '}' }
50
+ join(send(colors[Hash ]){ ', ' }) +
51
+ send(colors[Hash ]){ '}' }
51
52
 
52
- else ; if color = P.find_color(colors, result)
53
- P.send(color){ display }
53
+ else ; if color = find_color(colors, result)
54
+ send(color){ display }
54
55
  else
55
- P.send(colors[Object ]){ display }
56
+ send(colors[Object ]){ display }
56
57
  end
57
58
  end
58
59
  end
59
60
 
60
- def get_error e, backtrace=e.backtrace
61
+ # override StripBacktrace#get_error
62
+ def get_error err, backtrace=err.backtrace
61
63
  return super if Color.disabled?
62
- [format_color(e, "#{e.class.to_s}: #{e.message}"),
64
+ [format_color(err, "#{err.class.to_s}: #{err.message}"),
63
65
  backtrace.map{ |b|
64
66
  path, rest = ::File.split(b)
65
67
  name, msgs = rest.split(':', 2)
66
- msg = msgs.sub(/(\d+):/){P.red{$1}+':'}.sub(/`.+?'/){P.green{$&}}
68
+ msg = msgs.sub(/(\d+):/){red{$1}+':'}.sub(/`.+?'/){green{$&}}
67
69
 
68
- "#{path+'/'}#{P.yellow{name}}:#{msg}"
70
+ "#{path+'/'}#{yellow{name}}:#{msg}"
69
71
  }]
70
72
  end
71
73
 
72
- module Imp
73
- def find_color colors, value
74
- (colors.sort{ |(k1, v1), (k2, v2)|
75
- # Class <=> Class
76
- if k1 < k2 then -1
77
- elsif k1 > k2 then 1
78
- else 0
79
- end}.find{ |(klass, _)| value.kind_of?(klass) } || []).last
80
- end
81
74
 
82
- def color rgb
83
- "\x1b[#{rgb}m" + (block_given? ? "#{yield}#{reset}" : '')
84
- end
85
75
 
86
- def black &block; color(30, &block); end
87
- def red &block; color(31, &block); end
88
- def green &block; color(32, &block); end
89
- def yellow &block; color(33, &block); end
90
- def blue &block; color(34, &block); end
91
- def magenta &block; color(35, &block); end
92
- def cyan &block; color(36, &block); end
93
- def white &block; color(37, &block); end
94
- def reset &block; color('', &block); end
76
+ module_function
77
+ def find_color colors, value
78
+ (colors.sort{ |(k1, v1), (k2, v2)|
79
+ # Class <=> Class
80
+ if k1 < k2 then -1
81
+ elsif k1 > k2 then 1
82
+ else 0
83
+ end}.find{ |(klass, _)| value.kind_of?(klass) } || []).last
84
+ end
85
+
86
+ def color rgb
87
+ "\x1b[#{rgb}m" + (block_given? ? "#{yield}#{reset}" : '')
95
88
  end
96
89
 
97
- Plugin.extend(Imp)
90
+ def black &block; color(30, &block); end
91
+ def red &block; color(31, &block); end
92
+ def green &block; color(32, &block); end
93
+ def yellow &block; color(33, &block); end
94
+ def blue &block; color(34, &block); end
95
+ def magenta &block; color(35, &block); end
96
+ def cyan &block; color(36, &block); end
97
+ def white &block; color(37, &block); end
98
+ def reset &block; color('', &block); end
98
99
  end
@@ -6,14 +6,14 @@ module Rib::Edit
6
6
  include Rib::Plugin
7
7
  Shell.use(self)
8
8
 
9
- module EditImp
9
+ module Imp
10
10
  def edit
11
11
  return if Rib::Edit.disabled?
12
12
  file = Tempfile.new(['rib.edit', '.rb'])
13
13
  file.puts(Rib.vars[:edit])
14
14
  file.close
15
15
 
16
- system("$EDITOR #{file.path}")
16
+ system("#{ENV['EDITOR']} #{file.path}")
17
17
 
18
18
  if (shell = Rib.shell).running?
19
19
  shell.send(:multiline_buffer).pop
@@ -29,5 +29,5 @@ module Rib::Edit
29
29
  end
30
30
  end
31
31
 
32
- Rib.extend(EditImp)
32
+ Rib.extend(Imp)
33
33
  end
@@ -1,23 +1,24 @@
1
1
 
2
- require 'rib/more/multiline' # dependency
2
+ require 'rib/core/history' # dependency
3
+ require 'rib/core/multiline' # dependency
3
4
 
4
5
  module Rib::MultilineHistory
5
6
  include Rib::Plugin
6
7
  Shell.use(self)
7
8
 
9
+ # --------------- Rib API ---------------
10
+
11
+ def before_loop
12
+ @multiline_trash = 0
13
+ super
14
+ end
15
+
8
16
  def loop_eval input
9
17
  return super if MultilineHistory.disabled?
10
- value = super
11
- rescue Exception
12
- # might be multiline editing, ignore
13
- raise
14
- else
15
- if multiline_buffer.size > 1
16
- # so multiline editing is considering done here
17
- (multiline_buffer.size + (@multiline_trash || 0)).times{ history.pop }
18
- history << "\n" + multiline_buffer.join("\n")
19
- end
20
- value
18
+ super
19
+ ensure
20
+ # SyntaxError might mean we're multiline editing
21
+ handle_multiline unless multiline?($!)
21
22
  end
22
23
 
23
24
  def handle_interrupt
@@ -28,4 +29,15 @@ module Rib::MultilineHistory
28
29
  end
29
30
  super
30
31
  end
32
+
33
+
34
+
35
+ private
36
+ def handle_multiline
37
+ if multiline_buffer.size > 1
38
+ # so multiline editing is considering done here
39
+ (multiline_buffer.size + @multiline_trash).times{ history.pop }
40
+ history << "\n" + multiline_buffer.join("\n")
41
+ end
42
+ end
31
43
  end
@@ -5,17 +5,21 @@ module Rib::MultilineHistoryFile
5
5
  include Rib::Plugin
6
6
  Shell.use(self)
7
7
 
8
+ # --------------- Rib API ---------------
9
+
8
10
  def before_loop
9
11
  return super if MultilineHistoryFile.disabled?
10
12
  config[:multiline_history_file_token] ||= ' '
11
13
  super
12
14
  end
13
15
 
16
+ # --------------- Plugin API ---------------
17
+
14
18
  def read_history
15
19
  return super if MultilineHistoryFile.disabled?
16
20
  buffer = []
17
- File.exist?(history_file) && history.empty? &&
18
- IO.readlines(history_file).each{ |line|
21
+ File.exist?(history_file_path) && history.empty? &&
22
+ IO.readlines(history_file_path).each{ |line|
19
23
  if line.end_with?(
20
24
  "#{config[:multiline_history_file_token]}\n")
21
25
  buffer << line[0...
@@ -29,7 +33,7 @@ module Rib::MultilineHistoryFile
29
33
 
30
34
  def write_history
31
35
  return super if MultilineHistoryFile.disabled?
32
- @history = history.to_a.map{ |line|
36
+ config[:history] = history.to_a.map{ |line|
33
37
  line.gsub("\n", "#{config[:multiline_history_file_token]}\n")
34
38
  }
35
39
  super
data/lib/rib/plugin.rb CHANGED
@@ -1,8 +1,6 @@
1
1
 
2
2
  module Rib; end
3
3
  module Rib::Plugin
4
- Rib::P = self
5
-
6
4
  def self.included mod
7
5
  mod.send(:include, Rib)
8
6
 
@@ -28,8 +26,8 @@ module Rib::Plugin
28
26
  end
29
27
  end
30
28
 
31
- snake_name = mod.name[/::\w+$/].tr(':', ''). # remove namespaces
32
- gsub(/([A-Z][a-z]*)/, '\\1_').downcase[0..-2]
29
+ snake_name = mod.name.sub(/(\w+::)+?(\w+)$/, '\2').
30
+ gsub(/([A-Z][a-z]*)/, '\\1_').downcase[0..-2]
33
31
 
34
32
  code = (%w[enable disable].map{ |meth|
35
33
  <<-RUBY
data/lib/rib/runner.rb CHANGED
@@ -3,54 +3,66 @@ require 'rib'
3
3
 
4
4
  module Rib::Runner
5
5
  module_function
6
- def name
7
- File.basename($PROGRAM_NAME)
8
- end
9
-
10
6
  def options
11
- { # Ruby OPTIONS
12
- '-e, --eval LINE' =>
13
- 'Evaluate a LINE of code' ,
7
+ @options ||=
8
+ [['ruby options:' , '' ],
9
+ ['-e, --eval LINE' ,
10
+ 'Evaluate a LINE of code' ],
11
+
12
+ ['-d, --debug' ,
13
+ 'Set debugging flags (set $DEBUG to true)' ],
14
14
 
15
- '-d, --debug' =>
16
- 'Set debugging flags (set $DEBUG to true)' ,
15
+ ['-w, --warn' ,
16
+ 'Turn warnings on for your script (set $-w to true)' ],
17
17
 
18
- '-w, --warn' =>
19
- 'Turn warnings on for your script (set $-w to true)' ,
18
+ ['-I, --include PATH' ,
19
+ 'Specify $LOAD_PATH (may be used more than once)' ],
20
20
 
21
- '-I, --include PATH' =>
22
- 'Specify $LOAD_PATH (may be used more than once)' ,
21
+ ['-r, --require LIBRARY' ,
22
+ 'Require the library, before executing your script' ],
23
23
 
24
- '-r, --require LIBRARY' =>
25
- 'Require the library, before executing your script' ,
24
+ ['rib options:' , '' ],
25
+ ['-c, --config FILE', 'Load config from FILE' ],
26
+ ['-n, --no-config' , 'Suppress loading ~/.config/rib/config.rb'],
27
+ ['-h, --help' , 'Print this message' ],
28
+ ['-v, --version' , 'Print the version' ]] +
26
29
 
27
- # Rib OPTIONS
28
- '-c, --config FILE' => 'Load config from FILE' ,
29
- '-n, --no-config' => 'Suppress loading ~/.config/rib/config.rb',
30
- '-h, --help' => 'Print this message' ,
31
- '-v, --version' => 'Print the version' }
30
+ [['rib commands:' , '']] + commands
32
31
  end
33
32
 
34
- def run argv=ARGV
35
- if command = argv.find{ |a| a =~ /^[^-]/ }
36
- argv.delete(command)
37
- plugin = "rib-#{command}"
38
- path = `which #{plugin}`.strip
39
- if path == ''
40
- puts("Can't find `#{plugin}' in $PATH.\n" \
41
- "Please make sure `#{plugin}' is installed.\n" \
42
- "e.g. run `gem install #{plugin}`")
43
- else
44
- load(path)
45
- end
46
- else
47
- start(*argv)
48
- end
33
+ def commands
34
+ @commands ||=
35
+ command_names.map{ |n| [n, command_descriptions[n] || ' '] }
36
+ end
37
+
38
+ def command_names
39
+ @command_names ||=
40
+ Gem.path.map{ |path|
41
+ Dir["#{path}/bin/*"].map{ |f|
42
+ (File.executable?(f) && File.basename(f) =~ /^rib\-(\w+)$/ && $1) ||
43
+ nil # a trick to make false to be nil and then
44
+ }.compact # this compact could eliminate them
45
+ }.flatten
49
46
  end
50
47
 
51
- def start *argv
52
- unused = parse(argv.dup)
53
- warn "#{name}: Unused arguments: #{unused.inspect}" unless unused.empty?
48
+ def command_descriptions
49
+ @command_descriptions ||=
50
+ {'all' => 'Load all recommended plugins' ,
51
+ 'min' => 'Run the minimum essence' ,
52
+ 'auto' => 'Run as Rails or Ramaze console (auto-detect)',
53
+ 'rails' => 'Run as Rails console' ,
54
+ 'ramaze' => 'Run as Ramaze console' }
55
+ end
56
+
57
+ def run argv=ARGV
58
+ (@running_commands ||= []) << Rib.config[:name]
59
+ unused = parse(argv)
60
+ # if it's running a Rib command, the loop would be inside Rib itself
61
+ # so here we only parse args for the command
62
+ return if @running_commands.pop != 'rib'
63
+ # by comming to this line, it means now we're running Rib main loop,
64
+ # not any other Rib command
65
+ Rib.warn("Unused arguments: #{unused.inspect}") unless unused.empty?
54
66
  Rib.shell.loop
55
67
  end
56
68
 
@@ -58,23 +70,23 @@ module Rib::Runner
58
70
  unused = []
59
71
  until argv.empty?
60
72
  case arg = argv.shift
61
- when /-e=?(.*)/, /--eval=?(.*)/
62
- eval($1 || argv.shift, __FILE__, __LINE__)
73
+ when /-e=?(.+)?/, /--eval=?(.+)?/
74
+ eval($1 || argv.shift, binding, __FILE__, __LINE__)
63
75
 
64
76
  when '-d', '--debug'
65
77
  $DEBUG = true
66
78
 
67
79
  when '-w', '--warn'
68
- $-w = true
80
+ $-w, $VERBOSE = true, true
69
81
 
70
- when /-I=?(.*)/, /--include=?(.*)/
82
+ when /-I=?(.+)?/, /--include=?(.+)?/
71
83
  paths = ($1 || argv.shift).split(':')
72
84
  $LOAD_PATH.unshift(*paths)
73
85
 
74
- when /-r=?(.*)/, /--require=?(.*)/
86
+ when /-r=?(.+)?/, /--require=?(.+)?/
75
87
  require($1 || argv.shift)
76
88
 
77
- when /-c=?(.*)/, /--config=?(.*)/
89
+ when /-c=?(.+)?/, /--config=?(.+)?/
78
90
  Rib.config[:config] = $1 || argv.shift
79
91
 
80
92
  when '-n', '--no-config'
@@ -89,6 +101,9 @@ module Rib::Runner
89
101
  puts(Rib::VERSION)
90
102
  exit
91
103
 
104
+ when /^[^-]/
105
+ load_command(arg)
106
+
92
107
  else
93
108
  unused << arg
94
109
  end
@@ -97,10 +112,30 @@ module Rib::Runner
97
112
  end
98
113
 
99
114
  def help
100
- maxn = options.keys .map(&:size).max
101
- maxd = options.values.map(&:size).max
102
- "Usage: #{name} [Ruby OPTIONS] [Rib COMMAND] [Rib OPTIONS]\n" +
103
- options.map{ |name, desc|
104
- sprintf(" %-*s %-*s", maxn, name, maxd, desc) }.join("\n")
115
+ maxn = options.transpose.first.map(&:size).max
116
+ maxd = options.transpose.last .map(&:size).max
117
+ "Usage: #{Rib.config[:name]}" \
118
+ " [ruby OPTIONS] [rib OPTIONS] [rib COMMANDS]\n" +
119
+ options.map{ |(name, desc)|
120
+ if desc.empty?
121
+ name
122
+ else
123
+ sprintf(" %-*s %-*s", maxn, name, maxd, desc)
124
+ end
125
+ }.join("\n")
126
+ end
127
+
128
+ def load_command command
129
+ bin = "rib-#{command}"
130
+ path = `which #{bin}`.strip
131
+ if path == ''
132
+ Rib.warn(
133
+ "Can't find #{bin} in $PATH. Please make sure it is installed,",
134
+ "or is there any typo? You can try this to install it:\n" ,
135
+ " gem install #{bin}")
136
+ else
137
+ Rib.config[:name] = bin
138
+ load(path)
139
+ end
105
140
  end
106
141
  end
data/lib/rib/shell.rb CHANGED
@@ -13,13 +13,13 @@ class Rib::Shell
13
13
  attr_reader :config
14
14
  def initialize(config={})
15
15
  self.config = {
16
- :name => 'rib' ,
17
16
  :result_prompt => '=> ' ,
18
17
  :prompt => '>> ' ,
19
18
  :binding => TOPLEVEL_BINDING ,
20
19
  :exit => [nil, 'exit', 'quit'] ,
21
20
  :line => 1
22
21
  }.merge(config)
22
+ @running = false
23
23
  end
24
24
 
25
25
  # Loops shell until user exits
@@ -28,6 +28,9 @@ class Rib::Shell
28
28
  @running = true
29
29
  in_loop
30
30
  self
31
+ rescue Exception => e
32
+ Rib.warn("Error while running loop:\n #{format_error(e)}")
33
+ raise
31
34
  ensure
32
35
  @running = false
33
36
  after_loop
@@ -39,5 +42,4 @@ class Rib::Shell
39
42
 
40
43
  protected
41
44
  attr_writer :config
42
- attr_accessor :error_raised
43
45
  end