rubytext 0.0.40 → 0.0.41

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: 3af233ca88d1490559df7b8ebbdb4223dc55979a4cb66e7983e2e74df66d24ea
4
- data.tar.gz: e04c414c65e8591e37976e9f4c9fc5f324e0c30e08d8686764c4f36b8b5933e4
3
+ metadata.gz: bfb5ca1d05e1fc85ff4de0f5d028e83611176142fbf022b26b91bd608b6fe9e8
4
+ data.tar.gz: ae55d53aedf1a0da15cfcf2c9dff8d76bf7274804dc942ea67372adc287ca827
5
5
  SHA512:
6
- metadata.gz: 2f9b6b4c12291dee763807fe0f7dd4e2a27f5467f372d794443e24f3d1af3b6202af5a179495c96ebdac82ad3f89121e2ad4c1b8eadf101947c57767a4779c6b
7
- data.tar.gz: 53ce5b491f2c628672815930b5ab354a34e20436c5e8b71412983829a0feabfb7d1903cded03a5625405b8bb397676913d5c589ee4c4a9d50a743ad867b7c8a1
6
+ metadata.gz: 48d354dcf57a37dffbe6605da23bed650a34a8f05d8d024929c5fb681cc8beb13eea09cdedc5ba5583a03a2a0e99b35c57d4a9e3c380b297dbc119e073300dc0
7
+ data.tar.gz: 42ba4bbd57b6b7f5936dad8547a9f9f3b0db1bc968b549c578d480fe6d7f2b6131282b91ff428317ea90207c3931525b58ce45998b50d3af4083b4a326ec5e58
data/lib/keys.rb ADDED
@@ -0,0 +1,20 @@
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
19
+ end
20
+
data/lib/menu.rb ADDED
@@ -0,0 +1,94 @@
1
+ module RubyText
2
+
3
+ def self.saveback(high, wide, r, c)
4
+ @pos = STDSCR.rc
5
+ @save = []
6
+ 0.upto(high) do |h|
7
+ 0.upto(wide) do |w|
8
+ @save << STDSCR[h+r, w+c]
9
+ end
10
+ end
11
+ end
12
+
13
+ def self.restback(high, wide, r, c)
14
+ 0.upto(high) do |h|
15
+ 0.upto(wide) do |w|
16
+ STDSCR[h+r, w+c] = @save.shift
17
+ end
18
+ end
19
+ STDSCR.go *@pos
20
+ STDSCR.refresh
21
+ end
22
+
23
+ def self.menu(r: 0, c: 0, items:)
24
+ high = items.size + 2
25
+ wide = items.map(&:length).max + 2
26
+ saveback(high, wide, r, c)
27
+ @mywin = RubyText.window(high, wide, r, c, true, fg: :white, bg: :blue)
28
+ RubyText.set(:raw)
29
+ X.stdscr.keypad(true)
30
+ RubyText.hide_cursor
31
+ sel = 0
32
+ max = items.size - 1
33
+ loop do
34
+ items.each.with_index do |item, row|
35
+ @mywin.go row, 0
36
+ color = sel == row ? :yellow : :white
37
+ @mywin.puts color, " #{item} "
38
+ end
39
+ ch = getch
40
+ case ch
41
+ when X::KEY_UP
42
+ sel -= 1 if sel > 0
43
+ when X::KEY_DOWN
44
+ sel += 1 if sel < max
45
+ when 27
46
+ restback(high, wide, r, c)
47
+ return nil
48
+ when 10
49
+ restback(high, wide, r, c)
50
+ return sel
51
+ end
52
+ end
53
+ end
54
+
55
+ def selector(r: 0, c: 0, rows: 10, cols: 20, items:,
56
+ win:, callback:, quit: "q")
57
+ high = rows
58
+ wide = cols
59
+ saveback(high, wide, r, c)
60
+ menu_win = RubyText.window(high, wide, r, c, true, fg: :white, bg: :blue)
61
+ win2 = win
62
+ handler = callback
63
+ RubyText.set(:raw)
64
+ X.stdscr.keypad(true)
65
+ RubyText.hide_cursor
66
+ sel = 0
67
+ max = items.size - 1
68
+ handler.call(0, items[0])
69
+ loop do
70
+ items.each.with_index do |item, row|
71
+ menu_win.go row, 0
72
+ color = sel == row ? :yellow : :white
73
+ menu_win.puts color, " #{item} "
74
+ end
75
+ ch = getch
76
+ case ch
77
+ when X::KEY_UP
78
+ if sel > 0
79
+ sel -= 1
80
+ end
81
+ when X::KEY_DOWN
82
+ if sel < max
83
+ sel += 1
84
+ end
85
+ when quit # parameter
86
+ win2.clear
87
+ win2.puts "About to quit..."
88
+ sleep 1
89
+ exit
90
+ end
91
+ end
92
+ end
93
+ end
94
+
data/lib/output.rb ADDED
@@ -0,0 +1,180 @@
1
+ $LOAD_PATH << "lib"
2
+
3
+ def debug(*args)
4
+ return unless $debug
5
+ $debug.puts *args
6
+ end
7
+
8
+ def fb2cp(fg, bg)
9
+ fg ||= :blue
10
+ bg ||= :white
11
+ f2 = X.const_get("COLOR_#{fg.upcase}")
12
+ b2 = X.const_get("COLOR_#{bg.upcase}")
13
+ cp = $ColorPairs[[fg, bg]]
14
+ [f2, b2, cp]
15
+ end
16
+
17
+ module RubyText
18
+
19
+ Colors = [:black, :blue, :cyan, :green, :magenta, :red, :white, :yellow]
20
+ $ColorPairs = {}
21
+ num = 0
22
+ Colors.each do |fsym|
23
+ Colors.each do |bsym|
24
+ fg = X.const_get("COLOR_#{fsym.upcase}")
25
+ bg = X.const_get("COLOR_#{bsym.upcase}")
26
+ X.init_pair(num+=1, fg, bg) # FIXME backwards?
27
+ $ColorPairs[[fsym, bsym]] = num
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+
34
+ class RubyText::Window
35
+
36
+ def center(str)
37
+ r, c = self.rc
38
+ n = @win.maxx - str.length
39
+ go r, n/2
40
+ puts str
41
+ end
42
+
43
+ def putch(ch)
44
+ r, c = self.rc
45
+ self[r, c] = ch[0]
46
+ end
47
+
48
+ def need_crlf?(sym, args)
49
+ sym != :print && # print doesn't default to crlf
50
+ args[-1][-1] != "\n" # last char is a literal linefeed
51
+ end
52
+
53
+ def delegate_output(sym, *args)
54
+ args = [""] if args.empty?
55
+ RubyText::Window.colors(@win, @fg, @bg) # FIXME?
56
+ if sym == :p
57
+ args.map!(&:inspect)
58
+ else
59
+ args.map! do |x|
60
+ if RubyText::Colors.include? x
61
+ x
62
+ else
63
+ x.to_s
64
+ end
65
+ end
66
+ end
67
+ # STDOUT.puts "again: #{args.inspect}"
68
+ flag = need_crlf?(sym, args)
69
+ # Limitation: Can't print color symbols!
70
+ args.each do |arg|
71
+ if arg.is_a? Symbol # must be a color
72
+ RubyText::Window.colors(@win, arg, @bg) # FIXME?
73
+ else
74
+ arg.each_char {|ch| ch == "\n" ? crlf : @win.addch(ch) }
75
+ end
76
+ end
77
+ crlf if flag
78
+ RubyText::Window.colors(@win, @fg, @bg) # FIXME?
79
+ @win.refresh
80
+ end
81
+
82
+ def puts(*args)
83
+ delegate_output(:puts, *args)
84
+ end
85
+
86
+ def print(*args)
87
+ delegate_output(:print, *args)
88
+ end
89
+
90
+ def p(*args)
91
+ delegate_output(:p, *args)
92
+ end
93
+
94
+ def rcprint(r, c, *args)
95
+ self.go(r, c) { self.print *args }
96
+ end
97
+
98
+ def rcprint!(r, c, *args)
99
+ @win.setpos(r, c) # Cursor isn't restored
100
+ self.print *args
101
+ end
102
+
103
+ def crlf
104
+ # Technically not output...
105
+ r, c = rc
106
+ go r+1, 0
107
+ end
108
+
109
+ def self.clear(win)
110
+ num = win.maxx * win.maxy
111
+ win.setpos(0, 0)
112
+ win.addstr(' '*num)
113
+ win.setpos(0, 0)
114
+ win.refresh
115
+ end
116
+
117
+ def clear
118
+ win = @win
119
+ num = win.maxx * win.maxy
120
+ win.setpos(0, 0)
121
+ win.addstr(' '*num)
122
+ win.setpos(0, 0)
123
+ win.refresh
124
+ end
125
+
126
+ def output(&block)
127
+ $stdscr = self
128
+ block.call
129
+ $stdscr = STDSCR
130
+ end
131
+
132
+ def [](r, c)
133
+ save = self.rc
134
+ @win.setpos r, c
135
+ ch = @win.inch
136
+ @win.setpos *save
137
+ ch.chr
138
+ # go(r, c) { ch = @win.inch }
139
+ end
140
+
141
+ def []=(r, c, char)
142
+ @win.setpos(r, c)
143
+ @win.addch(char[0])
144
+ @win.refresh
145
+ end
146
+
147
+ def boxme
148
+ @outer.box(Vert, Horiz)
149
+ @outer.refresh
150
+ end
151
+
152
+ def refresh
153
+ @win.refresh
154
+ end
155
+ end
156
+
157
+ ### Stick stuff into Kernel for top level
158
+
159
+ module Kernel
160
+ def puts(*args) # Doesn't affect STDOUT.puts, etc.
161
+ $stdscr.puts(*args)
162
+ end
163
+
164
+ def print(*args)
165
+ $stdscr.print(*args)
166
+ end
167
+
168
+ def p(*args)
169
+ $stdscr.p(*args)
170
+ end
171
+
172
+ def rcprint(r, c, *args)
173
+ $stdscr.rcprint r, c, *args
174
+ end
175
+
176
+ def getch
177
+ X.getch
178
+ end
179
+ end
180
+
data/lib/rubytext.rb CHANGED
@@ -5,81 +5,36 @@ require 'version' # skeleton + version
5
5
 
6
6
  X = Curses # shorthand
7
7
 
8
- def debug(*args)
9
- return unless $debug
10
- $debug.puts *args
11
- end
8
+ require 'output'
9
+ require 'keys'
10
+ require 'menu'
12
11
 
13
- def fb2cp(fg, bg)
14
- fg ||= :blue
15
- bg ||= :white
16
- f2 = X.const_get("COLOR_#{fg.upcase}")
17
- b2 = X.const_get("COLOR_#{bg.upcase}")
18
- cp = $ColorPairs[[fg, bg]]
19
- [f2, b2, cp]
20
- end
21
12
 
22
- module RubyText
13
+ class RubyText::Window
14
+ attr_reader :fg, :bg
23
15
 
24
- Colors = [:black, :blue, :cyan, :green, :magenta, :red, :white, :yellow]
25
- $ColorPairs = {}
26
- num = 0
27
- Colors.each do |fsym|
28
- Colors.each do |bsym|
29
- fg = X.const_get("COLOR_#{fsym.upcase}")
30
- bg = X.const_get("COLOR_#{bsym.upcase}")
31
- X.init_pair(num+=1, fg, bg) # FIXME backwards?
32
- $ColorPairs[[fsym, bsym]] = num
33
- end
16
+ def self.colors(win, fg, bg)
17
+ cfg, cbg, cp = fb2cp(fg, bg)
18
+ X.init_pair(cp, cfg, cbg)
19
+ win.color_set(cp|X::A_NORMAL)
34
20
  end
35
21
 
36
- # $ColorPairs.each_pair {|k, v| STDOUT.puts "#{k.inspect} => #{v}" }
37
-
38
- module Keys
39
- Down = 258
40
- Up = 259
41
- Left = 260
42
- Right = 261
43
- Enter = 10
44
- F1 = 265
45
- F2 = 266
46
- F3 = 267
47
- F4 = 268
48
- F5 = 269
49
- F6 = 270
50
- F7 = 271
51
- F8 = 272
52
- F9 = 273
53
- F10 = 274
54
- F11 = 275
55
- F12 = 276
22
+ def self.colors!(win, fg, bg)
23
+ colors(win, fg, bg)
24
+ num = win.maxx * win.maxy
25
+ win.setpos(0, 0)
26
+ win.addstr(' '*num)
27
+ win.setpos(0, 0)
28
+ win.refresh
56
29
  end
57
30
 
58
- class Window
59
- attr_reader :fg, :bg
60
-
61
- def self.colors(win, fg, bg)
62
- cfg, cbg, cp = fb2cp(fg, bg)
63
- X.init_pair(cp, cfg, cbg)
64
- win.color_set(cp|X::A_NORMAL)
65
- end
66
-
67
- def self.colors!(win, fg, bg)
68
- colors(win, fg, bg)
69
- num = win.maxx * win.maxy
70
- win.setpos(0, 0)
71
- win.addstr(' '*num)
72
- win.setpos(0, 0)
73
- win.refresh
74
- end
75
-
76
- def self.main(fg: nil, bg: nil)
77
- @main_win = X.init_screen
78
- X.start_color
79
- colors!(@main_win, fg, bg)
80
- rows, cols = @main_win.maxy, @main_win.maxx
81
- @screen = self.make(@main_win, rows, cols, 0, 0, false,
82
- fg: fg, bg: bg)
31
+ def self.main(fg: nil, bg: nil)
32
+ @main_win = X.init_screen
33
+ X.start_color
34
+ colors!(@main_win, fg, bg)
35
+ rows, cols = @main_win.maxy, @main_win.maxx
36
+ @screen = self.make(@main_win, rows, cols, 0, 0, false,
37
+ fg: fg, bg: bg)
83
38
  # FIXME Why is this hard to inline?
84
39
  # @win = @main_win
85
40
  # obj = self.allocate
@@ -93,32 +48,29 @@ module RubyText
93
48
  # end
94
49
  # @win = @main_win # FIXME?
95
50
  # obj
96
- @screen
97
- end
98
-
99
- def self.make(cwin, high, wide, r0, c0, border, fg: :white, bg: :black)
100
- obj = self.allocate
101
- obj.instance_eval do
102
- debug " Inside instance_eval..."
103
- @outer = @win = cwin
104
- @wide, @high, @r0, @c0 = wide, high, r0, c0
105
- @fg, @bg = fg, bg
106
- @border = border
107
- @rows, @cols = high, wide
108
- @width, @height = @cols + 2, @rows + 2 if @border
109
- end
110
- obj
51
+ @screen
52
+ end
53
+
54
+ def self.make(cwin, high, wide, r0, c0, border, fg: :white, bg: :black)
55
+ obj = self.allocate
56
+ obj.instance_eval do
57
+ debug " Inside instance_eval..."
58
+ @outer = @win = cwin
59
+ @wide, @high, @r0, @c0 = wide, high, r0, c0
60
+ @fg, @bg = fg, bg
61
+ @border = border
62
+ @rows, @cols = high, wide
63
+ @width, @height = @cols + 2, @rows + 2 if @border
111
64
  end
65
+ obj
66
+ end
112
67
 
113
- def fg=(sym)
114
- self.colors(@win, fg, @bg)
115
- end
68
+ def fg=(sym)
69
+ self.colors(@win, fg, @bg)
116
70
  end
117
71
  end
118
72
 
119
-
120
73
  module RubyText
121
-
122
74
  def self.set(*args)
123
75
  # Allow a block?
124
76
  standard = [:cbreak, :raw, :echo, :keypad]
@@ -187,59 +139,6 @@ module RubyText
187
139
  def self.show_cursor!
188
140
  X.curs_set(2) # Doesn't work?
189
141
  end
190
-
191
- def self.saveback(high, wide, r, c)
192
- @pos = STDSCR.rc
193
- @save = []
194
- 0.upto(high) do |h|
195
- 0.upto(wide) do |w|
196
- @save << STDSCR[h+r, w+c]
197
- end
198
- end
199
- end
200
-
201
- def self.restback(high, wide, r, c)
202
- 0.upto(high) do |h|
203
- 0.upto(wide) do |w|
204
- STDSCR[h+r, w+c] = @save.shift
205
- end
206
- end
207
- STDSCR.go *@pos
208
- STDSCR.refresh
209
- end
210
-
211
- def self.menu(r: 0, c: 0, items:)
212
- high = items.size + 2
213
- wide = items.map(&:length).max + 2
214
- saveback(high, wide, r, c)
215
- @mywin = RubyText.window(high, wide, r, c, true, fg: :white, bg: :blue)
216
- RubyText.set(:raw)
217
- X.stdscr.keypad(true)
218
- RubyText.hide_cursor
219
- sel = 0
220
- max = items.size - 1
221
- loop do
222
- items.each.with_index do |item, row|
223
- @mywin.go row, 0
224
- color = sel == row ? :yellow : :white
225
- @mywin.puts color, " #{item} "
226
- end
227
- ch = getch
228
- case ch
229
- when X::KEY_UP
230
- sel -= 1 if sel > 0
231
- when X::KEY_DOWN
232
- sel += 1 if sel < max
233
- when 27
234
- restback(high, wide, r, c)
235
- return nil
236
- when 10
237
- restback(high, wide, r, c)
238
- return sel
239
- end
240
- end
241
- end
242
-
243
142
  end
244
143
 
245
144
  class RubyText::Window
@@ -273,73 +172,6 @@ class RubyText::Window
273
172
  @win.refresh
274
173
  end
275
174
 
276
- def center(str)
277
- r, c = self.rc
278
- n = @win.maxx - str.length
279
- go r, n/2
280
- puts str
281
- end
282
-
283
- def putch(ch)
284
- r, c = self.rc
285
- self[r, c] = ch[0]
286
- end
287
-
288
- def need_crlf?(sym, args)
289
- sym != :print && # print doesn't default to crlf
290
- args[-1][-1] != "\n" # last char is a literal linefeed
291
- end
292
-
293
- def delegate_output(sym, *args)
294
- args = [""] if args.empty?
295
- RubyText::Window.colors(@win, @fg, @bg) # FIXME?
296
- if sym == :p
297
- args.map!(&:inspect)
298
- else
299
- args.map! do |x|
300
- if RubyText::Colors.include? x
301
- x
302
- else
303
- x.to_s
304
- end
305
- end
306
- end
307
- # STDOUT.puts "again: #{args.inspect}"
308
- flag = need_crlf?(sym, args)
309
- # Limitation: Can't print color symbols!
310
- args.each do |arg|
311
- if arg.is_a? Symbol # must be a color
312
- RubyText::Window.colors(@win, arg, @bg) # FIXME?
313
- else
314
- arg.each_char {|ch| ch == "\n" ? crlf : @win.addch(ch) }
315
- end
316
- end
317
- crlf if flag
318
- RubyText::Window.colors(@win, @fg, @bg) # FIXME?
319
- @win.refresh
320
- end
321
-
322
- def puts(*args)
323
- delegate_output(:puts, *args)
324
- end
325
-
326
- def print(*args)
327
- delegate_output(:print, *args)
328
- end
329
-
330
- def p(*args)
331
- delegate_output(:p, *args)
332
- end
333
-
334
- def rcprint(r, c, *args)
335
- self.go(r, c) { self.print *args }
336
- end
337
-
338
- def rcprint!(r, c, *args)
339
- @win.setpos(r, c) # Cursor isn't restored
340
- self.print *args
341
- end
342
-
343
175
  def go(r, c)
344
176
  save = self.rc
345
177
  @win.setpos(r, c)
@@ -412,78 +244,4 @@ class RubyText::Window
412
244
  [@win.cury, @win.curx]
413
245
  end
414
246
 
415
- def self.clear(win)
416
- num = win.maxx * win.maxy
417
- win.setpos(0, 0)
418
- win.addstr(' '*num)
419
- win.setpos(0, 0)
420
- win.refresh
421
- end
422
-
423
- def clear
424
- win = @win
425
- num = win.maxx * win.maxy
426
- win.setpos(0, 0)
427
- win.addstr(' '*num)
428
- win.setpos(0, 0)
429
- win.refresh
430
- end
431
-
432
- def output(&block)
433
- $stdscr = self
434
- block.call
435
- $stdscr = STDSCR
436
- end
437
-
438
- def [](r, c)
439
- save = self.rc
440
- @win.setpos r, c
441
- ch = @win.inch
442
- @win.setpos *save
443
- ch.chr
444
- # go(r, c) { ch = @win.inch }
445
- end
446
-
447
- def []=(r, c, char)
448
- @win.setpos(r, c)
449
- @win.addch(char[0])
450
- @win.refresh
451
- end
452
-
453
- def boxme
454
- @outer.box(Vert, Horiz)
455
- @outer.refresh
456
- end
457
-
458
- def refresh
459
- @win.refresh
460
- end
461
- end
462
-
463
-
464
- ####
465
-
466
- ### Stick stuff into Kernel for top level
467
-
468
- module Kernel
469
- def puts(*args) # Doesn't affect STDOUT.puts, etc.
470
- $stdscr.puts(*args)
471
- end
472
-
473
- def print(*args)
474
- $stdscr.print(*args)
475
- end
476
-
477
- def p(*args)
478
- $stdscr.p(*args)
479
- end
480
-
481
- def rcprint(r, c, *args)
482
- $stdscr.rcprint r, c, *args
483
- end
484
-
485
- def getch
486
- X.getch
487
- end
488
247
  end
489
-
data/lib/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  module RubyText
3
- VERSION = "0.0.40"
3
+ VERSION = "0.0.41"
4
4
 
5
5
  Path = File.expand_path(File.join(File.dirname(__FILE__)))
6
6
  end
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.0.40
4
+ version: 0.0.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hal Fulton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-19 00:00:00.000000000 Z
11
+ date: 2018-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses
@@ -61,6 +61,9 @@ files:
61
61
  - examples/prog21.rb
62
62
  - examples/showme.rb
63
63
  - examples/slides
64
+ - lib/keys.rb
65
+ - lib/menu.rb
66
+ - lib/output.rb
64
67
  - lib/rubytext.rb
65
68
  - lib/version.rb
66
69
  homepage: https://github.com/Hal9000/rubytext