module Eft

Constants

CHECK_OPTS
DATE
EXIT
ON_OFF
OPT
OPTS

options per category

OU
VERSION
WHAT

translate method to option

WHIPTAIL

NB: dialog works as well as whiptail, but these options will be incompatible: –*-button (is: –*-label), –scrolltext

Public Class Methods

_file_or_temp(opts, &b) click to toggle source

call block w/ either ‘opts` or a tempfile w/ contents `opts`

# File lib/eft.rb, line 309
def self._file_or_temp(opts, &b)                              # {{{1
  file = opts[:file]; text = opts[:text]
  raise Error, 'can\'t have file and text' if file && text
  raise Error, 'must have file or text' if !file && !text
  if file
    b[file]
  else
    Dir.mktmpdir do |dir|
      open("#{dir}/eft", 'w') do |f|
        f.write text; f.close; b[f.path]
      end
    end
  end
end
_run_whip(args) click to toggle source

run whiptail @return [Hash] ‘{ exit: exitstatus, lines: chomped_lines }`

# File lib/eft.rb, line 298
def self._run_whip(args)
  IO.pipe do |r, w|
    s = OU.spawn_w(*args, err: w); w.close
    { exit: s.exitstatus, lines: r.readlines.map(&:chomp) }
  end
end
_whip(what, text, cfg, opts, args = [], &b) click to toggle source

process options, run whiptail, call ‘b` or `on_{cancel,no,esc}[]` @raise Error if unknown exitstatus

# File lib/eft.rb, line 264
def self._whip(what, text, cfg, opts, args = [], &b)          # {{{1
  o = _whip_opts what, cfg, opts; c = _whip_cmd text, opts, o, args
  r = _run_whip c
  case r[:exit]
  when EXIT[:ok_yes]    ; b[r[:lines]] if b
  when EXIT[:cancel_no] ; cfg.call :on_cancel; cfg.call :on_no
  when EXIT[:esc]       ; cfg.call :on_esc
  else                    raise Error, 'unknown exitstatus'
  end
  nil
end
_whip_cmd(text, opts, opt_args, args) click to toggle source

whiptail command

# File lib/eft.rb, line 288
def self._whip_cmd(text, opts, opt_args, args)                # {{{1
  h = opts[:height] || OU::Term.lines   - 4
  w = opts[:width]  || OU::Term.columns - 4
  s = opts.has_key?(:subheight) ? [opts[:subheight] || h - 8] : []
  a = [WHIPTAIL] + opt_args + ['--', text, h, w] + s + args
  a.map(&:to_s)
end
_whip_opts(what, cfg, opts) click to toggle source

process whiptail options

# File lib/eft.rb, line 277
def self._whip_opts(what, cfg, opts)                          # {{{1
   cfg._opts + [                  OPT[opts,:all],
    cfg.respond_to?(:on_ok)     ? OPT[opts,:ok]     : [],
    cfg.respond_to?(:on_cancel) ? OPT[opts,:cancel] : [],
    cfg.respond_to?(:on_yes)    ? OPT[opts,:yes]    : [],
    cfg.respond_to?(:on_no)     ? OPT[opts,:no]     : [],
    what == :menu               ? OPT[opts,:menu]   : [],
  ] .flatten + [WHAT[what]]
end
ask(text, opts = {}, &b) click to toggle source

ask for input w/ OK/Cancel buttons (and default)

# File lib/eft.rb, line 184
def self.ask(text, opts = {}, &b)
  c = CfgAsk.new(&b); a = opts[:default] ? [opts[:default]] : []
  _whip(:ask, text, c, opts, a) do |lines|
    c.call :on_ok, lines.first
  end
end
ask_pass(text, opts = {}, &b) click to toggle source

ask for password w/ OK/Cancel buttons

# File lib/eft.rb, line 192
def self.ask_pass(text, opts = {}, &b)
  c = CfgAskPass.new(&b)
  _whip(:ask_pass, text, c, opts) do |lines|
    c.call :on_ok, lines.first
  end
end
ask_yesno(text, opts = {}, &b) click to toggle source

ask w/ Yes/No buttons

# File lib/eft.rb, line 200
def self.ask_yesno(text, opts = {}, &b)
  c = CfgAskYesNo.new(&b)
  _whip(:ask_yesno, text, c, opts) { c.call :on_yes }
end
check(text, opts = {}, &b) click to toggle source

choose checkboxes w/ OK/Cancel buttons

# File lib/eft.rb, line 221
def self.check(text, opts = {}, &b)                           # {{{1
  c = CfgCheck.new(&b); i = c._check
  o = opts.merge(subheight: opts[:list_height]).freeze
  a = i.map { |x| [x[:tag],x[:item],ON_OFF[x[:selected]]] } .flatten
  _whip(:check, text, c, o, a) do |lines|
    c.call :on_ok, lines
  end
end
gauge(text, percent, opts = {}, &b) click to toggle source

show gauge; use lambda passed to block to move it forward by passing it ‘percent[, message]`

# File lib/eft.rb, line 245
def self.gauge(text, percent, opts = {}, &b)                  # {{{1
  IO.pipe do |r, w|
    mv  = ->(pct, msg = nil) {
      msg ? w.puts('XXX', pct, msg, 'XXX', pct) : w.puts(pct) # WTF!
    }
    c   = CfgGauge.new
    o   = _whip_opts :gauge, c, opts
    c   = _whip_cmd text, opts, o, [percent]
    pid = OU.spawn(*c, in: r)
    r.close; b[mv]; w.close; Process.wait pid
    raise Error, 'exitstatus != 0' if $?.exitstatus != 0
  end                                                         # }}}1
end
menu(text, opts = {}, &b) click to toggle source

choose from menu w/ OK/Cancel buttons

radio(text, opts = {}, &b) click to toggle source

choose radiobutton w/ OK/Cancel buttons

# File lib/eft.rb, line 231
def self.radio(text, opts = {}, &b)                           # {{{1
  s = opts[:selected] ; f = ->(x) { ON_OFF[x == s] }
  c = CfgRadio.new(&b); i = c._radio
  o = opts.merge(subheight: opts[:list_height]).freeze
  a = i.map { |x| [x[:tag],x[:item],f[x[:tag]]] } .flatten
  _whip(:radio, text, c, o, a) do |lines|
    c.call :on_ok, lines.first
  end                                                         # }}}1
end
show_info(text, opts = {}) click to toggle source

show message w/o buttons, don’t clear screen

# File lib/eft.rb, line 163
def self.show_info(text, opts = {})
  _whip :show_info, text, CfgShowInfo.new, opts
end
show_msg(text, opts = {}, &b) click to toggle source

show message w/ OK button

# File lib/eft.rb, line 168
def self.show_msg(text, opts = {}, &b)
  c = CfgShowMsg.new(&b)
  _whip(:show_msg, text, c, opts) { c.call :on_ok }
end
show_text(opts = {}, &b) click to toggle source

show file contents or text w/ OK button

# File lib/eft.rb, line 174
def self.show_text(opts = {}, &b)
  _file_or_temp opts do |f|
    c = CfgShowText.new(&b)
    _whip(:show_text, f, c, opts) { c.call :on_ok }
  end
end