Class: RubyCurses::Viewer

Inherits:
Object show all
Defined in:
lib/rbcurse/extras/viewer.rb

Overview

a data viewer for viewing some text or filecontents view filename, :close_key => KEY_RETURN send data in an array view Array, :close_key => KEY_RETURN, :layout => [0,0,23,80] when passing layout reserve 4 rows for window and border. So for 2 lines of text give 6 rows.

Since:

  • 1.2.0

Class Method Summary collapse

Class Method Details

.view(what, config = {}) { ... } ⇒ Object

NOTE: i am experimentally yielding textview object so i could supress borders just for kicks, but on can also bind_keys or events if one wanted.

Parameters:

  • filename

    as string or content as array

Yields:

  • textview object for further configuration before display

Since:

  • 1.2.0



22
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
54
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/rbcurse/extras/viewer.rb', line 22

def self.view what, config={} #:yield: textview
  case what
  when String # we have a path
    content = _get_contents(what)
  when Array
    content = what
  else
    raise ArgumentError, "Expecting Filename or Contents (array), but got #{what.class} "
  end
  wt = 0 # top margin
  wl = 0 # left margin
  wh = Ncurses.LINES-wt # height, goes to bottom of screen
  ww = Ncurses.COLS-wl  # width, goes to right end
  wt, wl, wh, ww = config[:layout] if config.has_key? :layout

  fp = config[:title] || ""
  pf = config.fetch(:print_footer, true)
  ta = config.fetch(:title_attrib, 'bold')
  fa = config.fetch(:footer_attrib, 'bold')

  layout = { :height => wh, :width => ww, :top => wt, :left => wl } 
  v_window = VER::Window.new(layout)
  v_form = RubyCurses::Form.new v_window
  textview = TextView.new v_form do
    name   "Viewer" 
    row  0
    col  0
    width ww
    height wh-0 # earlier 2 but seems to be leaving space.
    title fp
    title_attrib ta
    print_footer pf
    footer_attrib fa
  end

  t = textview
  t.bind_key('<'){ f = t.form.window; c = f.left - 1; f.hide; f.mvwin(f.top, c); f.show;
    f.reset_layout([f.height, f.width, f.top, c]); 
  }
  t.bind_key('>'){ f = t.form.window; c = f.left + 1; f.hide; f.mvwin(f.top, c); 
    f.reset_layout([f.height, f.width, f.top, c]); f.show;
  }
  t.bind_key('^'){ f = t.form.window; c = f.top - 1 ; f.hide; f.mvwin(c, f.left); 
    f.reset_layout([f.height, f.width, c, f.left]) ; f.show;
  }
  t.bind_key('V'){ f = t.form.window; c = f.top + 1 ; f.hide; f.mvwin(c, f.left); 
    f.reset_layout([f.height, f.width, c, f.left]); f.show;
  }
  # yielding textview so you may further configure or bind keys or events
  begin
  textview.set_content content #, :WRAP_WORD
  yield textview if block_given? # tentative
  v_form.repaint
  v_window.wrefresh
  Ncurses::Panel.update_panels
  # allow closing using q and Ctrl-q in addition to any key specified
  #  user should not need to specify key, since that becomes inconsistent across usages
    while((ch = v_window.getchar()) != ?\C-q.getbyte(0) )
      break if ch == config[:close_key] || ch == ?q.ord
      # if you've asked for RETURN then i also check for 10 and 13
      break if (ch == 10 || ch == 13) && config[:close_key] == KEY_RETURN
      v_form.handle_key ch
      v_form.repaint
    end
  rescue => err
    alert err.to_s
  ensure
    v_window.destroy if !v_window.nil?
  end
end