#!/usr/local/bin/kermit
#
# MENU
#
# Skeleton of a simple menu program to be executed by C-Kermit 7.0
# or later: http://www.columbia.edu/kermit/
#
# User gets a menu with a numbered list of choices.  User presses a digit
# key to make a choice.  Choice is executed.  Illustrates use of the SCREEN
# command to format the screen and the GETC command to read a single
# character from the keyboard, as well as nested WHILE loops, SWITCH, etc.
#
# Author: F. da Cruz, Columbia University
# V 1.0, 20 Apr 1999
#
local \%c                                 ; Local variable

define MSG {
    if > \v(argc) 2 screen move \%2 \%3   ; Move to given row/col (if any)
    screen cleol                          ; Clear to end of line
    if def \%1 xecho \fcontents(\%1)      ; Print message (if any)
}
define BOT {                              ; Put cursor on screen bottom
    screen move \v(rows) 0
}
define CENTER {                           ; Print argument centered
    local \%x                             ; Variable local to this routine
    if not def \%2 def \%2 1              ; Default if no column argument
    .\%x ::= (\v(cols)-\flen(\%1))/2      ; Calculate centered position
    msg {\%1} {\%2} {\%x}                 ; Print arg string centered
}
define PAINT {                            ; Put the menu on the screen
    screen clear                          ; Clear the screen
    center {Welcome to This Menu} 2       ; Print title centered
    msg {Choices:} 4                      ; Print heading
    msg { 1. File} 6                      ; Print choice lines
    msg { 2. Edit} 7
    msg { 3. Exit} 8
}
define DOFILE {                           ; Dummy FILE routine
    msg {Filing... } 12                   ; Fill in as desired...
}
define DOEDIT {                           ; Dummy EDIT routine
    msg {Editing...} 12                   ; Fill in as desired...
}
define DOEXIT {                           ; Dummy EXIT routine
    msg {Exiting...} 12                   ; Fill in as desired...
}

def \%c 0                                 ; Menu choice variable
paint                                     ; Display the menu
while ( != \%c 3 ) {                      ; Read and verify choice
    while true {                          ; Loop till we get a good one
	screen move 10                    ; Move to line 10 (left margin)
	screen cleol                      ; Clear this line
	getc \%c {Your choice: }          ; Prompt and get and echo 1 char
	if ( < \fcode(\%c) 32 ) continue  ; Ignore control characters
	xecho \%c                         ; Echo the choice
	if ( not numeric \%c ) {          ; Make sure it's a number
            msg {Not numeric - "\%c"} 12
            continue
        }
	if ( >= \%c 1 && <= \%c 3 ) break ; Range check - break if OK.
	msg {Out of range - "\%c"} 12     ; Keep looping if not OK.
    }
    switch \%c {                          ; Valid choice - execute it.
      :1, dofile, break
      :2, doedit, break
      :3, doexit, break
    }
}
bot                                       ; Leave cursor at screen bottom.
exit 0 Bye                                ; And exit.
