mister_bin 0.7.0 → 0.7.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
  SHA256:
3
- metadata.gz: a3079a8599aeffba78c30871ca736ab6c48dfac513400be8319c493ae24fd576
4
- data.tar.gz: 8b1a58023370dffafe99362ad4cddf211d1ea8f3ee5a1a88ca282ed8c68b94ea
3
+ metadata.gz: 513b43b53f6ec698926a9ab1238141b687b2e26b6fc874bac1573445b56b6d16
4
+ data.tar.gz: 8b7b49dc14d2a87675f9956e8b3307fe7b16a9e32b815f07f2fb1383466c29a0
5
5
  SHA512:
6
- metadata.gz: 8d62bc3efbe561981755ec5b90ffdfb9a3ab6efafbabc5281eb81e9aabbffbeec2c0601e658384aba05c401e5a396f9b0beabd265d36f6d9b1e5ddcdcf6423ea
7
- data.tar.gz: 317ea1137fdd923f27995fe933f7a4660579f8ca1e91a301701d5eaae44b54d3f9d2e1984fcc2738d5040d322bbffcb056b8f90f958997f7b939d85e128a3c4a
6
+ metadata.gz: 18acd89e58ee2fb9c57b60328233299cb28271f97f29624b94146f3112d55235af278631ca256e1557e6796c3662e82198f6907e67c70e00a83fb86776fba515
7
+ data.tar.gz: 666fdc19987cc2a0eeb2859325b25f6dd5cc90c5e62a309a1af958fda2991567dce62301a7c97d6a62f745e2b201acb363e12ec8e8726feba18b84baccbd85cb
data/README.md CHANGED
@@ -283,6 +283,19 @@ terminal = MisterBin::Terminal.new runner, {
283
283
  }
284
284
  ```
285
285
 
286
+ In addition, you may wish to provide your own code blocks for handling some
287
+ commands that are not handled by your runner. For example, this piece of code
288
+ will capture the `/cd ...` command from the terminal and pass it to your
289
+ block:
290
+
291
+ ```
292
+ terminal = MisterBin::Terminal.new runner
293
+ terminal.on '/cd' do |args|
294
+ Dir.chdir args[0] if args[0]
295
+ puts Dir.pwd
296
+ end
297
+ ```
298
+
286
299
  These are the available options. All string options are displayed with
287
300
  the [Colsole][5] `say` command so they support color markers.
288
301
 
@@ -321,6 +334,10 @@ The prefix character that if typed at the beginning of a command, will avoid
321
334
  executing the runner, and instead execute a system (shell) command.
322
335
  Default: `"/"`.
323
336
 
337
+ #### `disable_system_shell`
338
+
339
+ If true, commands that start with `/` will *not* be delegated to the stsrem.
340
+ Default: `false`.
324
341
 
325
342
 
326
343
  In the Wild
@@ -13,6 +13,10 @@ module MisterBin
13
13
  @options = options || {}
14
14
  end
15
15
 
16
+ def on(command, &block)
17
+ reserved_commands[command] = block
18
+ end
19
+
16
20
  def start
17
21
  Readline.completion_append_character = " "
18
22
  Readline.completion_proc = autocomplete_handler if autocomplete
@@ -23,28 +27,20 @@ module MisterBin
23
27
 
24
28
  private
25
29
 
26
- def safe_input_loop
27
- input_loop
28
- # :nocov:
29
- rescue Interrupt
30
- say exit_message if exit_message
31
- false
32
- rescue => e
33
- puts e.backtrace.reverse if ENV['DEBUG']
34
- say! "!txtred!#{e.class}: #{e.message}"
35
- true
36
- # :nocov:
30
+ def autocomplete
31
+ @autocomplete ||= options[:autocomplete]&.sort
37
32
  end
38
33
 
39
- def welcome_messages
40
- say header if header
41
- runner.run if show_usage
34
+ def autocomplete_handler
35
+ @autocomplete_handler ||= proc do |s|
36
+ # :nocov:
37
+ autocomplete.grep(/#{Regexp.escape(s)}/)
38
+ # :nocov:
39
+ end
42
40
  end
43
41
 
44
- def input_loop
45
- while input = Readline.readline(prompt, true) do
46
- break unless execute input
47
- end
42
+ def disable_system_shell
43
+ options[:disable_system_shell]
48
44
  end
49
45
 
50
46
  def execute(input)
@@ -60,47 +56,66 @@ module MisterBin
60
56
  def execute_command(input)
61
57
  command = Shellwords.shellwords input
62
58
 
63
- if command.first&.start_with? system_character
59
+ if reserved_commands.include? command.first
60
+ reserved_commands[command.first].call command[1..-1]
61
+ elsif !disable_system_shell and command.first&.start_with? system_character
64
62
  system input[1..-1]
65
63
  else
66
64
  runner.run command
67
65
  end
68
66
  end
69
67
 
68
+ def exit_message
69
+ @exit_message ||= options[:exit_message]
70
+ end
71
+
72
+ def exit_commands
73
+ @exit_commands ||= options[:exit_commands] || ['exit', 'q']
74
+ end
75
+
70
76
  def header
71
77
  @header ||= options[:header]
72
78
  end
73
79
 
74
- def show_usage
75
- options[:show_usage]
80
+ def input_loop
81
+ while input = Readline.readline(prompt, true) do
82
+ break unless execute input
83
+ end
76
84
  end
77
85
 
78
86
  def prompt
79
87
  @prompt ||= options[:prompt] || "\n> "
80
88
  end
81
89
 
82
- def autocomplete
83
- @autocomplete ||= options[:autocomplete]&.sort
90
+ def reserved_commands
91
+ @reserved_commands ||= {}
84
92
  end
85
93
 
86
- def exit_message
87
- @exit_message ||= options[:exit_message]
94
+ def safe_input_loop
95
+ input_loop
96
+ # :nocov:
97
+ rescue Interrupt
98
+ say exit_message if exit_message
99
+ false
100
+ rescue => e
101
+ puts e.backtrace.reverse if ENV['DEBUG']
102
+ say "!txtred!#{e.class}"
103
+ say e.message
104
+ true
105
+ # :nocov:
88
106
  end
89
107
 
90
- def exit_commands
91
- @exit_commands ||= options[:exit_commands] || ['exit', 'q']
108
+ def show_usage
109
+ options[:show_usage]
92
110
  end
93
111
 
94
112
  def system_character
95
113
  @system_character ||= options[:system_character] || '/'
96
114
  end
97
115
 
98
- def autocomplete_handler
99
- @autocomplete_handler ||= proc do |s|
100
- # :nocov:
101
- autocomplete.grep(/#{Regexp.escape(s)}/)
102
- # :nocov:
103
- end
116
+ def welcome_messages
117
+ say header if header
118
+ runner.run if show_usage
104
119
  end
105
120
 
106
121
  end
@@ -1,3 +1,3 @@
1
1
  module MisterBin
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mister_bin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-17 00:00:00.000000000 Z
11
+ date: 2019-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole