Module: RubyText

Defined in:
menu.rb,
rubytext.rb,
rubytext.rb,
rubytext_version.rb,
settings.rb,
widgets.rb,
window.rb

Overview

Skeleton… Can't put classes at top because of #initalize

Defined Under Namespace

Modules: Keys Classes: Color, Effects, Settings, Window

Constant Summary collapse

VERSION =
"0.1.22"
Path =
File.expand_path(File.join(File.dirname(__FILE__)))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.beepObject



77
78
79
# File 'settings.rb', line 77

def self.beep
  Curses.beep
end

.flashObject



81
82
83
# File 'settings.rb', line 81

def self.flash
  Curses.flash
end

.hide_cursorObject

remove later?



137
138
139
# File 'settings.rb', line 137

def self.hide_cursor    # remove later?
  Curses.curs_set(0)
end

.method_missing(name, *args) ⇒ Object

For passing through arbitrary method calls to the lower level…



128
129
130
131
132
133
134
135
# File 'settings.rb', line 128

def self.method_missing(name, *args)
  debug "method_missing: #{name}  #{args.inspect}"
  if name[0] == '_'
    Curses.send(name[1..-1], *args)
  else
    raise "#{name} #{args.inspect}" # NoMethodError
  end
end

.selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20, items:, fg: White, bg: Blue, win2:, callback:, enter: nil, quit: "q") ⇒ Object

Two-paned widget with menu on left, informtional area on right



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'menu.rb', line 272

def self.selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20, 
                  items:, fg: White, bg: Blue,
                  win2:, callback:, enter: nil, quit: "q")
  high = rows
  wide = cols
  mwin = RubyText.window(high, wide, r: r, c: c, fg: fg, bg: bg)
  handler = callback
  Curses.stdscr.keypad(true)
  RubyText.hide_cursor
  sel = 0
  max = items.size - 1
  handler.call(sel, items[sel], win2)
  loop do
    mwin.home
    items.each.with_index do |item, row|
      mwin.crlf
      style = (sel == row) ? :reverse : :normal
      mwin.print fx(" #{item}", style)
    end
    ch = getch
    case ch
      when Up
        if sel > 0
          sel -= 1
          handler.call(sel, items[sel], win2)
        end
      when Down
        if sel < max
          sel += 1
          handler.call(sel, items[sel], win2)
        end
      when Enter
        if enter
          del = enter.call(sel, items[sel], win2)
          if del
            items -= [items[sel]]
            raise 
          end
        end
      when Tab
        Curses.flash
      when quit  # parameter
        exit
      else Curses.beep    # all else is trash
    end
  end
rescue
  retry
end

.show_cursorObject

remove later?



141
142
143
# File 'settings.rb', line 141

def self.show_cursor    # remove later?
  Curses.curs_set(1)
end

.show_cursor!Object



145
146
147
# File 'settings.rb', line 145

def self.show_cursor!
  Curses.curs_set(2)  # Doesn't work? Device-dependent?
end

.spinner(label: "", win: STDSCR, &block) ⇒ Object

TODO delay, etc.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'widgets.rb', line 16

def self.spinner(label: "", win: STDSCR, &block) # TODO delay, etc.
  chars = "-\\|/"
  RubyText.hide_cursor
  t0 = Time.now.to_i
  thread = Thread.new do
    i=0
    loop do 
      t1 = Time.now.to_i
      elapsed = "0:%02d" % (t1-t0)   # FIXME breaks at 60 sec
      i = (i+1) % 4
      win.print " #{label} #{chars[i]}  #{elapsed}"
      win.left!
      sleep 0.04
    end
  end
  ret = block.call
  win.puts
  Thread.kill(thread)
  RubyText.show_cursor
  ret
end

.splash(msg) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'widgets.rb', line 38

def self.splash(msg)
  lines = msg.split("\n")
  high = lines.size + 4
  wide = lines.map {|x| x.length }.max + 4
  r0 = (STDSCR.rows - high)/2
  c0 = (STDSCR.cols - wide)/2
  STDSCR.saveback(high, wide, r0, c0)
  win = RubyText.window(high, wide, r: r0, c: c0, 
                        fg: White, bg: Red, title: "[Press any key]")
  win.print "\n "
  win.puts msg
  getch
  STDSCR.restback(high, wide, r0, c0)
end

.start(*args, log: "/tmp/rubytext.log", fg: White, bg: Blue, scroll: false) ⇒ Object

FIXME refactor save/restore, etc. - rep as binary vector?



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'settings.rb', line 89

def self.start(*args, log: "/tmp/rubytext.log", 
               fg: White, bg: Blue, scroll: false)
  $debug ||= File.new(log, "w") if log   # FIXME remove global

  args.each {|arg| raise "#{arg} is not valid" unless Settings::ValidArgs.include?(arg) }
  raise RTError("#{fg} is not a color") unless ::Colors.include? fg
  raise RTError("#{bg} is not a color") unless ::Colors.include? bg

  @settings = Settings.new
  @settings.set(*args)            # override defaults

  main = RubyText::Window.main(fg: fg, bg: bg, scroll: scroll)
  Object.const_set(:STDSCR, main) unless defined? STDSCR
  $stdscr = STDSCR  # FIXME global needed?
  Object.include(WindowIO)
  Curses.ESCDELAY = 10
  @started = true
# rescue => err
#   puts(err.inspect)
#   puts(err.backtrace)
#   raise RTError("#{err}")
end

.startedObject

remove later



69
70
71
# File 'settings.rb', line 69

def self.started   # remove later
  @started
end

.started?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'settings.rb', line 73

def self.started?
  @started
end

.stopObject



112
113
114
115
# File 'settings.rb', line 112

def self.stop
  @started = false
  Curses.close_screen
end

.ticker(row: STDSCR.rows-1, col: 0, width: STDSCR.cols, fg: White, bg: Blue, text:, delay: 0.1) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'widgets.rb', line 2

def self.ticker(row: STDSCR.rows-1, col: 0, width: STDSCR.cols, 
                fg: White, bg: Blue, text:, delay: 0.1)
  text = text.gsub("\n", " ") + " "
  win = RubyText.window(1, width, r: row, c: col, border: false, fg: fg, bg: bg)
  leader = " "*width + text
  leader = text.chars.cycle.each_cons(width)
  width.times { win.rcprint 0, 0, leader.next.join }
  repeat = text.chars.cycle.each_cons(width)
  loop do   # Warning: loops forever
    win.rcprint 0, 0, repeat.next.join
    sleep delay
  end
end

.window(high, wide, r: nil, c: nil, border: true, fg: White, bg: Blue, scroll: false, title: nil) ⇒ Object



3
4
5
6
7
8
9
10
# File 'window.rb', line 3

def self.window(high, wide, r: nil, c: nil, border: true, 
                fg: White, bg: Blue, scroll: false, title: nil)
  r ||= (STDSCR.rows - high)/2
  c ||= (STDSCR.cols - wide)/2
  win = RubyText::Window.new(high, wide, r, c, border, fg, bg, scroll)
  win.add_title(title) if title
  win
end

Instance Method Details

#resetObject



121
122
123
# File 'settings.rb', line 121

def reset
  @settings.reset
end

#set(*args) ⇒ Object



117
118
119
# File 'settings.rb', line 117

def set(*args)
  @settings.set(*args)
end