Beispiel #1
0
function ncurses_show_screen($showHelp)
{
    // basic settings
    ncurses_noecho();
    ncurses_curs_set(0);
    // set app title
    $title = "Red Ventures Selenium Runner";
    // commands to be listed in help
    $commands = array('q' => 'quit', 'r' => 'run selected tests', 'space' => 'toggle test selection', 'enter' => 'run highlighted tests', 'up' => 'move up', 'down' => 'move down', 'pgUp' => 'scroll description up', 'pgDown' => 'scroll description down', '?/h' => 'show this help panel');
    // create a fullscreen window
    $fullscreen = ncurses_newwin(0, 0, 0, 0);
    ncurses_getmaxyx($fullscreen, $max_y, $max_x);
    // enter the main event loop
    $do_loop = 1;
    while ($do_loop) {
        // calculate width of help window columns
        $c = $t = 0;
        foreach ($commands as $cmd => $txt) {
            $c = strlen($cmd) > $c ? strlen($cmd) : $c;
            $t = strlen($txt) > $t ? strlen($txt) : $t;
        }
        $h = count($commands);
        // calculate the help windows height
        if ($showHelp) {
            if (!empty($helpWin)) {
                ncurses_delwin($helpWin);
            }
            $helpWin = ncurses_newwin($h + 4, $c + $t + 5, ($max_y - $h - 4) / 2, ($max_x - $c - $t - 5) / 2);
            ncurses_wborder($helpWin, 0, 0, 0, 0, 0, 0, 0, 0);
            $i = 0;
            foreach ($commands as $cmd => $txt) {
                ncurses_mvwaddstr($helpWin, 2 + $i, 2, $cmd);
                ncurses_mvwaddstr($helpWin, 2 + $i, 2 + $c + 1, $txt);
            }
            if (!empty($helpWin)) {
                ncurses_wrefresh($helpWin);
            } else {
                ncurses_refresh();
            }
        }
        if (empty($helpWin)) {
            $key = ncurses_getch();
        } else {
            $key = ncurses_wgetch($helpWin);
        }
        switch ($key) {
            case 27:
                ncurses_flushinp();
                $do_loop = 0;
                break;
            default:
                $showHelp = $showHelp === true ? false : true;
                ncurses_show_screen($showHelp);
        }
    }
}
Beispiel #2
0
 /**
  * Processing press keys
  */
 public function processing()
 {
     $do = true;
     do {
         $k = ncurses_wgetch($this->getWindow());
         if ($k == Ncurses::XCURSES_KEY_ESC) {
             $do = false;
         } elseif ($k == Ncurses::XCURSES_KEY_LF) {
             $do = false;
         }
         $this->refresh();
     } while ($do);
 }
Beispiel #3
0
<?php

/**
 * nCurses Keys
 *
 * Show nCurses key code.
 *
 * @author Kenny Parnell <*****@*****.**>
 * @date Tue 05 Aug 2008 09:08:07 PM EDT
 */
define('ESCAPE_KEY', 27);
ncurses_init();
$fullscreen = ncurses_newwin(0, 0, 0, 0);
ncurses_border(0, 0, 0, 0, 0, 0, 0, 0);
ncurses_getmaxyx($fullscreen, $y, $x);
$small = ncurses_newwin(5, 7, ($y - 5) / 2, ($x - 7) / 2);
ncurses_wborder($small, 0, 0, 0, 0, 0, 0, 0, 0);
ncurses_refresh();
ncurses_mvwaddstr($small, 5, 2, "12");
ncurses_wrefresh($small);
do {
    $k = ncurses_wgetch($small);
    if ($k == ESCAPE_KEY) {
        ncurses_end();
        exit;
    } else {
        echo $k;
    }
} while (1);
/* vim:set ft=php ts=4 sw=4 et */
Beispiel #4
0
function dialog($params)
{
    ###############################################################################################
    if (empty($params) || !is_array($params)) {
        trigger_error('params must be non-empty array');
        return NULL;
    }
    $message = isset($params['message']) ? $params['message'] : '';
    $buttons = !empty($params['buttons']) && is_array($params['buttons']) ? $params['buttons'] : array('OK');
    $n_buttons = count($buttons);
    for ($i = 0; $i < $n_buttons; $i++) {
        $buttons[$i] = ' ' . $buttons[$i] . ' ';
    }
    $parent_rows = isset($params['rows']) && $params['rows'] > 0 ? (int) $params['rows'] : 25;
    $parent_cols = isset($params['cols']) && $params['cols'] > 0 ? (int) $params['cols'] : 80;
    if (empty($message) || empty($buttons) || $parent_rows <= 0 || $parent_cols <= 0) {
        trigger_error('wrong params');
        return NULL;
    }
    $message_lines = split("\n", $message);
    $message_width = 0;
    $n_message_lines = count($message_lines);
    for ($i = 0; $i < $n_message_lines; $i++) {
        $message_width = max(strlen($message_lines[$i]), $message_width);
    }
    $buttons_delim = '  ';
    $buttons_delim_len = strlen($buttons_delim);
    $buttons_len = strlen(implode($buttons_delim, $buttons));
    $width = 4 + max($buttons_len + 2 * $buttons_delim_len, $message_width);
    $height = 4 + $n_message_lines;
    $dlg_y = $parent_rows > $height ? $parent_rows - $height >> 1 : 1;
    $dlg_x = $parent_cols > $width ? $parent_cols - $width >> 1 : 1;
    $window = ncurses_newwin($height, $width, $dlg_y, $dlg_x);
    if (empty($window)) {
        trigger_error('unable to create window');
        return NULL;
    }
    ncurses_wborder($window, 0, 0, 0, 0, 0, 0, 0, 0);
    $i_x = 0;
    $i_y = 0;
    for ($i = 0; $i < $n_message_lines; $i++) {
        $i_y = 1 + $i;
        $i_x = 1 + ($width - 2 - strlen($message_lines[$i]) >> 1);
        ncurses_mvwaddstr($window, $i_y, $i_x, rtrim($message_lines[$i]));
    }
    $buttons_data = array();
    $buttons_shift_x = 1 + ($width - 1 - $buttons_len >> 1);
    $buttons_shift_y = 2 + $n_message_lines;
    $i_title = '';
    $i_x = $buttons_shift_x;
    for ($i = 0; $i < $n_buttons; $i++) {
        $i_title = $buttons[$i];
        $buttons_data[] = array('x' => $i_x, 's' => $i_title);
        if (0 == $i) {
            ncurses_wattron($window, NCURSES_A_REVERSE);
        }
        ncurses_mvwaddstr($window, $buttons_shift_y, $i_x, $i_title);
        if (0 == $i) {
            ncurses_wattroff($window, NCURSES_A_REVERSE);
        }
        $i_x += strlen($i_title) + $buttons_delim_len;
    }
    ncurses_wrefresh($window);
    ncurses_keypad($window, TRUE);
    ncurses_curs_set(0);
    ncurses_noecho();
    $result = -1;
    $do_loop = 1;
    $move = 0;
    $current = 0;
    while ($do_loop) {
        $key = ncurses_wgetch($window);
        $move = 0;
        switch ($key) {
            case NCURSES_KEY_LEFT:
                if ($current > 0) {
                    $move = -1;
                }
                break;
            case NCURSES_KEY_RIGHT:
                if ($current < $n_buttons - 1) {
                    $move = 1;
                }
                break;
            case XCURSES_KEY_LF:
            case XCURSES_KEY_CR:
                $result = $current;
                $do_loop = 0;
                break;
            case XCURSES_KEY_ESC:
                $do_loop = 0;
                break;
        }
        if (0 == $do_loop) {
            ncurses_flushinp();
        } elseif ($move) {
            ncurses_mvwaddstr($window, $buttons_shift_y, $buttons_data[$current]['x'], $buttons_data[$current]['s']);
            $current += $move;
            ncurses_wattron($window, NCURSES_A_REVERSE);
            ncurses_mvwaddstr($window, $buttons_shift_y, $buttons_data[$current]['x'], $buttons_data[$current]['s']);
            ncurses_wattroff($window, NCURSES_A_REVERSE);
            ncurses_wrefresh($window);
        }
    }
    ncurses_delwin($window);
    return $result;
}
 /**
  * Renders the dump
  */
 public function render()
 {
     // initialize ncurses window
     ncurses_init();
     ncurses_curs_set(0);
     ncurses_noecho();
     ncurses_cbreak();
     ncurses_start_color();
     ncurses_use_default_colors();
     ncurses_init_pair(self::COLOR_RED, NCURSES_COLOR_RED, -1);
     ncurses_init_pair(self::COLOR_GREEN, NCURSES_COLOR_GREEN, -1);
     ncurses_init_pair(self::COLOR_YELLOW, NCURSES_COLOR_YELLOW, -1);
     ncurses_init_pair(self::COLOR_BLUE, NCURSES_COLOR_BLUE, -1);
     ncurses_init_pair(self::COLOR_MAGENTA, NCURSES_COLOR_MAGENTA, -1);
     ncurses_init_pair(self::COLOR_CYAN, NCURSES_COLOR_CYAN, -1);
     ncurses_init_pair(self::COLOR_WHITE, NCURSES_COLOR_WHITE, -1);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_DEFAULT, 0, NCURSES_COLOR_WHITE);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_RED, NCURSES_COLOR_RED, NCURSES_COLOR_WHITE);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_GREEN, NCURSES_COLOR_GREEN, NCURSES_COLOR_WHITE);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_YELLOW, NCURSES_COLOR_YELLOW, NCURSES_COLOR_WHITE);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_BLUE, NCURSES_COLOR_BLUE, NCURSES_COLOR_WHITE);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_MAGENTA, NCURSES_COLOR_MAGENTA, NCURSES_COLOR_WHITE);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_CYAN, NCURSES_COLOR_CYAN, NCURSES_COLOR_WHITE);
     ncurses_init_pair(self::COLOR_HIGHLIGHTED_WHITE, NCURSES_COLOR_WHITE, NCURSES_COLOR_BLACK);
     // create a dummy pad which will receive user inputs
     $this->windowDummy = ncurses_newpad(1, 1);
     ncurses_keypad($this->windowDummy, true);
     // run interface
     $continue = true;
     $this->currentRenderer = $this->rendererVarDump;
     $this->createTitleWindow();
     $this->createFooterWindow();
     $this->refreshTitle();
     $this->refreshFooter();
     while ($continue) {
         $this->currentRenderer->refresh();
         $key = ncurses_wgetch($this->windowDummy);
         $continue = $this->onKeypress($key);
     }
     ncurses_end();
 }
Beispiel #6
0
 function GetUserInput()
 {
     static $bAlt = false;
     while ($c = ncurses_wgetch($this->userinputw)) {
         if ($c == -1) {
             break;
         }
         $this->torc->output->Output(BUFFER_CURRENT, "got " . $c);
         /*
          * We do this in a seperate switch so we can unset meta combinations if they don't fit our demands easily.
          */
         if ($bAlt) {
             $bAlt = false;
             switch (chr($c)) {
                 case 'a':
                     $this->SwitchToFirstActiveBuffer();
                     return;
                     break;
             }
         }
         switch ($c) {
             case 27:
                 // Alt modifier.
                 $bAlt = true;
                 break;
             case 13:
                 $usrp = $this->userinputt;
                 $this->sendhis[] = $usrp;
                 $this->sendhispt = count($this->sendhis) - 1;
                 $this->userinputt = '';
                 $this->setuserinput();
                 $this->sendhislu = true;
                 return $usrp;
                 break;
             case NCURSES_KEY_BACKSPACE:
                 $this->userinputt = substr($this->userinputt, 0, strlen($this->userinputt) - 1);
                 $this->setuserinput();
                 break;
             case NCURSES_KEY_NPAGE:
                 $this->aBuffers[$this->iCurrentBuffer]->ScrollUp();
                 $this->DrawBuffer($this->iCurrentBuffer);
                 break;
             case NCURSES_KEY_PPAGE:
                 $this->aBuffers[$this->iCurrentBuffer]->ScrollDown();
                 $this->DrawBuffer($this->iCurrentBuffer);
                 break;
             case NCURSES_KEY_UP:
                 if ($this->sendhispt >= 0) {
                     if (!$this->sendhislu) {
                         $this->sendhispt--;
                         $this->sendhislu = true;
                     }
                     $this->userinputt = $this->sendhis[$this->sendhispt];
                     $this->sendhispt--;
                     $this->setuserinput();
                 }
                 break;
             case NCURSES_KEY_DOWN:
                 if ($this->sendhispt + 1 < count($this->sendhis) - 1) {
                     if ($this->sendhislu) {
                         $this->sendhispt++;
                         $this->sendhislu = false;
                     }
                     $this->sendhispt++;
                     $this->userinputt = $this->sendhis[$this->sendhispt];
                     $this->setuserinput();
                 }
                 break;
             default:
                 $this->userinputt .= chr($c);
                 $this->setuserinput();
                 break;
         }
     }
     // And let caller know that nothing came out of the input buffer.
     return false;
 }
Beispiel #7
0
function menu_select($params)
{
    ###################################################################################################
    if (!is_array($params) || empty($params)) {
        trigger_error('wrong params');
        return NULL;
    }
    $menu = isset($params['items']) ? $params['items'] : NULL;
    $rows = isset($params['rows']) ? (int) $params['rows'] : 0;
    $cols = isset($params['cols']) ? (int) $params['cols'] : 0;
    $selected = isset($params['selected']) ? (int) $params['selected'] : 0;
    $centered = empty($params['centered']) ? 0 : 1;
    $y_menu = isset($params['y']) ? (int) $params['y'] : 0;
    $x_menu = isset($params['x']) ? (int) $params['x'] : 0;
    if (!is_array($menu) || empty($menu) || $rows <= 0 || $cols <= 0 || $y_menu < 0 || $x_menu < 0) {
        trigger_error('wrong params');
        return NULL;
    }
    $keys = array_keys($menu);
    $values = array();
    $current = 0;
    $width = 0;
    $height = count($menu) + 2;
    foreach ($menu as $value) {
        $width = max($width, strlen($value));
    }
    $i = 0;
    foreach ($menu as $k => $v) {
        $values[$i] = ' ' . $v . str_repeat(' ', 1 + $width - strlen($v));
        if ($k == $selected) {
            $current = $i;
        }
        $i++;
    }
    $width += 4;
    if ($centered) {
        $y_menu = $rows - $height >> 1;
        $x_menu = $cols - $width >> 1;
    }
    $window = ncurses_newwin($height, $width, $y_menu, $x_menu);
    if (empty($window)) {
        trigger_error('unable to create window');
        return NULL;
    }
    ncurses_wborder($window, 0, 0, 0, 0, 0, 0, 0, 0);
    for ($a = 0; $a < count($values); $a++) {
        if ($a == $current) {
            ncurses_wattron($window, NCURSES_A_REVERSE);
        }
        ncurses_mvwaddstr($window, 1 + $a, 1, $values[$a]);
        if ($a == $current) {
            ncurses_wattroff($window, NCURSES_A_REVERSE);
        }
    }
    ncurses_wrefresh($window);
    ncurses_keypad($window, TRUE);
    ncurses_curs_set(0);
    do {
        $key = ncurses_wgetch($window);
        $move = 0;
        switch ($key) {
            case NCURSES_KEY_UP:
                if ($current > 0) {
                    $move = -1;
                }
                break;
            case NCURSES_KEY_DOWN:
                if ($current < count($values) - 1) {
                    $move = 1;
                }
                break;
            case XCURSES_KEY_LF:
            case XCURSES_KEY_CR:
                $result = $keys[$current];
                break;
            case XCURSES_KEY_ESC:
                ncurses_flushinp();
                $result = '';
                break;
        }
        if ($move) {
            ncurses_mvwaddstr($window, 1 + $current, 1, $values[$current]);
            $current += $move;
            ncurses_wattron($window, NCURSES_A_REVERSE);
            ncurses_mvwaddstr($window, 1 + $current, 1, $values[$current]);
            ncurses_wattroff($window, NCURSES_A_REVERSE);
            ncurses_wrefresh($window);
        }
    } while (!isset($result));
    ncurses_delwin($window);
    return $result;
}
Beispiel #8
0
function dlg_input($params = array())
{
    ###############################################################################################
    $title = isset($params['title']) ? $params['title'] : NULL;
    $max_length = isset($params['max_len']) ? (int) $params['max_len'] : 10;
    $dlg_rows = isset($params['dlg_cols']) ? (int) $params['dlg_cols'] : 3;
    $dlg_cols = isset($params['dlg_cols']) ? (int) $params['dlg_cols'] : 40;
    $parent_cols = isset($params['cols']) ? (int) $params['cols'] : NULL;
    $parent_rows = isset($params['rows']) ? (int) $params['rows'] : NULL;
    $dlg_x = (int) (($parent_cols - $dlg_cols) / 2);
    if ($dlg_x < 0) {
        $dlg_x = 0;
    }
    $dlg_y = (int) (($parent_rows - $dlg_rows) / 2);
    if ($dlg_y < 0) {
        $dlg_y = 0;
    }
    if ($max_length <= 0 || $dlg_rows <= 0 || $dlg_cols <= 0) {
        trigger_error('wrong params');
        return NULL;
    }
    $dlg_window = ncurses_newwin($dlg_rows, $dlg_cols, $dlg_y, $dlg_x);
    if (empty($dlg_window)) {
        return NULL;
    }
    ncurses_wborder($dlg_window, 0, 0, 0, 0, 0, 0, 0, 0);
    if ($title) {
        ncurses_wattron($dlg_window, NCURSES_A_REVERSE);
        ncurses_mvwaddstr($dlg_window, 0, 2, ' ' . $title . ' ');
        ncurses_wattroff($dlg_window, NCURSES_A_REVERSE);
    }
    ncurses_curs_set(1);
    ncurses_wmove($dlg_window, 2, 2);
    ncurses_wrefresh($dlg_window);
    $do_getch = 1;
    $input_val = '';
    $input_char = '';
    $input_len = 0;
    $cursor_x = 2;
    $cursor_y = 1;
    ncurses_wmove($dlg_window, $cursor_y, $cursor_x);
    ncurses_noecho();
    ncurses_keypad($dlg_window, TRUE);
    while ($do_getch) {
        $key_code = ncurses_wgetch($dlg_window);
        if ($key_code == XCURSES_KEY_CR || $key_code == XCURSES_KEY_LF) {
            $do_getch = 0;
        } elseif ($key_code == NCURSES_KEY_BACKSPACE) {
            if ($input_len > 0) {
                $input_len--;
                $input_val = substr($input_val, 0, $input_len);
                $cursor_x--;
                ncurses_mvwaddstr($dlg_window, $cursor_y, $cursor_x, ' ');
                ncurses_wmove($dlg_window, $cursor_y, $cursor_x);
            }
        } elseif ($key_code < XCURSES_KEY_PRINTABLE_MIN || $key_code > XCURSES_KEY_PRINTABLE_MAX) {
            continue;
        } elseif ($input_len < $max_length) {
            $input_val .= $input_char = chr($key_code);
            $input_len++;
            $cursor_x++;
            ncurses_waddstr($dlg_window, $input_char);
        }
    }
    ncurses_delwin($dlg_window);
    return $input_val;
}
 /**
  * {@inheritdoc}
  */
 public function onKeyPress($keyCode)
 {
     switch ($keyCode) {
         // F5
         case NCURSES_KEY_F5:
             // collapse all
             $this->var->collapse(true);
             $this->gotoPositionY(0);
             break;
             // F6
         // F6
         case NCURSES_KEY_F6:
             // expand all visible elements
             foreach ($this->expandableList as $expandable) {
                 $expandable->expand();
             }
             break;
             // F7
         // F7
         case NCURSES_KEY_F7:
             // expand all elements
             $this->expandAll($this->var);
             break;
             // F8
         // F8
         case NCURSES_KEY_F8:
             // toggle cursor highlight
             $this->cursorHighlight = !$this->cursorHighlight;
             break;
             // F9
         // F9
         case NCURSES_KEY_F9:
             // search text
             $this->showSearchPad = true;
             $this->editSearchPad = true;
             $rawSearchText = $this->searchText;
             $previousSearchText = "";
             $this->refreshSearchPad();
             while (true) {
                 $searchKeyCode = ncurses_wgetch($this->pad);
                 $input = chr($searchKeyCode);
                 if (27 === $searchKeyCode || 13 === $searchKeyCode && "" === $this->searchText) {
                     // end search if pressed key is ESC
                     // end also if pressed key is ENTER and search text is empty
                     $this->showSearchPad = false;
                     $this->editSearchPad = false;
                     $this->searchText = "";
                     $this->searchFoundOccurences = 0;
                     $this->searchFoundList = array();
                     $this->var->clearSearch();
                     break;
                 } elseif (13 === $searchKeyCode) {
                     // end input if pressed key is ENTER
                     $this->editSearchPad = false;
                     $this->searchFoundList = $this->var->searchText($this->searchText);
                     $this->searchFoundOccurences = count($this->searchFoundList);
                     break;
                 } elseif (NCURSES_KEY_BACKSPACE === $searchKeyCode) {
                     // delete last character
                     $rawSearchText = $this->cleanString($rawSearchText);
                     $rawSearchText = mb_substr($rawSearchText, 0, -1, mb_detect_encoding($rawSearchText));
                     $this->searchText = $rawSearchText;
                     $previousSearchText = $this->searchText;
                     $this->refreshSearchPad();
                 } else {
                     // strip non printable characters (such as arrow keys, ...)
                     $rawSearchText .= $input;
                     $this->searchText = $this->cleanString($rawSearchText);
                     // refresh only if the new text is different from the previous
                     if (strlen($previousSearchText) < strlen($this->searchText)) {
                         $this->refreshSearchPad();
                     }
                     $previousSearchText = $this->searchText;
                 }
             }
             break;
             // "n" / "N" key
         // "n" / "N" key
         case 110:
         case 78:
             // got to next search occurence
             if ($this->showSearchPad && !$this->editSearchPad) {
                 // if no match, do nothing
                 if (empty($this->searchFoundList)) {
                     break;
                 }
                 // iterate through all found occurences
                 // find the first whose Y position is higher than cursor Y position, or the first hidden element
                 foreach ($this->searchFoundList as $var) {
                     $varPosY = $var->getLastPosY();
                     if (null === $varPosY || $varPosY > $this->cursorPositionY) {
                         // expand + expand parents
                         // go to its new Y pos
                         $this->disablePrint = true;
                         $var->expand(true);
                         $this->refresh();
                         $this->disablePrint = false;
                         $this->gotoPositionY($var->getLastPosY());
                         break 2;
                     }
                 }
                 // if none found, go to the first occurence
                 reset($this->searchFoundList);
                 $var = current($this->searchFoundList);
                 $this->disablePrint = true;
                 $var->expand(true);
                 $this->refresh();
                 $this->disablePrint = false;
                 $this->gotoPositionY($var->getLastPosY());
             }
             break;
             // "p" / "P" key
         // "p" / "P" key
         case 112:
         case 80:
             // got to previous search occurence
             if ($this->showSearchPad && !$this->editSearchPad) {
                 // if no match, do nothing
                 if (empty($this->searchFoundList)) {
                     break;
                 }
                 // iterate through all found occurences
                 // find the first whose Y position is lower than cursor Y position, or the first hidden element
                 $searchFoundList = array_reverse($this->searchFoundList);
                 foreach ($searchFoundList as $var) {
                     $varPosY = $var->getLastPosY();
                     if (null === $varPosY || $varPosY < $this->cursorPositionY) {
                         // expand + expand parents
                         // go to its new Y pos
                         $this->disablePrint = true;
                         $var->expand(true);
                         $this->refresh();
                         $this->disablePrint = false;
                         $this->gotoPositionY($var->getLastPosY());
                         break 2;
                     }
                 }
                 // if none found, go to the last occurence
                 end($this->searchFoundList);
                 $var = current($this->searchFoundList);
                 $this->disablePrint = true;
                 $var->expand(true);
                 $this->refresh();
                 $this->disablePrint = false;
                 $this->gotoPositionY($var->getLastPosY());
             }
             break;
             // enter key
         // enter key
         case 13:
             // expand array/object/string
             if (array_key_exists($this->highlightedPositionY, $this->expandableList)) {
                 $element = $this->expandableList[$this->highlightedPositionY];
                 $refUid = $element->getRefUid();
                 if ($element->getRefUid()) {
                     // element that refers to another
                     // find referenced element and expand it
                     $refTree = $this->var->findUid($refUid);
                     if (false === $refTree) {
                         return;
                     }
                     $refTree->expand(true);
                     // refresh to update elements positions
                     $this->disablePrint = true;
                     $this->refresh();
                     $this->disablePrint = false;
                     $dstY = $refTree->getLastPosY();
                     // goto to y position
                     if (false !== $dstY) {
                         $this->gotoPositionY($dstY);
                     }
                     // hold elements for highlighting
                     if ($this->highlightedReferencer) {
                         $this->highlightedReferencer->highlightAsReferencer(false);
                         $this->highlightedReferenced->highlightAsReferenced(false);
                     }
                     $element->highlightAsReferencer(true);
                     $refTree->highlightAsReferenced(true);
                     $this->highlightedReferencer = $element;
                     $this->highlightedReferenced = $refTree;
                 } else {
                     // regular element
                     $element->toggleExpand();
                     // if the selected line is not the first line of the element, go up until the first line
                     if ($this->cursorPositionY != $this->highlightedPositionY) {
                         $up = $this->cursorPositionY - $this->highlightedPositionY;
                         for ($i = 0; $i < $up; $i++) {
                             $this->onKeyPress(259);
                         }
                     }
                 }
             }
             break;
             // up arrow
         // up arrow
         case NCURSES_KEY_UP:
             do {
                 $newCursorPositionY = max(0, $this->cursorPositionY - 1);
                 if ($newCursorPositionY != $this->cursorPositionY) {
                     $this->cursorPositionY = $newCursorPositionY;
                     $this->updateHighlightedPosition($this->cursorPositionY);
                     // if the cursor is outside the screen, apply a shift in order to move the content
                     if ($newCursorPositionY - $this->decY < 0) {
                         $this->decY--;
                         break;
                     }
                 }
                 // if current position is the min position, break to avoid infinite loop
                 if ($this->cursorPositionY == 0) {
                     break;
                 }
             } while (array_key_exists($this->cursorPositionY, $this->highlightRefYList));
             break;
             // down arrow
         // down arrow
         case NCURSES_KEY_DOWN:
             $oldCursorPositionY = $this->cursorPositionY;
             do {
                 $newCursorPositionY = min($this->maxY - 1, $this->cursorPositionY + 1);
                 if ($newCursorPositionY != $this->cursorPositionY && $newCursorPositionY != $this->maxY) {
                     $this->cursorPositionY = $newCursorPositionY;
                     $this->updateHighlightedPosition($this->cursorPositionY);
                     // if the cursor is outside the screen, apply a shift in order to move the content
                     if ($newCursorPositionY - $this->decY >= $this->padHeight - 1) {
                         $this->decY++;
                         break;
                     }
                 }
                 // if current position is the max position, break to avoid infinite loop
                 if ($this->cursorPositionY == $this->maxY - 1) {
                     if (in_array($this->cursorPositionY, $this->highlightRefYList)) {
                         $this->cursorPositionY = $oldCursorPositionY;
                         $this->updateHighlightedPosition($this->cursorPositionY);
                     }
                     break;
                 }
             } while (array_key_exists($this->cursorPositionY, $this->highlightRefYList));
             break;
             // page up
         // page up
         case NCURSES_KEY_PPAGE:
             do {
                 $newCursorPositionY = max(0, $this->cursorPositionY - $this->padHeight);
                 if ($newCursorPositionY != $this->cursorPositionY) {
                     $this->cursorPositionY = $newCursorPositionY;
                     $this->updateHighlightedPosition($this->cursorPositionY);
                     // if the cursor is outside the screen, apply a shift in order to move the content
                     if ($newCursorPositionY - $this->decY < 0) {
                         $this->decY += $newCursorPositionY - $this->decY;
                         break;
                     }
                 }
                 // if current position is the min position, break to avoid infinite loop
                 if ($this->cursorPositionY == 0) {
                     break;
                 }
             } while (array_key_exists($this->cursorPositionY, $this->highlightRefYList));
             break;
             // page down
         // page down
         case NCURSES_KEY_NPAGE:
             $oldCursorPositionY = $this->cursorPositionY;
             do {
                 $newCursorPositionY = min($this->maxY - 1, $this->cursorPositionY + $this->padHeight);
                 if ($newCursorPositionY != $this->cursorPositionY) {
                     $this->cursorPositionY = $newCursorPositionY;
                     $this->updateHighlightedPosition($this->cursorPositionY);
                     // if the cursor is outside the screen, apply a shift in order to move the content
                     if ($newCursorPositionY - $this->decY >= $this->padHeight - 1) {
                         $this->decY = min($newCursorPositionY, $this->maxY - ($this->padHeight - 1));
                         break;
                     }
                 }
                 // if current position is the max position, break to avoid infinite loop
                 if ($this->cursorPositionY == $this->maxY - 1) {
                     if (in_array($this->cursorPositionY, $this->highlightRefYList)) {
                         $this->cursorPositionY = $oldCursorPositionY;
                         $this->updateHighlightedPosition($this->cursorPositionY);
                     }
                     break;
                 }
             } while (array_key_exists($this->cursorPositionY, $this->highlightRefYList));
             break;
             // right arrow
         // right arrow
         case NCURSES_KEY_RIGHT:
             $this->decX = min($this->padRealWidth - $this->padWidth, $this->decX + 1);
             break;
             // left arrow
         // left arrow
         case NCURSES_KEY_LEFT:
             $this->decX = max(0, $this->decX - 1);
             break;
             // end key
         // end key
         case NCURSES_KEY_END:
             $this->decX = $this->padRealWidth - $this->padWidth;
             break;
             // home key
         // home key
         case NCURSES_KEY_HOME:
             $this->decX = 0;
             break;
             // ctrl + right arrow
         // ctrl + right arrow
         case 555:
         case 559:
             $this->decX = min($this->padRealWidth - $this->padWidth, $this->decX + $this->padWidth);
             break;
             // ctrl + left arrow
         // ctrl + left arrow
         case 540:
         case 544:
             $this->decX = max(0, $this->decX - $this->padWidth);
             break;
         default:
     }
 }
Beispiel #10
0
function menu_check_list($params)
{
    ###############################################################################################
    if (!is_array($params) || empty($params)) {
        trigger_error('wrong_params');
        return NULL;
    }
    $menu = isset($params['items']) ? $params['items'] : NULL;
    $rows = isset($params['rows']) ? (int) $params['rows'] : 0;
    $cols = isset($params['cols']) ? (int) $params['cols'] : 0;
    $centered = empty($params['centered']) ? 0 : 1;
    $y_menu = isset($params['y']) ? (int) $params['y'] : 0;
    $x_menu = isset($params['x']) ? (int) $params['x'] : 0;
    if (!is_array($menu) || empty($menu) || $rows <= 0 || $cols <= 0 || $y_menu < 0 || $x_menu < 0) {
        trigger_error('wrong params');
        return NULL;
    }
    $keys = array_keys($menu);
    $n_menu = count($keys);
    $items = array();
    $checked = array();
    $current = 0;
    $width = 0;
    $height = $n_menu + 2;
    $i = 0;
    $k = NULL;
    $i_checked = NULL;
    for ($i = 0; $i < $n_menu; $i++) {
        $k = $keys[$i];
        $i_checked = isset($menu[$k][1]) && $menu[$k][1] == 1 ? 1 : 0;
        $items[$i] = ' [' . ($i_checked ? '*' : ' ') . '] ' . $menu[$k][0];
        $width = max($width, strlen($items[$i]));
        $checked[$i] = $i_checked;
    }
    for ($i = 0; $i < $n_menu; $i++) {
        $items[$i] = $items[$i] . str_repeat(' ', 2 + $width - strlen($items[$i]));
    }
    $width += 4;
    if ($centered) {
        $r = $rows - $height >> 1;
        $c = $cols - $width >> 1;
    }
    $window = ncurses_newwin($height, $width, $r, $c);
    if (empty($window)) {
        trigger_error('unable to create window');
        return NULL;
    }
    ncurses_wborder($window, 0, 0, 0, 0, 0, 0, 0, 0);
    $n_items = count($items);
    for ($i = 0; $i < $n_items; $i++) {
        if ($i == $current) {
            ncurses_wattron($window, NCURSES_A_REVERSE);
        }
        ncurses_mvwaddstr($window, 1 + $i, 1, $items[$i]);
        if ($i == $current) {
            ncurses_wattroff($window, NCURSES_A_REVERSE);
        }
    }
    ncurses_wrefresh($window);
    ncurses_keypad($window, TRUE);
    ncurses_noecho();
    ncurses_curs_set(0);
    $do_loop = 1;
    $save_result = 0;
    while ($do_loop) {
        $key = ncurses_wgetch($window);
        $move = 0;
        switch ($key) {
            case NCURSES_KEY_UP:
                if ($current > 0) {
                    $move = -1;
                }
                break;
            case NCURSES_KEY_DOWN:
                if ($current < $n_menu - 1) {
                    $move = 1;
                }
                break;
            case XCURSES_KEY_LF:
            case XCURSES_KEY_CR:
                $do_loop = 0;
                $save_result = 1;
                break;
            case XCURSES_KEY_SPACE:
                if ($checked[$current]) {
                    $checked[$current] = 0;
                    $items[$current] = ' [ ] ' . substr($items[$current], 5);
                } else {
                    $checked[$current] = 1;
                    $items[$current] = ' [*] ' . substr($items[$current], 5);
                }
                ncurses_wattron($window, NCURSES_A_REVERSE);
                ncurses_mvwaddstr($window, 1 + $current, 1, $items[$current]);
                ncurses_wattroff($window, NCURSES_A_REVERSE);
                ncurses_wrefresh($window);
                break;
            case XCURSES_KEY_ESC:
                ncurses_flushinp();
                $do_loop = 0;
                break;
        }
        if ($move) {
            ncurses_mvwaddstr($window, 1 + $current, 1, $items[$current]);
            $current += $move;
            ncurses_wattron($window, NCURSES_A_REVERSE);
            ncurses_mvwaddstr($window, 1 + $current, 1, $items[$current]);
            ncurses_wattroff($window, NCURSES_A_REVERSE);
            ncurses_wrefresh($window);
        }
    }
    ncurses_delwin($window);
    $result = NULL;
    if ($save_result) {
        for ($i = 0; $i < $n_menu; $i++) {
            $result[$keys[$i]] = $checked[$i];
        }
    }
    return $result;
}