-with-ncurses
option on a unix-like system. Not all terminals support color output, so I did not include the use of colors in the article.ncurses_end()
not called, your terminal will not work properly.ncurses_init();
to switch to ncurses mode. Without this, when calling the functions of the PHP library will fall with an error. <?php // $ncurse = ncurses_init(); // $fullscreen = ncurses_newwin ( 0, 0, 0, 0); // ncurses_border(0,0, 0,0, 0,0, 0,0); // $small = ncurses_newwin(10, 30, 7, 25); // ncurses_wborder($small,0,0, 0,0, 0,0, 0,0); ncurses_refresh(); // // ncurses_mvwaddstr($small, 5, 5, " Test String "); // ncurses_wrefresh($small); $pressed = ncurses_getch(); // ncurses_end(); // ncurses,
$pressed = ncurses_getch();
while (true) { $pressed = ncurses_getch(); // if ($pressed == 27) { break; } else { ncurses_mvwaddstr($small, 5, 5, $pressed); ncurses_wrefresh($small); } }
ncurses_refresh();
: ncurses_attron(NCURSES_A_REVERSE); ncurses_mvaddstr(0,1,"My first ncurses application"); ncurses_attroff(NCURSES_A_REVERSE);
<?php define('ESCAPE_KEY', 27); $ncurse = ncurses_init(); $fullscreen = ncurses_newwin ( 0, 0, 0, 0); ncurses_border(0,0, 0,0, 0,0, 0,0); $small = ncurses_newwin(10, 30, 7, 25); ncurses_wborder($small,0,0, 0,0, 0,0, 0,0); ncurses_attron(NCURSES_A_REVERSE); ncurses_mvaddstr(0,1,"My first ncurses application"); ncurses_attroff(NCURSES_A_REVERSE); ncurses_refresh(); $currently_selected = 0; $menu = array('one', 'two', 'three', 'four'); while (true) { for($i=0; $i<count($menu); $i++){ $out = $menu[$i]; if($currently_selected == intval($i)){ ncurses_wattron($small,NCURSES_A_REVERSE); ncurses_mvwaddstr($small, 1+$i, 1, $out); ncurses_wattroff($small,NCURSES_A_REVERSE); } else { ncurses_mvwaddstr($small, 1+$i, 1, $out); } } ncurses_wrefresh($small); $pressed = ncurses_getch(); if ($pressed == NCURSES_KEY_UP) { $currently_selected--; if ($currently_selected < 0) $currently_selected = 0; } elseif ($pressed == NCURSES_KEY_DOWN) { $currently_selected++; if ($currently_selected >= count($menu)) $currently_selected = count($menu)-1; } elseif($pressed == ESCAPE_KEY) { break; } else { ncurses_mvwaddstr($small, 5, 5, $pressed); } } ncurses_end();
Function | Description | Documentation |
---|---|---|
ncurses_init | Initializes ncurses | www.php.net/manual/en/function.ncurses-init.php |
ncurses_newwin | Creates a new window | www.php.net/manual/en/function.ncurses-newwin.php |
ncurses_getmaxyx (resource window, int return Y, int return X); | Writes to variables X and Y the maximum size of the terminal | - |
ncurses_border | Draws a frame around the main window | www.php.net/manual/en/function.ncurses-border.php |
ncurses_refresh | Updates the main window. To redraw secondary windows use ncurses_wrefresh | www.php.net/manual/en/function.ncurses-refresh.php |
ncurses_attron | Applies an attribute to the displayed text. | www.php.net/manual/en/function.ncurses-attron.php |
ncurses_attroff | Disable attribute use | www.php.net/manual/en/function.ncurses-attroff.php |
ncurses_mvaddstr | Displays the string | www.php.net/manual/en/function.ncurses-mvaddstr.php |
ncurses_wborder (resource window, int left, int right, int top, int bottom, int tl_corner, int tr_corner, int bl_corner, int br_corner); | Draws a frame for the background window. | - |
ncurses_wattron (resource window, int attribute) | Same as ncurses_attron , only applies to window | - |
ncurses_mvwaddstr | Places the string in the secondary window | www.php.net/manual/en/function.ncurses-mvwaddstr.php |
ncurses_wattroff (resource window, int attribute) | Identical to ncurses_wattroff , only used for window | - |
ncurses_wrefresh | Redraws the background window. | www.php.net/manual/en/function.ncurses-wrefresh.php |
ncurses_getch | Waiting for input from the keyboard or mouse. | www.php.net/manual/en/function.ncurses-getch.php |
<?php // define("ESCAPE_KEY", 27); define("ENTER_KEY", 13); // $tr_return = traceroute("www.zend.com"); array_shift($tr_return); $ncurses_session = ncurses_init(); $main = ncurses_newwin(0, 0, 0, 0); // ncurses_getmaxyx($main, $lines, $columns); ncurses_border(0, 0, 0, 0, 0, 0, 0, 0); // ncurses_attron(NCURSES_A_REVERSE); ncurses_mvaddstr(0,1, "Traceroute example"); ncurses_attroff(NCURSES_A_REVERSE); // , ... $lower_frame_window = ncurses_newwin ($lines-14, $columns-3, 13, 1); ncurses_wborder($lower_frame_window, 0,0, 0,0, 0,0, 0,0); // $lower_main_window = ncurses_newwin ($lines - 16, $columns-5, 15, 2); $main_list_window = ncurses_newwin (12, $columns-3, 1, 1); ncurses_wborder($main_list_window, 0,0, 0,0, 0,0, 0,0); // ncurses_refresh(); $currently_selected = 0; while(true) { for($a=0; $a < count($tr_return); $a++){ $out = $tr_return[$a]; if($currently_selected == intval($a)) { ncurses_wattron($main_list_window,NCURSES_A_REVERSE); ncurses_mvwaddstr ($main_list_window, 1+$a, 1, $out); ncurses_wattroff($main_list_window,NCURSES_A_REVERSE); } else { ncurses_mvwaddstr ($main_list_window, 1+$a, 1, $out); } } ncurses_move(-1,1); // ncurses_wrefresh($lower_frame_window); ncurses_wrefresh($lower_main_window); // ncurses_wrefresh($main_list_window); // $y = ncurses_getch($lower_main_window); if ($y == ENTER_KEY) { $newout = explode(" ", trim($tr_return[$currently_selected])); $rwhois_return = rwhois($newout[2]); foreach ($rwhois_return as $n => $l) { ncurses_mvwaddstr($lower_main_window, $n - 1, 1, $l); } } elseif($y == ESCAPE_KEY) { ncurses_end(); exit; } elseif ($y == NCURSES_KEY_UP) { $currently_selected--; if ($currently_selected < 0) $currently_selected = 0; } elseif($y == NCURSES_KEY_DOWN) { $currently_selected++; if ($currently_selected >= count($tr_return)) $currently_selected = count($tr_return)-1; } } // function traceroute($address) { exec("traceroute -n -m 10 $address", $trreturn); return $trreturn; } // reverse whois function rwhois($query) { $fp = fsockopen ("rwhois.arin.net", 4321, $errno, $errstr, 30); if (!$fp) { $ret[] = "$errstr ($errno)\n"; } else { fputs($fp, "$query\r\n"); while (!feof($fp)) { $back = trim(fgets ($fp, 256)); if (empty($back) || stripos($back, ':') === false || substr($back, 0, 1) == '#') continue; $ret[] = $back; }//wend fclose ($fp); } return $ret; }
/** Creates an ncurses window that is write-safe on the left-hand side of the screen * @param integer $size is how wide it will be * @return window handle of inner window. */ function left_window($size=15){ global $fullscreen; ncurses_getmaxyx($fullscreen, $MAX_Y, $MAX_X); $c = ncurses_newwin ($MAX_Y-2 ,$size, 1, 1); ncurses_wborder($c,0,0, 0,0, 0,0, 0,0); // border it // now create window overtop the other just // slightly smaller so that we won't write over // the border. $d = ncurses_newwin ($MAX_Y-4 ,$size-2, 1+1, 2); ncurses_wrefresh($c); // show it ncurses_wrefresh($d); return $d; } # # creates an upper-right window # function upperr_window($size=15){ global $fullscreen; ncurses_getmaxyx($fullscreen, $MAX_Y, $MAX_X); $c = ncurses_newwin ($size ,$size, 1, $MAX_X-($size+1)); ncurses_wborder($c,0,0, 0,0, 0,0, 0,0); // border it ncurses_wrefresh($c); // show it return $c; }
man ncurses
. In addition, each function has its own help page (eg man wborder
pecl install ncurses
( pecl install ncurses
rights, therefore with sudo)Source: https://habr.com/ru/post/186570/
All Articles