File: demo_getstr

package info (click to toggle)
perlmenu 4.0-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 408 kB
  • ctags: 86
  • sloc: perl: 2,439; makefile: 33; sh: 17
file content (125 lines) | stat: -rwxr-xr-x 4,068 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
#!/usr/bin/perl
#**************************************************************************
# demo_getstr --  Simple perl menu_getstr call demo
#
# Notes:   Perl4 - Requires curseperl
#          Perl5 - Requires William Setzer's "Curses" extension
#
#          Demonstrates "menu_getstr" utility routine (useful in perl
#          curses programs).
#
# Author:  Steven L. Kunz
#          Networked Applications
#          Iowa State University Computation Center
#          Ames, IA  50011
#          Email: skunz@iastate.edu
#
# Date:    February 1997
#**************************************************************************

# Perl5+Curses ONLY!
# Comment these lines for use with Perl4/curseperl
BEGIN { $Curses::OldCurses = 1; }
use Curses;                     # PerlMenu needs "Curses"
use perlmenu;                   # Main menu package (Perl5 only)
require "./menuutil.pl";        # For "pause" and "print_nl" routines.

# Perl4/curseperl ONLY!
# Uncomment these lines for use with Perl4/curseperl
# (Did you remember to run "create_menu.pl"?)
#require "./menu.pl";           # Main menu package (Perl4 only)
#require "./menuutil.pl";       # For "pause" and "print_nl" routines.

$| = 1;				# Flush after every write to stdout

$menu_default_arrow = $menu_default_top = 0;

#
# NOTE:  This program uses a regular perl menu to activate menu_getstr.  If
#        you do not use a perl menu prior to calling menu_getstr the first
#        time you must still call menu_init (only) ONCE before calling
#        menu_getstr to initialize the curses environment.
#
while (1) {
  &menu_init(1,"PerlMenu 4.0 - menu_getstr Demonstration");
  &menu_item("(Exit this demo)","exit");
  &menu_item("Regular (long) strings","regular");
  &menu_item("Regular (long) strings with default","regdef");
  &menu_item("Regular (max length 8) strings","max8");
  &menu_item("Regular (max length 8) strings with default","max8def");
  &menu_item("Hidden  (max length 8) strings with default","max8inv");
  $sel = &menu_display("",$menu_default_arrow,$menu_default_top);
  if ($sel eq "exit") {
    &endwin();
    exit(0);
  }
  elsif ($sel eq "regular") {
    &do_getstr("",0,0);		# No default, no max length, non-hidden
  }
  elsif ($sel eq "regdef") {
    &do_getstr("Barney",0,0);	# No default, no max length, non-hidden
  }
  elsif ($sel eq "max8") {
    &do_getstr("",8,0);		# No default, max length 8, non-hidden
  }
  elsif ($sel eq "max8def") {
    &do_getstr("Barney",8,0);   # Default of "Barney", max length 8, non-hidden
  }
  elsif ($sel eq "max8inv") {
    &do_getstr("password",8,1);	# Default of "password", max length 8, hidden
  }
}

#**********
# DO_GETSTR
#
# Function: Call menu_getstr with various parameters
#
# Input:    - Default string (may be null)
#           - Maximum length (may be zero)
#           - No-show (hidden) flag (0=show, 1=noshow)
#
# Returns:  Nothing
#
#**********
sub do_getstr {
  local($default,$maxlen,$hide) = @_;
  local($title);

# Generate a title indicating what we are doing
  if ($default) { $title .= "Default=\"$default\" "; }
  else { $title .= "No-Default "; }
  if ($maxlen) { $title .= "Maxlen=$maxlen "; }
  else { $title .= "No-Maxlen "; }
  if ($hide) { $title .= "Hidden "; }
  else { $title .= "Visible "; }

# Put out a title and instructions.
  &clear();
  &move(0,25);
  &addstr($title);
  &move(2,5);
  &addstr("Enter strings, using left and right arrows to move insert/deletion");
  &move(3,5);
  &addstr("point after you have entered some characters.");
  &move(4,5);
  &addstr("A null entry exits.");

# Loop, getting/displaying strings until a null value is entered
  while (1) {
    if ($maxlen) {
      $string = &menu_getstr(10,5,"Enter value: ",0,$default,$maxlen,$hide);
    } else {
      $string = &menu_getstr(10,5,"Enter value: ",0,$default,0,$hide);
    }
    if ($string eq "") { # Exit if we get a null string
      &clear();
      &refresh();
      last;
    }
    &move(12,5); # Display last string entered
    &clrtobot();
    &addstr("Last string entered: $string");
    &refresh();
  }
}