Changeset View
Changeset View
Standalone View
Standalone View
src/console/format.php
| Show First 20 Lines • Show All 177 Lines • ▼ Show 20 Lines | |||||
| /** | /** | ||||
| * Determine the width of the terminal, if possible. Returns `null` on failure. | * Determine the width of the terminal, if possible. Returns `null` on failure. | ||||
| * | * | ||||
| * @return int|null Terminal width in characters, or null on failure. | * @return int|null Terminal width in characters, or null on failure. | ||||
| */ | */ | ||||
| function phutil_console_get_terminal_width() { | function phutil_console_get_terminal_width() { | ||||
| static $width; | |||||
| if ($width === null) { | |||||
| if (phutil_is_windows()) { | if (phutil_is_windows()) { | ||||
| // TODO: Figure out how to get this working in Windows. | // TODO: Figure out how to get this working in Windows. | ||||
| return null; | return null; | ||||
| } | } | ||||
| $tmp = new TempFile(); | $tmp = new TempFile(); | ||||
| // NOTE: We can't just execute this because it won't be connected to a TTY | // NOTE: We can't just execute this because it won't be connected to a TTY | ||||
| // if we do. | // if we do. | ||||
| $err = phutil_passthru('tput cols > %s', $tmp); | $err = phutil_passthru('tput cols > %s', $tmp); | ||||
| if ($err) { | if ($err) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| try { | try { | ||||
| $cols = Filesystem::readFile($tmp); | $cols = Filesystem::readFile($tmp); | ||||
| } catch (FilesystemException $ex) { | } catch (FilesystemException $ex) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| $cols = (int)$cols; | $width = (int)$cols; | ||||
| if (!$cols) { | if (!$width) { | ||||
| return null; | $width = null; | ||||
| } | |||||
| } | } | ||||
| return $cols; | return $width; | ||||
| } | } | ||||