Exemplo n.º 1
0
 /**
  * {@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:
     }
 }