Passed
Pull Request — master (#12)
by Kris
05:47
created

ShellPrinter::getColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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