Remove all the {{{ and }}} folding
This commit is contained in:
102
src/Console.php
102
src/Console.php
@@ -26,7 +26,6 @@ namespace Domframework;
|
||||
class Console
|
||||
{
|
||||
// PROPERTIES
|
||||
// {{{
|
||||
/** Set the debug on if a filename is provided, or do not debug if false is
|
||||
* provided
|
||||
*/
|
||||
@@ -79,13 +78,11 @@ class Console
|
||||
/** Store the last cursor position in the last readline
|
||||
*/
|
||||
private $cursorPos = 1;
|
||||
// }}}
|
||||
|
||||
/** The constructor init the console.
|
||||
* Check if we have the rights to execute, if wa have in cli...
|
||||
*/
|
||||
public function __construct ()
|
||||
// {{{
|
||||
{
|
||||
if (! function_exists ("exec"))
|
||||
throw $this->ConsoleException ("No exec support in PHP");
|
||||
@@ -98,12 +95,10 @@ class Console
|
||||
exec ("stty -echo -icanon min 1 time 0 2>/dev/null");
|
||||
$this->updateTerminalSize ();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Update the terminal size
|
||||
*/
|
||||
public function updateTerminalSize ()
|
||||
// {{{
|
||||
{
|
||||
$this->termWidth = 80;
|
||||
$this->termHeight = 25;
|
||||
@@ -121,12 +116,10 @@ class Console
|
||||
$this->termHeight = intval ($termHeight);
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** The destructor return the terminal to initial state
|
||||
*/
|
||||
public function __destruct ()
|
||||
// {{{
|
||||
{
|
||||
if ($this->initSttyState !== "")
|
||||
exec ("stty $this->initSttyState");
|
||||
@@ -134,38 +127,31 @@ class Console
|
||||
$this->textUnderline (false);
|
||||
$this->textBold (false);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Each time a key is pressed by the user, display the value on screen (echo)
|
||||
*/
|
||||
public function setEcho ()
|
||||
// {{{
|
||||
{
|
||||
$this->echoMode = true;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Each time a key is pressed by the user, DO NOT display the value on screen
|
||||
* (echo disabled)
|
||||
*/
|
||||
public function unsetEcho ()
|
||||
// {{{
|
||||
{
|
||||
$this->echoMode = false;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Display a text on screen. Must be used before "readline" method because
|
||||
* the provided message will not be deleted by readline process.
|
||||
* @param string $message The message to display
|
||||
*/
|
||||
public function echo ($message)
|
||||
// {{{
|
||||
{
|
||||
echo $message;
|
||||
$this->lineContent .= $message;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Wait one valid character from the user.
|
||||
* The non printable chars are not displayed, nor returned
|
||||
@@ -173,14 +159,12 @@ class Console
|
||||
* @return the pressed char
|
||||
*/
|
||||
public function getc ()
|
||||
// {{{
|
||||
{
|
||||
$char = $this->getKey ();
|
||||
while (in_array (ord ($char), $this->nonWriteableChar))
|
||||
$char = $this->getKey ();
|
||||
return $char;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Wait one key pressed by the user. If the key pressed is an ESC sequence,
|
||||
* return this sequence
|
||||
@@ -189,7 +173,6 @@ class Console
|
||||
* @return the pressed char
|
||||
*/
|
||||
public function getKey ()
|
||||
// {{{
|
||||
{
|
||||
$char = fgetc (STDIN);
|
||||
if ($char === chr (27))
|
||||
@@ -262,7 +245,6 @@ class Console
|
||||
echo $char;
|
||||
return $char;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Get the line of characters pressed by the user and return the result.
|
||||
* Stop when the user valid by \n.
|
||||
@@ -273,7 +255,6 @@ class Console
|
||||
* @return string The typed string
|
||||
*/
|
||||
public function readline ($propo = "", $stopperChar = false)
|
||||
// {{{
|
||||
{
|
||||
// Gets can not delete chars before the call. Keep the prompt (if exists)
|
||||
if (! is_string ($propo))
|
||||
@@ -307,7 +288,6 @@ class Console
|
||||
if ($this->completionKeys !== false &&
|
||||
in_array ($char, $this->mb_str_split ($this->completionKeys)))
|
||||
// Manage autocompletion
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Autocompletion starting");
|
||||
// Take the last part of the string without space or double quotes
|
||||
@@ -412,7 +392,6 @@ class Console
|
||||
}
|
||||
$this->debug ("Autocompletion : end '$prompt.$string'");
|
||||
}
|
||||
// }}}
|
||||
elseif (ord ($char) === 0)
|
||||
// End of file
|
||||
{
|
||||
@@ -422,7 +401,6 @@ class Console
|
||||
}
|
||||
elseif (ord ($char) === 3)
|
||||
// Abort (Ctrl+C)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Abort Ctrl+C : ".ord ($char));
|
||||
$this->lineContent = "";
|
||||
@@ -431,20 +409,16 @@ class Console
|
||||
echo "$prompt\n";
|
||||
break;
|
||||
}
|
||||
// }}}
|
||||
elseif (ord ($char) === 4)
|
||||
// Logout (Ctrl+D)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Logout Ctrl+D : ".ord ($char));
|
||||
$string = "exit\n";
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
return $string;
|
||||
}
|
||||
// }}}
|
||||
elseif (ord($char) === 12)
|
||||
// Refresh page (Ctrl+L)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Refresh Ctrl+L : ".ord ($char));
|
||||
echo "\033[2J\033[;H\033c";
|
||||
@@ -452,10 +426,8 @@ class Console
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif (ord($char) === 21)
|
||||
// Empty line from prompt to cursor (Ctrl+U)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Empty line from prompt to cursor Ctrl+U : ".ord ($char));
|
||||
$string = mb_substr ($string, $cursorPos - $minLength);
|
||||
@@ -463,10 +435,8 @@ class Console
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif (ord($char) === 23)
|
||||
// Remove the last word (Ctrl+W)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Remove the last word Ctrl+W : ".ord ($char));
|
||||
$tmp = mb_substr ($string, 0, $cursorPos - $minLength);
|
||||
@@ -480,10 +450,8 @@ class Console
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif (ord($char) === 127 || ord($char) === 8)
|
||||
// Remove the previous char (Backspace)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Remove the previous char (Backspace) : ".ord ($char));
|
||||
if ($cursorPos <= $minLength)
|
||||
@@ -495,7 +463,6 @@ class Console
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif (ord ($char[0]) === 27)
|
||||
{
|
||||
// ESC SEQUENCE
|
||||
@@ -505,7 +472,6 @@ class Console
|
||||
$this->debug ("ESC SEQUENCE : $sequence");
|
||||
if ($char === chr (27).chr (91).chr (49).chr (59).chr (53).chr (67))
|
||||
// Cursor right + Ctrl : cursor jump by word
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Cursor right + Ctrl");
|
||||
$tmp = mb_substr ($string, $cursorPos - $minLength);
|
||||
@@ -517,10 +483,8 @@ class Console
|
||||
$cursorPos = mb_strlen ($prompt.$string) + 1;
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (49).chr (59).chr (53).chr (68))
|
||||
// Cursor left + Ctrl : cursor jump by word
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Cursor left + Ctrl");
|
||||
$tmp = mb_substr ($string, 0, $cursorPos - $minLength);
|
||||
@@ -530,10 +494,8 @@ class Console
|
||||
$cursorPos = $minLength + $pos;
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (65))
|
||||
// Cursor up : display the previous history if defined
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Cursor up");
|
||||
if (! isset ($historyTmp))
|
||||
@@ -551,10 +513,8 @@ class Console
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (66))
|
||||
// Cursor down : display the next history if defined
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Cursor down");
|
||||
if ($historyPos < count ($this->history) - 1)
|
||||
@@ -573,10 +533,8 @@ class Console
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (67))
|
||||
// Cursor right
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Cursor right");
|
||||
if ($cursorPos <= mb_strlen ($this->lineContent))
|
||||
@@ -585,10 +543,8 @@ class Console
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (68))
|
||||
// Cursor left
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Cursor left");
|
||||
if ($cursorPos > $minLength)
|
||||
@@ -597,28 +553,22 @@ class Console
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (70))
|
||||
// End key
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("End key");
|
||||
$cursorPos = $minLength + mb_strlen ($string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (72))
|
||||
// Home key
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Home key");
|
||||
$cursorPos = $minLength;
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
elseif ($char === chr (27).chr (91).chr (51).chr (126))
|
||||
// Remove the char under the cursor (Delete)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Delete key");
|
||||
if ($cursorPos > mb_strlen ($prompt.$string))
|
||||
@@ -629,18 +579,14 @@ class Console
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
}
|
||||
elseif (in_array (ord ($char), $this->nonWriteableChar))
|
||||
// Non writeable char : skip it
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Non writeable char : ".ord ($char));
|
||||
}
|
||||
// }}}
|
||||
else
|
||||
// Normal char : Add it to the string
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Normal char : ".ord ($char));
|
||||
$strArr = $this->mb_str_split ($string);
|
||||
@@ -653,19 +599,16 @@ class Console
|
||||
$this->rewriteLine ($prompt.$string);
|
||||
$this->moveCursor ($cursorPos);
|
||||
}
|
||||
// }}}
|
||||
}
|
||||
$this->debug ("End of readline '$string'");
|
||||
return $string;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Rewrite the line with the provided $text.
|
||||
* Delete all the old data
|
||||
* @param string $text The new text to use on line
|
||||
*/
|
||||
private function rewriteLine ($text)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Call rewriteLine ($text)");
|
||||
if ($this->echoMode)
|
||||
@@ -676,13 +619,11 @@ class Console
|
||||
$this->cursorPos = mb_strlen ($this->lineContent);
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Move the cursor on position $position. The first column is $cursorPos=1
|
||||
* @param integer $cursorPos The new position on line
|
||||
*/
|
||||
private function moveCursor ($cursorPos)
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Call moveCursor ($cursorPos)");
|
||||
if ($cursorPos < 1)
|
||||
@@ -708,12 +649,10 @@ class Console
|
||||
$this->cursorPos = $cursorPos;
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Clear the existing line.
|
||||
*/
|
||||
public function clearLine ()
|
||||
// {{{
|
||||
{
|
||||
$this->debug ("Call clearLine");
|
||||
$oldLength = mb_strlen ($this->lineContent);
|
||||
@@ -742,34 +681,27 @@ class Console
|
||||
$this->lineContent = "";
|
||||
$this->cursorPos = 1;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Clear all the screen and remove the scroll of the screen
|
||||
*/
|
||||
public function clearScreen ()
|
||||
// {{{
|
||||
{
|
||||
echo "\033[2J\033[;H\033c";
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Get the terminal Height
|
||||
*/
|
||||
public function getTermHeight ()
|
||||
// {{{
|
||||
{
|
||||
return $this->termHeight;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Get the terminal Width
|
||||
*/
|
||||
public function getTermWidth ()
|
||||
// {{{
|
||||
{
|
||||
return $this->termWidth;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Call a specific function when a completion key is pressed
|
||||
* The function must get the partial text as first parameter, and must return
|
||||
@@ -782,7 +714,6 @@ class Console
|
||||
* completion keys is pressed.
|
||||
*/
|
||||
public function completeFunction ($completionKeys, $completionFunction)
|
||||
// {{{
|
||||
{
|
||||
if (! is_string ($completionKeys) && ! is_boolean ($completionKeys))
|
||||
$this->consoleException ("Can not set the completionKeys : not a string");
|
||||
@@ -794,33 +725,27 @@ class Console
|
||||
$this->completionKeys = $completionKeys;
|
||||
$this->completionFunction = $completionFunction;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Get the actual history in memory
|
||||
*/
|
||||
public function getHistory ()
|
||||
// {{{
|
||||
{
|
||||
return $this->history;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Clear the history
|
||||
* This method do NOT write the empty history on disk
|
||||
*/
|
||||
public function clearHistory ()
|
||||
// {{{
|
||||
{
|
||||
$this->history = array ();
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Write the history to disk.
|
||||
* @param string $historyFile The history file where the history is stored
|
||||
*/
|
||||
public function writeHistory ($historyFile)
|
||||
// {{{
|
||||
{
|
||||
if (file_exists ($historyFile))
|
||||
{
|
||||
@@ -848,7 +773,6 @@ class Console
|
||||
file_put_contents ($historyFile, $history, FILE_APPEND|LOCK_EX);
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Read the history from the disk
|
||||
* If the file doesn't exists, return an empty array
|
||||
@@ -856,7 +780,6 @@ class Console
|
||||
* @return the read history with timestamp as key and command as value
|
||||
*/
|
||||
public function readHistory ($historyFile)
|
||||
// {{{
|
||||
{
|
||||
if (! file_exists ($historyFile))
|
||||
{
|
||||
@@ -882,7 +805,6 @@ class Console
|
||||
}
|
||||
return $this->history;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Add a new entry in history.
|
||||
* The new line can not be empty : it is not stored, but without error
|
||||
@@ -890,7 +812,6 @@ class Console
|
||||
* @param string The new entry to add in history
|
||||
*/
|
||||
public function addHistory ($line)
|
||||
// {{{
|
||||
{
|
||||
if (! is_string ($line))
|
||||
$this->consoleException ("Can not add line to history : ".
|
||||
@@ -902,14 +823,12 @@ class Console
|
||||
true);
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Get/Set the maximum number of entries in the history
|
||||
* If null, get the defined maximum number
|
||||
* @param integer|null $historyMaxSize The maximum number of entries
|
||||
*/
|
||||
public function historyMaxSize ($historyMaxSize = null)
|
||||
// {{{
|
||||
{
|
||||
if ($historyMaxSize === null)
|
||||
return $this->historyMaxSize;
|
||||
@@ -918,58 +837,48 @@ class Console
|
||||
"negative value provided");
|
||||
$this->historyMaxSize = intval ($historyMaxSize);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Error management
|
||||
* @param string $message The message to throw in the exception
|
||||
*/
|
||||
public function consoleException ($message)
|
||||
// {{{
|
||||
{
|
||||
throw new \Exception ($message, 500);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Set the text color
|
||||
* @param integer $colorNum The color number to use
|
||||
*/
|
||||
public function colorText ($colorNum)
|
||||
// {{{
|
||||
{
|
||||
if (! is_int ($colorNum))
|
||||
$this->consoleException ("ColorNum provided to colorText is not an ".
|
||||
"integer");
|
||||
echo "\033[38;5;${colorNum}m";
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Set the background text color
|
||||
* @param integer $colorNum The color number to use
|
||||
*/
|
||||
public function colorBackgroundText ($colorNum)
|
||||
// {{{
|
||||
{
|
||||
if (! is_int ($colorNum))
|
||||
$this->consoleException ("ColorNum provided to colorBackgroundText not ".
|
||||
"an integer");
|
||||
echo "\033[48;5;${colorNum}m";
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Reset the colors
|
||||
*/
|
||||
public function colorReset ()
|
||||
// {{{
|
||||
{
|
||||
echo "\033[0m";
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Underline the text
|
||||
* @param boolean $underline True to underline, false to remove the underline
|
||||
*/
|
||||
public function textUnderline ($underline)
|
||||
// {{{
|
||||
{
|
||||
if ($underline === false)
|
||||
$underline = 2;
|
||||
@@ -977,13 +886,11 @@ class Console
|
||||
$underline = "";
|
||||
echo "\033[${underline}4m";
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Bold the text
|
||||
* @param boolean $bold True to bold, false to remove the bold
|
||||
*/
|
||||
public function textBold ($bold)
|
||||
// {{{
|
||||
{
|
||||
if ($bold === false)
|
||||
$bold = 0;
|
||||
@@ -991,7 +898,6 @@ class Console
|
||||
$bold = 1;
|
||||
echo "\033[${bold}m";
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Return true if the TTY is enabled, or false if the program is called from pipe
|
||||
*/
|
||||
@@ -1007,7 +913,6 @@ class Console
|
||||
* @return array The tokens
|
||||
*/
|
||||
static public function tokenize ($line)
|
||||
// {{{
|
||||
{
|
||||
$tokens = array ();
|
||||
$token = strtok (trim ($line),' ');
|
||||
@@ -1022,7 +927,6 @@ class Console
|
||||
}
|
||||
return $tokens;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** This function return an array with each char, but supports UTF-8
|
||||
* @param string $string The string to explode
|
||||
@@ -1030,20 +934,17 @@ class Console
|
||||
* @return array
|
||||
*/
|
||||
private function mb_str_split ($string, $split_length = 1)
|
||||
// {{{
|
||||
{
|
||||
$res = array();
|
||||
for ($i = 0; $i < mb_strlen ($string); $i += $split_length)
|
||||
$res[] = mb_substr ($string, $i, $split_length);
|
||||
return $res;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** This function debug the data
|
||||
* @param mixed $data The data to store
|
||||
*/
|
||||
private function debug ($data)
|
||||
// {{{
|
||||
{
|
||||
if ($this->debug === false)
|
||||
return;
|
||||
@@ -1051,14 +952,12 @@ class Console
|
||||
$data = var_export ($data, true);
|
||||
file_put_contents ($this->debug, date ("H:i:s")." $data\n", FILE_APPEND);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Look in the array which first chars of each possibilites are identical.
|
||||
* @param array $completeArr The values to examine
|
||||
* @return string the identical chars
|
||||
*/
|
||||
private function shortestIdenticalValues ($completeArr)
|
||||
// {{{
|
||||
{
|
||||
if (! is_array ($completeArr))
|
||||
return "";
|
||||
@@ -1087,5 +986,4 @@ class Console
|
||||
}
|
||||
return $identicalString;
|
||||
}
|
||||
// }}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user