# Module: Tgio::Input

# Public Class Methods

### inputform (length)

Displays a colored input bar of specified length and returns a value. This may sound simple, but in order to provide more precise input control, the input is read character by character into an array. The array is a set of numbers which represent the character code. Before returning the value, it is converted to a string. (replaces ansigets)

Show source...

         # File bbs/lib/tgio/input.rb, line 190
    190:     def Input::inputform(length)
    191:       trap("INT") do
    192:         # ignore "CTRL-C"
    193:       end
    194:       tr = JLine::ConsoleReader.new
    195:       char = nil
    196:       enterkey = false
    197:       input = []
    198:       cursize = 0
    199:       print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" "*length)
    200:       print "\b" * length
    201:       # initialize
    202:       # Start loop until enter key is pressed
    203:       until enterkey == true
    204:         #print ANSI_RESET
    205:         char = tr.readVirtualKey
    206:         # handle [ENTER] key
    207:         if char == 10
    208:           enterkey = true
    209:         else
    210:           # handle [Backspace] key
    211:           if char == 8
    212:             unless cursize == 0
    213:               print "\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b"
    214:               input.pop                        #remove last type value
    215:               cursize -=1 unless cursize <= 0  #decrement cursize counter
    216:             end
    217:           # handle everything else
    218:           else
    219:             if cursize < length
    220:               print ""+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+char.chr
    221:               input.push(char)                  #add new value
    222:               cursize += 1                      #increment cursize counter
    223:             else
    224:               # if the size of the current input is > length, then drop the char
    225:               print "\b \b"
    226:               char = nil
    227:             end
    228:           end
    229:         end
    230:         print ANSI_RESET
    231:       end
    232:       # convert input array of character codes back into string
    233:       print ANSI_ON_BLUE+(" "*input.size)+ANSI_RESET+"\n"
    234:       value = ""
    235:       input.each do |c|
    236:         value.concat(c)
    237:       end
    238:       return value
    239:     end

### inputgender (template=nil)

Displays a colored input bar of length 1 and returns a boolean based on a yes/no question. Only valid characters are M & F.

Show source...

         # File bbs/lib/tgio/input.rb, line 398
    398: def Input::inputgender(template=nil)
    399:   # ignore "CTRL-C"
    400:   trap("INT") do
    401:   end
    402:   unless template.nil? == true
    403:     t = Tgtemplate::Template.new(template)
    404:     t.render()
    405:   end
    406:   # initialize
    407:   char = nil        # Ensure char is set to nil
    408:   done = false      # Initialize the condition
    409:   input = []        # Initialize the array of input chars
    410:   cursize = 0       # Start the counter
    411:   maxlength = 1     # Define the max length of the input box.
    412:   print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" "*(maxlength))
    413:   print "\b" * (maxlength)
    414:   # Start loop until enter key is pressed
    415:   until done == true
    416:     print ANSI_RESET
    417:     char = JLine::ConsoleReader.new.readVirtualKey
    418:     unless char == 8
    419:       case char.chr.upcase
    420:         when "M"
    421:           value = "M"
    422:           done = true
    423:         when "F"
    424:           value = "F"
    425:           done = true
    426:         else
    427:           #print "\b" * (maxlength)
    428:           #print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b"*(maxlength))
    429:       end #/case
    430:     end #/unless
    431:   end #/until
    432:   print ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+char.chr.upcase+ANSI_RESET+"\n"
    433:   return value
    434: end

### inputkey ()

Displays a colored input bar of length 2 and returns a value. Works similar to inputform, except does not require the [ENTER] key. Once user enters a valid value, returns immediately. If the / key is entered as the first value, a second entry wil be permitted but will return on the 2nd keypress.

Show source...

         # File bbs/lib/tgio/input.rb, line 63
     63:     def Input::inputkey
     64:       # ignore "CTRL-C"
     65:       trap("INT") do
     66:       end
     67:       # initialize
     68:       char = nil        # Ensure char is set to nil
     69:       done = false      # Initialize the condition
     70:       input = []        # Initialize the array of input chars
     71:       cursize = 0       # Start the counter
     72:       maxlength = 2     # Define the max length of the input box.
     73:       print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" "*(maxlength-1))
     74:       print "\b" * (maxlength-1)
     75:       # Start loop until enter key is pressed
     76:       until done == true
     77:         print ANSI_RESET
     78:         #char = STDIN.getc   # the Ruby Way (not so great)
     79:         # Java JLine way
     80:         char = JLine::ConsoleReader.new.readVirtualKey
     81:         # handle [ENTER] key
     82:         # If enter is pressed, then no value is returned.
     83:         # The calling function will get an empty string or nil value.
     84:         if char == 10 # [enter]
     85:           input = [] # since nothing happens, turn input to an empty string
     86:           #done = true
     87:         elsif char == 127 # [backspace]
     88:           #print "\b\b"+ANSI_BRIGHT_CYAN+ANSI_ON_BLUE+"   \b\b\b\b \b"
     89:           print "\b\b"+ANSI_RESET+"  \b\b\b"
     90:           print "\b\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b"
     91:           #input.pop                        #remove last type value
     92:           #cursize -=1 unless cursize <= 0  #decrement cursize counter
     93:         # handle everything else
     94:         elsif char == 32 # [space]
     95:           print "\b\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b\b"
     96:         elsif char == 92 || char == 27# [backslash] '\' or [esc]
     97:           char = nil
     98:         else
     99:           if cursize < maxlength
    100:             if char == 47 # the '/' key
    101:               print ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+char.chr.upcase
    102:               cursize +=1
    103:             else
    104:               print ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+char.chr.upcase
    105:               cursize +=2
    106:               done = true
    107:             end
    108:             input.push(char)                  #add new value
    109:           else
    110:             # if the size of the current input is > length, then drop the char
    111:             print "\b \b"
    112:             char = nil
    113:           end
    114:         end
    115:         print ANSI_RESET
    116:       end #/until
    117:       # convert input array of character codes back into string
    118:       value = ""
    119:       input.each do |c|
    120:         value.concat(c)
    121:       end
    122:        return value
    123:     end

### inputyn (template=nil)

Displays a colored input bar of length 1 and returns a boolean based on a yes/no question. Only valid characters are Y & N.

Show source...

         # File bbs/lib/tgio/input.rb, line 356
    356: def Input::inputyn(template=nil)
    357:   # ignore "CTRL-C"
    358:   trap("INT") do
    359:   end
    360:
    361:   unless template.nil? == true
    362:     t = Tgtemplate::Template.new(template)
    363:     t.render()
    364:   end
    365:
    366:   # initialize
    367:   char = nil        # Ensure char is set to nil
    368:   done = false      # Initialize the condition
    369:   input = []        # Initialize the array of input chars
    370:   cursize = 0       # Start the counter
    371:   maxlength = 1     # Define the max length of the input box.
    372:   print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" "*(maxlength))
    373:   print "\b" * (maxlength)
    374:   # Start loop until enter key is pressed
    375:   until done == true
    376:     print ANSI_RESET
    377:     char = JLine::ConsoleReader.new.readVirtualKey
    378:     unless char == 8
    379:       case char.chr.upcase
    380:         when "Y"
    381:           value = true
    382:           done = true
    383:         when "N"
    384:           value = false
    385:           done = true
    386:         else
    387:           #print "\b" * (maxlength)
    388:           #print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b"*(maxlength))
    389:       end #/case
    390:     end #/unless
    391:   end #/until
    392:   print ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+char.chr.upcase+ANSI_RESET+"\n"
    393:   return value
    394: end

### loginform ()

Login Prompt

Show source...

         # File bbs/lib/tgio/input.rb, line 243
    243:     def Input::loginform
    244:       trap("INT") do
    245:         # ignore "CTRL-C"
    246:       end
    247:       tr = JLine::ConsoleReader.new
    248:       char = nil
    249:       enterkey = false
    250:       input = []
    251:       cursize = 0
    252:       length = 16
    253:       print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" "*length)
    254:       print "\b" * length
    255:       # initialize
    256:       # Start loop until enter key is pressed
    257:       until enterkey == true
    258:         #print ANSI_RESET
    259:         char = tr.readVirtualKey
    260:         # handle [ENTER] key
    261:         if char == 10
    262:           enterkey = true
    263:         else
    264:           # handle [Backspace] key
    265:           if char == 8
    266:             unless cursize == 0
    267:             print "\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b"
    268:             input.pop                        #remove last type value
    269:             cursize -=1 unless cursize <= 0  #decrement cursize counter
    270:             end
    271:           # handle everything else
    272:           else
    273:             if cursize < length
    274:               print ""+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+char.chr.upcase
    275:               input.push(char)                  #add new value
    276:               cursize += 1                      #increment cursize counter
    277:             else
    278:               # if the size of the current input is > length, then drop the char
    279:               print "\b \b"
    280:               char = nil
    281:             end
    282:           end
    283:         end
    284:         print ANSI_RESET
    285:       end
    286:       # convert input array of character codes back into string
    287:       value = ""
    288:       input.each do |c|
    289:         value.concat(c)
    290:       end
    291:       print ANSI_RESET+"\n"
    292:       return value.upcase
    293:     end

### menuprompt (template, validkeys, menudata=nil)

Menu Prompt: Recieve input restricted to a list of validkeys

Show source...

         # File bbs/lib/tgio/input.rb, line 438
    438: def Input::menuprompt(template, validkeys, menudata=nil)
    439:   trap("INT") do
    440:   end
    441:   # Append globalkeys to supplied validkeys array. This is for DRY within each menu prompt.
    442:   globalkeys = ['/?', '/G', '/H','/M','/P', '/T', '/W']
    443:   validkeys = validkeys.concat(globalkeys)
    444:   # Initialize loop condition
    445:   done = false
    446:   until done == true
    447:     # Display menu template only if user preference is True
    448:     if $session.pref_show_menus == true
    449:       Tgtemplate::display(template, menudata)
    450:     end
    451:
    452:     # Display the prompt
    453:     Tgtemplate::display('menu_prompt.ftl')
    454:     selection = self.inputkey.upcase
    455:     # Check if user supplied selection is included in validkeys array
    456:     if validkeys.include?(selection)
    457:       # Check for Global Input Values with prepended slash
    458:        case selection
    459:          when "/?" # Show the shortcut / commands menu
    460:            Tgtemplate::display('menu_shortcuts.ftl')
    461:          when "/G" # Fast Goodybye
    462:            Telegard::goodbye_fast
    463:            return 0
    464:          when "/H" # Help Menu - probably dont want this here
    465:            Telegard.unimplemented
    466:          when "/M" # Toggle $session.prefs_show_menus
    467:            $session.pref_show_menus = $session.pref_show_menus.toggle
    468:            $session.save
    469:            Tgtemplate::display('toggle_show_menus.ftl', {"togglevalue" => $session.pref_show_menus.to_s})
    470:          when "/P" # Toggle $session.prefs_term_pager
    471:            $session.pref_term_pager = $session.pref_term_pager.toggle
    472:            $session.save
    473:            Tgtemplate::display('toggle_term_pager.ftl', {"togglevalue" => $session.pref_term_pager.toggle.to_s})
    474:          when "/T" # Show time remaining
    475:            Tgtemplate::display('time_remain.ftl', {
    476:                    'timeremain'=> $session.timeremain.to_s,
    477:                    'curtime' => Time.now.to_s,
    478:                    'expires' => $session.expires.to_s
    479:            })
    480:          when "/W"
    481:            puts "Who's online"
    482:          when "/X"
    483:            puts "Fast Return to Main"
    484:        end
    485:         done = true
    486:     else
    487:       Tgtemplate::display('menu_invalidkey.ftl')
    488:     end
    489:   end
    490:   return selection
    491: end

### pagerkey_default ()

Same as ‘inputkey’ method but for defaultpager

Show source...

         # File bbs/lib/tgio/input.rb, line 126
    126:     def Input::pagerkey_default
    127:       # ignore "CTRL-C"
    128:       trap("INT") do
    129:       end
    130:       # initialize
    131:       char = nil        # Ensure char is set to nil
    132:       done = false      # Initialize the condition
    133:       input = []        # Initialize the array of input chars
    134:       cursize = 0       # Start the counter
    135:       maxlength = 1     # Define the max length of the input box.
    136:       print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" "*(maxlength))
    137:       print "\b" * (maxlength)
    138:       # Start loop until enter key is pressed
    139:       until done == true
    140:         print ANSI_RESET
    141:         #char = STDIN.getc   # the Ruby Way (not so great)
    142:         # Java JLine way
    143:         char = JLine::ConsoleReader.new.readVirtualKey
    144:         # handle [ENTER] key
    145:         # If enter is pressed, then no value is returned.
    146:         # The calling function will get an empty string or nil value.
    147:         if char == 10 # [enter]
    148:           input = [] # since nothing happens, turn input to an empty string
    149:           done = true
    150:           return 'Y'
    151:         elsif char == 127 # [backspace]
    152:           #print "\b\b"+ANSI_BRIGHT_CYAN+ANSI_ON_BLUE+"   \b\b\b\b \b"
    153:           print "\b\b"+ANSI_RESET+"  \b\b\b"
    154:           print "\b\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b"
    155:           #input.pop                        #remove last type value
    156:           #cursize -=1 unless cursize <= 0  #decrement cursize counter
    157:         # handle everything else
    158:         elsif char == 32 # [space]
    159:           print "\b\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b\b"
    160:         elsif char == 92 || char == 27# [backslash] '\' or [esc]
    161:           char = nil
    162:         else
    163:           if cursize < maxlength
    164:             print "\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+char.chr
    165:             input.push(char)                  #add new value
    166:               cursize +=2
    167:               done = true
    168:
    169:           else
    170:             # if the size of the current input is > length, then drop the char
    171:             print "\b \b"
    172:             char = nil
    173:           end
    174:         end
    175:         print ANSI_RESET
    176:       end #/until
    177:       # convert input array of character codes back into string
    178:       value = ""
    179:       input.each do |c|
    180:         value.concat(c)
    181:       end
    182:        return value
    183:     end

### passwordform ()

Password Prompt character reader. Replaces input with % for output.

Show source...

         # File bbs/lib/tgio/input.rb, line 297
    297:     def Input::passwordform
    298:       trap("INT") do
    299:         # ignore "CTRL-C"
    300:       end
    301:       tr = JLine::ConsoleReader.new
    302:       char = nil
    303:       enterkey = false
    304:       input = []
    305:       cursize = 0
    306:       length = 25
    307:       print ANSI_RESET+(ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" "*length)
    308:       print "\b" * length
    309:       # initialize
    310:       # Start loop until enter key is pressed
    311:       until enterkey == true
    312:         #print ANSI_RESET
    313:         char = tr.readVirtualKey
    314:         # handle [ENTER] key
    315:         if char == 10
    316:           enterkey = true
    317:         else
    318:           # handle [Backspace] key
    319:           if char == 8
    320:             # TODO: bugfix - minor visual issue where additional 3 spaces
    321:             # are added to end of a line when backspace occurs after a certain
    322:             # point during the inputform.
    323:             unless cursize == 0
    324:             print "\b"+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+" \b"
    325:             input.pop                        #remove last type value
    326:             cursize -=1 unless cursize <= 0  #decrement cursize counter
    327:             end
    328:           # handle everything else
    329:           else
    330:             if cursize < length
    331:               print ""+ANSI_BRIGHT_WHITE+ANSI_ON_BLUE+"%"
    332:               input.push(char)                  #add new value
    333:               cursize += 1                      #increment cursize counter
    334:             else
    335:               # if the size of the current input is > length, then drop the char
    336:               print "\b \b"
    337:               char = nil
    338:             end
    339:           end
    340:         end
    341:         print ANSI_RESET
    342:       end
    343:       # convert input array of character codes back into string
    344:       value = ""
    345:       input.each do |c|
    346:         value.concat(c)
    347:       end
    348:       print "\n"
    349:       return value
    350:     end