Lots of fixes for 2.2, see README.txt for details
[PsN.git] / lib / ui_subs.pm
bloba32ee7bddce3161ce4494c7321ebd6819fb1b561
1 # Like the Debug class the user_interface (ui) class is a little bit
2 # special in that it should never be instanciated. An instance (named
3 # ui) is kept globally which can be accessed by the members of the
4 # ui class if they are called statically. Calling a member staticaly
5 # means that you adress them using the perl module name, for example:
7 # ui -> print( 'print this on the screen' );
9 # Notice that there is no $ in front of 'ui'. Here print is called
10 # "statically". In other words, it means "call a member without an
11 # instance".
14 # {{{ include
15 start include statements
16 use Carp;
17 use Carp qw(cluck);
18 use Text::Wrap;
20 # These variables are used for mapping level names to numbers.
22 my $the_instance = ui::new();
24 end include statements
25 # }}}
27 # {{{ new
28 start new
29 end new
30 # }}}
32 # {{{ category
34 start category
36 # Usage:
38 # ui -> category( 'bootstrap' )
40 # If you give a value to category as a single argument, you will
41 # set the global category of user interface messages.
43 # my $current_category = ui -> category
45 # If you don't give any argument the current category is returned.
47 if( defined($parm) ){
48 $the_instance -> {'category'} = $parm;
49 } else {
50 return $the_instance -> {'category'};
52 return;
54 end category
56 # }}}
58 # {{{ package
60 start package
62 # Usage:
64 # ui -> package( 'output' )
66 # If you give a value to package as a single argument, you will
67 # set the global package of ui messages.
69 # my $current_package = ui -> package
71 # If you don't give any argument the current package is returned.
73 if( defined($parm) ){
74 $the_instance -> {'package'} = $parm;
75 } else {
76 return $the_instance -> {'package'};
78 return;
80 end package
82 # }}}
84 # {{{ subroutine
86 start subroutine
88 # Usage:
90 # ui -> subroutine( 'output' )
92 # If you give a value to subroutine as a single argument, you will
93 # set the global subroutine of ui messages.
95 # my $current_subroutine = ui -> subroutine
97 # If you don't give any argument the current subroutine is returned.
99 if( defined($parm) ){
100 $the_instance -> {'subroutine'} = $parm;
101 } else {
102 return $the_instance -> {'subroutine'};
104 return;
106 end subroutine
108 # }}}
110 # {{{ print
111 start print
114 # Usage:
116 # ui -> print( category => 'bootstrap',
117 # message => 'This is a message' );
119 # ui::print will print out messages corresponding to the category specified as
120 # argument. ui::print will look at the category given and the
121 # global category to see whether anything should be printed.
124 # NOTICE the lack of "\n" at the end of the message, ui::print
125 # will append one "\n". In case there ever is a GUI for PsN
126 # ui::print could be used to create a message in the GUI. And
127 # in that case, an extra "\n" might be annoying.
128 return if( $the_instance -> {'silent'} );
129 if( $the_instance -> {'category'} eq $category or $category eq 'all' ){
130 my ( $package_junk, $filename, $line, $subroutine, $junk ) = caller(1);
131 my @names = split('::', $subroutine );
132 $subroutine = $names[$#names];
133 my $package = join( '::', @names[0..$#names-1] );
135 if ( ( not defined $the_instance -> {'package'} or
136 $the_instance -> {'package'} eq $package ) and
137 ( not defined $the_instance -> {'subroutine'} or
138 $the_instance -> {'subroutine'} eq $subroutine ) ) {
140 my $prefix;
141 my $text;
143 my $nl = $newline ? "\n" : '';
145 if ( $wrap ) {
146 $text = wrap('', '', $message . $nl );
147 } else {
148 $text = $message . $nl;
151 print STDERR ( $text );
155 end print
156 # }}}
158 # {{{ status_bar
159 start status_bar
161 if ( $goal != 0 ) {
162 my $part = int(($sofar/$goal)*$width);
163 $bar = "\r".'|'.'.' x $part . ' ' x ($width-$part) . '|';
166 end status_bar
167 # }}}
169 # {{{ silent
170 start silent
172 if( defined($parm) ){
173 $the_instance -> {'silent'} = $parm;
174 } else {
175 return $the_instance -> {'silent'};
177 return;
179 end silent
180 # }}}