/** * 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(); }
/** * Creates the ncurses pad. * If the pad already exists, it is deleted and recreated. * @throws \RuntimeException */ protected function createPad() { if (null !== $this->pad) { ncurses_delwin($this->pad); } elseif (null === $this->padRealWidth) { $this->calculatePadRealSize(); } $globalWidth = null; $globalHeight = null; ncurses_getmaxyx(STDSCR, $globalHeight, $globalWidth); $this->padWidth = $globalWidth; $this->padHeight = $globalHeight - 3; $w = max($this->padWidth, $this->padRealWidth); $h = max($this->padHeight, $this->padRealHeight); $this->pad = ncurses_newpad($h, $w); if (false === $this->pad) { throw new \RuntimeException("Failed to create a ncurses pad (width: {$this->padRealWidth}, height: {$this->padRealHeight})"); } ncurses_keypad($this->pad, true); }
function ncurses_show_text($title, $text, $question, $keys = TRUE) { // prepare text $textH = count($text); $textW = 1; $textLEN = array(); for ($i = 0; $i < $textH; $i++) { $textLEN[$i] = strlen($text[$i]); if ($textLEN[$i] > $textW) { $textW = $textLEN[$i]; } } // create text pad (invisible window) $textWIN = ncurses_newpad($textH, $textW); // fill it with text for ($i = 0; $i < $textH; $i++) { ncurses_mvwaddstr($textWIN, $i, 0, $text[$i]); } // prepare question $questionH = count($question); $questionLastW = strlen($question[$questionH - 1]); // initialize... $posX = $posY = 0; $screenH = $screenW = 0; // loop around... while (1) { // get actual screen size $oldH = $screenH; $oldW = $screenW; ncurses_getmaxyx(STDSCR, $screenH, $screenW); // something changed? if ($screenH != $oldH || $screenW != $oldW) { if ($oldH > 0) { ncurses_delwin($upperWIN); ncurses_delwin($lowerWIN); } ncurses_erase(); ncurses_refresh(STDSCR); $upperWIN = ncurses_newwin($screenH - (2 + $questionH), $screenW, 0, 0); $lowerWIN = ncurses_newwin(2 + $questionH, $screenW, $screenH - (2 + $questionH), 0); $upperH = $screenH - (4 + $questionH); $upperW = $screenW - 2; $copyH = $upperH > $textH ? $textH : $upperH; $copyW = $upperW > $textW ? $textW : $upperW; // border lower window ncurses_wborder($lowerWIN, 0, 0, 0, 0, 0, 0, 0, 0); // print text in lower window for ($i = 0; $i < $questionH; $i++) { ncurses_mvwaddstr($lowerWIN, $i + 1, 1, $question[$i]); } } // check and fix positions if ($posY < 0 || $upperH >= $textH) { $posY = 0; } else { if ($upperH + $posY > $textH) { $posY = $textH - $upperH; } } if ($posX < 0 || $upperW >= $textW) { $posX = 0; } else { if ($upperW + $posX > $textW) { $posX = $textW - $upperW; } } // border upper window ncurses_wborder($upperWIN, 0, 0, 0, 0, 0, 0, 0, 0); // draw title and info line ncurses_wattron($upperWIN, NCURSES_A_REVERSE); ncurses_mvwaddstr($upperWIN, 0, 2, ' ' . $title . ' '); if ($upperH < $textH) { ncurses_mvwaddstr($upperWIN, $upperH + 1, 2, ' line ' . ($posY + 1) . '-' . ($posY + $copyH) . '/' . $textH . ' '); } ncurses_wattroff($upperWIN, NCURSES_A_REVERSE); // draw < and > at left/right side when horizontal scrolling is nesseccary if ($upperW < $textW) { for ($i = 0; $i < $copyH; $i++) { if ($textLEN[$i + $posY] > $copyW + $posX) { ncurses_mvwaddstr($upperWIN, $i + 1, $screenW - 1, '>'); } if ($posX > 0 && $textLEN[$i + $posY] > 0) { ncurses_mvwaddstr($upperWIN, $i + 1, 0, '<'); } } } // draw upper window ncurses_wrefresh($upperWIN); // copy a part of the text (pad) to the screen ncurses_prefresh($textWIN, $posY, $posX, 1, 1, $upperH, $upperW); // move cursor to end of last line of question ncurses_wmove($lowerWIN, $questionH, $questionLastW + 1); // draw lower window ncurses_wrefresh($lowerWIN); // get a character and do... $char = ncurses_getch(); if (is_array($keys) && array_search($char, $keys) !== FALSE) { break; } else { if ($char == NCURSES_KEY_UP) { $posY--; } else { if ($char == NCURSES_KEY_DOWN) { $posY++; } else { if ($char == NCURSES_KEY_LEFT) { $posX--; } else { if ($char == NCURSES_KEY_RIGHT) { $posX++; } else { if ($char == NCURSES_KEY_PPAGE) { $posY -= $copyH - 1; } else { if ($char == NCURSES_KEY_NPAGE) { $posY += $copyH - 1; } else { if ($char == 362) { // HOME $posX = 0; } else { if ($char == 385) { // END $posX = 99999; } else { if ($char == 410 || $char == -1) { // these "characters" are pressed on resizing } else { if ($keys === TRUE) { break; } } } } } } } } } } } } //end loop // free all resources ncurses_delwin($textWIN); ncurses_delwin($upperWIN); ncurses_delwin($lowerWIN); // return the pressed character return $char; }