intar 2.3 → 2.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f69cab2073de237de8c934b10e796b5a424fde33d92ab2bc41ab1aa612da14b7
4
- data.tar.gz: 8095bb85ab4571c089859e0274c5e6df02e971f2d79334b20fceb93699857870
3
+ metadata.gz: 2bb2575fd8680cb18e04afcd3a09adb4565bc5ac37a4231db3b3b57e80074c6c
4
+ data.tar.gz: 8d2bf0a511a093e2b3d6feb8922a1da6466ce44ef0b77f016862f8d91e0f98dd
5
5
  SHA512:
6
- metadata.gz: ecc99bcf076cd30fa4463e229f5ce6fcc02d4952d360ab683b10693de718da6de06bb6155a09446b40f3b4d671a4755888c3752472aa6482fed3b542f260ef9f
7
- data.tar.gz: e0a1b9c484c4062a098b4881020aca84392a87bf99a6ff4f1a53826cf7841e90d13c2659e699ed066181295f537b11f36e349e0b4b4ccc55991ec25ef1e61f8e
6
+ metadata.gz: 3c22c8280845f8c99896196b9548ee0043fd5378d8d1da2f8c8c011f490e4dc342c638572935cac04e3815a5d03f9561e961a5cd515d3efc7f439d86bac446af
7
+ data.tar.gz: 054dc4d33647bf5aa286feb9b014610529de8051cfa5342f428b5a65e34175567c1eb786de38103a009a25e2ac0cf3627bfa1950b12530c3d44f7862c21e3de8
data/README CHANGED
@@ -18,6 +18,21 @@ Yet, Intar is more robust. Try this in Irb and in Intar:
18
18
 
19
19
 
20
20
 
21
+ == Usage
22
+
23
+ === Subcommands
24
+
25
+ obj.method & enter a sub-Intar
26
+ obj.each & enter multiple sub-Intars
27
+
28
+ \q exit from the innermost sub-Intar
29
+ \q! exit from the innermost sub-Intar loop
30
+ \q!! exit from all levels
31
+
32
+ \h get a list of all subcommands
33
+
34
+
35
+
21
36
  == Author
22
37
 
23
38
  Bertram Scharpf <software@bertram-scharpf.de>
data/lib/intar/prompt.rb CHANGED
@@ -16,8 +16,20 @@ class Intar
16
16
  @new = 0
17
17
  end
18
18
 
19
+ def push text
20
+ Readline.pre_input_hook = proc {
21
+ Readline.insert_text text
22
+ Readline.redisplay
23
+ }
24
+ nil
25
+ end
26
+
19
27
  def ask prompt
20
- l = Readline.readline prompt
28
+ begin
29
+ Readline.readline prompt
30
+ ensure
31
+ Readline.pre_input_hook = nil
32
+ end
21
33
  rescue Interrupt
22
34
  puts
23
35
  retry
@@ -34,12 +46,11 @@ class Intar
34
46
  i = Readline::HISTORY.length
35
47
  while i > 0 do
36
48
  i -= 1
37
- l = Readline::HISTORY[i]
38
- yield l
49
+ yield Readline::HISTORY[i]
39
50
  end
40
51
  end
41
52
 
42
- def push item
53
+ def push_history item
43
54
  item.empty? and return
44
55
  last != item or return
45
56
  Readline::HISTORY.push item
@@ -16,22 +16,24 @@ class Intar
16
16
  class Redirect
17
17
  def redirect_output
18
18
  out = outfile
19
- stdin, stdout = $stdin.dup, $stdout.dup
20
- $stdin .reopen "/dev/null"
21
- $stdout.reopen out
22
- yield
23
- ensure
24
- $stdin .reopen stdin
25
- $stdout.reopen stdout
26
- out.close
19
+ begin
20
+ stdin, stdout = $stdin.dup, $stdout.dup
21
+ $stdin .reopen "/dev/null"
22
+ $stdout.reopen out
23
+ yield
24
+ ensure
25
+ $stdin .reopen stdin
26
+ $stdout.reopen stdout
27
+ out.close
28
+ end
27
29
  end
28
30
  end
29
31
 
30
32
  class RedirectPipe < Redirect
31
33
  class <<self
32
34
  def detect line, pager
33
- if line.slice! /\s+\|\z/ then
34
- new pager
35
+ if line.slice! /\s+\|(\b[^|&;{}()\[\]]*)?\z/ then
36
+ new $1||pager
35
37
  end
36
38
  end
37
39
  end
@@ -39,7 +41,9 @@ class Intar
39
41
  @pager = pager||ENV[ "PAGER"]||"more"
40
42
  end
41
43
  def outfile
42
- IO.popen @pager.to_s, "w" rescue raise Failed, "Pipe error: #$!"
44
+ IO.popen @pager.to_s, "w"
45
+ rescue Errno::ENOENT
46
+ raise Failed, "Pipe error: #$!"
43
47
  end
44
48
  end
45
49
 
data/lib/intar/version.rb CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  class Intar
6
6
 
7
- VERSION = "2.3".freeze
7
+ VERSION = "2.6".freeze
8
8
 
9
9
  end
10
10
 
data/lib/intar.rb CHANGED
@@ -63,7 +63,7 @@ class Intar
63
63
 
64
64
 
65
65
  DEFAULTS = {
66
- prompt: "%(32)c%i%c:%1c%03n%c%> ",
66
+ prompt: "%(32)c%16i%c:%1c%d:%3n%c%> ",
67
67
  color: true,
68
68
  show: 1,
69
69
  shownil: false,
@@ -74,56 +74,66 @@ class Intar
74
74
  histmax: 500,
75
75
  }
76
76
 
77
+ @@current = nil
78
+
79
+ attr_reader :params, :prompt, :depth, :n
77
80
  def initialize obj = nil, **params
78
81
  @obj = obj.nil? ? (eval "self", TOPLEVEL_BINDING) : obj
79
- @params = DEFAULTS.dup.update params
80
- @binding = @obj.intar_binding
82
+ if @@current then
83
+ @params = @@current.params
84
+ @prompt = @@current.prompt
85
+ @depth = @@current.depth + 1
86
+ else
87
+ @params = DEFAULTS.dup.update params
88
+ @prompt = Prompt.new
89
+ @depth = 0
90
+ end
81
91
  @n = 1
82
- @prompt = Prompt.new
92
+
93
+ @binding = @obj.intar_binding
83
94
  end
84
95
 
85
96
 
86
97
  class Quit < Exception ; end
87
- class Failed < Exception ; end
98
+ class Bye < Quit ; end
99
+ class Break < Bye ; end
100
+ class Failed < StandardError ; end
88
101
 
89
102
  def run
90
- prompt_load_history
91
- oldset = eval OLDSET, @binding
92
- loop do
93
- begin
94
- l = readline
95
- l or break
96
- @redir = find_redirect l
97
- r = if l =~ /\A\\(\w+|.)\s*(.*?)\s*\Z/ then
98
- m = get_metacommand $1
99
- send m.method, (eval_param $2)
100
- else
101
- begin
102
- @redir.redirect_output do eval l, @binding, @file end
103
+ handle_history do
104
+ set_current do
105
+ oldset = eval OLDSET, @binding
106
+ loop do
107
+ l = readline
108
+ l or break
109
+ @redir = find_redirect l
110
+ r = begin
111
+ if l =~ /\A\\(\w+|.)\s*(.*?)\s*\Z/ then
112
+ send (get_metacommand $1).method, (eval_param $2)
113
+ else
114
+ l.sub! %r/\s*&\s*\z/, SUB
115
+ @redir.redirect_output do eval l, @binding, @file end
116
+ end
117
+ rescue Bye
118
+ raise if @depth.nonzero?
119
+ break
120
+ rescue Quit
121
+ break
103
122
  rescue SyntaxError
104
123
  raise if l.end_with? $/
105
124
  @previous = l
106
125
  next
126
+ rescue Exception
127
+ break if SystemExit === $! and not @params[ :catch_exit]
128
+ show_exception
129
+ $!
107
130
  end
131
+ display r
132
+ oldset.call r, @n
133
+ @n += 1
108
134
  end
109
- rescue Quit
110
- break
111
- rescue Failed
112
- switchcolor 31, 1
113
- puts $!
114
- switchcolor
115
- r = $!
116
- rescue Exception
117
- break if SystemExit === $! and not @params[ :catch_exit]
118
- show_exception
119
- r = $!
120
- else
121
- display r
122
135
  end
123
- oldset.call r, @n
124
- @n += 1
125
136
  end
126
- prompt_save_history
127
137
  end
128
138
 
129
139
  def execute code
@@ -133,6 +143,26 @@ class Intar
133
143
 
134
144
  private
135
145
 
146
+ def handle_history
147
+ unless @depth.nonzero? then
148
+ begin
149
+ prompt_load_history
150
+ yield
151
+ ensure
152
+ prompt_save_history
153
+ end
154
+ else
155
+ yield
156
+ end
157
+ end
158
+
159
+ def set_current
160
+ old, @@current = @@current, self
161
+ yield
162
+ ensure
163
+ @@current = old
164
+ end
165
+
136
166
  def find_redirect line
137
167
  RedirectPipe.detect line, @params[ :pager] or
138
168
  RedirectFile.detect line, @params[ :output] or
@@ -140,6 +170,14 @@ class Intar
140
170
  end
141
171
 
142
172
 
173
+ SUB = <<~EOT
174
+ \ do |obj|
175
+ Intar.run obj
176
+ rescue Intar::Break
177
+ break
178
+ end
179
+ EOT
180
+
143
181
  OLDSET = <<~EOT
144
182
  _, __, ___ = nil, nil, nil
145
183
  proc { |r,n|
@@ -158,44 +196,56 @@ class Intar
158
196
 
159
197
  def cur_prompt prev
160
198
  p = @params[ :prompt].to_s
161
- p.gsub /%(?:
162
- \(([^\)]+)?\)
163
- |
164
- ([+-]?[0-9]+(?:\.[0-9]+)?)
165
- )?(.)/nx do
166
- case $3
167
- when "s" then @obj.to_s
168
- when "i" then $1 ? (@obj.send $1) : @obj.inspect
169
- when "n" then "%#$2d" % @n
170
- when "t" then Time.now.strftime $1||"%X"
171
- when "u" then Etc.getpwuid.name
172
- when "h" then Socket.gethostname
173
- when "w" then cwd_short
174
- when "W" then File.basename cwd_short
175
- when "c" then color *($1 || $2 || "").split.map { |x| x.to_i }
199
+ p.gsub /%
200
+ (\d+)?
201
+ (?:\(([^)]*)\)|\{([^}]*)\})?
202
+ (.)/nx do
203
+ sub = $2||$3
204
+ case $4
205
+ when "s" then str_axe $1, @obj.to_s
206
+ when "i" then str_axe $1, (sub ? (@obj.send sub) : @obj.inspect)
207
+ when "C" then str_axe $1, @obj.class.name
208
+ when "n" then "%0#$1d" % @n
209
+ when "d" then "%0#$1d" % @depth
210
+ when "t" then str_axe $1, (Time.now.strftime sub||"%X")
211
+ when "u" then str_axe $1, Etc.getpwuid.name
212
+ when "h" then str_axe $1, Socket.gethostname
213
+ when "w" then str_axe $1, cwd_short
214
+ when "W" then str_axe $1, (File.basename cwd_short)
215
+ when "c" then color [$1,sub].compact.map { |c| c.scan %r/\d+/ }.flatten
176
216
  when ">" then prev ? "." : Process.uid == 0 ? "#" : ">"
177
- when "%" then $3
217
+ when "%" then $4
178
218
  else $&
179
219
  end
180
220
  end
181
221
  end
182
222
 
183
- def color *c
223
+ def str_axe len, str
224
+ if len then
225
+ str.axe len.to_i
226
+ else
227
+ str
228
+ end
229
+ end
230
+
231
+ def color codes
184
232
  if @params[ :color] then
185
- s = c.map { |i| "%d" % i }.join ";"
186
- "\e[#{s}m"
233
+ "\e[#{codes.join ';'}m"
187
234
  end
188
235
  end
189
236
 
190
237
  def switchcolor *c
191
- s = color *c
238
+ s = color c
192
239
  print s
193
240
  end
194
241
 
195
242
  def cwd_short
196
243
  r = Dir.pwd
197
244
  h = Etc.getpwuid.dir
198
- r[ 0, h.length] == h and r[ 0, h.length] = "~"
245
+ if r[ 0, h.length] == h then
246
+ n = r[ h.length]
247
+ r[ 0, h.length] = "~" if !n || n == "/"
248
+ end
199
249
  r
200
250
  end
201
251
 
@@ -205,7 +255,7 @@ class Intar
205
255
  cp = cur_prompt r
206
256
  l = @prompt.ask cp
207
257
  return if l.nil?
208
- @prompt.push l unless !r and @params[ :histhid] and l =~ /\A[ \t]+/
258
+ @prompt.push_history l unless !r and @params[ :histhid] and l =~ /\A[ \t]+/
209
259
  if r then
210
260
  r << $/ << l
211
261
  else
@@ -246,11 +296,11 @@ class Intar
246
296
 
247
297
  def show_exception
248
298
  unless $!.to_s.empty? then
249
- switchcolor 31, 1
299
+ switchcolor 1, 31
250
300
  print $!
251
301
  print " " unless $!.to_s =~ /\s\z/
252
302
  end
253
- switchcolor 31, 22
303
+ switchcolor 22, 31
254
304
  puts "(#{$!.class})"
255
305
  switchcolor 33
256
306
  $@.each { |b|
@@ -351,36 +401,36 @@ class Intar
351
401
  Leave Intar.
352
402
  EOT
353
403
  def cmd_quit x
354
- raise Quit
404
+ lx = $&.length.nonzero? if x =~ /!*/
405
+ raise lx ? (lx > 1 ? Bye : Break) : Quit
355
406
  end
356
407
 
357
408
  metacmd %w(cd), "Change directory", <<~EOT
358
409
  Switch to a different working directory.
359
- Former directories will be kept in a stack.
410
+ Former directories will be pushed to a stack.
360
411
 
361
- %[N] exchange with stack item #N
362
- =[N] drop and set to stack item #N
363
- -[N] drop stack item #N
412
+ N|PATH|% change directory and push
413
+ -N|PATH|% drop stack item
414
+ =N|PATH|% change directory but do not push
364
415
 
365
- Default N is 1.
416
+ Default N or % is 1.
366
417
  EOT
367
418
  def cmd_cd x
368
419
  @wds ||= []
369
420
  y = Dir.getwd
370
- if x =~ /\A([%=-])?(\d+)?\z/ then
371
- x = $2 ? (@wds.delete_at -$2.to_i) : @wds.pop
372
- x or raise Failed, ($2 ? "No directory ##$2." : "No last directory.")
373
- case $1
374
- when "-" then x = nil
375
- when "=" then y = nil
376
- end
377
- end
378
421
  if x then
379
- x = File.expand_path x
380
- Dir.chdir x rescue raise Failed, $!.to_s
381
- @wds.push y if y
382
- y = Dir.getwd
383
- @wds.delete y
422
+ cmd = x.slice! /\A[=-]/
423
+ x = case x
424
+ when /\A\d+\z/ then @wds[ -x.to_i] or raise Failed, "No directory ##{x}."
425
+ when /\A%?\z/ then @wds.last or raise Failed, "No last directory."
426
+ else File.expand_path x
427
+ end
428
+ @wds.delete x
429
+ if cmd != "-" then
430
+ Dir.chdir x
431
+ @wds.push y if cmd != "="
432
+ y = x
433
+ end
384
434
  end
385
435
  @redir.redirect_output do
386
436
  i = @wds.length
@@ -467,7 +517,7 @@ class Intar
467
517
  EOT
468
518
  def cmd_input x
469
519
  x or raise Failed, "No input file given."
470
- l = File.read x rescue raise Failed, $!.to_s
520
+ l = File.read x
471
521
  @redir.redirect_output do
472
522
  eval l, @binding, x
473
523
  end
@@ -478,7 +528,7 @@ class Intar
478
528
  EOT
479
529
  def cmd_output x
480
530
  if x then
481
- File.open x, "w" do end rescue raise Failed, "File error: #$!"
531
+ File.open x, "w" do end
482
532
  end
483
533
  @params[ :output] = x
484
534
  end
@@ -498,7 +548,6 @@ class Intar
498
548
  p = File.read fn
499
549
  p.strip!
500
550
  @prompt.push p
501
- @redir.redirect_output do eval p, @binding, @file end
502
551
  ensure
503
552
  File.unlink fn
504
553
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intar
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.3'
4
+ version: '2.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-18 00:00:00.000000000 Z
11
+ date: 2022-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appl