sr 1.1.0 → 1.1.1

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: 840b10dde9c75efdffc21996c199a7bfb4dfd3c8
4
- data.tar.gz: be910e535a257a2dc572c6709b59c2f5d85b0c3f
3
+ metadata.gz: f1bc2d0b60073323e10a7be29431f0b40d87a869
4
+ data.tar.gz: 4cf0d268cb08eebfb551225a1b9227cc6b6b28c6
5
5
  SHA512:
6
- metadata.gz: 3533e3f363322ca51324c7a23239115c0fbf1f2b27711f3259730c20f0ba4b359895f3b4d16ec1324b7567c5b1cbe4136b8843d18b44c81def07fb6900c0232d
7
- data.tar.gz: fadced5d39bd00241518406353a3d4930cdd876ee4fc183c8299cfb957ec5a841fa411410646299cefa46040176286f08b1b75c23a3caf540d0bdb09f38e3232
6
+ metadata.gz: c57141042b502fbbfdf33bee545d90ad48172b8cf549950fa53d59f7500253f82f5e347155d4164077b780ae6e548c7e4f20fd44b4e3895fb5439f9ec6f843d0
7
+ data.tar.gz: ec40659eb174978cebb80c6e2f4ae9996fde55cdd5bc06f18756294cf19b2d876303c14b52127ae5fa22bb757e9d61b42e8c002e1aadff8f3b261bfdd65d1fa2
data/README.md CHANGED
@@ -81,7 +81,7 @@ and resets the default binding to the given object.
81
81
  **$XDG_CONFIG_DIRS** and **$XDG_CONFIG_HOME**. By default, these files would
82
82
  be '/etc/xdg/sr/config.rb' for system-wide configuration and
83
83
  '~/.config/sr/config.rb' for user-specific configuration. These files are
84
- simply Ruby files which are loaded into `sr` via `require`.
84
+ simply Ruby files which are loaded into `sr` via `Kernel#load`.
85
85
 
86
86
  You may also tell `sr` to load alternative configuration files (or suppress all
87
87
  configuration) with the '-f' option. Given no arguments, this option suppresses
@@ -94,8 +94,8 @@ $ sr -f # start with no configuration
94
94
  $ sr -f alt.rb # start with alt.rb as configuration
95
95
  ```
96
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`
97
+ In addition to this, `sr` simply uses `STDIN.gets` as its default input method;
98
+ if you would prefer to use Readline, simply add the following to your `sr`
99
99
  configuration file:
100
100
 
101
101
  ```ruby
data/bin/sr CHANGED
@@ -3,35 +3,8 @@
3
3
 
4
4
  require 'optparse'
5
5
 
6
- $0.start_with?('/') ? require('sr') : require_relative('../lib/sr')
7
-
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
27
-
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
35
-
36
- ARGV.empty? ? repl(options) : ($stderr.puts parser.help ; exit 1)
6
+ ($0 =~ /^\// ? %w(sr sr/cli) : %w(../lib/sr ../lib/sr/cli)).each do |file|
7
+ file =~ /^\./ ? require_relative(file) : require(file)
37
8
  end
9
+
10
+ SR::CLI.call
data/lib/sr.rb CHANGED
@@ -33,14 +33,17 @@ module SR
33
33
 
34
34
  @prompt = {
35
35
  error: proc { |e| "!! #{e.class}: #{e.message}" },
36
- input: proc { "== #{@context.receiver}:\n>> " },
36
+ input: proc do
37
+ c = @context
38
+ "== #{c.respond_to?(:receiver) ? c.receiver : c.eval('self')}:\n>> "
39
+ end,
37
40
  more: '.. ',
38
41
  return: proc { |v| "-> #{v.inspect}" },
39
42
  }
40
43
 
41
44
  @input_handler ||= ->(more) do
42
45
  print(call_prompt(more ? :more : :input))
43
- input = gets
46
+ input = STDIN.gets
44
47
  input = "#{more}#{input}" if more
45
48
  input
46
49
  end
@@ -108,8 +111,8 @@ module SR
108
111
  # given input
109
112
  # @return [Object] the return value of the given input
110
113
  private_class_method def self.read_eval_print(more = nil)
111
- input = @input_handler.call(more)
112
- value = @context.eval("_ = #{input}")
114
+ value = @context.eval(input = @input_handler.call(more), '(sr)', 0)
115
+ @context.local_variable_set(:_, value)
113
116
  puts call_prompt(:return, value) if @prompt[:return]
114
117
  rescue *@exception_handlers.keys => ex
115
118
  @exception_handlers.find { |(c,_)| ex.is_a?(c) }[1].call(ex, input, more)
@@ -124,7 +127,7 @@ module SR
124
127
  # `:config_files` (`Array<String>`) and `:context` (`#__binding__`)
125
128
  # @return [void]
126
129
  def self.repl(context = nil, **options)
127
- options.fetch(:config_files, CONFIGURATION_FILES).each(&method(:require))
130
+ options.fetch(:config_files, CONFIGURATION_FILES).each(&method(:load))
128
131
  context = context || options[:context]
129
132
  reset_binding(context) unless context.nil?
130
133
  @enabled = true
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SR
4
+ module CLI
5
+ def self.call
6
+ options = {}
7
+ parser = OptionParser.new do |opts|
8
+ opts.on('-c', '--context=obj', 'set the sr context to obj') do |obj|
9
+ options[:context] = eval(obj, TOPLEVEL_BINDING)
10
+ end
11
+ opts.on('-f [FILE]', 'suppress configuration (or load FILE)') do |f|
12
+ if f && File.exist?(f)
13
+ (options[:config_files] ||= []).push(File.expand_path(f))
14
+ else
15
+ fail "#{f} is not a valid file" if f
16
+ options[:config_files] = []
17
+ end
18
+ end
19
+ opts.on('-v', '--version', 'display sr version') do
20
+ puts "sr #{SR::VERSION} [ruby #{RUBY_VERSION} #{RUBY_PLATFORM}]"
21
+ exit
22
+ end
23
+ opts.on('-h', '--help', 'display this message') do
24
+ puts opts
25
+ exit
26
+ end
27
+ end
28
+ parser.parse!(ARGV)
29
+ SR.repl(options)
30
+ rescue SystemExit
31
+ # Do nothing -- this exists so the exception handling below doesn't pick
32
+ # up system exits from the option parser itself.
33
+ rescue Exception => ex
34
+ $stderr.puts ex.message
35
+ exit 1
36
+ end
37
+ end
38
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module SR
4
4
  # Semantic version of `sr`.
5
- VERSION = '1.1.0'
5
+ VERSION = '1.1.1'
6
6
  end
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.1.0
4
+ version: 1.1.1
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-23 00:00:00.000000000 Z
11
+ date: 2017-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sxdg
@@ -50,6 +50,7 @@ files:
50
50
  - UNLICENSE.md
51
51
  - bin/sr
52
52
  - lib/sr.rb
53
+ - lib/sr/cli.rb
53
54
  - lib/sr/core_patches.rb
54
55
  - lib/sr/input/readline.rb
55
56
  - lib/sr/version.rb
@@ -74,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  version: '0'
75
76
  requirements: []
76
77
  rubyforge_project:
77
- rubygems_version: 2.6.8
78
+ rubygems_version: 2.6.11
78
79
  signing_key:
79
80
  specification_version: 4
80
81
  summary: sr is a simple, tiny, hackable REPL for Ruby.