rib 0.1.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
data/Rakefile CHANGED
@@ -10,10 +10,12 @@ task 'gem:spec' do
10
10
  require 'rib/version'
11
11
  s.name = 'rib'
12
12
  s.version = Rib::VERSION
13
- s.executables = [s.name]
14
13
 
15
- %w[bond] .each{ |g| s.add_runtime_dependency(g) }
16
- %w[bacon rr].each{ |g| s.add_development_dependency(g) }
14
+ %w[].each{ |g| s.add_runtime_dependency(g) }
15
+ %w[bond
16
+ hirb
17
+ bacon
18
+ rr] .each{ |g| s.add_development_dependency(g) }
17
19
  end
18
20
 
19
21
  Gemgem.write
data/TODO.md ADDED
@@ -0,0 +1,6 @@
1
+ # Rib TODO
2
+
3
+ * Runner tests
4
+ * Documentation
5
+ * Fix Rubinius weird Runtime BOOM error
6
+ * Implement exception_spy
data/bin/rib-all ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rib/runner'
4
+ require 'rib/all'
5
+ Rib::Runner.run(ARGV)
data/bin/rib-auto ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rib/runner'
4
+ # create the shell before app to prvent your bundler (if any) kicks in
5
+ Rib.shell
6
+ require 'rib/app/auto'
7
+ # load the app
8
+ Rib::Auto.load
9
+ Rib::Runner.run(ARGV)
data/bin/rib-min ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rib/runner'
4
+ Rib.shells << Rib::Shell.new(Rib.config)
5
+ Rib::Runner.run(ARGV)
data/bin/rib-rails ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rib/runner'
4
+ # create the shell before app to prvent your bundler (if any) kicks in
5
+ Rib.shell
6
+ require 'rib/app/rails'
7
+ # load the app
8
+ Rib::Rails.load
9
+ Rib::Runner.run(ARGV)
data/bin/rib-ramaze ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rib/runner'
4
+ # create the shell before app to prvent your bundler (if any) kicks in
5
+ Rib.shell
6
+ require 'rib/app/ramaze'
7
+ # load the app
8
+ Rib::Ramaze.load
9
+ Rib::Runner.run(ARGV)
data/lib/rib.rb CHANGED
@@ -3,40 +3,104 @@ require 'rib/shell'
3
3
 
4
4
  module Rib
5
5
  module_function
6
+ # All default Rib configs, would be passed to Shell.new in Rib.shell,
7
+ # but calling Shell.new directly won't bring this in.
6
8
  def config
7
- @config ||= {:config => '~/.config/rib/config.rb'}
9
+ @config ||= {:config => '~/.config/rib/config.rb', :name => 'rib'}
8
10
  end
9
11
 
12
+ # All shells in the memory
10
13
  def shells
11
14
  @shells ||= []
12
15
  end
13
16
 
17
+ # All shared variables for all shells
14
18
  def vars
15
19
  @vars ||= {}
16
20
  end
17
21
 
22
+ # Convenient shell accessor, which would just give you current last shell
23
+ # or create one and load ~/.config/rib/config.rb if non has existed. If you
24
+ # need a clean shell which does not load rc file, use Shell.new instead.
18
25
  def shell
19
26
  shells.last || begin
20
- require_rc
27
+ if config_path
28
+ require_config
29
+ else # for those who don't have a config, we use core plugins
30
+ require 'rib/core'
31
+ end
21
32
  (shells << Shell.new(config)).last
22
33
  end
23
34
  end
24
35
 
36
+ # All plugins which have been loaded into the memory regardless
37
+ # it's enabled or not.
25
38
  def plugins
26
39
  Shell.ancestors[1..-1].select{ |a| a < Plugin }
27
40
  end
28
41
 
42
+ # Convenient way to disable all plugins in the memory.
43
+ # This could also take a list of plugins and disable them.
29
44
  def disable_plugins plugs=plugins
30
45
  plugs.each(&:disable)
31
46
  end
32
47
 
48
+ # Convenient way to enable all plugins in the memory.
49
+ # This could also take a list of plugins and enable them.
33
50
  def enable_plugins plugs=plugins
34
51
  plugs.each(&:enable)
35
52
  end
36
53
 
37
- def require_rc
38
- config[:config] &&
39
- File.exist?(rc = File.expand_path(config[:config])) &&
40
- require(rc)
54
+ # Load (actually require) ~/.config/rib/config.rb if exists.
55
+ # This might emit warnings if there's some error while loading it.
56
+ def require_config
57
+ config_path && require(config_path)
58
+ rescue Exception => e
59
+ Rib.warn("Error loading #{config[:config]}\n" \
60
+ " #{Rib::API.format_error(e)}")
61
+ end
62
+
63
+ # The config path where Rib tries to load upon Rib.shell
64
+ def config_path
65
+ return nil unless config[:config]
66
+ path = File.expand_path(config[:config])
67
+ if File.exist?(path)
68
+ path
69
+ else
70
+ nil
71
+ end
72
+ end
73
+
74
+ # Say (print to $stdout, with colors in the future, maybe)
75
+ # something by the name of Rib
76
+ def say *words
77
+ $stdout.puts(Rib.prepare(words))
78
+ end
79
+
80
+ # Warn (print to $stderr, with colors in the future, maybe)
81
+ # something by the name of Rib
82
+ def warn *words
83
+ $stderr.puts(Rib.prepare(words))
84
+ end
85
+
86
+ # Warn (print to $stderr, with colors in the future, maybe)
87
+ # something by the name of Rib and then exit(1)
88
+ def abort *words
89
+ warn(words)
90
+ exit(1)
91
+ end
92
+
93
+ def silence
94
+ w, v = $-w, $VERBOSE
95
+ $-w, $VERBOSE = false, false
96
+ yield
97
+ ensure
98
+ $-w, $VERBOSE = w, v
99
+ end
100
+
101
+ private
102
+ def self.prepare words
103
+ name = config[:name]
104
+ "#{name}: #{words.join("\n#{' '*(name.size+2)}")}"
41
105
  end
42
106
  end
data/lib/rib/all.rb CHANGED
@@ -1,4 +1,3 @@
1
1
 
2
2
  require 'rib/core'
3
3
  require 'rib/more'
4
- require 'rib/zore'
data/lib/rib/api.rb CHANGED
@@ -1,46 +1,57 @@
1
1
 
2
2
  module Rib; end
3
3
  module Rib::API
4
+ extend self
5
+
6
+ # Called before shell starts looping
4
7
  def before_loop
5
- read_history
6
8
  self
7
9
  end
8
10
 
11
+ # Called after shell finishes looping
12
+ def after_loop
13
+ self
14
+ end
15
+
16
+ # Handle interrupt (control-c)
17
+ def handle_interrupt; puts ; end
18
+ # The prompt string of this shell
19
+ def prompt ; config[:prompt] ; end
20
+ # The result prompt string of this shell
21
+ def result_prompt; config[:result_prompt] ; end
22
+ # The name of this shell
23
+ def name ; config[:name] ; end
24
+ # The binding for evaluation
25
+ def eval_binding ; config[:binding] ; end
26
+ # The line number for next evaluation
27
+ def line ; config[:line] ; end
28
+
29
+ # Main loop
9
30
  def in_loop
10
31
  input = catch(:rib_exit){ loop_once while true }
11
32
  puts if input == nil
12
33
  end
13
34
 
14
- # Runs through one loop iteration: gets input, evals and prints result
35
+ # Loop iteration: REPL
15
36
  def loop_once
16
- self.error_raised = nil
17
- input = get_input
37
+ input, result, err = get_input, nil, nil
18
38
  throw(:rib_exit, input) if config[:exit].include?(input)
19
- if input.strip == ''
20
- eval_input(input)
21
- else
22
- print_result(eval_input(input))
39
+ catch(:rib_skip) do
40
+ result, err = eval_input(input)
41
+ if err
42
+ print_eval_error(err)
43
+ elsif input.strip != ''
44
+ print_result(result)
45
+ else
46
+ # print nothing for blank input
47
+ end
23
48
  end
49
+ [result, err]
24
50
  rescue Interrupt
25
51
  handle_interrupt
26
52
  end
27
53
 
28
- # Handles interrupt (Control-C) by printing a newline
29
- def handle_interrupt() puts end
30
-
31
- # Sets @result to result of evaling input and print unexpected errors
32
- def eval_input(input)
33
- loop_eval(input)
34
- rescue Exception => e
35
- self.error_raised = true
36
- print_eval_error(e)
37
- ensure
38
- config[:line] += 1
39
- end
40
-
41
- # When extending this method, ensure your plugin disables readline:
42
- # Readline.config[:readline] = false.
43
- # @return [String, nil] Prints #prompt and returns input given by user
54
+ # Get user input. This is most likely overrided in Readline plugin
44
55
  def get_input
45
56
  print(prompt)
46
57
  if input = $stdin.gets
@@ -50,56 +61,41 @@ module Rib::API
50
61
  end
51
62
  end
52
63
 
53
- # @return [String]
54
- def prompt
55
- config[:prompt]
56
- end
57
-
58
- def read_history
59
- end
60
-
61
- def write_history
62
- end
63
-
64
- def history
65
- @history ||= []
66
- end
67
-
68
- # Evals user input using @binding, @name and @line
69
- def loop_eval(input)
70
- config[:binding].eval(input, "(#{config[:name]})", config[:line])
64
+ # Evaluate the input using #loop_eval and handle it
65
+ def eval_input input
66
+ [loop_eval(input), nil]
67
+ rescue Exception => e
68
+ [nil, e]
69
+ ensure
70
+ config[:line] += 1
71
71
  end
72
72
 
73
- # Prints error formatted by #format_error to STDERR. Could be extended to
74
- # handle certain exceptions.
75
- # @param [Exception]
76
- def print_eval_error(err)
77
- warn format_error(err)
73
+ # Evaluate user input with #eval_binding, name and line
74
+ def loop_eval input
75
+ eval_binding.eval(input, "(#{name})", line)
78
76
  end
79
77
 
80
- # Prints result using #format_result
81
- def print_result(result)
82
- puts(format_result(result)) unless error_raised
78
+ # Print result using #format_result
79
+ def print_result result
80
+ puts(format_result(result))
83
81
  rescue StandardError, SyntaxError => e
84
- warn "#{config[:name]}: Error while printing result:\n" \
85
- "#{format_error(e)}"
82
+ Rib.warn("Error while printing result:\n #{format_error(e)}")
86
83
  end
87
84
 
88
- # Formats errors raised by eval of user input
89
- # @param [Exception]
90
- # @return [String]
91
- def format_error(e)
92
- "#{e.class}: #{e.message}\n #{e.backtrace.join("\n ")}"
85
+ # Print evaluated error using #format_error
86
+ def print_eval_error err
87
+ puts(format_error(err))
88
+ rescue StandardError, SyntaxError => e
89
+ Rib.warn("Error while printing error:\n #{format_error(e)}")
93
90
  end
94
91
 
95
- # @return [String] Formats result using result_prompt
96
- def format_result(result)
97
- config[:result_prompt] + result.inspect
92
+ # Format result using #result_prompt
93
+ def format_result result
94
+ result_prompt + result.inspect
98
95
  end
99
96
 
100
- # Called after shell finishes looping.
101
- def after_loop
102
- write_history
103
- self
97
+ # Format error raised in #loop_eval
98
+ def format_error err
99
+ "#{err.class}: #{err.message}\n #{err.backtrace.join("\n ")}"
104
100
  end
105
101
  end
@@ -0,0 +1,25 @@
1
+
2
+ module Rib; end
3
+ module Rib::Auto
4
+ module_function
5
+ def load
6
+ app, name = %w[ramaze rails].find{ |name|
7
+ require "rib/app/#{name}"
8
+ app = Rib.const_get(name.capitalize)
9
+ if app.send("#{name}?")
10
+ break app, name
11
+ end
12
+ }
13
+
14
+ if app
15
+ Rib.say("Found #{name.capitalize}, loading it...")
16
+ begin
17
+ app.load
18
+ rescue LoadError => e
19
+ Rib.warn("Is this a #{app} app?\n #{e}")
20
+ end
21
+ else
22
+ Rib.warn("No app found")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+
2
+ module Rib; end
3
+ module Rib::Rails
4
+ module_function
5
+ def load
6
+ load_rails
7
+ rescue LoadError => e
8
+ Rib.abort("Is this a Rails app?\n #{e}")
9
+ end
10
+
11
+ def load_rails
12
+ require './config/boot'
13
+
14
+ if File.exist?('./config/application.rb')
15
+ Rib::Rails.load_rails3
16
+ else
17
+ Rib::Rails.load_rails2
18
+ end
19
+
20
+ puts("Loading #{::Rails.env} environment (Rails #{::Rails.version})")
21
+ end
22
+
23
+ def load_rails2
24
+ ['./config/environment',
25
+ 'console_app' ,
26
+ 'console_with_helpers'].each{ |f| require f }
27
+ end
28
+
29
+ def load_rails3
30
+ ['./config/application',
31
+ 'rails/console/app' ,
32
+ 'rails/console/helpers'].each{ |f| require f }
33
+
34
+ ::Rails.application.require_environment!
35
+ end
36
+
37
+ def rails?
38
+ File.exist?('./config/boot.rb') &&
39
+ File.exist?('./config/environment.rb')
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module Rib; end
3
+ module Rib::Ramaze
4
+ module_function
5
+ def load
6
+ load_ramaze
7
+ rescue LoadError => e
8
+ Rib.abort("Is this a Ramaze app?\n #{e}")
9
+ end
10
+
11
+ def load_ramaze
12
+ # try to produce consistent error message, and yet lazy loading ramaze
13
+ require './start' unless ramaze?
14
+
15
+ require 'ramaze'
16
+ ::Ramaze.options.started = true
17
+
18
+ require './start'
19
+ at_exit{ puts('Ramazement has ended, go in peace.') }
20
+ end
21
+
22
+ def ramaze?
23
+ File.exist?('./start.rb')
24
+ end
25
+ end