rubytext 0.0.37 → 0.0.38

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: f82e028f0fcc61304b43307716aa8300010e31358f7945569a57053040f3bf85
4
- data.tar.gz: 632ecf99dbef00a9e3518e383ee21db379becc47a0a02f6da9d818d878e13055
3
+ metadata.gz: f76ca1f9288f586dee2ea9270f4213e22561b90009a7bbd6e773de9e024af442
4
+ data.tar.gz: 00345a5a7fe85aa24966717d612ab6d602d0d1b95b361fd547b1570ab0101e6e
5
5
  SHA512:
6
- metadata.gz: 94e0ac4ee9dcd8876ddba84063ab10d56f48878330e4993ce906011d7d3bfa326f8e54893c67ae5a1023545b2bb31d640e4d2cfbdc29811943933c654c4fa577
7
- data.tar.gz: 55a3dc93323093e5aeb93750a9bbee0a39e86c60436da8dce533a539129c047551e00fb49e5d61fd4296e86bd52da085bf2d018c4a38ca152ec33e674d034913
6
+ metadata.gz: 7f675a7b1c5d3f174abec10d03137e309d2401e44a138bd88d4bc389cf577583cbd9b19a225588e1e1640d691367ec4b400fc7245f268427a5f55f2d133d2060
7
+ data.tar.gz: 4e1101676ff0a27dd3d9cf22b4150d356dcf38a7b4f842f8840c1d5e450ca0d8ca2a23c93772563d6f008fd2104260f4194e119b47cd1d1ac0a3cb3f1258b1c7
data/README.md CHANGED
@@ -5,7 +5,14 @@ RubyText is a curses wrapper that is in the early experimental
5
5
  stages. The code has bugs. Running it may cause general bad luck
6
6
  as well as demon infestation.
7
7
 
8
- For now, just run the `examples/demo.rb` program.
8
+ Install the `rubytext` gem and then run the demo or the slides
9
+ or both.
10
+
11
+ ```
12
+ $ gem install rubytext
13
+ $ rubytext demo # 2-minute demo
14
+ $ rubytext slides # longer "slideshow"
15
+ ```
9
16
 
10
17
  There is also `examples/ide.rb` ("world's simplest Ruby IDE").
11
18
  It is _very_ dumb. :)
data/examples/ide.rb CHANGED
@@ -7,31 +7,34 @@ end
7
7
 
8
8
  require 'rubytext'
9
9
 
10
- print " " # FIXME - bug requires this?
10
+ RubyText.start
11
+
12
+ # print " " # FIXME - bug requires this?
11
13
 
12
14
  @lib, @code = ARGV
13
15
 
14
16
  def menu
15
17
  $debug.puts "Entering menu"
16
- m = @mywin
17
- m.boxme # FIXME - dumb hack
18
- m.clear
19
- m.puts
20
- m.puts " World's Simplest Ruby IDE\n "
21
- m.puts " Lib = #{@lib}"
22
- m.puts " Code = #{@code}"
23
- m.puts
24
- m.puts " 1 Edit lib"
25
- m.puts " 2 Edit code"
26
- m.puts " 3 Run code"
27
- m.puts " 4 pry"
28
- m.puts " 5 Shell"
29
- m.puts " 6 irb"
30
- m.puts " 7 RubyDocs"
31
- m.puts
32
- m.puts " 0 Quit"
33
- m.print "\n Choice = "
34
- m.refresh # FIXME - dumb hack
18
+ @mywin.output do
19
+ # boxme # FIXME - dumb hack
20
+ @mywin.clear
21
+ puts
22
+ puts " World's Simplest Ruby IDE\n "
23
+ puts " Lib = #{@lib}"
24
+ puts " Code = #{@code}"
25
+ puts
26
+ puts " 1 Edit lib"
27
+ puts " 2 Edit code"
28
+ puts " 3 Run code"
29
+ puts " 4 pry"
30
+ puts " 5 Shell"
31
+ puts " 6 irb"
32
+ puts " 7 RubyDocs"
33
+ puts
34
+ puts " 0 Quit"
35
+ print "\n Choice = "
36
+ @mywin.refresh # FIXME - dumb hack
37
+ end
35
38
  $debug.puts "Exiting menu"
36
39
  end
37
40
 
@@ -50,9 +53,7 @@ end
50
53
 
51
54
  loop do
52
55
  menu
53
- # $debug.puts "about to call getch"
54
56
  cmd = getch.chr
55
- # $debug.puts "called getch - '#{cmd}'"
56
57
  case cmd
57
58
  when "1"; system("vi #{@lib}")
58
59
  when "2"; system("vi #{@code}")
data/examples/prog08.rb CHANGED
@@ -1,14 +1,14 @@
1
- win = RubyText.window(10, 50, 0, 5, true, fg: :yellow, bg: :blue)
1
+ win = RubyText.window(12, 50, 0, 5, true, fg: :yellow, bg: :blue)
2
2
 
3
3
  win.puts "We can use the []= method (0-based)"
4
4
  win.puts "to address individual window locations"
5
- win.puts "and place characters there. [BUG!]\n "
5
+ win.puts "and place characters there.\n "
6
6
 
7
7
  sleep 2
8
- win[4,2] = "X"
8
+ win[4,22] = "X"
9
9
 
10
10
  sleep 2
11
- win[8,10] = "y"
11
+ win[8,20] = "y"
12
12
 
13
13
  sleep 2
14
- win[6,18] = "Z"
14
+ win[6,38] = "Z"
@@ -0,0 +1,13 @@
1
+ win = RubyText.window(12, 65, 2, 6, true, fg: :green, bg: :blue)
2
+
3
+ win.puts "This is EXPERIMENTAL."
4
+ win.puts "Use a color symbol to change text color temporarily:\n "
5
+
6
+ win.puts "This is", :yellow, " another color", :white, " and yet another."
7
+ win.puts "And this is normal again.\n "
8
+
9
+ win.puts "This does mean that you can't print a symbol that is"
10
+ win.puts "also a color name... you'd need a workaround.\n "
11
+
12
+ sym = :red
13
+ win.puts "The symbol is ", sym.inspect, " which works", sym, " just fine."
@@ -0,0 +1,14 @@
1
+ puts "This very crude menu is also EXPERIMENTAL."
2
+ puts "At the moment, it only works inside STDSCR!\n"
3
+ puts "It knows up, down, Enter, and Escape.\n "
4
+ puts "Press any key to display the menu..."
5
+ getch
6
+
7
+ days = %w[Monday Tuesday Wednesday Thursday Friday]
8
+ picked = RubyText.menu(c: 30, items: days)
9
+
10
+ if picked.nil?
11
+ puts "You picked nothing!"
12
+ else
13
+ puts "You picked item #{picked} which is #{days[picked].inspect}"
14
+ end
data/examples/showme.rb CHANGED
@@ -21,7 +21,8 @@ def show_code(prog)
21
21
  true, fg: :green, bg: :black)
22
22
  code.puts text
23
23
 
24
- STDSCR.go prog_top, 61
24
+ right = STDSCR.cols - prog.length - 8
25
+ STDSCR.go prog_top, right
25
26
  STDSCR.print "[ #{prog} ]"
26
27
  STDSCR.go 0,0
27
28
  end
data/examples/slides CHANGED
@@ -10,7 +10,7 @@ exit unless rc
10
10
 
11
11
  # FIXME
12
12
  # - use names instead of numbers?
13
- # - convert rt-show into a method?
13
+ # - convert showme into a method?
14
14
 
15
15
  1.upto(19) do |n|
16
16
  next if n == 11 # junk this one
data/lib/rubytext.rb CHANGED
@@ -13,26 +13,28 @@ end
13
13
  def fb2cp(fg, bg)
14
14
  fg ||= :blue
15
15
  bg ||= :white
16
- fg = X.const_get("COLOR_#{fg.upcase}")
17
- bg = X.const_get("COLOR_#{bg.upcase}")
16
+ f2 = X.const_get("COLOR_#{fg.upcase}")
17
+ b2 = X.const_get("COLOR_#{bg.upcase}")
18
18
  cp = $ColorPairs[[fg, bg]]
19
- [fg, bg, cp]
19
+ [f2, b2, cp]
20
20
  end
21
21
 
22
22
  module RubyText
23
23
 
24
- Colors = %w[black blue cyan green magenta red white yellow]
24
+ Colors = [:black, :blue, :cyan, :green, :magenta, :red, :white, :yellow]
25
25
  $ColorPairs = {}
26
26
  num = 0
27
- Colors.each do |fc|
28
- Colors.each do |bc|
29
- fg = X.const_get("COLOR_#{fc.upcase}")
30
- bg = X.const_get("COLOR_#{bc.upcase}")
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
31
  X.init_pair(num+=1, fg, bg) # FIXME backwards?
32
- $ColorPairs[[fg, bg]] = num
32
+ $ColorPairs[[fsym, bsym]] = num
33
33
  end
34
34
  end
35
35
 
36
+ # $ColorPairs.each_pair {|k, v| STDOUT.puts "#{k.inspect} => #{v}" }
37
+
36
38
  module Keys
37
39
  Down = 258
38
40
  Up = 259
@@ -107,6 +109,10 @@ module RubyText
107
109
  end
108
110
  obj
109
111
  end
112
+
113
+ def fg=(sym)
114
+ self.colors(@win, fg, @bg)
115
+ end
110
116
  end
111
117
  end
112
118
 
@@ -114,13 +120,36 @@ end
114
120
  module RubyText
115
121
 
116
122
  def self.set(*args)
123
+ # Allow a block?
124
+ standard = [:cbreak, :raw, :echo, :keypad]
125
+ @flags = [] # FIXME can set/reset individually. hmmm
117
126
  args.each do |arg|
118
- flag = arg.to_s
119
- flag.sub!(/_/, "no")
120
- X.send(flag)
127
+ if standard.include? arg
128
+ flag = arg.to_s
129
+ @flags << arg
130
+ flag.sub!(/_/, "no")
131
+ X.send(flag)
132
+ else
133
+ @flags << arg
134
+ case arg
135
+ when :cursor
136
+ X.show_cursor
137
+ when :_cursor, :nocursor
138
+ X.hide_cursor
139
+ end
140
+ end
121
141
  end
122
142
  end
123
143
 
144
+ def save_flags
145
+ @fstack ||= []
146
+ @fstack.push @flags
147
+ end
148
+
149
+ def rest_flags
150
+ @flags = @fstack.pop
151
+ end
152
+
124
153
  def self.start(*args, log: nil, fg: nil, bg: nil)
125
154
  $debug = File.new(log, "w") if log
126
155
  Object.const_set(:STDSCR, RubyText::Window.main(fg: fg, bg: bg))
@@ -159,12 +188,65 @@ module RubyText
159
188
  X.curs_set(2) # Doesn't work?
160
189
  end
161
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
+
162
243
  end
163
244
 
164
245
  class RubyText::Window
165
246
  Vert, Horiz = X::A_VERTICAL, X::A_HORIZONTAL
166
247
 
167
- attr_reader :win, :rows, :cols, :width, :height, :fg, :bg
248
+ attr_reader :win, :rows, :cols, :width, :height
249
+ attr_writer :fg, :bg
168
250
 
169
251
  def initialize(high=nil, wide=nil, r0=1, c0=1, border=false, fg=nil, bg=nil)
170
252
  debug "RT::Win.init: #{[high, wide, r0, c0, border]}"
@@ -203,23 +285,37 @@ class RubyText::Window
203
285
  self[r, c] = ch[0]
204
286
  end
205
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
+
206
293
  def delegate_output(sym, *args)
207
294
  args = [""] if args.empty?
208
- debug "delegate: colors are #@fg, #@bg"
209
295
  RubyText::Window.colors(@win, @fg, @bg) # FIXME?
210
- # debug "#{sym}: args = #{args.inspect}"
211
296
  if sym == :p
212
297
  args.map!(&:inspect)
213
298
  else
214
- args.map!(&:to_s)
299
+ args.map! do |x|
300
+ if RubyText::Colors.include? x
301
+ x
302
+ else
303
+ x.to_s
304
+ end
305
+ end
215
306
  end
216
- str = sprintf(*args)
217
- flag = true if sym != :print && str[-1] != "\n"
218
- # FIXME: color-handling code here
219
- str.each_char do |ch|
220
- ch == "\n" ? crlf : @win.addch(ch)
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
221
316
  end
222
317
  crlf if flag
318
+ RubyText::Window.colors(@win, @fg, @bg) # FIXME?
223
319
  @win.refresh
224
320
  end
225
321
 
data/lib/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  module RubyText
3
- VERSION = "0.0.37"
3
+ VERSION = "0.0.38"
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.37
4
+ version: 0.0.38
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-18 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses
@@ -57,6 +57,8 @@ files:
57
57
  - examples/prog17.rb
58
58
  - examples/prog18.rb
59
59
  - examples/prog19.rb
60
+ - examples/prog20.rb
61
+ - examples/prog21.rb
60
62
  - examples/showme.rb
61
63
  - examples/slides
62
64
  - lib/rubytext.rb