ruby-shell 0.3.1 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -0
  3. data/bin/rsh +80 -63
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae06a31d3704b03f547c511116723ebfc3b890000fb74fac0b5cd098a44dbad5
4
- data.tar.gz: 81a6f4a939ebed04a679579e16daa6921d25a45bed4f30e8b59caba5c1300e75
3
+ metadata.gz: 03f10b7b8ca979952b3a717420c8cc6002c2ad443933b7006ba9c460e83ef09e
4
+ data.tar.gz: fee02bd24b9796a4ab8852fa4a9ca6d591942b4aa08461e7decaf7ecf73da334
5
5
  SHA512:
6
- metadata.gz: 40bf4cf2c129d0ef8bdc114aec76e96b41b961467b2cc72bf962a2f7bb43d2225f6b9d2a142c7c90037ef4b526e35cb4e892906b984f22a85a142b1f9b0b5ae3
7
- data.tar.gz: 1cd7e4bc2ad55bf3906d107702f604e1931d555cad50b491a573556d4cc23d0273980aa0744f803dbec1d6f23a582f6adf7904f473b257917e06a00c0cf85a14
6
+ metadata.gz: 817094937e32711d26dcc291c5c470e85a1f3901a335c427eede150465747f0761939ce5ccd686630bc3daf0200976533d4fe5e9f3259a364ed384c983aafb0f
7
+ data.tar.gz: 1f6401e2afef2bb22089b5e28e5c977cf42b85e66182a223e780c25319ec77e168588afc74ea9713224e20bbd1d892da52510c66dc02876b1cec67013c09d231
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # rsh
2
+ ![Ruby](https://img.shields.io/badge/language-Ruby-red) [![Gem Version](https://badge.fury.io/rb/ruby-shell.svg)](https://badge.fury.io/rb/ruby-shell) ![Unlicense](https://img.shields.io/badge/license-Unlicense-green) ![Stay Amazing](https://img.shields.io/badge/Stay-Amazing-important)
3
+
2
4
  The Ruby SHell
3
5
 
4
6
  # Why?
@@ -29,6 +31,9 @@ Add command nicks (aliases) with `:nick "some_nick = some_command"`, e.g. `:nick
29
31
  ## Tab completion
30
32
  You can tab complete almost anything. Hitting `TAB` will try to complete in this priority: nicks, gnicks, commands, dirs/files. Hitting `TAB`after a `-` will list the command switches for the preceding command with a short explanation (from the command's --help), like this `ls -`(`TAB`) will list all the switches/options for the `ls` command.
31
33
 
34
+ ## Integrations
35
+ rsh is integrated with the [rtfm file manager](https://github.com/isene/RTFM) and with [fzf](https://github.com/junegunn/fzf). Just enter the command `r` and rtfm will be launched - and when you quit the file manager, you will drop back into rsh in the directory you where you exited rtfm. Enter the command `fzf` to launch the fuzzy finder - select the directory/file you want, press `ENTER` and you will find yourself in the directory where that item resides.
36
+
32
37
  ## Syntax highlighting
33
38
  rsh will highlight nicks, gnicks, commands and dirs/files as they are written on the command line.
34
39
 
@@ -44,6 +49,7 @@ Variable | Description
44
49
  `@c_path` | Color for valid path
45
50
  `@c_tabselect` | Color for selected tabcompleted item
46
51
  `@c_taboption` | Color for unselected tabcompleted item
52
+ `@c_stamp` | Color for time stamp/command
47
53
 
48
54
  # Enter the world of Ruby
49
55
  By entering `:some-ruby-command` you have full access to the Ruby universe right from your command line. You can do anything from `:puts 2 + 13` or `:if 0.7 > Math::sin(34) then puts "OK" end` or whatever tickles you fancy.
data/bin/rsh CHANGED
@@ -101,31 +101,31 @@ begin # Requires
101
101
  end
102
102
  begin # Initialization
103
103
  # Theming
104
- @c_prompt = 196 # Color for basic prompt
105
- @c_cmd = 48 # Color for valid command
106
- @c_nick = 51 # Color for matching nick
107
- @c_gnick = 87 # Color for matching gnick
108
- @c_path = 208 # Color for valid path
109
- @c_switch = 148 # Color for switches/options
110
- @c_tabselect = 207 # Color for selected tabcompleted item
111
- @c_taboption = 244 # Color for unselected tabcompleted item
104
+ @c_prompt = 196 # Color for basic prompt
105
+ @c_cmd = 48 # Color for valid command
106
+ @c_nick = 51 # Color for matching nick
107
+ @c_gnick = 87 # Color for matching gnick
108
+ @c_path = 208 # Color for valid path
109
+ @c_switch = 148 # Color for switches/options
110
+ @c_tabselect = 207 # Color for selected tabcompleted item
111
+ @c_taboption = 244 # Color for unselected tabcompleted item
112
+ @c_stamp = 244 # Color for time stamp/command
112
113
  # Prompt
113
- @user = Etc.getlogin # For use in @prompt
114
- @node = Etc.uname[:nodename] # For use in @prompt
115
- @prompt = "rsh >".c(@c_prompt).b # Very basic prompt if not defined in .rshrc
114
+ @prompt = "rsh > ".c(@c_prompt).b # Very basic prompt if not defined in .rshrc
116
115
  # Hash & array initializations
117
- @nick = {} # Initiate alias/nick hash
118
- @gnick = {} # Initiate generic/global alias/nick hash
119
- @history = [] # Initiate history array
116
+ @nick = {} # Initiate alias/nick hash
117
+ @gnick = {} # Initiate generic/global alias/nick hash
118
+ @history = [] # Initiate history array
120
119
  # Paths
120
+ @user = Etc.getpwuid(Process.euid).name # For use in @prompt
121
121
  @path = ["/bin",
122
122
  "/usr/bin",
123
- "/home/#{@user}/bin"] # Basic paths for executables if not set in .rshrc
123
+ "/home/#{@user}/bin"] # Basic paths for executables if not set in .rshrc
124
124
  # History
125
- @histsize = 100 # Max history if not set in .rshrc
126
- @hloaded = false # Variable to determine if history is loaded
125
+ @histsize = 100 # Max history if not set in .rshrc
126
+ @hloaded = false # Variable to determine if history is loaded
127
127
  # Variable initializations
128
- @cmd = "" # Initiate variable @cmd
128
+ @cmd = "" # Initiate variable @cmd
129
129
  end
130
130
  # GENERIC FUNCTIONS
131
131
  def getchr # Process key presses
@@ -184,9 +184,9 @@ def getstr # A custom Readline-like function
184
184
  text = @history_copy[@stk]
185
185
  @c.clear_line
186
186
  print @prompt
187
- row, pos0 = @c.pos
187
+ row, @pos0 = @c.pos
188
188
  print cmd_check(text)
189
- @c.col(pos0 + @pos)
189
+ @c.col(@pos0 + @pos)
190
190
  chr = getchr
191
191
  return "\n" if chr == "C-G" # Ctrl-G escapes the reasdline
192
192
  case chr
@@ -225,7 +225,7 @@ def getstr # A custom Readline-like function
225
225
  @history_copy[@stk][@pos] = ""
226
226
  end
227
227
  when 'WBACK' # Delete one word to the left (Ctrl-W)
228
- unless @pos == pos0
228
+ unless @pos == @pos0
229
229
  until @history_copy[@stk][@pos - 1] == " " or @pos == 0
230
230
  @pos -= 1
231
231
  @history_copy[@stk][@pos] = ""
@@ -244,25 +244,29 @@ def getstr # A custom Readline-like function
244
244
  @history_copy[@stk] = ""
245
245
  @pos = 0
246
246
  when 'TAB' # Tab completion of dirs and files
247
- @tabstr = @history_copy[@stk][0...@pos]
248
- @tabend = @history_copy[@stk][@pos..]
249
- elements = @tabstr.split(" ")
250
- elements.append("") if @tabstr.match(" $")
247
+ @tabstr = @history_copy[@stk][0...@pos]
248
+ @tabend = @history_copy[@stk][@pos..]
249
+ elements = @tabstr.split(" ")
250
+ if @tabstr.match(" $")
251
+ elements.append("")
252
+ @tabsearch = ""
253
+ else
254
+ @tabsearch = elements.last.to_s
255
+ @tabstr = @tabstr[0...-@tabsearch.length]
256
+ end
251
257
  i = elements.length - 1
252
- m = elements[i].to_s
253
- if m == "-"
258
+ if @tabsearch =~ /^-/
254
259
  until i == 0
255
260
  i -= 1
256
261
  if elements[i] !~ /^-/
257
- @tabstr.chop!
258
262
  tab_switch(elements[i])
259
263
  break
260
264
  end
261
265
  end
262
266
  else
263
- tab_all(m)
267
+ tab_all(@tabsearch)
264
268
  end
265
- @history_copy[@stk] = @tabstr + @tabend
269
+ @history_copy[@stk] = @tabstr.to_s + @tabsearch.to_s + @tabend.to_s
266
270
  when /^.$/
267
271
  @history_copy[@stk].insert(@pos,chr)
268
272
  @pos += 1
@@ -277,13 +281,17 @@ def getstr # A custom Readline-like function
277
281
  @history[0]
278
282
  end
279
283
  def tab_switch(str) # TAB completion for command switches (TAB after "-")
280
- hlp = `#{str} --help`
281
- hlp = hlp.split("\n").grep(/^\s*-{1,2}[^-]/)
282
- hlp = hlp.map {|h| h.sub(/^\s*/, '')}
283
- switch, tab = tabselect(hlp)
284
- switch = switch.sub(/ .*/, '').sub(/,/, '')
285
- @tabstr += switch
286
- @pos = @tabstr.length
284
+ begin
285
+ hlp = `#{str} --help`
286
+ hlp = hlp.split("\n").grep(/^\s*-{1,2}[^-]/)
287
+ hlp = hlp.map {|h| h.sub(/^\s*/, '')}
288
+ switch = tabselect(hlp)
289
+ switch = switch.sub(/ .*/, '').sub(/,/, '')
290
+ @tabsearch.chop!
291
+ @tabsearch += switch
292
+ @pos = @tabstr.length + @tabsearch.length
293
+ rescue
294
+ end
287
295
  end
288
296
  def tab_all(str) # TAB completion for Dirs/files, nicks and commands
289
297
  exe = []
@@ -294,29 +302,18 @@ def tab_all(str) # TAB completion for Dirs/files, nicks and commands
294
302
  end
295
303
  exe.prepend(*@nick.keys)
296
304
  exe.prepend(*@gnick.keys)
297
- compl = exe.select {|s| s =~ Regexp.new("^" + str)}
298
- fdir = File.expand_path(str)
299
- fdir += "/" if Dir.exist?(fdir)
300
- fdir += "*"
305
+ compl = exe.select {|s| s =~ Regexp.new("^" + str)}
306
+ fdir = str
307
+ fdir += "/" if Dir.exist?(fdir)
308
+ fdir += "*"
301
309
  compl.prepend(*Dir.glob(fdir))
302
- @tabstr = @tabstr.sub(/#{str}$/, '') unless compl.empty?
303
- match, tab = tabselect(compl) unless compl.empty?
304
- @tabstr += match if match
305
- @tabstr += str if match == ""
306
- @pos = @tabstr.length if match
307
- end
308
- def nextline # Handle going to the next line in the terminal
309
- row, col = @c.pos
310
- if row == @maxrow
311
- @c.scroll_down
312
- @c_row -= 1
313
- end
314
- @c.next_line
310
+ match = tabselect(compl) unless compl.empty?
311
+ @tabsearch = match if match
312
+ @pos = @tabstr.length + @tabsearch.length if match
315
313
  end
316
314
  def tabselect(ary) # Let user select from the incoming array
317
315
  @c_row, @c_col = @c.pos
318
316
  chr = ""
319
- tab = false
320
317
  i = 0
321
318
  ary.length < 5 ? l = ary.length : l = 5
322
319
  while chr != "ENTER"
@@ -324,11 +321,15 @@ def tabselect(ary) # Let user select from the incoming array
324
321
  l.times do |x|
325
322
  if x == 0
326
323
  @c.clear_line
327
- print "#{@prompt}#{@tabstr}#{ary[i].sub(/(.*?)[ ,].*/, '\1')}#{@tabend}"
324
+ tabstart = cmd_check(@tabstr)
325
+ tabchoice = ary[i].sub(/(.*?)[ ,].*/, '\1')[@tabsearch.length..]
326
+ tabline = "#{@prompt}#{tabstart}#{@tabsearch.c(@c_tabselect)}#{tabchoice}#{@tabend}"
327
+ print tabline # Full command line
328
+ @c_col = @pos0 + @tabstr.length + @tabsearch.length + tabchoice.length
328
329
  nextline
329
- print " #{ary[i]}".c(@c_tabselect)
330
+ print " #{ary[i]}".c(@c_tabselect) # Top option selected
330
331
  else
331
- print " #{ary[x+i]}".c(@c_taboption)
332
+ print " #{ary[x+i]}".c(@c_taboption) # Next option unselected
332
333
  end
333
334
  nextline
334
335
  end
@@ -343,15 +344,30 @@ def tabselect(ary) # Let user select from the incoming array
343
344
  i += 1 unless i > ary.length - 2
344
345
  when 'UP'
345
346
  i -= 1 unless i == 0
346
- when'TAB'
347
+ when 'TAB'
347
348
  chr = "ENTER"
348
- tab = true
349
+ when 'BACK'
350
+ @tabsearch.chop! unless @tabsearch == ''
351
+ tab_all(@tabsearch)
352
+ return @tabsearch
353
+ when /^[[:print:]]$/
354
+ @tabsearch += chr
355
+ tab_all(@tabsearch)
356
+ return @tabsearch
349
357
  end
350
358
  end
351
359
  @c.clear_screen_down
352
360
  @c.row(@c_row)
353
361
  @c.col(@c_col)
354
- return ary[i], tab
362
+ return ary[i]
363
+ end
364
+ def nextline # Handle going to the next line in the terminal
365
+ row, col = @c.pos
366
+ if row == @maxrow
367
+ @c.scroll_down
368
+ @c_row -= 1
369
+ end
370
+ @c.next_line
355
371
  end
356
372
  def hist_clean # Clean up @history
357
373
  @history.uniq!
@@ -445,13 +461,14 @@ end
445
461
 
446
462
  # MAIN PART
447
463
  loop do
464
+ @user = Etc.getpwuid(Process.euid).name # For use in @prompt
465
+ @node = Etc.uname[:nodename] # For use in @prompt
448
466
  h = @history; load(Dir.home+'/.rshrc') if File.exist?(Dir.home+'/.rshrc'); @history = h # reload prompt but not history
449
467
  @prompt.gsub!(/#{Dir.home}/, '~') # Simplify path in prompt
450
468
  @cmd = getstr # Main work is here
451
469
  hist_clean # Clean up the history
452
470
  @cmd = "ls" if @cmd == "" # Default to ls when no command is given
453
471
  print "\n" # Newline
454
- if @cmd == "x" then rshrc; break; end # A simple way to exit rsh
455
472
  if @cmd == "r" # Integration with rtfm (https://github.com/isene/RTFM)
456
473
  File.write("/tmp/.rshpwd", Dir.pwd)
457
474
  system("rtfm /tmp/.rshpwd")
@@ -471,7 +488,7 @@ loop do
471
488
  @cmd = @cmd.gsub(Regexp.union(ca.keys), @nick)
472
489
  ga = @gnick.transform_keys {|k| /\b#{Regexp.escape k}\b/}
473
490
  @cmd = @cmd.gsub(Regexp.union(ga.keys), @gnick)
474
- puts "#{Time.now.strftime("%H:%M:%S")}: #{@cmd}".c(244)
491
+ puts "#{Time.now.strftime("%H:%M:%S")}: #{@cmd}".c(@c_stamp)
475
492
  begin
476
493
  if @cmd == "fzf" # fzf integration (https://github.com/junegunn/fzf)
477
494
  res = `#{@cmd}`.chomp
metadata CHANGED
@@ -1,18 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-shell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geir Isene
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-24 00:00:00.000000000 Z
11
+ date: 2023-05-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'A shell written in Ruby with extensive tab completions, aliases/nicks,
14
- history, syntax highlighting and theming. New in 0.3.1: Removed unwanted control
15
- chars in input.'
14
+ history, syntax highlighting and theming. New in 0.4: Much better tab completion.'
16
15
  email: g@isene.com
17
16
  executables:
18
17
  - rsh