harp 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/examples/usage.rb +1 -1
  2. data/lib/harp.rb +4 -4
  3. data/lib/harp/repl.rb +24 -1
  4. metadata +4 -4
data/examples/usage.rb CHANGED
@@ -13,9 +13,9 @@ class UsefulThing
13
13
 
14
14
  # Set it up
15
15
  setup_harp do |harp|
16
- command_names = harp.command_names.select {|name| name.size > 1 }
17
16
 
18
17
  command("help", :alias => "h") do
18
+ command_names = harp.command_names.select {|name| name.size > 1 }
19
19
  puts "* Available commands: " << command_names.sort.join(", ")
20
20
  puts "* Tab completion works for commands."
21
21
  end
data/lib/harp.rb CHANGED
@@ -16,12 +16,12 @@ module Harp
16
16
  @command_manager.instance_exec(command_manager, &block)
17
17
  end
18
18
 
19
- def self.repl
20
- REPL.new(@command_manager)
19
+ def self.repl(options={})
20
+ REPL.new(@command_manager, options)
21
21
  end
22
22
 
23
- def repl
24
- self.class.repl.run(self)
23
+ def repl(options={})
24
+ self.class.repl(options).run(self)
25
25
  end
26
26
 
27
27
  def self.cli
data/lib/harp/repl.rb CHANGED
@@ -10,9 +10,25 @@ module Harp
10
10
  class REPL
11
11
  Prompt = "<3: "
12
12
 
13
- def initialize(command_manager)
13
+ def initialize(command_manager, options={})
14
14
  @command_manager = command_manager
15
15
  Readline.completion_proc = self.method(:complete)
16
+ if options[:history_file]
17
+ limit = options[:history_limit] || 100
18
+ if File.exist?(options[:history_file])
19
+ string = File.read(options[:history_file])
20
+ string.each_line do |line|
21
+ Readline::HISTORY << line.chomp
22
+ end
23
+ end
24
+ at_exit do
25
+ File.open(options[:history_file], "w") do |f|
26
+ Readline::HISTORY.to_a.last(limit).each do |line|
27
+ f.puts line unless line.empty?
28
+ end
29
+ end
30
+ end
31
+ end
16
32
  end
17
33
 
18
34
  def commands
@@ -71,6 +87,13 @@ module Harp
71
87
  stty_save = `stty -g`.chomp
72
88
  trap("INT") { system "stty", stty_save; exit }
73
89
  while @run && (line = Readline.readline(Prompt, true).strip)
90
+
91
+ # Don't fill history with immediate duplicates.
92
+ one, two = Readline::HISTORY.to_a.last(2)
93
+ if one == two
94
+ Readline::HISTORY.pop
95
+ end
96
+
74
97
  # Treat ! as the shell out command
75
98
  if line[0] == "!"
76
99
  system(line.slice(1..-1))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
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: 2013-01-07 00:00:00.000000000 Z
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: starter
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.1.1
21
+ version: 0.1.7
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.1.1
29
+ version: 0.1.7
30
30
  description:
31
31
  email:
32
32
  executables: []