sup

A curses threads-with-tags style email client

ncursesw-ruby.git

git clone https://supmua.dev/git/ncursesw-ruby/

examples/form2.rb (4914B) - raw

      1 #!/usr/bin/ruby
      2 # Copyright (c) 2004 by Simon Kaczor <skaczor@cox.net>
      3 # Simple example of a form in action, based on the NCURSES Programming HOWTO:
      4 # http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/
      5 #
      6 # All standard field types are created in the form.
      7 # Additionnally a custom field is created to illustrate
      8 # custom field validation using Ruby Proc objects, as shown in the example.
      9 #
     10 # The original example contained the following copyright:
     11 # Copyright (c) 2001 by Pradeep Padala. This document may be distributed
     12 # under the terms set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
     13 
     14 require 'ncursesw'
     15 
     16 include Ncurses
     17 include Ncurses::Form
     18 
     19 def print_in_middle(win, starty, startx, width, string, color)
     20 
     21   if(win == nil)
     22     win = stdscr;
     23   end
     24   x = Array.new
     25   y = Array.new
     26   Ncurses.getyx(win, y, x);
     27   if(startx != 0)
     28     x[0] = startx;
     29   end
     30   if(starty != 0)
     31     y[0] = starty;
     32   end
     33   if(width == 0)
     34     width = 80;
     35   end
     36   length = string.length;
     37   temp = (width - length)/ 2;
     38   x[0] = startx + temp.floor;
     39   win.attron(color);
     40   win.mvprintw(y[0], x[0], "%s", string);
     41   win.attroff(color);
     42   Ncurses.refresh();
     43 end
     44 
     45 fields = Array.new
     46 
     47 states = {"MI" => "Michigan",
     48           "VA" => "Virginia",
     49           "VE" => "Vermont"}
     50 fieldcheck = proc { |afield|
     51   val = afield.field_buffer(0)
     52   val.strip!
     53   if (states[val] != nil)
     54     afield.set_field_buffer(0,states[val])
     55     return true
     56   else
     57     return false
     58   end
     59 }
     60 charcheck = proc { |ch|
     61   if (('A'..'Z').include?(ch))
     62     return true
     63   else
     64     return false
     65   end
     66 }
     67 
     68 # Initialize curses
     69 begin
     70   stdscr = Ncurses.initscr();
     71   Ncurses.start_color();
     72   Ncurses.cbreak();
     73   Ncurses.noecho();
     74   Ncurses.keypad(stdscr, true);
     75 
     76   # Initialize few color pairs
     77   Ncurses.init_pair(1, COLOR_RED, COLOR_BLACK);
     78   Ncurses.init_pair(2, COLOR_BLACK, COLOR_WHITE);
     79   Ncurses.init_pair(3, COLOR_BLACK, COLOR_BLUE);
     80   stdscr.bkgd(Ncurses.COLOR_PAIR(2));
     81 
     82   # Initialize the fields
     83   (1..9).each { |i|
     84     field = FIELD.new(1, 10, i*2, 1, 0, 0)
     85     field.set_field_back(A_UNDERLINE)
     86     fields.push(field)
     87   }
     88 
     89   customtype = FIELDTYPE.new(fieldcheck, charcheck);
     90 
     91   fields[1].set_field_type(TYPE_ALNUM, 0);
     92   fields[2].set_field_type(TYPE_ALPHA, 0);
     93   fields[3].set_field_type(TYPE_INTEGER, 0, 0, 1000);
     94   fields[4].set_field_type(TYPE_NUMERIC, 2, 0, 1000);
     95   fields[5].set_field_type(TYPE_ENUM, ["one","two","three"], false, false);
     96   fields[6].set_field_type(TYPE_REGEXP, "^ *[0-9]* *$");
     97   fields[7].set_field_type(TYPE_IPV4);
     98   fields[8].set_field_type(customtype);
     99 
    100 
    101   # Create the form and post it
    102   my_form = FORM.new(fields);
    103 
    104   my_form.user_object = "My identifier"
    105 
    106   # Calculate the area required for the form
    107   rows = Array.new()
    108   cols = Array.new()
    109   my_form.scale_form(rows, cols);
    110 
    111   # Create the window to be associated with the form
    112   my_form_win = WINDOW.new(rows[0] + 3, cols[0] + 14, 1, 1);
    113   my_form_win.bkgd(Ncurses.COLOR_PAIR(3));
    114   my_form_win.keypad(TRUE);
    115 
    116   # Set main window and sub window
    117   my_form.set_form_win(my_form_win);
    118   my_form.set_form_sub(my_form_win.derwin(rows[0], cols[0], 2, 12));
    119 
    120   # Print a border around the main window and print a title */
    121   my_form_win.box(0, 0);
    122   print_in_middle(my_form_win, 1, 0, cols[0] + 14, "My Form", Ncurses.COLOR_PAIR(1));
    123 
    124   my_form.post_form();
    125 
    126   # Print field types
    127   my_form_win.mvaddstr(4, 2, "No Type")
    128   my_form_win.mvaddstr(6, 2, "Alphanum")
    129   my_form_win.mvaddstr(8, 2, "Alpha")
    130   my_form_win.mvaddstr(10, 2, "Integer")
    131   my_form_win.mvaddstr(12, 2, "Numeric")
    132   my_form_win.mvaddstr(14, 2, "Enum")
    133   my_form_win.mvaddstr(16, 2, "Regexp")
    134   my_form_win.mvaddstr(18, 2, "IP")
    135   my_form_win.mvaddstr(20, 2, "Custom")
    136 
    137   my_form_win.wrefresh();
    138 
    139   stdscr.mvprintw(Ncurses.LINES - 2, 28, "Use UP, DOWN arrow keys to switch between fields");
    140   stdscr.mvprintw(Ncurses.LINES - 1, 28, "Press F1 to quit");
    141   stdscr.refresh();
    142 
    143   # Loop through to get user requests
    144   while((ch = my_form_win.getch()) != KEY_F1)
    145     case ch
    146     when KEY_DOWN
    147       # Go to next field */
    148       my_form.form_driver(REQ_VALIDATION);
    149       my_form.form_driver(REQ_NEXT_FIELD);
    150       # Go to the end of the present buffer
    151       # Leaves nicely at the last character
    152       my_form.form_driver(REQ_END_LINE);
    153 
    154     when KEY_UP
    155       # Go to previous field
    156       my_form.form_driver(REQ_VALIDATION);
    157       my_form.form_driver(REQ_PREV_FIELD);
    158       my_form.form_driver(REQ_END_LINE);
    159 
    160     when KEY_LEFT
    161       # Go to previous field
    162       my_form.form_driver(REQ_PREV_CHAR);
    163 
    164     when KEY_RIGHT
    165       # Go to previous field
    166       my_form.form_driver(REQ_NEXT_CHAR);
    167 
    168     when KEY_BACKSPACE
    169       my_form.form_driver(REQ_DEL_PREV);
    170     else
    171       # If this is a normal character, it gets Printed
    172       my_form.form_driver(ch);
    173     end
    174   end
    175   # Un post form and free the memory
    176   my_form.unpost_form();
    177   my_form.free_form();
    178   fields.each {|f| f.free_field()}
    179 
    180 
    181 ensure
    182   Ncurses.endwin();
    183 end
    184