irb_hacks 0.2.4 → 0.2.5

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.
@@ -2,51 +2,79 @@ module IrbHacks
2
2
  module CoreExtensions #:nodoc:
3
3
  module Kernel #:nodoc:
4
4
  module SingletonMethods #:nodoc:
5
- # See InstanceMethods for documentation.
5
+ # @see {InstanceMethods#less}
6
6
  def less(*args, &block)
7
- if not block
8
- # Non-block invocation.
9
- if args.size < 1
10
- # We're interactive anyway. Why not give user a quick prompt?
11
- STDERR.puts "Nothing to show. Invoke as less(args) or less(options, &block)"
7
+ # Handle content & options.
8
+ content = []
9
+ o = {}
10
+ options = {}
11
+
12
+ begin
13
+ if block
14
+ args.each do |arg|
15
+ if arg.is_a? Hash
16
+ options.merge! arg
17
+ elsif [String, Symbol].any? {|klass| arg.is_a? klass}
18
+ options.merge!(arg.to_sym => true)
19
+ else
20
+ raise ArgumentError, "Unsupported argument: #{arg.inspect}"
21
+ end
22
+ end
23
+
24
+ o[k = :line] = options.delete(k)
25
+ o[k = :stderr] = options.delete(k)
12
26
  else
13
- File.popen(IrbHacks.conf.less_cmd, "w") do |f|
14
- f.puts args
27
+ # Non-block.
28
+ args.each do |arg|
29
+ if arg.is_a? Hash
30
+ options.merge! arg
31
+ else
32
+ content << arg
33
+ end
15
34
  end
16
- end
17
- else
18
- # Block invocation.
19
35
 
20
- # Handle options.
21
- options = {}
36
+ o[k = :line] = options.delete(k)
37
+ end # if block
22
38
 
23
- args.each do |arg|
24
- if arg.is_a? Hash
25
- ##p "arg hash", arg
26
- options.merge! arg
27
- elsif [Symbol, String].include? arg.class
28
- ##p "arg sym/str", arg
29
- options.merge! arg.to_sym => true
30
- else
31
- raise ArgumentError, "Unsupported argument #{arg.inspect}"
32
- end
39
+ if content.empty?
40
+ raise ArgumentError, "No content to browse"
33
41
  end
34
42
 
35
- o_stderr = (v = options.delete(:stderr)).nil?? false : v
43
+ if not options.empty?
44
+ raise ArgumentError, "Unknown option(s): #{options.inspect}"
45
+ end
46
+ rescue ArgumentError => e
47
+ # NOTE: We are interactive, print it friendly.
48
+ return STDERR.puts e.message
49
+ end # begin/rescue
50
+
51
+ ##DT.p "content", content
52
+ ##DT.p "options", options
53
+ ##DT.p "o", o
36
54
 
37
- raise ArgumentError, "Unknown option(s): #{options.inspect}" if not options.empty?
55
+ cmd = [
56
+ IrbHacks.conf.less_cmd,
57
+ ("+#{o[:line]}" if o[:line]),
58
+ ].compact.join(" ")
38
59
 
60
+ # Run the pager.
61
+ if block
39
62
  old_stdout = STDOUT.clone
40
- old_stderr = STDERR.clone if o_stderr
63
+ old_stderr = STDERR.clone if o[:stderr]
41
64
 
42
- File.popen(IrbHacks.conf.less_cmd, "w") do |f|
65
+ File.popen(cmd, "w") do |f|
43
66
  STDOUT.reopen(f)
44
- STDERR.reopen(f) if o_stderr
67
+ STDERR.reopen(f) if o[:stderr]
45
68
  yield
46
69
  STDOUT.reopen(old_stdout)
47
- STDERR.reopen(old_stderr) if o_stderr
70
+ STDERR.reopen(old_stderr) if o[:stderr]
71
+ end
72
+ else
73
+ # Non-block.
74
+ File.popen(cmd, "w") do |f|
75
+ f.puts content
48
76
  end
49
- end # if block
77
+ end
50
78
 
51
79
  nil
52
80
  end # less
@@ -55,12 +83,13 @@ module IrbHacks
55
83
  module InstanceMethods
56
84
  private
57
85
 
58
- # Dump program data with GNU <tt>less</tt> or the other configured OS pager.
86
+ # Browse program data with GNU <tt>less</tt> or other configured OS pager.
59
87
  #
60
88
  # Plain form:
61
89
  #
90
+ # less "hello"
62
91
  # less "hello", "world"
63
- # less mydata
92
+ # less data, :line => 10
64
93
  #
65
94
  # Block form:
66
95
  #
@@ -73,15 +102,9 @@ module IrbHacks
73
102
  # STDERR.puts "to stderr"
74
103
  # end
75
104
  #
76
- # Block form options:
77
- #
78
- # :stderr => T|F # Redirect STDERR too.
79
- #
80
- # If block form option is String or Symbol, it's automatically
81
- # converted to Hash like <tt>{:var => true}</tt>. Thus, you can write <tt>less(:stderr)</tt>
82
- # for <tt>less(:stderr => true)</tt>, they are functionally identical.
105
+ # less(:stderr) {...} # Shortcut form of saying `:stderr => true`.
83
106
  #
84
- # See also IrbHacks::Config::less_cmd.
107
+ # @see {IrbHacks::Config::less_cmd}
85
108
  def less(*args, &block)
86
109
  ::Kernel.less(*args, &block)
87
110
  end
@@ -1,4 +1,4 @@
1
1
  module IrbHacks
2
2
  # Gem version.
3
- VERSION = "0.2.4"
3
+ VERSION = "0.2.5"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: irb_hacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-01 00:00:00.000000000 Z
12
+ date: 2012-07-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -88,3 +88,4 @@ signing_key:
88
88
  specification_version: 3
89
89
  summary: Yet another set of IRB hacks
90
90
  test_files: []
91
+ has_rdoc: