ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: rename FullCommandArgs to CommandNameAndArgs

Per discussion in #1198.
This commit is contained in:
Yuya Nishihara 2023-02-06 13:13:44 +09:00
parent eb95b31b78
commit 7bb7d2bd35
4 changed files with 31 additions and 31 deletions

View file

@ -56,7 +56,7 @@ use jujutsu_lib::{dag_walk, file_util, git, revset};
use thiserror::Error;
use tracing_subscriber::prelude::*;
use crate::config::{AnnotatedValue, FullCommandArgs, LayeredConfigs};
use crate::config::{AnnotatedValue, CommandNameAndArgs, LayeredConfigs};
use crate::formatter::{Formatter, PlainTextFormatter};
use crate::merge_tools::{ConflictResolveError, DiffEditError};
use crate::template_parser::{self, TemplateParseError};
@ -1557,7 +1557,7 @@ pub fn serialize_config_value(value: &config::Value) -> String {
}
pub fn run_ui_editor(settings: &UserSettings, edit_path: &PathBuf) -> Result<(), CommandError> {
let editor: FullCommandArgs = settings
let editor: CommandNameAndArgs = settings
.config()
.get("ui.editor")
.unwrap_or_else(|_| "pico".into());

View file

@ -325,23 +325,23 @@ fn read_config_path(config_path: &Path) -> Result<config::Config, config::Config
/// Command name and arguments specified by config.
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Deserialize)]
#[serde(untagged)]
pub enum FullCommandArgs {
pub enum CommandNameAndArgs {
String(String),
Vec(NonEmptyCommandArgsVec),
}
impl FullCommandArgs {
impl CommandNameAndArgs {
/// Returns command name and arguments.
///
/// The command name may be an empty string (as well as each argument.)
pub fn split_name_and_args(&self) -> (Cow<str>, Cow<[String]>) {
match self {
FullCommandArgs::String(s) => {
CommandNameAndArgs::String(s) => {
// Handle things like `EDITOR=emacs -nw` (TODO: parse shell escapes)
let mut args = s.split(' ').map(|s| s.to_owned());
(args.next().unwrap().into(), args.collect())
}
FullCommandArgs::Vec(NonEmptyCommandArgsVec(a)) => {
CommandNameAndArgs::Vec(NonEmptyCommandArgsVec(a)) => {
(Cow::Borrowed(&a[0]), Cow::Borrowed(&a[1..]))
}
}
@ -356,18 +356,18 @@ impl FullCommandArgs {
}
}
impl<T: AsRef<str> + ?Sized> From<&T> for FullCommandArgs {
impl<T: AsRef<str> + ?Sized> From<&T> for CommandNameAndArgs {
fn from(s: &T) -> Self {
FullCommandArgs::String(s.as_ref().to_owned())
CommandNameAndArgs::String(s.as_ref().to_owned())
}
}
impl fmt::Display for FullCommandArgs {
impl fmt::Display for CommandNameAndArgs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FullCommandArgs::String(s) => write!(f, "{s}"),
CommandNameAndArgs::String(s) => write!(f, "{s}"),
// TODO: format with shell escapes
FullCommandArgs::Vec(a) => write!(f, "{}", a.0.join(" ")),
CommandNameAndArgs::Vec(a) => write!(f, "{}", a.0.join(" ")),
}
}
}
@ -408,18 +408,18 @@ mod tests {
.build()
.unwrap();
assert!(config.get::<FullCommandArgs>("empty_array").is_err());
assert!(config.get::<CommandNameAndArgs>("empty_array").is_err());
let command_args: FullCommandArgs = config.get("empty_string").unwrap();
assert_eq!(command_args, FullCommandArgs::String("".to_owned()));
let command_args: CommandNameAndArgs = config.get("empty_string").unwrap();
assert_eq!(command_args, CommandNameAndArgs::String("".to_owned()));
let (name, args) = command_args.split_name_and_args();
assert_eq!(name, "");
assert!(args.is_empty());
let command_args: FullCommandArgs = config.get("array").unwrap();
let command_args: CommandNameAndArgs = config.get("array").unwrap();
assert_eq!(
command_args,
FullCommandArgs::Vec(NonEmptyCommandArgsVec(
CommandNameAndArgs::Vec(NonEmptyCommandArgsVec(
["emacs", "-nw",].map(|s| s.to_owned()).to_vec()
))
);
@ -427,10 +427,10 @@ mod tests {
assert_eq!(name, "emacs");
assert_eq!(args, ["-nw"].as_ref());
let command_args: FullCommandArgs = config.get("string").unwrap();
let command_args: CommandNameAndArgs = config.get("string").unwrap();
assert_eq!(
command_args,
FullCommandArgs::String("emacs -nw".to_owned())
CommandNameAndArgs::String("emacs -nw".to_owned())
);
let (name, args) = command_args.split_name_and_args();
assert_eq!(name, "emacs");

View file

@ -35,7 +35,7 @@ use jujutsu_lib::tree::Tree;
use jujutsu_lib::working_copy::{CheckoutError, SnapshotError, TreeState};
use thiserror::Error;
use crate::config::FullCommandArgs;
use crate::config::CommandNameAndArgs;
use crate::ui::Ui;
#[derive(Debug, Error)]
@ -408,7 +408,7 @@ struct MergeTool {
}
impl MergeTool {
pub fn with_edit_args(command_args: &FullCommandArgs) -> Self {
pub fn with_edit_args(command_args: &CommandNameAndArgs) -> Self {
let (name, args) = command_args.split_name_and_args();
MergeTool {
program: name.into_owned(),
@ -418,7 +418,7 @@ impl MergeTool {
}
}
pub fn with_merge_args(command_args: &FullCommandArgs) -> Self {
pub fn with_merge_args(command_args: &CommandNameAndArgs) -> Self {
let (name, args) = command_args.split_name_and_args();
MergeTool {
program: name.into_owned(),
@ -455,8 +455,8 @@ fn get_diff_editor_from_settings(
) -> Result<MergeTool, ExternalToolError> {
let args = editor_args_from_settings(ui, settings, "ui.diff-editor")?;
let maybe_editor = match &args {
FullCommandArgs::String(name) => get_tool_config(settings, name)?,
FullCommandArgs::Vec(_) => None,
CommandNameAndArgs::String(name) => get_tool_config(settings, name)?,
CommandNameAndArgs::Vec(_) => None,
};
Ok(maybe_editor.unwrap_or_else(|| MergeTool::with_edit_args(&args)))
}
@ -467,8 +467,8 @@ fn get_merge_tool_from_settings(
) -> Result<MergeTool, ExternalToolError> {
let args = editor_args_from_settings(ui, settings, "ui.merge-editor")?;
let maybe_editor = match &args {
FullCommandArgs::String(name) => get_tool_config(settings, name)?,
FullCommandArgs::Vec(_) => None,
CommandNameAndArgs::String(name) => get_tool_config(settings, name)?,
CommandNameAndArgs::Vec(_) => None,
};
let editor = maybe_editor.unwrap_or_else(|| MergeTool::with_merge_args(&args));
if editor.merge_args.is_empty() {
@ -485,10 +485,10 @@ fn editor_args_from_settings(
ui: &mut Ui,
settings: &UserSettings,
key: &str,
) -> Result<FullCommandArgs, ExternalToolError> {
) -> Result<CommandNameAndArgs, ExternalToolError> {
// TODO: Make this configuration have a table of possible editors and detect the
// best one here.
match settings.config().get::<FullCommandArgs>(key) {
match settings.config().get::<CommandNameAndArgs>(key) {
Ok(args) => Ok(args),
Err(config::ConfigError::NotFound(_)) => {
let default_editor = "meld";

View file

@ -19,12 +19,12 @@ use std::{fmt, io, mem};
use crossterm::tty::IsTty;
use crate::config::FullCommandArgs;
use crate::config::CommandNameAndArgs;
use crate::formatter::{Formatter, FormatterFactory, LabeledWriter};
pub struct Ui {
color: bool,
pager_cmd: FullCommandArgs,
pager_cmd: CommandNameAndArgs,
paginate: PaginationChoice,
progress_indicator: bool,
formatter_factory: FormatterFactory,
@ -100,7 +100,7 @@ impl Default for PaginationChoice {
}
}
fn pager_setting(config: &config::Config) -> FullCommandArgs {
fn pager_setting(config: &config::Config) -> CommandNameAndArgs {
config
.get("ui.pager")
.unwrap_or_else(|_| "less -FRX".into())
@ -320,7 +320,7 @@ impl UiOutput {
}
}
fn new_paged(pager_cmd: &FullCommandArgs) -> io::Result<UiOutput> {
fn new_paged(pager_cmd: &CommandNameAndArgs) -> io::Result<UiOutput> {
let mut child = pager_cmd.to_command().stdin(Stdio::piped()).spawn()?;
let child_stdin = child.stdin.take().unwrap();
Ok(UiOutput::Paged { child, child_stdin })