rubytext 0.0.65 → 0.0.66

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: d60ee098ba99ff96172a641b1e77715ce7c1177b857ca54c7e4502549e2cf3e5
4
- data.tar.gz: 34931ad3fc293e543c2a24187a040319a9578879ca69c2b43d3cb4a2d07d3038
3
+ metadata.gz: 1026a49ec8ff0ecd96f14d6e34cb5bf689e277e2c619ea39f9746c899f5238a9
4
+ data.tar.gz: 8baa928f1c76383407165447ea3df23876f1fb698cfd05444088e78d5067fd5b
5
5
  SHA512:
6
- metadata.gz: 2fdbdb36477e756af00e44c36bc76f76e80d62b485910f83b4cbf70ed96a1a2f766ee56e8eb548e4053772163a0ebfcce10f320273f507351659adea3accb25b
7
- data.tar.gz: 6cda04f52196315b520a1db9cf4311d7fb09d6bd8f18d729a5aae4bd69d24345f189830c8222a74ae303ca62201484c45f4c131272e9d072ee9cd007cb095360
6
+ metadata.gz: e4e118a18444c9831ecc6136f6ac36caa16fa6f60adb6fd4de5640d3b66901f623348d46306ac812387183163e30c674e47f0f2533e8c81f650f74d6094e10b4
7
+ data.tar.gz: 73a1fe904439c2f680dace4927528a7cff96afe84a4f3bb43ecd04ef7e9b9f4af4f191833dd61beef968cb91887b98f8cf88882521af86e511ba864524512bee
data/examples/_demo.rb ADDED
@@ -0,0 +1,118 @@
1
+ $LOAD_PATH.unshift "lib"
2
+
3
+ require 'rubytext'
4
+
5
+ def delay(sec, speed=1.0)
6
+ sleep sec/speed
7
+ end
8
+
9
+ speed = ARGV.first || 1.0
10
+ speed = speed.to_f
11
+ delay(speed)
12
+
13
+ RubyText.start(log: "mylog.txt", fg: Green, bg: Black)
14
+
15
+ print "Here goes... "
16
+ delay 2
17
+
18
+ ymax, xmax = STDSCR.rows, STDSCR.cols
19
+
20
+ puts "STDSCR is #{ymax.inspect} by #{xmax.inspect}"
21
+ delay 2
22
+
23
+ print "\nI can write here "
24
+ delay 2
25
+ puts "and then continue."
26
+
27
+ delay 2
28
+ print "I can jump away "
29
+
30
+ delay 2
31
+ STDSCR.go(20, 15) { puts "and print somewhere else" }
32
+ # ^ Same as: rcprint(20, 15, "and print somewhere else" }
33
+
34
+ delay 2
35
+ puts "and then return."
36
+
37
+ delay 3
38
+ print "I can hide the cursor... "
39
+ delay 1
40
+ RubyText.hide_cursor
41
+ delay 1
42
+ print "then show it again... "
43
+ delay 1
44
+ RubyText.show_cursor
45
+ delay 1
46
+ print "and hide it again. "
47
+ delay 1
48
+ RubyText.hide_cursor
49
+ delay 3
50
+
51
+ puts "\n\nNow watch as I create a window:"
52
+
53
+ mywin = RubyText.window(16, 40, 8, 14, fg: :blue, bg: :yellow)
54
+
55
+ delay 3
56
+ mywin.puts "\nNow I'm writing in a window."
57
+ mywin.puts "\nIts size is #{mywin.rows} rows by #{mywin.cols} cols"
58
+ mywin.puts "(or #{mywin.height}x#{mywin.width} with the frame).\n "
59
+
60
+ delay 4
61
+ mywin.puts "I always try to honor the borders of my window."
62
+
63
+ delay 2
64
+ mywin.puts "\nWatch as I place 50 random stars here..."
65
+
66
+ delay 2
67
+
68
+ 50.times do
69
+ r = rand(mywin.rows)
70
+ c = rand(mywin.cols)
71
+ delay 0.1
72
+ mywin.rcprint(r, c, "*")
73
+ end
74
+
75
+ delay 4
76
+ mywin.output do
77
+ mywin.clear
78
+ puts "The window method called 'output'"
79
+ puts "will temporarily override STDSCR so that (in the code)"
80
+ puts "we don't have to use the window name over and over for stream I/O."
81
+ delay 4
82
+ # STDSCR.rcprint 25, 3, "Of course I can still print here if I want."
83
+ STDSCR.go 25, 3
84
+ STDSCR.print "Of course I can still print here if I want."
85
+ delay 3
86
+ puts "\nOne moment..."
87
+ delay 4
88
+ end
89
+
90
+ mywin.output do
91
+ mywin.clear
92
+ puts "The []= method allows us to place characters in"
93
+ puts "the window (without auto-refresh)."
94
+ puts "\nLet's see the '50 stars' trick again:"
95
+ delay 5
96
+ 50.times do
97
+ r = rand(mywin.rows)
98
+ c = rand(mywin.cols)
99
+ mywin[r, c] = "*"
100
+ end
101
+ mywin.cwin.refresh
102
+ end
103
+
104
+ delay 5
105
+ mywin.output do
106
+ mywin.clear
107
+ mywin.puts "The [] method (zero-based) is still buggy, but let's try it."
108
+ mywin.puts "XYZ"
109
+ ch = mywin[2,2]
110
+ mywin.puts "The char at [2,2] is: #{ch.chr.inspect}"
111
+ end
112
+
113
+ delay 4
114
+ mywin.puts "\nThat's all for now."
115
+ delay 1
116
+ mywin.puts "\nPress any key to exit."
117
+
118
+ getch
@@ -0,0 +1,58 @@
1
+ require 'rubytext'
2
+ require 'timeout'
3
+
4
+ def next_slide
5
+ RubyText.hide_cursor
6
+ sleep 0.2
7
+ Timeout.timeout(999) do
8
+ STDSCR.go @rmax-1-@upward, 0
9
+ STDSCR.center "Press any key..."
10
+ getch
11
+ end
12
+ rescue Timeout::Error
13
+ end
14
+
15
+ def show_code(prog, upward=0)
16
+ text = File.read(prog)
17
+ nlines = text.split("\n").size
18
+
19
+ prog_top = @rmax-nlines-3 - upward.to_i
20
+ code = RubyText.window(nlines+2, @cmax-2, prog_top, 1,
21
+ fg: Green, bg: Black)
22
+ code.puts text
23
+
24
+ right = STDSCR.cols - prog.length - 8
25
+ STDSCR.go prog_top, right
26
+ STDSCR.print "[ #{prog} ]"
27
+ STDSCR.go 0,0
28
+ end
29
+
30
+ def check_window
31
+ if @rmax < 25 || @cmax < 80
32
+ puts "\n Your window should be 25x80 or larger,"
33
+ puts " but this one is only #{@rmax}x#{@cmax}."
34
+ puts " Please resize and run again!"
35
+ getch
36
+ exit 1
37
+ end
38
+ end
39
+
40
+ #### Main
41
+
42
+ RubyText.start(:cbreak, log: "/tmp/showme.log", fg: White, bg: Black)
43
+
44
+ @cmax = STDSCR.cols
45
+ @rmax = STDSCR.rows
46
+
47
+ RubyText.hide_cursor
48
+
49
+ check_window
50
+
51
+ prog, @upward = ARGV
52
+ @upward ||= 0
53
+
54
+ show_code(prog, @upward)
55
+
56
+ require_relative prog
57
+
58
+ next_slide # if @upward == 0
data/examples/_slides ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ mylib = `gem which rubytext`.chomp
4
+ examples = mylib.sub(%r[/lib/rubytext.rb], "/examples")
5
+
6
+ Dir.chdir(examples)
7
+
8
+ rc = system("ruby check.rb")
9
+ exit unless rc
10
+
11
+ # FIXME
12
+ # - use names instead of numbers?
13
+ # - convert showme into a method?
14
+
15
+ 1.upto(22) do |n|
16
+ next if n == 11 # junk this one
17
+ num = '%02d' % n
18
+ prog = "prog#{num}.rb"
19
+ upward = 11 if n == 22 # dumbest hack ever!
20
+ system("ruby showme.rb #{prog} #{upward}")
21
+ end
22
+
data/examples/check.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'rubytext'
2
2
 
3
- RubyText.start(:raw, fg: :white, bg: :black)
3
+ RubyText.start(:raw, log: "foo", fg: :white, bg: :black)
4
4
 
5
5
  cmax = STDSCR.cols
6
6
  rmax = STDSCR.rows
data/examples/foo ADDED
@@ -0,0 +1,7 @@
1
+ #<ArgumentError: wrong number of arguments (given 2, expected 1)>
2
+ /Users/Hal/.rvm/gems/ruby-2.4.2/gems/rubytext-0.0.65/lib/color.rb:30:in `[]'
3
+ /Users/Hal/.rvm/gems/ruby-2.4.2/gems/rubytext-0.0.65/lib/color.rb:30:in `colors'
4
+ /Users/Hal/.rvm/gems/ruby-2.4.2/gems/rubytext-0.0.65/lib/color.rb:35:in `colors!'
5
+ /Users/Hal/.rvm/gems/ruby-2.4.2/gems/rubytext-0.0.65/lib/window.rb:33:in `main'
6
+ /Users/Hal/.rvm/gems/ruby-2.4.2/gems/rubytext-0.0.65/lib/settings.rb:75:in `start'
7
+ check.rb:3:in `<main>'
data/examples/slides CHANGED
@@ -16,7 +16,7 @@ exit unless rc
16
16
  next if n == 11 # junk this one
17
17
  num = '%02d' % n
18
18
  prog = "prog#{num}.rb"
19
- upward = 15 if n == 22 # dumbest hack ever!
19
+ upward = 10 if n == 22 # dumbest hack ever!
20
20
  system("ruby showme.rb #{prog} #{upward}")
21
21
  end
22
22
 
data/lib/_color.rb ADDED
@@ -0,0 +1,69 @@
1
+
2
+ def fb2cp(fg, bg)
3
+ fg ||= Blue
4
+ bg ||= White
5
+ f2 = X.const_get("COLOR_#{fg.upcase}")
6
+ b2 = X.const_get("COLOR_#{bg.upcase}")
7
+ cp = RubyText::ColorPairs[[fg, bg]]
8
+ [f2, b2, cp]
9
+ end
10
+
11
+ class RubyText::Color
12
+ attr_reader :curses
13
+
14
+ def self.pair(fg, bg)
15
+ RubyText::ColorPairs[[fg, bg]]
16
+ end
17
+
18
+ def initialize(sym)
19
+ @color = sym
20
+ @curses = X.const_get("COLOR_#{sym.upcase}")
21
+ end
22
+ end
23
+
24
+ module RubyText
25
+ Colors = [Black, Blue, Cyan, Green, Magenta, Red, White, Yellow]
26
+
27
+ # Initialize all color pairs
28
+ ColorPairs = {}
29
+ num = 0
30
+ color_file = File.new("pairs.out", "w")
31
+ Colors.each do |fsym|
32
+ Colors.each do |bsym|
33
+ cfg, cbg = Color.new(fsym).curses, Color.new(bsym).curses
34
+ X.init_pair(num+=1, cfg, cbg)
35
+ ColorPairs[[fsym, bsym]] = num
36
+ # debug "pair #{num} = #{[fsym, bsym]}"
37
+ color_file.puts "pair #{num} = #{[fsym, bsym]}"
38
+ end
39
+ end
40
+ color_file.close
41
+ end
42
+
43
+ class RubyText::Window
44
+ def self.set_colors(cwin, fg, bg)
45
+ @cfile = File.new("colors-#{rand(1000)}.out", "w")
46
+ cp = RubyText::Color.pair(fg, bg)
47
+ @cfile.puts "Setting: #{cp} = pair #{[fg, bg].inspect}"
48
+ @cfile.close
49
+ exit
50
+ cwin.color_set(cp)
51
+ end
52
+
53
+ def self.colorize!(win, fg, bg)
54
+ set_colors(win, fg, bg)
55
+ num = win.maxx * win.maxy
56
+ win.setpos(0, 0)
57
+ win.addstr(' '*num)
58
+ win.setpos(0, 0)
59
+ win.refresh
60
+ end
61
+
62
+ def fg=(sym)
63
+ self.class.set_colors(@cwin, sym, @bg) # BREAK??
64
+ end
65
+
66
+ def bg=(sym)
67
+ self.class.set_colors(@cwin, @fg, sym)
68
+ end
69
+ end
data/lib/_window.rb ADDED
@@ -0,0 +1,92 @@
1
+ class RubyText::Window
2
+ Vert, Horiz = X::A_VERTICAL, X::A_HORIZONTAL
3
+
4
+ attr_reader :cwin, :rows, :cols, :width, :height, :scrolling
5
+ attr_writer :fg, :bg
6
+
7
+ # Better to use Window.window IRL
8
+
9
+ def initialize(high=nil, wide=nil, r0=1, c0=1, border=false, fg=nil, bg=nil, scroll=false)
10
+ @wide, @high, @r0, @c0 = wide, high, r0, c0
11
+ @border, @fg, @bg = border, fg, bg
12
+ @cwin = X::Window.new(high, wide, r0, c0)
13
+ RubyText::Window.colorize!(@cwin, fg, bg)
14
+ if @border
15
+ @cwin.box(Vert, Horiz)
16
+ @outer = @cwin
17
+ @outer.refresh
18
+ @cwin = X::Window.new(high-2, wide-2, r0+1, c0+1)
19
+ RubyText::Window.colorize!(@cwin, fg, bg)
20
+ else
21
+ @outer = @cwin
22
+ end
23
+ @rows, @cols = @cwin.maxy, @cwin.maxx # unnecessary really...
24
+ @width, @height = @cols + 2, @rows + 2 if @border
25
+ @scrolling = scroll
26
+ @cwin.scrollok(scroll)
27
+ @cwin.refresh
28
+ end
29
+
30
+ def self.main(fg: nil, bg: nil, scroll: false)
31
+ main_win = X.init_screen
32
+ X.start_color
33
+ colorize!(main_win, fg, bg)
34
+ rows, cols = main_win.maxy, main_win.maxx
35
+ self.make(main_win, rows, cols, 0, 0, border: false,
36
+ fg: fg, bg: bg, scroll: scroll)
37
+ end
38
+
39
+ def self.make(cwin, high, wide, r0, c0, border: true, fg: White, bg: Black, scroll: false)
40
+ obj = self.allocate
41
+ obj.instance_eval do
42
+ @outer = @cwin = cwin
43
+ @wide, @high, @r0, @c0 = wide, high, r0, c0
44
+ @fg, @bg = fg, bg
45
+ @border = border
46
+ @rows, @cols = high, wide
47
+ @width, @height = @cols + 2, @rows + 2 if @border
48
+ end
49
+ obj.scrolling(scroll)
50
+ obj
51
+ end
52
+
53
+ # FIXME refactor bad code
54
+
55
+ def scrolling(flag=true)
56
+ @scrolling = flag
57
+ @cwin.scrollok(flag)
58
+ end
59
+
60
+ def noscroll
61
+ @scrolling = false
62
+ @cwin.scrollok(false)
63
+ end
64
+
65
+ def scroll(n=1)
66
+ if n < 0
67
+ @cwin.scrl(n)
68
+ (-n).times {|i| rcprint i, 0, (' '*@cols) }
69
+ else
70
+ n.times do |i|
71
+ @cwin.scroll
72
+ scrolling(false)
73
+ rcprint @rows-1, 0, (' '*@cols)
74
+ scrolling
75
+ end
76
+ end
77
+ @cwin.refresh
78
+ end
79
+
80
+ def screen_text(file = nil)
81
+ lines = []
82
+ 0.upto(self.rows-1) do |r|
83
+ line = ""
84
+ 0.upto(self.cols-1) do |c|
85
+ line << self[r, c]
86
+ end
87
+ lines << line
88
+ end
89
+ File.open(file, "w") {|f| f.puts lines } if file
90
+ lines
91
+ end
92
+ end
data/lib/output.rb CHANGED
@@ -1,13 +1,11 @@
1
1
  $LOAD_PATH << "lib"
2
2
 
3
- # require 'global' # FIXME later
4
-
5
3
  class RubyText::Window
6
4
  def center(str)
7
5
  r, c = self.rc
8
6
  n = @cwin.maxx - str.length
9
7
  go r, n/2
10
- puts str
8
+ self.puts str
11
9
  end
12
10
 
13
11
  def putch(ch)
@@ -15,44 +13,32 @@ class RubyText::Window
15
13
  self[r, c] = ch[0]
16
14
  end
17
15
 
18
- def need_crlf?(sym, args)
19
- sym != :print && # print doesn't default to crlf
20
- args[-1][-1] != "\n" # last char is a literal linefeed
21
- end
22
-
23
16
  # FIXME Please refactor the Hal out of this.
24
17
 
18
+ def special?(arg)
19
+ RubyText::Colors.include?(arg) ||
20
+ arg.is_a?(RubyText::Effects)
21
+ end
22
+
25
23
  def delegate_output(sym, *args)
26
24
  args = [""] if args.empty?
25
+ args += ["\n"] unless sym == :print
27
26
  RubyText::Window.colors(@cwin, @fg, @bg) # FIXME?
28
27
  if sym == :p
29
28
  args.map!(&:inspect)
30
29
  else
31
- args.map! do |x|
32
- if RubyText::Colors.include?(x) || x.is_a?(RubyText::Effects)
33
- x
34
- else
35
- x.to_s
36
- end
37
- end
30
+ args.map! {|x| special?(x) ? x : x.to_s }
38
31
  end
39
- flag = need_crlf?(sym, args) # Limitation: Can't print color symbols!
40
32
  args.each do |arg|
41
- if arg.is_a? Symbol # must be a color
33
+ case
34
+ when arg.is_a?(Symbol) # must be a color
42
35
  RubyText::Window.colors(@cwin, arg, @bg) # FIXME?
43
- elsif arg.is_a? RubyText::Effects
36
+ when arg.is_a?(RubyText::Effects)
44
37
  X.attrset(arg.value)
45
38
  else
46
- arg.each_char do |ch|
47
- if ch == "\n"
48
- crlf
49
- else
50
- @cwin.addch(ch)
51
- end
52
- end
39
+ arg.each_char {|ch| ch == "\n" ? crlf : @cwin.addch(ch) }
53
40
  end
54
41
  end
55
- crlf if flag
56
42
  RubyText::Window.colors(@cwin, @fg, @bg) # FIXME?
57
43
  @cwin.refresh
58
44
  end
@@ -105,12 +91,11 @@ class RubyText::Window
105
91
  end
106
92
 
107
93
  def clear
108
- win = @cwin
109
- num = win.maxx * win.maxy
110
- win.setpos(0, 0)
111
- win.addstr(' '*num)
112
- win.setpos(0, 0)
113
- win.refresh
94
+ num = @cwin.maxx * @cwin.maxy
95
+ home
96
+ @cwin.addstr(' '*num)
97
+ home
98
+ @cwin.refresh
114
99
  end
115
100
 
116
101
  def output(&block)
@@ -120,12 +105,10 @@ class RubyText::Window
120
105
  end
121
106
 
122
107
  def [](r, c)
123
- save = self.rc
124
- @cwin.setpos r, c
125
- ch = @cwin.inch
126
- @cwin.setpos *save
108
+ ch = nil
109
+ go(r, c) { ch = @cwin.inch }
110
+ debug "ch = #{ch} ch.chr = #{ch.chr}"
127
111
  ch.chr
128
- # go(r, c) { ch = @cwin.inch }
129
112
  end
130
113
 
131
114
  def []=(r, c, char)
data/lib/rubytext.rb CHANGED
@@ -11,6 +11,8 @@ Black, Blue, Cyan, Green, Magenta, Red, White, Yellow =
11
11
  module RubyText
12
12
  end
13
13
 
14
+ $debug = File.new("wtf.out", "w")
15
+
14
16
  require 'version'
15
17
  require 'output' # RubyText, RubyText::Window, Kernel
16
18
  require 'keys' # RubyText::Keys
data/lib/settings.rb CHANGED
@@ -71,8 +71,9 @@ module RubyText
71
71
  end
72
72
 
73
73
  def self.start(*args, log: nil, fg: nil, bg: nil, scroll: false)
74
- $debug = File.new(log, "w") if log
75
- Object.const_set(:STDSCR, RubyText::Window.main(fg: fg, bg: bg, scroll: scroll))
74
+ $debug ||= File.new(log, "w") if log
75
+ main = RubyText::Window.main(fg: fg, bg: bg, scroll: scroll)
76
+ Object.const_set(:STDSCR, main)
76
77
  $stdscr = STDSCR
77
78
  fg, bg, cp = fb2cp(fg, bg)
78
79
  self.set(:_echo, :cbreak) # defaults
@@ -80,7 +81,8 @@ module RubyText
80
81
  rescue => err
81
82
  debug(err.inspect)
82
83
  debug(err.backtrace)
83
- raise RTError("#{err}")
84
+ exit
85
+ # raise RTError("#{err}")
84
86
  end
85
87
 
86
88
  def self.stop
data/lib/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  module RubyText
3
- VERSION = "0.0.65"
3
+ VERSION = "0.0.66"
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.65
4
+ version: 0.0.66
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-29 00:00:00.000000000 Z
11
+ date: 2018-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses
@@ -41,8 +41,12 @@ files:
41
41
  - "./rubytext.gemspec"
42
42
  - bin/rubytext
43
43
  - examples/README
44
+ - examples/_demo.rb
45
+ - examples/_showme.rb
46
+ - examples/_slides
44
47
  - examples/check.rb
45
48
  - examples/demo.rb
49
+ - examples/foo
46
50
  - examples/ide.rb
47
51
  - examples/prog01.rb
48
52
  - examples/prog02.rb
@@ -68,6 +72,8 @@ files:
68
72
  - examples/prog22.rb
69
73
  - examples/showme.rb
70
74
  - examples/slides
75
+ - lib/_color.rb
76
+ - lib/_window.rb
71
77
  - lib/color.rb
72
78
  - lib/effects.rb
73
79
  - lib/keys.rb