rubytext 0.1.20 → 0.1.21

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: adb60b745db0f540e875aaec148466bcf8421b14de2d0b4aee92ef9c9416bde5
4
- data.tar.gz: 68da81e432734dfd8dc3827b72c7db0440f6ba0a6203bf89b28b25ab5b0c9740
3
+ metadata.gz: 90fd407e8a80cfc0fb79bdfed6f97184984ed43df8598b23bc166a5427402c5e
4
+ data.tar.gz: 491e23b7274377c3e26f5b5375d965eaf666d009f373c74c72fc563c4f2ee4a2
5
5
  SHA512:
6
- metadata.gz: 516c0b907a73e0bd6b4fe4788f198584f4eeb08f36319214520110d8cbb3646f32a833d68289b7d434032f33d45462df5eb4c8f2a956251a467e2ad70619505c
7
- data.tar.gz: 1f79d1b030ba7eb1d0d360f672214dfb1b4386c96f434eb560897671c62c967043843567ec5fc26a137835f70683e32a638d21e26aa88103fd119aac588e91aa
6
+ metadata.gz: a434a77e4195559a342ff089dc764999b36faeb33de55a9d703b6c3d87051c1643c13ab7f41600587f26757ef38fd3d598a291c595d94d9c4b3e8befcd06a56c
7
+ data.tar.gz: e6b6b2e88ba130c1da44316d911925a4ed9743d5368e69f7d28a71425d8fa07dd449dde25bac7681c1cff69b2b53946f55bf4b1933778c803a8b6d513177c400
@@ -1,20 +1,41 @@
1
1
  module RubyText::Keys
2
- Down = 258
3
- Up = 259
4
- Left = 260
5
- Right = 261
6
- Enter = 10
7
- F1 = 265
8
- F2 = 266
9
- F3 = 267
10
- F4 = 268
11
- F5 = 269
12
- F6 = 270
13
- F7 = 271
14
- F8 = 272
15
- F9 = 273
16
- F10 = 274
17
- F11 = 275
18
- F12 = 276
2
+ Down = 258
3
+ Up = 259
4
+ Left = 260
5
+ Right = 261
6
+
7
+ Enter = 10
8
+ NL = 10
9
+ LF = 10
10
+
11
+ EOF = 4
12
+ CtlD = 4
13
+
14
+ Back = 8 # backspace
15
+ BS = 8
16
+
17
+ Tab = 9
18
+ TAB = 9
19
+
20
+ Escape = 27
21
+ ESC = 27
22
+ Esc = 27
23
+
24
+ Delete = 127
25
+ DEL = 127
26
+ Del = 127
27
+
28
+ F1 = 265
29
+ F2 = 266
30
+ F3 = 267
31
+ F4 = 268
32
+ F5 = 269
33
+ F6 = 270
34
+ F7 = 271
35
+ F8 = 272
36
+ F9 = 273
37
+ F10 = 274
38
+ F11 = 275
39
+ F12 = 276
19
40
  end
20
41
 
@@ -1,6 +1,75 @@
1
1
  module RubyText
2
2
 
3
3
  class Window
4
+ def topmenu(items:, curr: 0,
5
+ title: nil, fg: Green, bg: Black)
6
+ r, c = 0, 0
7
+ border = false
8
+ high = 1
9
+
10
+ RubyText.hide_cursor
11
+ if items.is_a?(Hash)
12
+ results = items.values
13
+ items = items.keys
14
+ hash_flag = true
15
+ else
16
+ results = items
17
+ end
18
+
19
+ tlen = title.length + 8 rescue 0
20
+ width = 0 # total width
21
+ cols = [] # start-column of each item
22
+ items.each do |item|
23
+ cols << width
24
+ iwide = item.to_s.length + 2
25
+ width += iwide
26
+ end
27
+
28
+ r, c = self.coords(r, c)
29
+ # puts "topmenu saved"
30
+ # sleep 2
31
+ self.saveback(high, width, r, c)
32
+ mr, mc = r+self.r0, c+self.c0
33
+ title = nil
34
+ mwin = RubyText.window(high, width, r: mr, c: mc, border: border,
35
+ fg: fg, bg: bg, title: title)
36
+ Curses.stdscr.keypad(true)
37
+ sel = curr
38
+ max = items.size - 1
39
+ loop do
40
+ items.each.with_index do |item, num|
41
+ item = " #{item} "
42
+ mwin.go 0, cols[num]
43
+ style = (sel == num) ? :reverse : :normal
44
+ mwin.print fx(item, style)
45
+ end
46
+ ch = getch
47
+ case ch
48
+ when Curses::KEY_LEFT
49
+ sel -= 1 if sel > 0
50
+ when Curses::KEY_RIGHT
51
+ sel += 1 if sel < max
52
+ when 27
53
+ self.restback(high, width, r, c)
54
+ RubyText.show_cursor
55
+ return [nil, nil]
56
+ when 10
57
+ self.restback(high, width, r, c)
58
+ # puts "topmenu restored"
59
+ # sleep 2
60
+ RubyText.show_cursor
61
+ choice = results[sel]
62
+ return [sel, choice] if choice.is_a? String
63
+ result = choice.call
64
+ next if result.nil?
65
+ next if result.empty?
66
+ return result
67
+ else Curses.beep
68
+ end
69
+ RubyText.show_cursor
70
+ end
71
+ end
72
+
4
73
  def menu(r: :center, c: :center, items:, curr: 0,
5
74
  border: true,
6
75
  title: nil, fg: Green, bg: Black)
@@ -24,6 +93,8 @@ module RubyText
24
93
  row = row - high/2 if r == :center
25
94
  col = col - wide/2 if c == :center
26
95
  r, c = row, col
96
+ # puts "menu2 saved"
97
+ # sleep 2
27
98
  self.saveback(high, wide, r, c)
28
99
  mr, mc = r+self.r0, c+self.c0
29
100
  title = nil unless border
@@ -37,7 +108,7 @@ module RubyText
37
108
  items.each.with_index do |item, row|
38
109
  mwin.go row, 0
39
110
  style = (sel == row) ? :reverse : :normal
40
- label = (" "*2 + item + " "*8)[0..wide-1]
111
+ label = (" "*2 + item.to_s + " "*8)[0..wide-1]
41
112
  mwin.print fx(label, style)
42
113
  end
43
114
  ch = getch
@@ -53,7 +124,11 @@ module RubyText
53
124
  when 10
54
125
  self.restback(high, wide, r, c)
55
126
  RubyText.show_cursor
56
- return [sel, results[sel]]
127
+ choice = results[sel]
128
+ return [sel, choice] if choice.is_a? String
129
+ result = choice.call
130
+ return [nil, nil] if result.nil? || result.empty?
131
+ return result
57
132
  else Curses.beep
58
133
  end
59
134
  RubyText.show_cursor
@@ -1,6 +1,10 @@
1
1
  $LOAD_PATH << "lib"
2
2
 
3
+ require 'keys'
4
+
3
5
  class RubyText::Window
6
+ include ::RubyText::Keys
7
+
4
8
  def center(str)
5
9
  r, c = self.rc
6
10
  n = @cwin.maxx - str.length
@@ -249,29 +253,36 @@ class RubyText::Window
249
253
  # echo assumed to be OFF, keypad ON
250
254
  @history = history
251
255
  gs = GetString.new(self, default, history: history, limit: limit, tab: tab)
256
+ count = 0
252
257
  loop do
258
+ count += 1 # Escape has special meaning if first char
253
259
  ch = self.getch
254
260
  case ch
255
- when 10
261
+ when Escape
262
+ return Escape if count == 1
263
+ gs.enter
264
+ break
265
+ when CtlD
266
+ return CtlD if count == 1
256
267
  gs.enter
257
268
  break
258
- when 8, 127, 63 # backspace, del, ^H (huh?)
269
+ when Enter
270
+ gs.enter
271
+ break
272
+ when BS, DEL, 63 # backspace, del, ^H (huh?)
259
273
  gs.backspace
260
- when 9 # tab
274
+ when Tab
261
275
  gs.complete
262
- when 260 # left-arrow
276
+ when Left
263
277
  gs.left_arrow
264
- when 261 # right-arrow
278
+ when Right
265
279
  gs.right_arrow
266
- when 259 # up
280
+ when Up
267
281
  next if @history.nil? # move this?
268
282
  gs.history_prev
269
- when 258 # down
283
+ when Down
270
284
  next if @history.nil? # move this?
271
285
  gs.history_next
272
- when 27 # escape
273
- gs.enter
274
- break
275
286
  when Integer
276
287
  Curses.beep
277
288
  else
@@ -1,6 +1,6 @@
1
1
 
2
2
  module RubyText
3
- VERSION = "0.1.20"
3
+ VERSION = "0.1.21"
4
4
 
5
5
  Path = File.expand_path(File.join(File.dirname(__FILE__)))
6
6
  end
@@ -37,14 +37,17 @@ module RubyText
37
37
 
38
38
  def self.splash(msg)
39
39
  lines = msg.split("\n")
40
- high = lines.size + 2
41
- wide = lines.map {|x| x.length }.max + 2
42
- STDSCR.saveback(high, wide, 10, 20)
43
- win = RubyText.window(high, wide, r: 10, c: 20, fg: White, bg: Red)
40
+ high = lines.size + 4
41
+ wide = lines.map {|x| x.length }.max + 4
42
+ r0 = (STDSCR.rows - high)/2
43
+ c0 = (STDSCR.cols - wide)/2
44
+ STDSCR.saveback(high, wide, r0, c0)
45
+ win = RubyText.window(high, wide, r: r0, c: c0,
46
+ fg: White, bg: Red, title: "[Press any key]")
47
+ win.print "\n "
44
48
  win.puts msg
45
49
  getch
46
- STDSCR.restback(high, wide, 10, 20)
50
+ STDSCR.restback(high, wide, r0, c0)
47
51
  end
48
52
 
49
- # TODO add splash
50
53
  end
@@ -13,6 +13,7 @@ end
13
13
 
14
14
  class RubyText::Window
15
15
  Vert, Horiz = Curses::A_VERTICAL, Curses::A_HORIZONTAL
16
+ ScreenStack = []
16
17
 
17
18
  attr_reader :cwin, :rows, :cols, :width, :height, :scrolling
18
19
  attr_reader :r0, :c0
@@ -120,32 +121,45 @@ class RubyText::Window
120
121
  lines
121
122
  end
122
123
 
124
+ def background(high=STDSCR.rows, wide=STDSCR.cols, r=0, c=0)
125
+ saveback(high, wide, r, c)
126
+ yield
127
+ restback(high, wide, r, c)
128
+ end
129
+
123
130
  def saveback(high=STDSCR.rows, wide=STDSCR.cols, r=0, c=0)
124
131
  debug "saveback: #{[high, wide, r, c].inspect}"
125
- @pos = self.rc
126
- @save = []
132
+ save = [self.rc]
127
133
  0.upto(high-1) do |h|
128
134
  0.upto(wide-1) do |w|
129
135
  row, col = h+r-1, w+c-1
130
136
  row += 1 if self == STDSCR # wtf?
131
137
  col += 1 if self == STDSCR
132
- @save << self[row, col]
138
+ save << self[row, col]
133
139
  end
134
140
  end
141
+ ScreenStack.push save
135
142
  end
136
143
 
137
144
  def restback(high=STDSCR.rows, wide=STDSCR.cols, r=0, c=0)
145
+ save = ScreenStack.pop
146
+ pos = save.shift
138
147
  0.upto(high-1) do |h|
139
148
  line = ""
140
- 0.upto(wide-1) {|w| line << @save.shift }
149
+ 0.upto(wide-1) {|w| line << save.shift }
141
150
  row, col = h+r-1, c-1
142
151
  row += 1 if self == STDSCR # wtf?
143
152
  col += 1 if self == STDSCR
144
153
  self.go row, col
145
154
  self.print line
146
155
  end
147
- self.go *@pos
156
+ self.go *pos
148
157
  @cwin.refresh
158
+ rescue => err
159
+ puts "Error!"
160
+ puts err
161
+ puts err.backtrace.join("\n")
162
+ sleep 8
149
163
  end
150
164
 
151
165
  def fg=(sym)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubytext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.20
4
+ version: 0.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hal Fulton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-18 00:00:00.000000000 Z
11
+ date: 2020-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses