Changeset View
Changeset View
Standalone View
Standalone View
src/toolset/ArcanistPrompt.php
| Show First 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | try { | ||||
| // TOOLSETS: Clean this up to provide more details to the user about how | // TOOLSETS: Clean this up to provide more details to the user about how | ||||
| // they can configure prompts to be answered. | // they can configure prompts to be answered. | ||||
| // Throw after echoing the prompt so the user has some idea what happened. | // Throw after echoing the prompt so the user has some idea what happened. | ||||
| echo $query."\n"; | echo $query."\n"; | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| // NOTE: We're making stdin nonblocking so that we can respond to signals | |||||
| // immediately. If we don't, and you ^C during a prompt, the program does | |||||
| // not handle the signal until fgets() returns. | |||||
| $stdin = fopen('php://stdin', 'r'); | $stdin = fopen('php://stdin', 'r'); | ||||
| if (!$stdin) { | if (!$stdin) { | ||||
| throw new Exception(pht('Failed to open stdin for reading.')); | throw new Exception(pht('Failed to open stdin for reading.')); | ||||
| } | } | ||||
| // NOTE: We're making stdin nonblocking so that we can respond to signals | |||||
| // immediately. If we don't, and you ^C during a prompt, the program does | |||||
| // not handle the signal until fgets() returns. | |||||
| // On Windows, we skip this because stdin can not be made nonblocking. | |||||
| if (!phutil_is_windows()) { | |||||
| $ok = stream_set_blocking($stdin, false); | $ok = stream_set_blocking($stdin, false); | ||||
| if (!$ok) { | if (!$ok) { | ||||
| throw new Exception(pht('Unable to set stdin nonblocking.')); | throw new Exception(pht('Unable to set stdin nonblocking.')); | ||||
| } | } | ||||
| } | |||||
| echo "\n"; | echo "\n"; | ||||
| $result = null; | $result = null; | ||||
| $is_saved = false; | $is_saved = false; | ||||
| while (true) { | while (true) { | ||||
| if ($saved_response !== null) { | if ($saved_response !== null) { | ||||
| $is_saved = true; | $is_saved = true; | ||||
| $response = $saved_response; | $response = $saved_response; | ||||
| $saved_response = null; | $saved_response = null; | ||||
| } else { | } else { | ||||
| echo tsprintf( | echo tsprintf( | ||||
| '**<bg:cyan> %s </bg>** %s %s ', | '**<bg:cyan> %s </bg>** %s %s ', | ||||
| '>>>', | '>>>', | ||||
| $query, | $query, | ||||
| $options); | $options); | ||||
| while (true) { | |||||
| $is_saved = false; | $is_saved = false; | ||||
| if (phutil_is_windows()) { | |||||
| $response = fgets($stdin); | |||||
| } else { | |||||
| while (true) { | |||||
| $read = array($stdin); | $read = array($stdin); | ||||
| $write = array(); | $write = array(); | ||||
| $except = array(); | $except = array(); | ||||
| $ok = @stream_select($read, $write, $except, 1); | $ok = @stream_select($read, $write, $except, 1); | ||||
| if ($ok === false) { | if ($ok === false) { | ||||
| // NOTE: We may be interrupted by a system call, particularly if | // NOTE: We may be interrupted by a system call, particularly if | ||||
| // the window is resized while a prompt is shown and the terminal | // the window is resized while a prompt is shown and the terminal | ||||
| // sends SIGWINCH. | // sends SIGWINCH. | ||||
| // If we are, just continue below and try to read from stdin. If | // If we are, just continue below and try to read from stdin. If | ||||
| // we were interrupted, we should read nothing and continue | // we were interrupted, we should read nothing and continue | ||||
| // normally. If the pipe is broken, the read should fail. | // normally. If the pipe is broken, the read should fail. | ||||
| } | } | ||||
| $response = ''; | $response = ''; | ||||
| while (true) { | while (true) { | ||||
| $bytes = fread($stdin, 8192); | $bytes = fread($stdin, 8192); | ||||
| if ($bytes === false) { | if ($bytes === false) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht('fread() from stdin failed with an error.')); | pht('fread() from stdin failed with an error.')); | ||||
| } | } | ||||
| if (!strlen($bytes)) { | if (!strlen($bytes)) { | ||||
| break; | break; | ||||
| } | } | ||||
| $response .= $bytes; | $response .= $bytes; | ||||
| } | } | ||||
| if (!strlen($response)) { | if (!strlen($response)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| break; | break; | ||||
| } | } | ||||
| } | |||||
| $response = trim($response); | $response = trim($response); | ||||
| if (!strlen($response)) { | if (!strlen($response)) { | ||||
| $response = $default; | $response = $default; | ||||
| } | } | ||||
| } | } | ||||
| $save_scope = null; | $save_scope = null; | ||||
| ▲ Show 20 Lines • Show All 146 Lines • Show Last 20 Lines | |||||