1
|
|
|
<?php |
2
|
|
|
require_once __DIR__ .'/../vendor/autoload.php'; |
3
|
|
|
use Kristuff\Mishell\Console; |
4
|
|
|
|
5
|
|
|
Console::clear(); |
6
|
|
|
printLoader(); |
7
|
|
|
goIndex(); |
8
|
|
|
//Console::newWindow(); |
9
|
|
|
|
10
|
|
|
function goIndex() |
11
|
|
|
{ |
12
|
|
|
printHeader(); |
13
|
|
|
printIndex(); |
14
|
|
|
askIndex(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function printLoader() |
18
|
|
|
{ |
19
|
|
|
Console::log(); |
20
|
|
|
Console::log(); |
21
|
|
|
Console::log(' '. Console::text(" _ ____ _ _ _ ", 'red')); |
22
|
|
|
Console::log(' '. Console::text(" _ __ ___ (_)/ ___|| |__ ___| | | ", 'red')); |
23
|
|
|
Console::log(' '. Console::text(" | '_ ` _ \| |\___ \| '_ \ / _ \ | | ", 'red')); |
24
|
|
|
Console::log(' '. Console::text(" | | | | | | | ___) | | | | __/ | | ", 'red')); |
25
|
|
|
Console::log(' '. Console::text(" |_| |_| |_|_||____/|_| |_|\___|_|_| ", 'red')); |
26
|
|
|
Console::log(' '. Console::text(" ", 'red')); |
27
|
|
|
Console::log(' '. Console::text(" Version 1.5 ", 'red')); |
28
|
|
|
Console::log(' '. Console::text(" © 2017-2021 Kristuff ", 'red')); |
29
|
|
|
Console::log(' '. Console::text(" ", 'red')); |
30
|
|
|
Console::log(); |
31
|
|
|
Console::log(); |
32
|
|
|
Console::log(); |
33
|
|
|
|
34
|
|
|
$pad = ' '; |
35
|
|
|
|
36
|
|
|
// fake progress message |
37
|
|
|
for ($i=0 ; $i<=100 ; $i++) { |
38
|
|
|
|
39
|
|
|
// Overwrite progress message. |
40
|
|
|
Console::relog($pad.Console::text('This is a fake progress message... [', 'white'). |
41
|
|
|
Console::text($i .'%', 'green'). |
42
|
|
|
Console::text('] completed', 'white')); |
43
|
|
|
|
44
|
|
|
// wait for a while, so we see the animation |
45
|
|
|
usleep(17000); |
46
|
|
|
} |
47
|
|
|
// Overwrite progress message. |
48
|
|
|
Console::relog($pad.Console::text('Done!', 'white', 'green', 'underlined').str_pad(' ', 100)); |
49
|
|
|
usleep(45000); |
50
|
|
|
Console::clear(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function getIndex() |
54
|
|
|
{ |
55
|
|
|
$index = []; |
56
|
|
|
|
57
|
|
|
$index[1] = ['Styles', 'How to get available basic styles', 'demo.styles.php']; |
58
|
|
|
$index[2] = ['Colors', 'How to get available foreground colors', 'demo.colors.php']; |
59
|
|
|
$index[3] = ['Backgrounds', 'How to get available background colors', 'demo.bgcolors.php']; |
60
|
|
|
// $index[4] = ['Text', 'Console::text() overview', 'demo.log.php']; |
61
|
|
|
// $index[5] = ['Log', 'Console::log() overview', 'demo.log.php']; |
62
|
|
|
// $index[6] = ['ReLog', 'Console::relog() overview', 'demo.relog.php']; |
63
|
|
|
$index[7] = ['Pad', 'Console::pad() overview', 'demo.pad.php']; |
64
|
|
|
$index[8] = ['Size', 'How to get the number of columns and lines in terminal', 'demo.size.php']; |
65
|
|
|
|
66
|
|
|
$index[11] = ['Ask', 'How to ask? (get user input)', 'demo.ask.php']; |
67
|
|
|
$index[12] = ['Ask Number', 'How to ask and expect a number?', 'demo.askint.php']; |
68
|
|
|
$index[13] = ['Ask Password', 'How to ask a password? (do not print user input)', 'demo.askpassword.php']; |
69
|
|
|
|
70
|
|
|
$index[14] = ['Table', 'How to print a table?', 'demo.table.php']; |
71
|
|
|
$index[15] = ['Bell', 'How to run the bell?', 'demo.bell.php']; |
72
|
|
|
$index[16] = ['Progress', 'How to output progress message?', 'demo.progress.php']; |
73
|
|
|
$index[17] = ['New window', 'How to open new/restore window?', 'demo.window.php']; |
74
|
|
|
|
75
|
|
|
$index[20] = ['BlueScreen', 'Really nice full screen centered message sample', 'demo.bluescreen.php']; |
76
|
|
|
|
77
|
|
|
return $index; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
function printHeader() |
81
|
|
|
{ |
82
|
|
|
Console::clear(); |
83
|
|
|
Console::log(); |
84
|
|
|
Console::log(Console::text(' Kristuff/Mishell ', 'darkgray') . Console::text(' v1.2 ', 'white', 'green')); |
85
|
|
|
Console::log(Console::text(' Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France', 'darkgray')); |
86
|
|
|
Console::log( |
87
|
|
|
Console::text(' © 2017-2021 Kristuff (', 'darkgray') . |
88
|
|
|
Console::text('https://github.com/kristuff)', 'darkgray', 'underlined') . |
89
|
|
|
Console::text(')', 'darkgray') |
90
|
|
|
); |
91
|
|
|
Console::log(); |
92
|
|
|
Console::log(' '.Console::text(' Interactive sample ', 'white', 'blue', 'bold')); |
93
|
|
|
Console::log(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
function printSampleHeader($index, $title) |
97
|
|
|
{ |
98
|
|
|
Console::log(' ' . Console::text(' - '. $index .' - ' . $title . ' ', 'white', 'blue')); |
99
|
|
|
Console::log(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
function printIndex() |
103
|
|
|
{ |
104
|
|
|
$rowHeaders = ['Index' => 6, 'Item' => 15, 'Description' => 55]; |
105
|
|
|
$i = 0; |
106
|
|
|
|
107
|
|
|
// customize table separator |
108
|
|
|
Console::$horizontalSeparator = '-'; |
109
|
|
|
Console::$verticalSeparator = ' '; |
110
|
|
|
Console::$verticalInnerSeparator = ' '; |
111
|
|
|
Console::$tableCellPadding = ' '; |
112
|
|
|
|
113
|
|
|
Console::log(''.Console::tableRowSeparator($rowHeaders, 'darkgray')); |
114
|
|
|
Console::log(''.Console::tableRow($rowHeaders, 'darkgray')); |
115
|
|
|
Console::log(''.Console::tableRowSeparator($rowHeaders, 'darkgray')); |
116
|
|
|
Console::log(); |
117
|
|
|
|
118
|
|
|
foreach (getIndex() as $key => $value){ |
119
|
|
|
if (file_exists( __DIR__ . '/'. $value[2])) { |
120
|
|
|
Console::log(''. |
121
|
|
|
Console::TableRowStart(). |
122
|
|
|
Console::TableRowCell(Console::text($key,'lightgray'), 6, Console::ALIGN_CENTER). //no align => default is left |
123
|
|
|
Console::TableRowCell(Console::text($value[0], 'lightgray'), 15). |
124
|
|
|
Console::TableRowCell(Console::text($value[1], 'lightgray'), 55) |
125
|
|
|
); |
126
|
|
|
$i++; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
Console::log(''); |
130
|
|
|
Console::log(''.Console::tableRowSeparator($rowHeaders, 'darkgray')); |
131
|
|
|
Console::log(''); |
132
|
|
|
Console::log(' '. Console::text('Tips:', 'underlined', 'bold')); |
133
|
|
|
Console::log(' '. Console::text(' - At any time you can stop this program using [') .Console::text('Ctrl+C', 'green') .Console::text(']')); |
134
|
|
|
Console::log(''); |
135
|
|
|
// reset table separators to defaults |
136
|
|
|
Console::resetDefaults(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function askIndex() |
140
|
|
|
{ |
141
|
|
|
$base = Console::text(' kristuff/mishell-demo' , 'yellow'); |
142
|
|
|
$base .= Console::text('~$ ' , 'gray'); |
143
|
|
|
$selectedIndex = Console::askInt($base . Console::text(' Enter desired index then press [Enter] to run sample > ', 'white')); |
144
|
|
|
$index = getIndex(); |
145
|
|
|
|
146
|
|
|
switch($selectedIndex){ |
147
|
|
|
case 0: |
148
|
|
|
Console::log(); |
149
|
|
|
// Console::restoreWindow(); |
150
|
|
|
exit(); |
|
|
|
|
151
|
|
|
break; |
152
|
|
|
|
153
|
|
|
default: |
154
|
|
|
if (array_key_exists($selectedIndex, getIndex())) { |
|
|
|
|
155
|
|
|
|
156
|
|
|
$title = $index[$selectedIndex][0]; |
157
|
|
|
$fileName = $index[$selectedIndex][2]; |
158
|
|
|
$filePath = __DIR__ . '/'. $fileName; |
159
|
|
|
|
160
|
|
|
Console::clear(); |
161
|
|
|
printHeader(); |
162
|
|
|
printSampleHeader($selectedIndex, $title); |
163
|
|
|
|
164
|
|
|
Console::log($base . Console::text('Start running [', 'white') . |
165
|
|
|
Console::text( $title, 'lightcyan') . |
166
|
|
|
Console::text('] in file [', 'white') . |
167
|
|
|
Console::text( $fileName, 'lightcyan') . |
168
|
|
|
Console::text(']', 'white')); |
169
|
|
|
|
170
|
|
|
if (file_exists($filePath)){ |
171
|
|
|
Console::log(); |
172
|
|
|
include $filePath; |
173
|
|
|
Console::log(); |
174
|
|
|
Console::log($base . Console::text('End running [', 'white') . |
175
|
|
|
Console::text( $title, 'lightcyan') . |
176
|
|
|
Console::text(']', 'white')); |
177
|
|
|
|
178
|
|
|
$response = Console::ask($base . Console::text('Do you want to see the code that has been executed? (type y/Y to see the code) > ', 'white')); |
179
|
|
|
if (strtoupper($response) === 'Y') { |
180
|
|
|
Console::log($base . Console::text('The code in file [', 'white') . |
181
|
|
|
Console::text( $fileName, 'lightcyan') . |
182
|
|
|
Console::text('] is:', 'white')); |
183
|
|
|
Console::log(); |
184
|
|
|
|
185
|
|
|
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
186
|
|
|
$count = 1; |
187
|
|
|
|
188
|
|
|
Console::log(Console::pad(' ', 100, '-'), 'darkgray'); |
189
|
|
|
foreach($lines as $line){ |
190
|
|
|
$codeLine = rtrim($line); |
191
|
|
|
$isComment = substr(ltrim($codeLine), 0, 2) === '//'; |
192
|
|
|
$isPhp = substr(ltrim($codeLine), 0, 5) === '<?php' || substr(ltrim($codeLine), 0, 2) === '?>'; |
193
|
|
|
$color = $isPhp ? 'blue' : ($isComment ? 'green' : 'lightgray'); |
194
|
|
|
Console::log(' '. Console::text($codeLine, $color)); |
195
|
|
|
$count++; |
196
|
|
|
} |
197
|
|
|
Console::log(Console::pad(' ', 100, '-'), 'darkgray'); |
198
|
|
|
Console::log(); |
199
|
|
|
} |
200
|
|
|
} else { |
201
|
|
|
Console::log($base . Console::text('Error' , 'red')); |
202
|
|
|
Console::log($base . Console::text(' => File missing [' . $fileName . ']' , 'red')); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
} else { |
206
|
|
|
Console::log($base . Console::text('Error:', 'red')); |
207
|
|
|
Console::log($base . Console::text('=> the value you entered is not a valid index number.', 'red')); |
208
|
|
|
askIndex(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
Console::ask($base . Console::text('Press [Enter] to go back to index > ', 'white')); |
212
|
|
|
goIndex(); |
213
|
|
|
} |
214
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.