sr 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 061f3869f4efe07024e5a09389787a7b587a27e2
4
- data.tar.gz: 36eebe8d7ee9d6db3d6ac9201edc0e802cae62f6
3
+ metadata.gz: 840b10dde9c75efdffc21996c199a7bfb4dfd3c8
4
+ data.tar.gz: be910e535a257a2dc572c6709b59c2f5d85b0c3f
5
5
  SHA512:
6
- metadata.gz: d2b46015eaecf94a3754b5c1a3c60aa64c54657bd552a73ec8f9e7cb13a270536141df2fc10e19e124b8d66fe299d51273b79ea91b79385e310f142b161b657d
7
- data.tar.gz: aaa9b5446e9a5f352560f94a6217fb2c18b9d7d39e7f3d9d5ee8befcc6283f69f5411ce02fef041ffddaaec0f4b97b19b7b2363693ec8cbee2d32611ac613433
6
+ metadata.gz: 3533e3f363322ca51324c7a23239115c0fbf1f2b27711f3259730c20f0ba4b359895f3b4d16ec1324b7567c5b1cbe4136b8843d18b44c81def07fb6900c0232d
7
+ data.tar.gz: fadced5d39bd00241518406353a3d4930cdd876ee4fc183c8299cfb957ec5a841fa411410646299cefa46040176286f08b1b75c23a3caf540d0bdb09f38e3232
data/README.md CHANGED
@@ -4,12 +4,29 @@
4
4
 
5
5
  ## Usage
6
6
 
7
- Simply call `sr` to use `sr` as a Ruby REPL like `irb` or `pry`:
7
+ Simply call `sr` to use `sr` as a Ruby REPL:
8
8
 
9
9
  ```sh
10
10
  $ sr
11
11
  ```
12
12
 
13
+ You may also set the default `sr` context to any valid object using the '-c' or
14
+ '--context' option:
15
+
16
+ ```sh
17
+ $ sr --context Array.new
18
+ # -- OR --
19
+ $ sr -cArray.new
20
+ ```
21
+
22
+ Any arguments to be passed to Ruby itself may be given via the **$RUBYOPT**
23
+ environment variable. Common options (such as '-I' for manipulating the load
24
+ path or '-r' to require libraries) may be given in this manner:
25
+
26
+ ```sh
27
+ $ RUBYOPT=-ryaml sr -cPsych
28
+ ```
29
+
13
30
  `sr` can also be used to include a REPL in your own code (for debugging
14
31
  purposes, for example) by requiring 'sr' and calling `SR.repl`:
15
32
 
@@ -56,7 +73,7 @@ arguments at any time to completely clear the context stack and reset the
56
73
  binding to the default (usually the top level binding).
57
74
 
58
75
  When given an argument, however, `SR.reset_binding` clears the context stack
59
- and resets the default binding to the _given object_.
76
+ and resets the default binding to the given object.
60
77
 
61
78
  ## Configuration
62
79
 
@@ -66,6 +83,31 @@ be '/etc/xdg/sr/config.rb' for system-wide configuration and
66
83
  '~/.config/sr/config.rb' for user-specific configuration. These files are
67
84
  simply Ruby files which are loaded into `sr` via `require`.
68
85
 
86
+ You may also tell `sr` to load alternative configuration files (or suppress all
87
+ configuration) with the '-f' option. Given no arguments, this option suppresses
88
+ all configuration loading; given a file, it removes the default configuration
89
+ files and adds the given file to the list of configuration files to load. This
90
+ option may be specified multiple times to load multiple alternative files.
91
+
92
+ ```sh
93
+ $ sr -f # start with no configuration
94
+ $ sr -f alt.rb # start with alt.rb as configuration
95
+ ```
96
+
97
+ In addition to this, `sr` simply uses `gets` as its default input method; if
98
+ you would prefer to use Readline, simply add the following to your `sr`
99
+ configuration file:
100
+
101
+ ```ruby
102
+ require 'sr/input/readline'
103
+ ```
104
+
105
+ Alternatively, to use Readline temporarily, simply use **$RUBYOPT**:
106
+
107
+ ```sh
108
+ $ RUBYOPT=-rsr/input/readline sr
109
+ ```
110
+
69
111
  ## License
70
112
 
71
113
  `sr` is free and unencumbered software released into the public domain.
data/bin/sr CHANGED
@@ -1,18 +1,37 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'optparse'
5
+
4
6
  $0.start_with?('/') ? require('sr') : require_relative('../lib/sr')
5
7
 
6
- if ARGV.size > 0
7
- valid = %w(-h --help).any? { |a| ARGV.first == a }
8
- (valid ? $stdout : $stderr).puts <<-END
9
- sr #{SR::VERSION}
8
+ module SR
9
+ options = {}
10
+ parser = OptionParser.new do |opts|
11
+ opts.on('-c', '--context=obj', 'set the sr context to obj') do |obj|
12
+ options[:context] = eval(obj)
13
+ end
14
+ opts.on('-f [FILE]', 'suppress configuration (load FILE if given)') do |f|
15
+ options[:config_files] =
16
+ f ? (options[:config_files] ||= []).push(File.expand_path(f)) : []
17
+ end
18
+ opts.on('-v', '--version', 'display sr version') do
19
+ puts "sr #{SR::VERSION} [ruby #{RUBY_VERSION} #{RUBY_PLATFORM}]"
20
+ exit
21
+ end
22
+ opts.on('-h', '--help', 'display this message') do
23
+ puts opts
24
+ exit
25
+ end
26
+ end
10
27
 
11
- sr is a simple, tiny, hackable REPL for Ruby.
28
+ begin
29
+ parser.parse!
30
+ rescue OptionParser::InvalidOption => ex
31
+ $stderr.puts ex.message
32
+ $stderr.puts parser.help
33
+ exit 1
34
+ end
12
35
 
13
- usage: #{$0} [-h|--help]
14
- END
15
- exit valid ? 0 : 1
36
+ ARGV.empty? ? repl(options) : ($stderr.puts parser.help ; exit 1)
16
37
  end
17
-
18
- SR.repl
@@ -0,0 +1,9 @@
1
+ require 'readline'
2
+
3
+ module SR
4
+ @input_handler = ->(more) do
5
+ input = "#{Readline.readline(call_prompt(more ? :more : :input), true)}\n"
6
+ input = "#{more}#{input}" if more
7
+ input
8
+ end
9
+ end
data/lib/sr/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module SR
4
4
  # Semantic version of `sr`.
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
  end
data/lib/sr.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require_relative 'sr/core_patches'
2
2
  require_relative 'sr/version'
3
- require 'readline'
4
3
  require 'sxdg/as_xdg'
5
4
 
6
5
  # `sr` is a simple, tiny, hackable REPL for Ruby.
@@ -10,7 +9,10 @@ module SR
10
9
  attr_reader :context
11
10
 
12
11
  # @return [Hash{Class=>#call}] the hash of exception handlers
13
- attr_reader :handlers
12
+ attr_reader :exception_handlers
13
+
14
+ # @return [#call] the input handler
15
+ attr_accessor :input_handler
14
16
 
15
17
  # @return [Hash{Symbol=>#call,#to_s}] the hash of prompts to display; valid
16
18
  # keys are `:error`, `:input`, `:more`, and `:return`; valid values must
@@ -27,7 +29,7 @@ module SR
27
29
  # The regular expression used to determine whether or not a syntax error is
28
30
  # a valid continuation which should prompt for more user input.
29
31
  CONTINUATION_REGEXP =
30
- /unterminated (?:regexp|string)|unexpected end-of-input|tIDENTIFIER/
32
+ /unterminated (?:regexp|string)|unexpected end-of-(?:file|input)|tIDENTIFIER/
31
33
 
32
34
  @prompt = {
33
35
  error: proc { |e| "!! #{e.class}: #{e.message}" },
@@ -36,7 +38,14 @@ module SR
36
38
  return: proc { |v| "-> #{v.inspect}" },
37
39
  }
38
40
 
39
- @handlers = {
41
+ @input_handler ||= ->(more) do
42
+ print(call_prompt(more ? :more : :input))
43
+ input = gets
44
+ input = "#{more}#{input}" if more
45
+ input
46
+ end
47
+
48
+ @exception_handlers = {
40
49
  SyntaxError => proc do |e, i|
41
50
  e.message =~ CONTINUATION_REGEXP ? read_eval_print(i) : report(e)
42
51
  end,
@@ -95,22 +104,15 @@ module SR
95
104
  $stderr.puts call_prompt(:error, ex) if @prompt[:error]
96
105
  end
97
106
 
98
- # @return [Array<Boolean>] an array representing whether or not each
99
- # configuration file was loaded by `require`
100
- private_class_method def self.load_configuration
101
- CONFIGURATION_FILES.map(&method(:require))
102
- end
103
-
104
107
  # @param more [String] the previous input to be evaluated along with the
105
108
  # given input
106
109
  # @return [Object] the return value of the given input
107
110
  private_class_method def self.read_eval_print(more = nil)
108
- input = "#{Readline.readline(call_prompt(more ? :more : :input), true)}\n"
109
- input = "#{more}#{input}" if more
111
+ input = @input_handler.call(more)
110
112
  value = @context.eval("_ = #{input}")
111
113
  puts call_prompt(:return, value) if @prompt[:return]
112
- rescue *@handlers.keys => ex
113
- @handlers.find { |(c,_)| ex.is_a?(c) }[1].call(ex, input, more)
114
+ rescue *@exception_handlers.keys => ex
115
+ @exception_handlers.find { |(c,_)| ex.is_a?(c) }[1].call(ex, input, more)
114
116
  end
115
117
 
116
118
  # Loads the configuration for SR, resets the default binding to the given
@@ -118,9 +120,12 @@ module SR
118
120
  #
119
121
  # @param context [#__binding__] the object providing the default context for
120
122
  # `sr`; defaults to the top level binding if no context given
123
+ # @param options [Hash] the options for the REPL; valid options are
124
+ # `:config_files` (`Array<String>`) and `:context` (`#__binding__`)
121
125
  # @return [void]
122
- def self.repl(context = nil)
123
- load_configuration
126
+ def self.repl(context = nil, **options)
127
+ options.fetch(:config_files, CONFIGURATION_FILES).each(&method(:require))
128
+ context = context || options[:context]
124
129
  reset_binding(context) unless context.nil?
125
130
  @enabled = true
126
131
  read_eval_print while @enabled
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Owen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sxdg
@@ -51,6 +51,7 @@ files:
51
51
  - bin/sr
52
52
  - lib/sr.rb
53
53
  - lib/sr/core_patches.rb
54
+ - lib/sr/input/readline.rb
54
55
  - lib/sr/version.rb
55
56
  - sr.gemspec
56
57
  homepage: https://github.com/vthrenody/sr