Module: Io

Included in:
PromptMenu, RubyCurses::ActionManager, RubyCurses::Widget, RubyCurses::WidgetMenu
Defined in:
lib/rbcurse/core/include/io.rb

Overview

******************************************************* Some common io routines for getting data or putting at some point Arunachalesha

2010-03-06 12:10 
Some are outdated.
Current are:
  * rbgetstr (and those it calls)
  * display_cmenu and create_mitem

Changes: 2011-12-6 : removed many old, outdated methods. *******************************************************#

Defined Under Namespace

Classes: CMenuItem, PromptMenu

Instance Method Summary collapse

Instance Method Details

create a 2 line window at bottom to accept user input



17
18
19
# File 'lib/rbcurse/core/include/io.rb', line 17

def __create_footer_window h = 2 , w = Ncurses.COLS, t = Ncurses.LINES-2, l = 0
  ewin = VER::Window.new(h, w , t, l)
end

#clear_this(win, r, c, color, len) ⇒ Object



228
229
230
# File 'lib/rbcurse/core/include/io.rb', line 228

def clear_this win, r, c, color, len
  print_this(win, "%-*s" % [len," "], color, r, c)
end

#get_file(prompt, config = {}) ⇒ String

This is just experimental, trying out tab_completion Prompt user for a file name, allowing him to tab to complete filenames

Parameters:

  • label (String)

    to print before field

  • max (Fixnum)

    length of field

Returns:

  • (String)

    filename or blank if user cancelled



218
219
220
221
222
223
224
225
226
227
# File 'lib/rbcurse/core/include/io.rb', line 218

def get_file prompt, config={}  #:nodoc:
  maxlen = 70
  tabc = Proc.new {|str| Dir.glob(str +"*") }
  config[:tab_completion] ||= tabc
  #config[:default] = "test"
  ret, str = rbgetstr(nil,0,0, prompt, maxlen, config)
  #$log.debug " get_file returned #{ret} , #{str} "
  return "" if ret != 0
  return str
end

prints given text to window, in color at x and y coordinates

Parameters:

  • window (Window)

    to write to

  • text (String)

    to print

  • color (int)

    pair such as $datacolor or $promptcolor

  • x (int)

    row

  • y (int)

    col

See Also:

  • Window#printstring


242
243
244
245
246
247
248
249
250
# File 'lib/rbcurse/core/include/io.rb', line 242

def print_this(win, text, color, x, y)
  raise "win nil in print_this" unless win
  color=Ncurses.COLOR_PAIR(color);
  win.attron(color);
  #win.mvprintw(x, y, "%-40s" % text);
  win.mvprintw(x, y, "%s" % text);
  win.attroff(color);
  win.refresh
end

#rb_getchar(prompt, config = {}) ⇒ Fixnum

get a character. unlike rb_gets allows user to enter control or alt or function character too. If default provided, then ENTER returns the default

Parameters:

  • prompt (String)

    or label to show.

  • configuration (Hash)

    such as default or regexp for validation

Returns:

  • (Fixnum)

    nil if canceled, or ret value of getchar which is numeric



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rbcurse/core/include/io.rb', line 164

def rb_getchar(prompt, config={}) # yield field
  begin
    win = __create_footer_window
    #form = Form.new win
    r = 0; c = 1;
    default = config[:default] 
    prompt = "#{prompt} [#{default}] " if default
    win.mvprintw(r, c, "%s: " % prompt);
    bg = Ncurses.COLORS >= 236 ? 236 : :blue
    color_pair = get_color($reversecolor, :white, bg)
    win.printstring r, c + prompt.size + 2, " ", color_pair

    win.wrefresh
    prevchar = 0
    entries = nil
    while ((ch = win.getchar()) != 999)
      return default.ord if default && (ch == 13 || ch == KEY_ENTER)
      return nil if ch == ?\C-c.getbyte(0) || ch == ?\C-g.getbyte(0)
      if ch == KEY_F1
        help_text = config[:help_text] || "No help provided. C-c/C-g aborts."
        print_status_message help_text, :wait => 7
        win.wrefresh # nevr had to do this with ncurses, but have to with ffi-ncurses ??
        next
      end
      if config[:regexp]
        reg = config[:regexp]
        if ch > 0 && ch < 256
          chs = ch.chr
          return ch if chs =~ reg
          alert "Wrong character. #{reg} "
        else
          alert "Wrong character. #{reg} "
        end
      else
        return ch
      end
      #form.handle_key ch
      win.wrefresh
    end
  rescue => err
    Ncurses.beep
    $log.error "EXC in rbgetsr #{err} "
    $log.error(err.backtrace.join("\n")) 
  ensure
    win.destroy if win
  end
  return nil
end

#rb_gets(prompt, config = {}) {|Field| ... } ⇒ String?

get a string at the bottom of the screen

help_text is displayed on F1 tab_completion is a proc which helps to complete user input

Parameters:

  • prompt (String)
    • label to show

  • config (Hash) (defaults to: {})
    • :default, :display_length of Field, :help_text, :tab_completion

Yields:

  • (Field)

    for overriding or customization

Returns:

  • (String, nil)

    String if entered, nil if canceled



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rbcurse/core/include/io.rb', line 53

def rb_gets(prompt, config={}) # yield field
    if config.is_a? Array
      options = config
      completion_proc = Proc.new{|str| 
        options.dup.grep Regexp.new("^#{str}");
      }
      config = {}
      config[:tab_completion] = completion_proc
    end
  begin
    win = __create_footer_window
    form = Form.new win
    r = 0; c = 1;
    default = config[:default] || ""
    prompt = "#{prompt} [#{default}]:" if default.size > 0
    _max = FFI::NCurses.COLS-1-prompt.size-4
    displen = config[:display_length] || [config[:maxlen] || 999, _max].min
    maxlen = config[:maxlen] || _max
    field = Field.new form, :row => r, :col => c, :maxlen => maxlen, :default => default, :label => prompt,
      :display_length => displen
    bg = Ncurses.COLORS >= 236 ? 233 : :blue
    field.bgcolor = bg
    field.cursor_end if default.size > 0
    def field.default=(x); default(x);end

    # if user wishes to use the yield and say "field.history = [x,y,z] then
    # we should alredy have extended this, so lets make it permanent
    #if config[:history]
      #raise ArgumentError, "Field history must be an array" unless config[:history].is_a? Array
      require 'rbcurse/core/include/rhistory'
      field.extend(FieldHistory)
      #field.history_config :row => 
      field.history = config[:history]
    #end

    yield field if block_given?
    form.repaint
    win.wrefresh
    prevchar = 0
    entries = nil
    oldstr = nil # for tab completion, origal word entered by user
    while ((ch = win.getchar()) != 999)
      break if ch == 10 || ch == 13 || ch == KEY_ENTER
      #return -1, nil if ch == ?\C-c.getbyte(0) || ch == ?\C-g.getbyte(0)
      return nil if ch == ?\C-c.getbyte(0) || ch == ?\C-g.getbyte(0)
      #if ch == ?\M-h.getbyte(0) #                            HELP KEY
      #help_text = config[:help_text] || "No help provided"
      #color = $datacolor
      #print_help(win, r, c, color, help_text)
      ## this will come over our text
      #end
      # TODO tab completion and help_text print on F1
      # that field objects can extend, same for tab completion and gmail completion
      if ch == KEY_TAB
        if config
          str = field.text
          if prevchar == KEY_TAB
            if !entries.nil? && !entries.empty?
              str = entries.delete_at(0)
            else
              str = oldstr if oldstr
              prevchar = ch = nil # so it can start again completing
            end
          else
            tabc = config[:tab_completion] unless tabc
            next unless tabc
            oldstr = str.dup
            entries = tabc.call(str).dup
            $log.debug " tab got #{entries} for str=#{str}"
            str = entries.delete_at(0) unless entries.nil? || entries.empty?
            str = str.to_s.dup
          end
          if str
            field.text = str
            field.cursor_end
            field.set_form_col # shit why are we doign this, text sets curpos to 0
          end
          form.repaint
          win.wrefresh
        end

        # tab_completion
        # if previous char was not tab, execute tab_completion_proc and push first entry
        # else push the next entry
      elsif ch == KEY_F1
        help_text = config[:help_text] || "No help provided. C-c/C-g aborts. <TAB> completion. Alt-h history. C-a/e"
        print_status_message help_text, :wait => 7
      else
        form.handle_key ch
      end
      prevchar = ch
      win.wrefresh
    end
  rescue => err
    Ncurses.beep
    textdialog [err.to_s, *err.backtrace], :title => "Exception"
    $log.error "EXC in rbgetsr #{err} "
    $log.error(err.backtrace.join("\n")) 
  ensure
    win.destroy if win
  end
  config[:history] << field.text if config[:history] && field.text
  return field.text
end

#rbgetstr(nolongerused, r, c, prompt, maxlen, config = {}) ⇒ Object

routine to get a string at bottom of window. The first 3 params are no longer required since we create a window of our own. help_text is displayed on F1 tab_completion is a proc which helps to complete user input This method is now only for backward compatibility rbgetstr had various return codes based on whether user asked for help possibly mimicking alpine, or because i could do nothing about it. Now, rbgets handles that and only returns if the user cancels or enters a string, so rbgets does not need to return other codes.

Parameters:

  • prompt (String)
    • label to show

  • maxlen (Fixnum)
    • max length of input

  • config (Hash) (defaults to: {})
    • :default, :display_length of Field, :help_text, :tab_completion



35
36
37
38
39
40
41
42
43
# File 'lib/rbcurse/core/include/io.rb', line 35

def rbgetstr(nolongerused, r, c, prompt, maxlen, config={})
  config[:maxlen] = maxlen
  str = rb_gets(prompt, config)
  if str
    return 0, str
  else
    return -1, nil
  end
end

#warn(string) ⇒ Object

warn user: currently flashes and places error in log file experimental, may change interface later it does not say anything on screen

Parameters:

  • text (String)

    of error/warning to put in log

Since:

  • 1.1.5



259
260
261
262
# File 'lib/rbcurse/core/include/io.rb', line 259

def warn string
  $log.warn string
  Ncurses.beep
end