Module: RubyText

Defined in:
lib/rubytext.rb,
lib/menu.rb,
lib/color.rb,
lib/version.rb,
lib/settings.rb

Overview

Skeleton… Can’t put at top because of #initalize

Defined Under Namespace

Modules: Keys Classes: Effects, Window

Constant Summary collapse

Colors =
[:black, :blue, :cyan, :green, :magenta, 
:red, :white, :yellow]
VERSION =
"0.0.47"
Path =
File.expand_path(File.join(File.dirname(__FILE__)))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flagsObject

Hmm, all these are module-level.



3
4
5
# File 'lib/settings.rb', line 3

def self.flags
  @fstack.last
end

.hide_cursorObject



85
86
87
# File 'lib/settings.rb', line 85

def self.hide_cursor
  X.curs_set(0)
end


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/menu.rb', line 23

def self.menu(r: 0, c: 0, items:, curr: 0)
  high = items.size + 2
  wide = items.map(&:length).max + 4
  saveback(high, wide, r, c)
  win = RubyText.window(high, wide, r, c, true, fg: :white, bg: :blue)
  RubyText.set(:raw)
  X.stdscr.keypad(true)
  RubyText.hide_cursor
  sel = curr
  max = items.size - 1
  loop do
    items.each.with_index do |item, row|
      win.go row, 0
      color = sel == row ? :yellow : :white
      win.puts color, " #{item} "
    end
    ch = getch
    case ch
      when X::KEY_UP
        sel -= 1 if sel > 0
      when X::KEY_DOWN
        sel += 1 if sel < max
      when 27
        restback(high, wide, r, c)
        return [nil, nil]
      when 10
        restback(high, wide, r, c)
        return [sel, items[sel]]
    end
  end
end

.method_missing(name, *args) ⇒ Object

For passing through arbitrary method calls to the lower level…



72
73
74
75
76
77
78
79
# File 'lib/settings.rb', line 72

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

.resetObject



38
39
40
# File 'lib/settings.rb', line 38

def self.reset
  restflags
end

.rest_flagsObject



47
48
49
50
51
# File 'lib/settings.rb', line 47

def self.rest_flags
  @flags = @fstack.pop
rescue 
  @flags = @defaults
end

.restback(high, wide, r, c) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/menu.rb', line 13

def self.restback(high, wide, r, c)
  0.upto(high) do |h|
    0.upto(wide) do |w|
      STDSCR[h+r, w+c] = @save.shift
    end
  end
  STDSCR.go *@pos
  STDSCR.refresh
end

.save_flagsObject



42
43
44
45
# File 'lib/settings.rb', line 42

def self.save_flags
  @fstack ||= []
  @fstack.push @flags
end

.saveback(high, wide, r, c) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/menu.rb', line 3

def self.saveback(high, wide, r, c)
  @pos = STDSCR.rc
  @save = []
  0.upto(high) do |h|
    0.upto(wide) do |w|
      @save << STDSCR[h+r, w+c]
    end
  end
end

.set(*args) ⇒ Object

Allow a block?



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/settings.rb', line 7

def self.set(*args)   # Allow a block?
  standard = [:cbreak, :raw, :echo, :keypad]
  @defaults = [:cbreak, :echo, :keypad, :cursor, :_raw]
  @flags = @defaults.dup
  save_flags
  args.each do |arg|
    @flags << arg
    flag = arg.to_s
    if standard.include? flag.to_sym
      X.send(flag)
    elsif flag[0] == "_" && standard.include?(flag[1..-1].to_sym)
      flag.sub!(/^_/, "no")
      X.send(flag)
    else
      case arg
        when :cursor
          X.show_cursor
        when :_cursor, :nocursor
          X.hide_cursor
        else 
          self.stop
          raise RTError
      end
    end
  end
  if block_given?
    yield
    rest_flags
  end
end

.show_cursorObject



89
90
91
# File 'lib/settings.rb', line 89

def self.show_cursor
  X.curs_set(1)
end

.show_cursor!Object



93
94
95
# File 'lib/settings.rb', line 93

def self.show_cursor!
  X.curs_set(2)  # Doesn't work?
end

.start(*args, log: nil, fg: nil, bg: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/settings.rb', line 53

def self.start(*args, log: nil, fg: nil, bg: nil)
  $debug = File.new(log, "w") if log
  Object.const_set(:STDSCR, RubyText::Window.main(fg: fg, bg: bg))
  $stdscr = STDSCR
  fg, bg, cp = fb2cp(fg, bg)
  self.set(:_echo, :cbreak, :raw)  # defaults
#   X.stdscr.keypad(true)
  self.set(*args)  # override defaults
rescue 
  raise RTError
end

.stopObject



65
66
67
# File 'lib/settings.rb', line 65

def self.stop
  X.close_screen
end

.window(high, wide, r0, c0, border = false, fg: nil, bg: nil) ⇒ Object



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

def self.window(high, wide, r0, c0, border=false, fg: nil, bg: nil)
  RubyText::Window.new(high, wide, r0, c0, border, fg, bg)
end

Instance Method Details

#selector(r: 0, c: 0, rows: 10, cols: 20, items:, win:, callback:, quit: "q") ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/menu.rb', line 55

def selector(r: 0, c: 0, rows: 10, cols: 20, items:, 
             win:, callback:, quit: "q")
  high = rows
  wide = cols
  saveback(high, wide, r, c)
  menu_win = RubyText.window(high, wide, r, c, true, fg: :white, bg: :blue)
  win2 = win
  handler = callback
  RubyText.set(:raw)
  X.stdscr.keypad(true)
  RubyText.hide_cursor
  sel = 0
  max = items.size - 1
  handler.call(0, items[0])
  loop do
    items.each.with_index do |item, row|
      menu_win.go row, 0
      color = sel == row ? :yellow : :white
      menu_win.puts color, " #{item} "
    end
    ch = getch
    case ch
      when X::KEY_UP
        if sel > 0
          sel -= 1
          handler.call(sel, items[sel])
        end
      when X::KEY_DOWN
        if sel < max
          sel += 1
          handler.call(sel, items[sel])
        end
      when quit  # parameter
        exit
    end
  end
end