-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmdIO.php
129 lines (108 loc) · 3.33 KB
/
CmdIO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/* ----------------------------------
* A Class to handle Comandline output and input
* ---------------------------------- */
class CmdIO{
var $writeFileHandler = null; //
var $writeMode = 0; // 0 > only to screen, 2 > only to file, 1 > screen & file
function __construct() {
}
function confirm($message = 'please confirm to continue') {
if (strtolower($this->read($message . ' (y/N)')) == 'y'){
return true;
}
return false;
}
function wait($message = 'press enter to continue') { return $this->readStdInn($message);}
function read($message = 'press enter to continue') { return $this->readStdInn($message);}
function readStdInn($message = 'press enter to continue'){
$this->cmd_print($message.': ', false);
$stdin = fopen('php://stdin', 'r');
$input = fgets($stdin, 255);
fclose($stdin);
$c_input = str_replace(array("\n", "\r"), "", $input);
return $c_input;
}
/* -----------------------------------------
* Function to print something to the cmd
*
* @text - the text
* @nl - boolean, if the text should be in a new line
* ----------------------------------------- */
function cmd_print($text = '', $nl = true, $mode = 0){
$defLineStart = "> ";
$sep = "------------------------------------------------------------------------------\r\n";
$line = '';
switch ($mode){
default:
$line = $text;
break;
case 1:
$line = "\r\n".$sep;
$line .= "| ".$text."\r\n";
$line .= $sep;
break;
case 2:
$line = $sep;
$line .= $defLineStart.$text."\r\n";
break;
case 3:
$line = $defLineStart.$text."\r\n";
$line .= $sep;
break;
}
if ($nl) $line .= "\r\n";
if($this->writeMode < 2 ) {
echo $line;
}
if($this->writeMode > 0) {
fwrite($this->writeFileHandler, $line);
}
}
/* -----------------------------------------
* Function to print something to the cmd
*
* @text - the text
* @nl - boolean, if the text should be in a new line
* ----------------------------------------- */
function out($text = '', $nl = true, $mode = 0) {
$this->cmd_print($text, $nl, $mode);
}
function fatal($text, $location = '?'){
$this->cmd_print('> [FATAL ERROR] in '.$location.': '.$text);
die();
}
function error($text, $location = '?') {
$this->cmd_error($text, $location);
}
function cmd_error($text, $location = '?') {
$this->cmd_print('> [ERROR] in '.$location.': '.$text);
}
function warn($text, $location = '?') {
$this->cmd_print('> [WARNING] in '.$location.': '.$text);
}
// 0 > only to screen, 2 > only to file, 1 > screen & file
function setWriteMode($mode, $filepath = null){
if (is_int($mode)){
$this->writeMode = $mode;
} else {
switch($mode){
default:
$this->writeMode = 0;
break;
case 'screen & file':
$this->writeMode = 1;
break;
case 'file':
$this->writeMode = 2;
break;
}
}
// close the current handler, if one is present
if ($this->writeFileHandler) fclose($this->writeFileHandler);
if ($filepath && $this->writeMode > 0) {
$this->writeFileHandler = fopen($filepath, 'w');
}
}
}
?>