Added some small boring scripts and programs writen in few last years
[mirrors/Programs.git] / perl / curses / curses.pl
CommitLineData
21c4e167
H
1#!/usr/bin/env perl
2#pacman -S perl-curses-ui
3
4use utf8;
5
6use strict;
7use warnings;
8use Curses::UI;
9
10#use I18N::Collate;
11#setlocale('LC_COLLATE', 'cs_CZ.utf8');
12#use encoding;
13
14#binmode(STDOUT, ":crlf");
15#binmode(STDIN, ":crlf");
16
17#binmode(STDOUT, ":encoding(utf8)");
18#binmode(STDIN, ":encoding(utf8)");
19
20
21
22
23my $cui = new Curses::UI( -color_support => 1, -compat => 0 );
24
25#Create a menu ^
26
27 my @menu = (
28 { -label => 'File',
29 -submenu => [
30 { -label => 'Exit ^Q', -value => \&exit_dialog }
31 ]
32 },
33 );
34
35#In order to describe the structure of a menu Curses::UI uses a rather ugly construct out of hash and arrayrefs. See Curses::UI::Menubar for details. What you do at this point is to create a Menubar with just one entry and one submenu. The entry is 'File' and the submenu is 'Exit'. The value of this menu item is a reference to a sub called exit_dialog.
36#Dialogs ^
37
38 sub exit_dialog()
39 {
40 my $return = $cui->dialog(
41 -message => "Do you really want to quit?",
42 -title => "Are you sure???",
43 -buttons => ['yes', 'no'],
44
45 );
46
47 exit(0) if $return;
48 }
49
50#The dialog method of Curses::UI gives us an easy and convenient way to create dialogs on the main screen. A dialog is a way to interact with the user in order to ask him a question or give him important information. This dialog is a more complex one, which asks the question whether or not you really want to exit. As the button for "yes" would return us a true value, you can easily exit on this return value.
51#Add the Menubar ^
52
53 my $menu = $cui->add(
54 'menu','Menubar',
55 -menu => \@menu,
56 -fg => "blue",
57 );
58
59#To finally add the Menubar to our root object, you have to call the add method on the Curses UI object. You specify the internal name of the widget as the first argument, the widget type as the second argument (like Label, TextViewer, etc.) and the menu structure you created at the beginning as an array reference as third object. Because you want the Menubar to have a blue theme, you give him the -fg option "blue". There are a couple of colors you can use, see Curses::UI::Color for details.
60#Add a window ^
61
62 my $win1 = $cui->add(
63 'win1', 'Window',
64 -border => 1,
65 -y => 1,
66 -bfg => 'red',
67 );
68
69#There are only two types of object you can add to the Curses::UI root object: Menubars and Windows. All other widgets have to be inserted into a window. Of course you can add a Menubar to a window, but not vice versa ;-). The add method always has the same two first arguments: the internal name and the widget type. The internal name can be used to find an object. The method getobj takes this name and returns us the corresponding object out of the hierarchy. See Curses::UI for details. Again you want some fancy colors, so you tell the window to have a border, leave some space for the Menubar (-y => 1) and set the border foreground color to red.
70#Add a widget ^
71
72 my $texteditor = $win1->add("text", "TextEditor",
73 -text => "Here is some text ěščřžýáíé\n"
74 . "And some more");
75
76#The next step is to add a useful widget to our new small Curses::UI app. Here you take a TextEditor widget which performs basic tasks as a text editor. You add some initial text to the widget to make it not seem that empty.
77#Making keybindings ^
78
79 $cui->set_binding(sub {$menu->focus()}, "\cX");
80 $cui->set_binding( \&exit_dialog , "\cQ");
81
82#You want to be able to focus the Menubar if you finished editing in the TextEditor widget. Therefore you set a binding to the focus function of the menu and the key sequence Control (specified by \c) combined with X. Now you can easily return to the menu after editing. Because it is easier to have a shortcut for closing the application you add a binding for the sequence Control-Q to our nice exit_dialog method.
83#The final steps ^
84
85 $texteditor->focus();
86 $cui->mainloop();
This page took 0.930226 seconds and 4 git commands to generate.