Class: RubyCurses::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/experimental/widgets/resultsetbrowser.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.

Class Method Summary collapse

Class Method Details

.browse(dbconn, tablename, columns, rows, 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. def self.view what, config={} #:yield: textview

Parameters:

  • filename

    as string or content as array

Yields:

  • textview object for further configuration before display



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rbcurse/experimental/widgets/resultsetbrowser.rb', line 198

def self.browse dbconn, tablename, columns, rows, config={} #:yield: ???
  wt = 0 # top margin
  wl = 0 # left margin
  wh = Ncurses.LINES-wt-3 # height, goes to bottom of screen
  ww = Ncurses.COLS-wl-3  # 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')

  wh = Ncurses.LINES-0
  ww = Ncurses.COLS-0
  layout = { :height => wh, :width => ww, :top => wt, :left => wl } 
  #v_window = config[:window] # using previous window cause crash seg fault
  #v_window ||= VER::Window.new(layout) # copywin gives -1 and prints nothing
  v_window = VER::Window.root_window
  v_window.printstring 0, 30, "Database Browser Demo", $datacolor
  @form = RubyCurses::Form.new v_window # only for some widgets that are not editable
  header = app_header "rbcurse ", :text_center => "ResultsetBrowser Demo", :text_right =>"New Improved!", :color => :black, :bgcolor => :white, :attr => :bold 
  sl = status_line :row => v_window.height == 0 ? Ncurses.LINES-1 : v.window.height-1
  sl.command { "Record Navigation: C-n C-p. Scrolling M-n, M-p, M-l, M-h" }
  
  @form.repaint

  #rb = ResultsetBrowser.new v_form, :row => 2, :col => 2
  rb = ResultsetBrowser.new v_window, :row => 2, :col => 2, :height => 20, :width => 95
  rb.columns = columns
  rb.data = rows
  rb.repaint


  # yielding textview so you may further configure or bind keys or events
  begin
  #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] 
      rb.handle_key ch
      #v_form.handle_key ch
    end
  rescue => err
    $log.error err.to_s
    $log.error err.backtrace.join("\n")
    alert err.to_s

  ensure
    v_window.destroy if !v_window.nil?
  end
end

.browse_sql(dbname, tablename, sql, config = {}) ⇒ Object

:yield: ???



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rbcurse/experimental/widgets/resultsetbrowser.rb', line 182

def self.browse_sql dbname, tablename, sql, config={} #:yield: ???
  raise "file not found" unless File.exist? dbname
  require 'sqlite3'
  db = SQLite3::Database.new(dbname)
  columns, *rows = db.execute2(sql)
  #$log.debug "XXX COLUMNS #{sql}  "
  content = rows
  return nil if content.nil? or content[0].nil?
  datatypes = content[0].types 
  self.browse db, tablename, columns, rows, config
end