Created
April 11, 2012 17:00
-
-
Save oranj/2360540 to your computer and use it in GitHub Desktop.
Stream Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Stream { | |
private static $stdin_handle = false; | |
private static $stdout_handle = false; | |
static function in() { | |
if (self::$stdin_handle === false && PHP_SAPI === 'cli') { | |
self::$stdin_handle = fopen("php://stdin", "r"); | |
} | |
if (self::$stdin_handle !== false) { | |
return trim(fgets(STDIN)); | |
} | |
} | |
static function clear() { | |
system('clear'); | |
} | |
static function out() { | |
$formatted_text = call_user_func_array('sprintf', func_get_args()); | |
if (self::$stdout_handle === false && PHP_SAPI === 'cli') { | |
self::$stdout_handle = fopen("php://stdout", "r"); | |
} | |
if (self::$stdout_handle !== false) { | |
return fputs(self::$stdout_handle, $formatted_text); | |
} | |
} | |
static function prompt() { | |
$formatted_text = call_user_func_array('sprintf', func_get_args()); | |
return self::prompt($text); | |
} | |
static function in_quiet() { | |
$command = "/usr/bin/env bash -c 'echo OK'"; | |
if (rtrim(shell_exec($command)) !== 'OK') { | |
trigger_error("Can't invoke bash"); | |
return; | |
} | |
$command = "/usr/bin/env bash -c 'read -s -p \"" | |
. "\" mypassword && echo \$mypassword'"; | |
$output = rtrim(shell_exec($command)); | |
self::out("\n"); | |
return $output; | |
} | |
static function prompt_quiet() { | |
$formatted_text = call_user_func_array('sprintf', func_get_args()); | |
return self::prompt_noecho($text); | |
} | |
static function get_tty_width() { | |
$output = exec('tput cols'); | |
return $output; | |
} | |
static function beep($int_beeps = 1) { | |
for ($i = 0; $i < $int_beeps; $i++) { | |
$string_beeps .= "\x07"; | |
} | |
isset ($_SERVER['SERVER_PROTOCOL']) ? false : print $string_beeps; | |
} | |
static function strip_cli($text) { | |
$out = preg_replace('/\033\[[0-9;]+m/', '', $text); | |
return $out; | |
} | |
static function text_fill($text, $fill = '=', $align = 'left') { | |
$fill_width = strlen(self::strip_cli($fill)); | |
$full_width = self::get_tty_width(); | |
$ratio = ($fill_width / strlen($fill)); | |
$close_fill = ($ratio != 1)?"\033[0m":''; | |
$text = ($align == 'left'?($text.' '): | |
($align == 'right'?(' '.$text):(' '.$text.' '))); | |
$remainder_width = $full_width - strlen(self::strip_cli($text)); | |
switch ($align) { | |
case 'left': | |
$left_width = 0; | |
break; | |
case 'right': | |
$left_width = $remainder_width; | |
break; | |
case 'center': | |
default: | |
$left_width = ceil($remainder_width / 2); | |
break; | |
} | |
$left_pad = str_repeat($fill, $left_width / $fill_width).$close_fill; | |
$right_pad = str_repeat($fill, ($remainder_width - $left_width) / $fill_width); | |
$str = $left_pad.$text.$right_pad. $close_fill; | |
return $str."\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment