tcollier-commando 0.1.2 → 0.2.0

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
  SHA1:
3
- metadata.gz: 5068609960424981817aae65f87d2bb72aff7e6c
4
- data.tar.gz: c29d26dfa5bbc1cb4d8520282978afa12ded2c6b
3
+ metadata.gz: 9bab78dc964fa7ce13e372cfed2902f152c3a0a2
4
+ data.tar.gz: c34eddbfb57a271a68c635d79d278c76053cb38b
5
5
  SHA512:
6
- metadata.gz: bc279c68c0428d8189770de8ecaf093dc5426cd14f23aca2fa8832906c2a13c68ec7828c608945130bc403e4a5c06c0d9c07ee9976a26e72af33a4d4a7e5c8fa
7
- data.tar.gz: a2a589ed3016084bf27f3eb3ece1613fea5dbd2dcaa92c6010b580dbf90cf08df96ac49c8ddad4e948eb8db0db84139f0d6d3e7b85efdde487d878011b7d4b01
6
+ metadata.gz: 57425515b9b2cd72748222130d6ea4ea39b65834c4315b3921c7e933505b70b4af0112ebb81f68c03155139437fd1af496f6dc842abe27b18d7df0fc5ee6df91
7
+ data.tar.gz: 8a7870cfe6aed8d9bab534d3161fbfacd448e7a0a1639b8682cc496db9b892197f20fd1ff78a454c4e113fbf3845cea2bbf79aa221f6ad47d71465cf5939bf19
data/README.md CHANGED
@@ -7,6 +7,7 @@ A command line interface builder with Readline support
7
7
  * `0.1.0` - Initial release
8
8
  * `0.1.1` - Alphabetize commands printed via `help`
9
9
  * `0.1.2` - Remove empty lines from history
10
+ * `0.2.0` - Persist history across CLI sessions
10
11
 
11
12
  ## Installation
12
13
 
@@ -31,9 +32,16 @@ of available commands to use.
31
32
 
32
33
  ```ruby
33
34
  Commando.configure do |config|
35
+ # The greeting to print when the CLI is started
34
36
  config.greeting = 'Welcome to my CLI. Type "help" for a list of commands'
37
+
38
+ # The prompt to print every time a new command is desired
35
39
  config.prompt = 'my-app> '
36
40
 
41
+ # An optional file where command line history is stored across sessions
42
+ config.history_file = '/tmp/.commando_history'
43
+
44
+ # Register multiple commands
37
45
  config.register 'addfriend', MyApp::AddFriend, 'Adds a friend to your network'
38
46
  end
39
47
  ```
data/lib/commando.rb CHANGED
@@ -1,16 +1,10 @@
1
1
  require_relative 'commando/config'
2
2
  require_relative 'commando/interpreter'
3
+ require_relative 'commando/io_handler'
3
4
  require_relative 'commando/quit_exception'
4
5
  require_relative 'commando/validation_error'
5
6
  require_relative 'commando/version'
6
7
 
7
- require 'readline'
8
- # Configure readline
9
- comp = proc { |s| Commando.config.commands.grep(/^#{Regexp.escape(s)}/) }
10
-
11
- Readline.completion_append_character = " "
12
- Readline.completion_proc = comp
13
-
14
8
  # Entry point for the Command Line Interface (CLI).
15
9
  #
16
10
  # Present the user with a text-based interface, where a prompt is printed,
@@ -29,38 +23,22 @@ module Commando
29
23
 
30
24
  # Begin the prompt, get input, process loop.
31
25
  def self.start(output: $stdout)
32
- Readline.output = output
33
26
  output.puts config.greeting
34
- quit = false
35
27
 
36
- loop do
37
- line = Readline.readline(config.prompt, true)
28
+ io = IOHandler.new(output: output)
38
29
 
39
- if line.nil?
40
- # When the user presses <CMD>+D, this comes through as nil. In that
41
- # case we want to exit
42
- output.puts
43
- break
44
- elsif line.strip == ''
45
- # If the user just hit enter without typing a command, remove that line
46
- # from history.
47
- Readline::HISTORY.pop
48
- else
49
- # When the user enters a non-empty string, pass the line to the
50
- # interpreter and handle the command.
51
- #
52
- # If the user enters an empty string (e.g. "" or " "), we simply
53
- # go back to the top of the loop to print out the prompt and wait
54
- # for the next command.
55
- interpreter = Interpreter.new(input: line, output: output)
56
-
57
- begin
30
+ loop do
31
+ begin
32
+ if line = io.readline
33
+ # When the user enters a non-empty string, pass the line to the
34
+ # interpreter and handle the command.
35
+ interpreter = Interpreter.new(input: line, output: output)
58
36
  interpreter.interpret
59
- rescue ValidationError => error
60
- output.puts "Error: #{error}"
61
- rescue QuitException
62
- break
63
37
  end
38
+ rescue ValidationError => error
39
+ output.puts "Error: #{error}"
40
+ rescue QuitException
41
+ break
64
42
  end
65
43
  end
66
44
  end
@@ -12,6 +12,8 @@ module Commando
12
12
  DEFAULT_GREETING =
13
13
  'Welcome to the commando interface. Type "help" to list available commands'
14
14
 
15
+ attr_accessor :history_file
16
+
15
17
  attr_writer :prompt, :greeting
16
18
 
17
19
  def initialize
@@ -0,0 +1,61 @@
1
+ require 'readline'
2
+
3
+ require_relative 'quit_exception'
4
+
5
+ module Commando
6
+ # Handle the prompt/input for the command line interface
7
+ class IOHandler
8
+ def initialize(output:)
9
+ @output = output
10
+
11
+ configure_readline
12
+ load_history
13
+ end
14
+
15
+ def readline
16
+ line = Readline.readline(Commando.config.prompt, true)
17
+ if line.nil?
18
+ # When the user presses <CMD>+D, this comes through as nil. In that
19
+ # case we want to exit
20
+ output.puts
21
+ raise QuitException
22
+ elsif line.strip == ''
23
+ # If the user just hit enter without typing a command, remove that line
24
+ # from history.
25
+ Readline::HISTORY.pop
26
+ nil
27
+ else
28
+ save_history(line)
29
+ line
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :output
36
+
37
+ def configure_readline
38
+ Readline.output = output
39
+ Readline.completion_proc =
40
+ proc { |s| Commando.config.commands.grep(/^#{Regexp.escape(s)}/) }
41
+ end
42
+
43
+ def load_history
44
+ if Commando.config.history_file
45
+ File.readlines(Commando.config.history_file).each do |line|
46
+ Readline::HISTORY.push(line.chomp)
47
+ end
48
+ end
49
+ end
50
+
51
+ def save_history(line)
52
+ if Commando.config.history_file
53
+ File.open(Commando.config.history_file, 'a') do |f|
54
+ f.puts line
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ private_constant :IOHandler
61
+ end
@@ -1,3 +1,3 @@
1
1
  module Commando
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcollier-commando
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Collier
@@ -76,6 +76,7 @@ files:
76
76
  - lib/commando/action/quit.rb
77
77
  - lib/commando/config.rb
78
78
  - lib/commando/interpreter.rb
79
+ - lib/commando/io_handler.rb
79
80
  - lib/commando/quit_exception.rb
80
81
  - lib/commando/validation_error.rb
81
82
  - lib/commando/version.rb