Changeset View
Changeset View
Standalone View
Standalone View
src/console/PhutilInteractiveEditor.php
Show All 16 Lines | |||||
*/ | */ | ||||
final class PhutilInteractiveEditor extends Phobject { | final class PhutilInteractiveEditor extends Phobject { | ||||
private $name = ''; | private $name = ''; | ||||
private $content = ''; | private $content = ''; | ||||
private $offset = 0; | private $offset = 0; | ||||
private $preferred; | private $preferred; | ||||
private $fallback; | private $fallback; | ||||
private $taskMessage; | |||||
/* -( Creating a New Editor )---------------------------------------------- */ | /* -( Creating a New Editor )---------------------------------------------- */ | ||||
/** | /** | ||||
* Constructs an interactive editor, using the text of a document. | * Constructs an interactive editor, using the text of a document. | ||||
* | * | ||||
Show All 36 Lines | public function editInteractively() { | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
Filesystem::remove($tmp); | Filesystem::remove($tmp); | ||||
throw $ex; | throw $ex; | ||||
} | } | ||||
$editor = $this->getEditor(); | $editor = $this->getEditor(); | ||||
$offset = $this->getLineOffset(); | $offset = $this->getLineOffset(); | ||||
$binary = basename($editor); | |||||
// This message is primarily an assistance to users with GUI-based | |||||
// editors configured. Users with terminal-based editors won't have a | |||||
// chance to see this prior to the editor being launched. | |||||
echo tsprintf( | |||||
"%s\n", | |||||
pht('Launching editor "%s"...', $binary)); | |||||
$task_message = $this->getTaskMessage(); | |||||
if ($task_message !== null) { | |||||
echo tsprintf("%s\n", $task_message); | |||||
} | |||||
$err = $this->invokeEditor($editor, $path, $offset); | $err = $this->invokeEditor($editor, $path, $offset); | ||||
if ($err) { | if ($err) { | ||||
// See T13297. On macOS, "vi" and "vim" may exit with errors even though | // See T13297. On macOS, "vi" and "vim" may exit with errors even though | ||||
// the edit succeeded. If the binary is "vi" or "vim" and we get an exit | // the edit succeeded. If the binary is "vi" or "vim" and we get an exit | ||||
// code, we perform an additional test on the binary. | // code, we perform an additional test on the binary. | ||||
$vi_binaries = array( | $vi_binaries = array( | ||||
'vi' => true, | 'vi' => true, | ||||
'vim' => true, | 'vim' => true, | ||||
); | ); | ||||
$binary = basename($editor); | |||||
if (isset($vi_binaries[$binary])) { | if (isset($vi_binaries[$binary])) { | ||||
// This runs "Q" (an invalid command), then "q" (a valid command, | // This runs "Q" (an invalid command), then "q" (a valid command, | ||||
// meaning "quit"). Vim binaries with behavior that makes them poor | // meaning "quit"). Vim binaries with behavior that makes them poor | ||||
// interactive editors will exit "1". | // interactive editors will exit "1". | ||||
list($diagnostic_err) = exec_manual('%R +Q +q', $binary); | list($diagnostic_err) = exec_manual('%R +Q +q', $binary); | ||||
// If we get an error back, the binary is badly behaved. Ignore the | // If we get an error back, the binary is badly behaved. Ignore the | ||||
// original error and assume it's not meaningful, since it just | // original error and assume it's not meaningful, since it just | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | /* -( Configuring Options )------------------------------------------------- */ | ||||
* | * | ||||
* @task config | * @task config | ||||
*/ | */ | ||||
public function setPreferredEditor($editor) { | public function setPreferredEditor($editor) { | ||||
$this->preferred = $editor; | $this->preferred = $editor; | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* Set the message that identifies the task for which the editor is being | |||||
* launched, displayed to the user prior to it being launched. | |||||
* | |||||
* @param string The message to display to the user. | |||||
* @return $this | |||||
* | |||||
* @task config | |||||
*/ | |||||
public function setTaskMessage($task_message) { | |||||
$this->taskMessage = $task_message; | |||||
return $this; | |||||
} | |||||
/** | |||||
* Retrieve the current message that will display to the user just prior to | |||||
* invoking the editor. | |||||
* | |||||
* @return string The message that will display to the user, or null if no | |||||
* message will be displayed. | |||||
* | |||||
* @task config | |||||
*/ | |||||
public function getTaskMessage() { | |||||
return $this->taskMessage; | |||||
} | |||||
/** | /** | ||||
* Get the name of the editor program to use. The value of the environmental | * Get the name of the editor program to use. The value of the environmental | ||||
* variable $EDITOR will be used if available; otherwise, the `editor` binary | * variable $EDITOR will be used if available; otherwise, the `editor` binary | ||||
* if present; otherwise the best editor will be selected. | * if present; otherwise the best editor will be selected. | ||||
* | * | ||||
* @return string Command-line editing program. | * @return string Command-line editing program. | ||||
* | * | ||||
Show All 32 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0