Class: RubyCurses::Viewer

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

Overview

a data viewer for viewing some text or filecontents view filename, :close_key => KEY_ENTER send data in an array view Array, :close_key => KEY_ENTER, :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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbcurse/core/util/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, "Viewer: 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')
  type = config[:content_type]

  layout = { :height => wh, :width => ww, :top => wt, :left => wl } 
  v_window = VER::Window.new(layout)
  v_form = RubyCurses::Form.new v_window
  colors = Ncurses.COLORS
  back = :blue
  back = 235 if colors >= 256
  blue_white = get_color($datacolor, :white, back)
  #blue_white = RubyCurses::Utils.get_color($datacolor, :white, 235)
  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
    #border_attrib :reverse
    border_color blue_white
  end
  require 'rbcurse/core/include/multibuffer'
  textview.extend(RubyCurses::MultiBuffers)

  t = textview
  t.bind_key('<', 'move window left'){ 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('>', 'move window right'){ 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('^', 'move window up'){ 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', 'move window down'){ 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, :content_type => type
  textview.add_content content, :content_type => type
  # the next can also be used to use formatted_text(text, :ansi)
  yield textview if block_given? 
  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 || ch == 2727 # added double esc 2011-12-27 
      # if you've asked for ENTER then i also check for 10 and 13
      break if (ch == 10 || ch == 13) && config[:close_key] == KEY_ENTER
      v_form.handle_key ch
      v_form.repaint
    end
  rescue => err
      $log.error " VIEWER ERROR #{err} "
      $log.debug(err.backtrace.join("\n"))
      textdialog ["Error in viewer: #{err} ", *err.backtrace], :title => "Exception"
  ensure
    v_window.destroy if !v_window.nil?
  end
end