spinal_tap 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,11 +3,9 @@ module SpinalTap
3
3
  module ClientHelpers
4
4
  def setup(server)
5
5
  @server = server
6
+ @history = SpinalTap::History.new
6
7
 
7
- @history = []
8
- @history_pos = 0
9
-
10
- reset_buffer
8
+ reset
11
9
 
12
10
  setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
13
11
 
@@ -27,6 +25,7 @@ module SpinalTap
27
25
 
28
26
  case command
29
27
  when 'help' then exec_help
28
+ when 'history' then exec_history
30
29
  when 'eval' then exec_eval(args.join(' '))
31
30
  when 'quit'
32
31
  close
@@ -40,13 +39,13 @@ module SpinalTap
40
39
  @server.unregister(Thread.current)
41
40
  end
42
41
 
43
- def reset_buffer
44
- @buffer = ''
42
+ def reset
43
+ @buffer = @history.current
45
44
  @cursor_pos = 1
46
45
  end
47
46
 
48
47
  def read_parsed_line
49
- reset_buffer
48
+ reset
50
49
  redraw_cmd_line
51
50
 
52
51
  while byte = getbyte
@@ -70,17 +69,15 @@ module SpinalTap
70
69
  if (byte = getbyte) == 91 # [ Char.
71
70
  case getbyte
72
71
  when 65 # A Char - Up Arrow.
73
- if @history_pos > 0
74
- @history_pos -= 1
75
- @buffer = @history[@history_pos].to_s
72
+ if (result = @history.previous)
73
+ @buffer = result
76
74
  @cursor_pos = @buffer.length + 1
77
75
  else
78
76
  bell
79
77
  end
80
78
  when 66 # B Char - Down Arrow.
81
- if @history_pos < @history.length
82
- @history_pos += 1
83
- @buffer = @history[@history_pos].to_s
79
+ if (result = @history.next)
80
+ @buffer = result
84
81
  @cursor_pos = @buffer.length + 1
85
82
  else
86
83
  bell
@@ -109,8 +106,7 @@ module SpinalTap
109
106
  args = tokens[1..-1]
110
107
 
111
108
  if @buffer.length > 0
112
- @history.push(@buffer)
113
- @history_pos = @history.length
109
+ @history.append(@buffer)
114
110
  end
115
111
 
116
112
  return {:command => command, :args => args}
@@ -137,7 +133,13 @@ module SpinalTap
137
133
  end
138
134
 
139
135
  def exec_help
140
- write("Commands: help quit eval\r\n")
136
+ write("Commands: help quit history eval\r\n")
137
+ end
138
+
139
+ def exec_history
140
+ @history.all.each_with_index do |entry, index|
141
+ write("#{index.to_s.ljust(4, ' ')} #{entry}\r\n")
142
+ end
141
143
  end
142
144
 
143
145
  def exec_eval(code)
@@ -0,0 +1,6 @@
1
+ module SpinalTap
2
+
3
+ class CmdLine
4
+ end
5
+
6
+ end
@@ -0,0 +1,77 @@
1
+ module SpinalTap
2
+
3
+ class History
4
+ MAX_LINES = 100
5
+
6
+ def initialize
7
+ @history = ['']
8
+ @history_pos = 0
9
+ end
10
+
11
+ def current
12
+ cur = @history[@history_pos]
13
+ cur = cur.clone unless last?
14
+
15
+ return cur
16
+ end
17
+
18
+ def previous?
19
+ return @history_pos > 0
20
+ end
21
+
22
+ def previous
23
+ if previous?
24
+ @history_pos -= 1
25
+ return current
26
+ else
27
+ return false
28
+ end
29
+ end
30
+
31
+ def next?
32
+ return @history_pos < @history.length - 1
33
+ end
34
+
35
+ def next
36
+ if next?
37
+ @history_pos += 1
38
+ return current
39
+ else
40
+ return false
41
+ end
42
+ end
43
+
44
+ def last?
45
+ return @history_pos >= @history.length - 1
46
+ end
47
+
48
+ def append(cmd_line)
49
+ @history.pop
50
+ @history << cmd_line.clone
51
+ @history << ''
52
+
53
+ trim
54
+ fast_forward
55
+
56
+ return true
57
+ end
58
+
59
+ def all
60
+ return @history[0..-2].map { |e| e.clone }
61
+ end
62
+
63
+ def fast_forward
64
+ @history_pos = @history.length - 1
65
+
66
+ return current
67
+ end
68
+
69
+ private
70
+
71
+ def trim
72
+ while @history.length > MAX_LINES + 1
73
+ @history.shift
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,3 +1,3 @@
1
1
  module SpinalTap
2
- VERSION = "0.1.3"
2
+ VERSION = '0.1.4'
3
3
  end
data/lib/spinal_tap.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'spinal_tap/version'
2
2
  require 'spinal_tap/server'
3
+ require 'spinal_tap/cmd_line'
4
+ require 'spinal_tap/history'
3
5
  require 'spinal_tap/client_helpers'
4
6
 
5
7
  module SpinalTap
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spinal_tap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -122,6 +122,8 @@ files:
122
122
  - Rakefile
123
123
  - lib/spinal_tap.rb
124
124
  - lib/spinal_tap/client_helpers.rb
125
+ - lib/spinal_tap/cmd_line.rb
126
+ - lib/spinal_tap/history.rb
125
127
  - lib/spinal_tap/server.rb
126
128
  - lib/spinal_tap/version.rb
127
129
  - spec/spec_helper.rb
@@ -140,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
142
  version: '0'
141
143
  segments:
142
144
  - 0
143
- hash: -1111823406379054715
145
+ hash: -2867284756879300883
144
146
  required_rubygems_version: !ruby/object:Gem::Requirement
145
147
  none: false
146
148
  requirements:
@@ -149,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
151
  version: '0'
150
152
  segments:
151
153
  - 0
152
- hash: -1111823406379054715
154
+ hash: -2867284756879300883
153
155
  requirements: []
154
156
  rubyforge_project:
155
157
  rubygems_version: 1.8.23