Skip to content

Commit 3f1ed15

Browse files
committed
move files
1 parent 5beff3f commit 3f1ed15

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

example/cli.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
require 'classes/jblond/cli/Cli.php';
3+
require 'classes/jblond/cli/CliColors.php';
4+
$cli = new \jblond\cli\Cli();
5+
$colors = new \jblond\cli\CliColors();
6+
$string = $colors->getColoredString('This is a test','red','black');
7+
$cli->output($string);
8+
$cli->error($string);
9+
echo $cli->input('Hello world: ',array('Hello','world'));
10+
echo $cli->input('Test2: ', 'test');

src/jblond/cli/Cli.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace jblond\cli;
3+
4+
5+
/**
6+
* Class cli
7+
* @package jblond\helper
8+
*/
9+
class Cli {
10+
11+
/**
12+
* @param string $prompt
13+
* @param array|string $validInputs
14+
* @param string $default
15+
* @return string
16+
*/
17+
public function input($prompt, $validInputs, $default = ''){
18+
echo $prompt;
19+
$input = trim(fgets(fopen('php://stdin', 'r')));
20+
while(
21+
!isset($input) ||
22+
(
23+
is_array($validInputs) &&
24+
!in_array($input, $validInputs)
25+
) ||
26+
(
27+
$validInputs == 'is_file' &&
28+
!is_file($input)
29+
)
30+
){
31+
echo $prompt;
32+
$input = trim(fgets(fopen('php://stdin', 'r')));
33+
if(empty($input) && !empty($default)) {
34+
$input = $default;
35+
}
36+
}
37+
return $input;
38+
}
39+
40+
/**
41+
* @param string $output
42+
*/
43+
public function output($output){
44+
$out = fopen('php://output', 'w');
45+
fputs($out, $output);
46+
fclose($out);
47+
}
48+
49+
/**
50+
* @param string $string
51+
*/
52+
public function error($string){
53+
$stdError = fopen('php://stderr', 'w');
54+
fwrite($stdError, $string);
55+
fclose($stdError);
56+
}
57+
}

src/jblond/cli/CliColors.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
namespace jblond\cli;
3+
4+
/**
5+
* Class CliColors
6+
*
7+
* Works on bash
8+
* @package jblond\helper
9+
*/
10+
class CliColors {
11+
12+
/**
13+
* @var array
14+
*/
15+
private $foregroundColors = array(
16+
'black' => '0;30',
17+
'dark_gray' => '1;30',
18+
'blue' => '0,34',
19+
'light_blue' => '1;34',
20+
'green' => '0;32',
21+
'light_green' => '1;32',
22+
'cyan' => '0;36',
23+
'light_cyan' => '1;36',
24+
'red' => '0;31',
25+
'light_red' => '1;31',
26+
'purple' => '0;35',
27+
'light_purple' => '1;35',
28+
'brown' => '0;33',
29+
'yellow' => '1;33',
30+
'light_gray' => '0;37',
31+
'white' => '1;37'
32+
);
33+
34+
/**
35+
* @var array
36+
*/
37+
private $backgroundColors = array(
38+
'black' => '40',
39+
'red' => '41',
40+
'green' => '42',
41+
'yellow' => '43',
42+
'blue' => '44',
43+
'magenta' => '45',
44+
'cyan' => '46',
45+
'light_gray' => '47'
46+
);
47+
48+
/**
49+
* @param string $string
50+
* @param null|string $foregroundColor
51+
* @param null|string $backgroundColor
52+
* @return string
53+
*/
54+
public function getColoredString($string, $foregroundColor = null, $backgroundColor = null){
55+
$coloredString = '';
56+
57+
if(isset($this->foregroundColors["$foregroundColor"])){
58+
$coloredString .= "\033[" . $this->foregroundColors[$foregroundColor] . "m";
59+
}
60+
61+
if(isset($this->backgroundColors["$backgroundColor"])){
62+
$coloredString .= "\033[" . $this->backgroundColors[$backgroundColor] . "m";
63+
}
64+
65+
$coloredString .= $string . "\033[0m";
66+
return $coloredString;
67+
}
68+
69+
/**
70+
* Returns all foreground color names
71+
*
72+
* @return array
73+
*/
74+
public function getForegroundColors() {
75+
return array_keys($this->foregroundColors);
76+
}
77+
78+
/**
79+
* Returns all background color names
80+
*
81+
* @return array
82+
*/
83+
public function getBackgroundColors() {
84+
return array_keys($this->backgroundColors);
85+
}
86+
}

0 commit comments

Comments
 (0)