public function addOutput($text) { // Store current cursor position ncurses_getyx(STDSCR, $cury, $curx); // Move the cursor to a given line ncurses_move($this->outputLine, 0); // Output text ncurses_addstr($text); // Move to the next line $this->outputLine++; // Return to the previous position ncurses_move($cury, $curx); }
<?php $password = ''; ncurses_init(); ncurses_addstr("Enter your password:\n"); /* Do not display the keystrokes as they are typed */ ncurses_noecho(); while (true) { // get a character from the keyboard $c = chr(ncurses_getch()); if ("\r" == $c || "\n" == $c) { // if it's a newline, break out of the loop, we've got our password break; } elseif ("" == $c) { /* if it's a backspace, delete the previous char from $password */ $password = substr_replace($password, '', -1, 1); } elseif ("" == $c) { // if it's Control-C, clear $password and break out of the loop $password = NULL; break; } else { // otherwise, add the character to the password $password .= $c; } } ncurses_end();
<?php $line = ''; ncurses_init(); ncurses_addstr("Type a message, ending with !\n"); /* Display the keystrokes as they are typed */ ncurses_echo(); while (($c = ncurses_getch()) != ord("!")) { $line .= chr($c); } ncurses_end(); print "You typed: [{$line}]\n";
<?php ncurses_init(); ncurses_start_color(); ncurses_init_pair(1, NCURSES_COLOR_GREEN, NCURSES_COLOR_BLACK); ncurses_init_pair(2, NCURSES_COLOR_RED, NCURSES_COLOR_BLACK); ncurses_init_pair(3, NCURSES_COLOR_WHITE, NCURSES_COLOR_BLACK); ncurses_color_set(1); ncurses_addstr("OK "); ncurses_color_set(3); ncurses_addstr("Something succeeded!\n"); ncurses_color_set(2); ncurses_addstr("FAIL "); ncurses_color_set(3); ncurses_addstr("Something succeeded!\n");
/** * get IPC message and display * * @param callback $callback * return true to end the loop, called on every loop */ function curlInfoIPC($callback = null) { $queue = $this->getMessageQueue(true)[0]; $nc = ncurses_init(); $window = ncurses_newwin(0, 0, 0, 0); $caption = ''; $list = array(); $lastClear = time(); while (true) { $time = time(); if ($time - $lastClear > 10) { ncurses_clear(); $lastClear = $time; } $error = ''; if (msg_receive($queue, 0, $msgtype, 1024 * 1024, $msg, true, MSG_IPC_NOWAIT, $errorCode)) { if (strlen($msg['caption']) > strlen($caption)) { $caption = $msg['caption']; } $pid = $msg['pid']; $isLast = $msg['isLast']; unset($msg['pid'], $msg['isLast'], $msg['caption']); $list[$pid] = $msg; if (true === $isLast) { unset($list[$pid]); ncurses_clear(); if (empty($list)) { break; } } } else { if ($errorCode != MSG_ENOMSG) { $error = 'IPC receive error, errorCode=' . $errorCode; } } $content = ''; $output = ''; foreach ($list as $k => $v) { $content .= $v['content'] . "\n"; $output .= $v['output'] . "\n"; } $str = trim($caption . "\n" . $content . "\n" . $output . "\n" . $error); ncurses_move(0, 0); ncurses_addstr($str); ncurses_refresh(); if (isset($callback)) { if (call_user_func($callback)) { break; } } usleep(100 * 1000); } ncurses_end(); msg_remove_queue($queue); }