Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
wncurses.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * WHAT ncurses utilities functions
9  *
10  * @author Anakeen 2004
11  * @version $Id: wncurses.php,v 1.3 2005/04/18 12:53:39 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  */
15 /**
16  */
17 
18 define("NCURSES_KEY_CR", 13);
19 global $fullscreen;
20 
21 function ncurses_exit()
22 {
23  ncurses_clear();
24  ncurses_attron(NCURSES_A_BOLD);
25  ncurses_color_set(1);
26  ncurses_mvaddstr(0, 1, _("terminal too small (must ne at least 80 columns by 24 lines)"));
27  ncurses_attroff(NCURSES_A_BOLD);
28  ncurses_refresh();
29  $pressed = ncurses_getch(); // wait for a user keypress
30  ncurses_end(); // clean up our screen
31  exit(1);
32 }
33 
34 function ncurses_select($tr, $msg = "Select", $iselect = 0, $pairsel = 0)
35 {
36  global $fullscreen;
37  ncurses_getmaxyx($fullscreen, $lines, $columns);
38  $wsel = ncurses_newwin($lines - 9, $columns - 4, 7, 2);
39  ncurses_wborder($wsel, 0, 0, 0, 0, 0, 0, 0, 0);
40  ncurses_wcolor_set($wsel, 3);
41  foreach ($tr as $k => $v) {
42  if ($k == $iselect) {
43  ncurses_wcolor_set($wsel, ($pairsel == 0) ? 1 : $pairsel);
44  ncurses_wattron($wsel, NCURSES_A_REVERSE);
45  } else {
46  ncurses_wcolor_set($wsel, 1);
47  }
48  ncurses_mvwaddstr($wsel, $k + 1, 4, ($k + 1) . ") $v");
49  if ($k == $iselect) {
50  ncurses_wattroff($wsel, NCURSES_A_REVERSE);
51  } else {;
52  }
53  }
54  ncurses_wrefresh($wsel);
55 
56  if ($pairsel == 0) {
57  ncurses_mvaddstr($lines - 2, 4, $msg);
58  //ncurses_refresh();
59  $pressed = ncurses_getch(); // wait for a user keypress
60  $maxsel = count($tr) - 1;
61  while ($pressed != NCURSES_KEY_CR) {
62  switch ($pressed) {
63  case NCURSES_KEY_UP:
64  if ($iselect > 0) $iselect--;
65  break;
66 
67  case NCURSES_KEY_DOWN:
68  if ($iselect < $maxsel) $iselect++;
69  break;
70  }
71  ncurses_getyx($wsel, $y, $x);
72  ncurses_select($tr, $msg, $iselect, 1);
73  $pressed = ncurses_getch();
74  }
75  ncurses_select($tr, $msg, $iselect, 4);
76  }
77  return $iselect;
78 }
79 
81 {
82  $small = ncurses_newwin(10, 50, 7, 25);
83  if (!$small) {
84  ncurses_exit();
85  }
86  ncurses_wborder($small, 0, 0, 0, 0, 0, 0, 0, 0);
87  ncurses_wcolor_set($small, 1);
88  ncurses_mvwaddstr($small, 4, 5, $ret);
89  ncurses_wattron($small, NCURSES_A_BOLD);
90  ncurses_mvwaddstr($small, 5, 5, $err);
91  ncurses_wattroff($small, NCURSES_A_BOLD);
92  // show our handiwork and refresh our small window
93  ncurses_wrefresh($small);
94  $pressed = ncurses_getch(); // wait for a user keypress
95  ncurses_end(); // clean up our screen
96  exit;
97 }
98 
99 function ncurses_getln()
100 {
101  global $fullscreen;
102  $s = "";
103  $pressed = ncurses_getch();
104  while ($pressed != NCURSES_KEY_CR) {
105  switch ($pressed) {
106  case NCURSES_KEY_BACKSPACE:
107  if ($s != "") {
108  $s = substr($s, 0, -1);
109  ncurses_delch();
110  }
111  break;
112 
113  default:
114  $s.= chr($pressed);
115  break;
116  }
117  $pressed = ncurses_getch();
118  }
119  return $s;
120 }
121 
123 {
124  global $fullscreen;
125  // we begin by initializing ncurses
126  $ncurse = ncurses_init();
127  // let ncurses know we wish to use the whole screen
128  $fullscreen = ncurses_newwin(0, 0, 0, 0);
129  if (!$fullscreen) {
130  ncurses_exit();
131  }
132  ncurses_getmaxyx($fullscreen, $lines, $columns);
133  // draw a border around the whole thing.
134  ncurses_border(0, 0, 0, 0, 0, 0, 0, 0);
135  if (ncurses_has_colors()) {
136  ncurses_start_color();
137  ncurses_init_pair(1, NCURSES_COLOR_RED, NCURSES_COLOR_BLACK);
138  ncurses_init_pair(2, NCURSES_COLOR_BLUE, NCURSES_COLOR_BLACK);
139  ncurses_init_pair(3, NCURSES_COLOR_YELLOW, NCURSES_COLOR_BLACK);
140  ncurses_init_pair(4, NCURSES_COLOR_GREEN, NCURSES_COLOR_BLACK);
141  ncurses_init_pair(5, NCURSES_COLOR_MAGENTA, NCURSES_COLOR_BLACK);
142  ncurses_init_pair(6, NCURSES_COLOR_CYAN, NCURSES_COLOR_BLACK);
143  ncurses_init_pair(7, NCURSES_COLOR_WHITE, NCURSES_COLOR_BLACK);
144  }
145  ncurses_attron(NCURSES_A_BOLD);
146  ncurses_mvaddstr(0, 1, $title);
147  ncurses_attroff(NCURSES_A_BOLD);
148  ncurses_color_set(6);
149 
150  ncurses_refresh();
151 
152  return $fullscreen;
153 }
154 
155 function ncurses_getchw($chars)
156 {
157  $pressed = ncurses_getch(); // wait for a user keypress
158  $cpress = strtoupper(chr($pressed));
159  if (strpos($chars, $cpress) === false) {
160  ncurses_beep();
161  $cpress = ncurses_getchw($chars);
162  }
163  return $cpress;
164 }
165 function ncurses_execute(&$actions)
166 {
167  global $fullscreen;
168  ncurses_getmaxyx($fullscreen, $lines, $columns);
169 
170  $wact = ncurses_newwin($lines - 9, $columns - 4, 7, 2);
171  ncurses_wborder($wact, 0, 0, 0, 0, 0, 0, 0, 0);
172  ncurses_wcolor_set($wact, 3);
173 
174  ncurses_list($actions, 0, $wact);
175  ncurses_mvaddstr($lines - 2, 4, "Execute (Y|N|I) ? ");
176  //ncurses_refresh();
177  $cpress = ncurses_getchw("YNI"); // wait for a user keypress
178  if (($cpress != "Y") && ($cpress != "I")) exit;
179  $slice = $lines - 14;
180 
181  ncurses_list($actions, 0, $wact, true);
182 
183  $format = "%2d)%-" . ($columns - 9) . "s";
184  foreach ($actions as $k => $v) {
185  $ki = $k + 1;
186  if ($k > $slice) {
187  // need scroll
188  $j = 1;
189  for ($i = $k - $slice; $i < $k; $i++) {
190 
191  ncurses_wcolor_set($wact, $colors[$i]);
192  ncurses_wattron($wact, NCURSES_A_BOLD);
193  ncurses_mvwaddstr($wact, $j++, 1, sprintf($format, ($i + 1) , substr($actions[$i], 0, $columns - 9)));
194  ncurses_wattroff($wact, NCURSES_A_BOLD);
195  }
196  $ki = $slice + 1;
197  }
198  ncurses_wattron($wact, NCURSES_A_BLINK);
199  ncurses_wcolor_set($wact, 5); // OK
200  ncurses_mvwaddstr($wact, $ki, 1, sprintf($format, ($k + 1) , substr($v, 0, $columns - 9)));
201  ncurses_wattroff($wact, NCURSES_A_BLINK);
202  ncurses_wrefresh($wact);
203  if (($cpress == "I") && ($v[0] != "#")) {
204  ncurses_mvaddstr($lines - 2, 4, "(S)kip, (E)xecute, (A)bord, (C)ontinue ?");
205  $ccont = ncurses_getchw("SEAC"); // wait for a user keypress
206  if ($ccont == "C") $cpress = "Y";
207  elseif ($ccont == "A") {
208  ncurses_end();
209  exit;
210  }
211  }
212 
213  if (($cpress != "I") || ($ccont == "E")) {
214  ncurses_mvaddstr($lines - 2, 4, "Executing " . ($k + 1) . "..." . str_repeat(" ", 40));
215  ncurses_refresh($wact);
216  if ($v[0] != '#') {
217  $err = system("echo `date` \"" . addslashes($v) . "\" >>" . getTmpDir() . "/whatchk.log");
218  $err = system("(" . $v . ")" . " >>" . getTmpDir() . "/whatchk.log 2>&1", $ret);
219  }
220  // $err=system ("(".str_replace(array("(",")"),array("\(","\)"),$v).")"." >>".getTmpDir()."/whatchk.log 2>&1");
221  if ($ret != 0) {
222  if ($v[0] == '#') $colors[$k] = 5;
223  else $colors[$k] = 1;
224  ncurses_wcolor_set($wact, $colors[$k]); // KO
225 
226  } else {
227  if ($v[0] == '#') $colors[$k] = 5;
228  else $colors[$k] = 4;
229  ncurses_wcolor_set($wact, $colors[$k]); // OK
230 
231  }
232  } else {
233  if ($v[0] == '#') $colors[$k] = 5;
234  else $colors[$k] = 6;
235  ncurses_wcolor_set($wact, $colors[$k]); // Skip
236 
237  }
238  ncurses_wattron($wact, NCURSES_A_BOLD);
239  ncurses_mvwaddstr($wact, $ki, 1, sprintf($format, ($k + 1) , substr($v, 0, $columns - 9)));
240  ncurses_wattroff($wact, NCURSES_A_BOLD);
241  ncurses_wrefresh($wact);
242  }
243 }
244 
245 function ncurses_list(&$actions, $start = 0, $wlist = "", $nogetch = false)
246 {
247  global $fullscreen;
248  ncurses_getmaxyx($fullscreen, $lines, $columns);
249  $slice = $lines - 14;
250  if ($wlist == "") $wlist = ncurses_newwin($lines - 9, $columns - 4, 7, 2);
251  ncurses_wborder($wlist, 0, 0, 0, 0, 0, 0, 0, 0);
252  ncurses_wcolor_set($wlist, 3);
253 
254  $maxsel = count($actions);
255  $fini = false;
256 
257  if ($maxsel > $slice) {
258  $next = true; // need scroll
259 
260  }
261  $format = "%2d)%-" . ($columns - 9) . "s";
262  while (!$fini) {
263  $end = $start + $slice;
264  if ($end > $maxsel) $end = $maxsel;
265  $fini = true;
266 
267  for ($k = $start; $k < $end; $k++) {
268  ncurses_mvwaddstr($wlist, $k + 1 - $start, 1, sprintf($format, ($k + 1) , substr($actions[$k], 0, $columns - 9)));
269  }
270  if ($end < $maxsel) ncurses_mvwaddstr($wlist, $end - $start + 1, 4, " next...");
271  else ncurses_mvwaddstr($wlist, $end - $start + 1, 4, " ");
272 
273  ncurses_wrefresh($wlist);
274  ncurses_mvaddstr($lines - 2, 4, "Press Return Key to continue" . str_repeat(" ", 40));
275  if (!$nogetch) {
276  $pressed = ncurses_getch(); // wait for a user keypress
277  if ($pressed != NCURSES_KEY_CR) {
278  $fini = false;
279  if ($next) {
280  $fini = false;
281  switch ($pressed) {
282  case NCURSES_KEY_UP:
283  if ($start > 0) $start--;
284  break;
285 
286  case NCURSES_KEY_DOWN:
287  if ($start < ($maxsel - $slice)) $start++;
288  break;
289 
290  case NCURSES_KEY_NPAGE:
291  if ($start < $maxsel) $start+= $slice - 1;
292  if ($start >= ($maxsel - $slice)) $start = $maxsel - $slice;
293  break;
294 
295  case NCURSES_KEY_PPAGE:
296  if ($start < $maxsel) $start-= $slice - 1;
297  if ($start < 0) $start = 0;
298  break;
299 
300  case NCURSES_KEY_CR:
301  $fini = true;;
302 
303  break;
304  }
305  } else {
306 
307  ncurses_mvaddstr($lines - 2, 4, "Press Return Key" . str_repeat(" ", 40));
308  }
309  }
310  }
311  }
312 
313  ncurses_wrefresh($wlist);
314  }
315 ?>
← centre documentaire © anakeen - published under CC License - Dynacase