Module: Canis::Io

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

Instance Method Summary collapse

Instance Method Details

create a 2 line window at bottom to accept user input



26
27
28
# File 'lib/canis/core/include/io.rb', line 26

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

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



256
257
258
# File 'lib/canis/core/include/io.rb', line 256

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

See Also:



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/canis/core/include/io.rb', line 243

def get_file prompt, config={}  #:nodoc:
  maxlen = 70
  tabc = Proc.new {|str| Dir.glob(str +"*") }
  config[:tab_completion] ||= tabc
  config[:maxlen] ||= maxlen
  #config[:default] = "test"
  #ret, str = rb_getstr(nil,0,0, prompt, maxlen, config)
  # 2014-04-25 - 12:42 removed call to deprecated method
  str = rb_gets(prompt, config)
  #$log.debug " get_file returned #{ret} , #{str} "
  str ||= ""
  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:



270
271
272
273
274
275
276
277
278
# File 'lib/canis/core/include/io.rb', line 270

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



188
189
190
191
192
193
194
195
196
197
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
# File 'lib/canis/core/include/io.rb', line 188

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 rb_getstr #{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, :width of Field, :help_text, :tab_completion

Yields:

  • (Field)

    for overriding or customization

Returns:

  • (String, nil)

    String if entered, nil if canceled



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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/canis/core/include/io.rb', line 39

def rb_gets(prompt, config={}) # yield field
  if config.is_a? Hash
    # okay
    #
  elsif config.is_a? Array
    # an array is supplied, and tabbing helps complete from that
    # array.
    options = config
    completion_proc = Proc.new{|str| 
      options.dup.grep Regexp.new("^#{str}");
    }
    config = {}
    config[:tab_completion] = completion_proc
  elsif config == Pathname
    # we need to return a pathname TODO and prepend dirname
    # allow user to specify startdir else take current
    completion_proc = Proc.new {|str| Dir.glob(str +"*").collect { |f| File.directory?(f) ? f+"/" : f  } }
    help_text = "Enter start of filename and tab to get completion"
    config = {}
    config[:tab_completion] = completion_proc
    config[:help_text] = help_text
  elsif config == Integer
    config = {}
    config[:datatype] = 1.class
    config[:type] = :integer
  elsif config == Float
    config = {}
    v = 0.11
    config[:datatype] = v.class
    config[:type] = :float
  elsif config == :phone

    config = {}
  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[:width] || [config[:maxlen] || 999, _max].min
    maxlen = config[:maxlen] || _max
    field = LabeledField.new form, :row => r, :col => c, :maxlen => maxlen, :default => default, :label => prompt,
      :width => displen
    field.datatype(config[:datatype]) if config[:datatype]
    field.type(config[:type]) if config[:type]
    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
    require 'canis/core/include/rhistory'
    field.extend(FieldHistory)
    field.history = config[:history]

    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)
      if ch == 10 || ch == 13 || ch == KEY_ENTER
        begin
          # validate in case ranges or other validation given
          field.on_leave
          break
        rescue FieldValidationException => err # added 2011-10-2 v1.3.1 so we can rollback
          #alert err.to_s
          # unable to use alert now, since closing alert repaints root window and clears the
          # window this was on (command window in numbered menu).
          print_this(win, err.to_s, $errorcolor, 1, 0)
          form.setpos
        rescue => err
          Ncurses.beep
          break
        end
      end
      #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
      # 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
          # tab_completion
          # if previous char was not tab, execute tab_completion_proc and push first entry
          # else push the next entry
          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

      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 rb_getstr #{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

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

Deprecated.

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 NOTE : This method is now only for **backward compatibility** rb_getstr had various return codes based on whether user asked for help possibly mimicking alpine, or because i could do nothing about it. Now, rb_getstr handles that and only returns if the user cancels or enters a string, so rb_getstr does not need to return other codes.

Parameters:

  • prompt (String)
    • label to show

  • maxlen (Fixnum)
    • max length of input

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



307
308
309
310
311
312
313
314
315
# File 'lib/canis/core/include/io.rb', line 307

def rb_getstr(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



287
288
289
290
# File 'lib/canis/core/include/io.rb', line 287

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