1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ _ _ _ |
5
|
|
|
* _ __ (_)__| |_ ___| | | |
6
|
|
|
* | ' \| (_-< ' \/ -_) | | |
7
|
|
|
* |_|_|_|_/__/_||_\___|_|_| |
8
|
|
|
* |
9
|
|
|
* This file is part of Kristuff\Mishell. |
10
|
|
|
* (c) Kr1s7uff For the full copyright and license information, |
11
|
|
|
* please view the LICENSE file that was distributed with this |
12
|
|
|
* source code. |
13
|
|
|
* |
14
|
|
|
* @version 1.6.2 |
15
|
|
|
* @copyright 2017-2024 Kr157uff |
16
|
|
|
*/ |
17
|
|
|
namespace Kristuff\Mishell; |
18
|
|
|
|
19
|
|
|
abstract class ShellPrinter |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* EOF constant |
23
|
|
|
* |
24
|
|
|
* @access protected |
25
|
|
|
* @static var |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected static $EOF = "\n"; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get whether the current platform is Windows or not. |
32
|
|
|
* |
33
|
|
|
* @access protected |
34
|
|
|
* @static |
35
|
|
|
* |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
protected static function isWin() |
39
|
|
|
{ |
40
|
|
|
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the number of columns in terminal |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* @static |
48
|
|
|
* |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
public static function getColumns(): int |
52
|
|
|
{ |
53
|
|
|
if (!self::isWin() ){ |
54
|
|
|
return (int) shell_exec('tput cols'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return -1; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the number of lines in terminal |
62
|
|
|
* |
63
|
|
|
* @access public |
64
|
|
|
* @static |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public static function getLines() |
69
|
|
|
{ |
70
|
|
|
if (!self::isWin() ){ |
71
|
|
|
return (int) shell_exec('tput lines'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Switch to new window |
77
|
|
|
* |
78
|
|
|
* @access public |
79
|
|
|
* @static |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public static function newWindow() |
84
|
|
|
{ |
85
|
|
|
if (!self::isWin() ){ |
86
|
|
|
system('tput smcup'); |
87
|
|
|
self::clear(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Restore primary window |
93
|
|
|
* |
94
|
|
|
* @access public |
95
|
|
|
* @static |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public static function restoreWindow() |
100
|
|
|
{ |
101
|
|
|
if (!self::isWin() ){ |
102
|
|
|
system('tput rmcup'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Hide user input in console |
108
|
|
|
* |
109
|
|
|
* @access public |
110
|
|
|
* @static |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public static function hideInput() |
115
|
|
|
{ |
116
|
|
|
if (!self::isWin() ){ |
117
|
|
|
system('stty -echo'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Restore user input in console |
123
|
|
|
* |
124
|
|
|
* @access public |
125
|
|
|
* @static |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public static function restoreInput() |
130
|
|
|
{ |
131
|
|
|
if (!self::isWin()){ |
132
|
|
|
system('stty echo'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Clear the console |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
* @static |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public static function clear() |
145
|
|
|
{ |
146
|
|
|
// use 'cls' for Windows users or 'clear' for Linux users : |
147
|
|
|
system(self::isWin() ? 'cls' : 'clear'); |
148
|
|
|
} |
149
|
|
|
} |