cli: completion: derive clap::ValueEnum for ColorChoice
Some checks are pending
binaries / Build binary artifacts (push) Waiting to run

This enables autocompletion for `jj --color <TAB>`
This commit is contained in:
Jakob Hellermann 2025-01-25 18:00:17 +01:00
parent a4fa4b6dbd
commit 3293a1aa97
5 changed files with 14 additions and 22 deletions

View file

@ -3128,7 +3128,7 @@ pub struct GlobalArgs {
#[derive(clap::Args, Clone, Debug)] #[derive(clap::Args, Clone, Debug)]
pub struct EarlyArgs { pub struct EarlyArgs {
/// When to colorize output (always, never, debug, auto) /// When to colorize output
#[arg(long, value_name = "WHEN", global = true)] #[arg(long, value_name = "WHEN", global = true)]
pub color: Option<ColorChoice>, pub color: Option<ColorChoice>,
/// Silence non-primary command output /// Silence non-primary command output

View file

@ -27,7 +27,6 @@ use std::mem;
use std::process::Child; use std::process::Child;
use std::process::ChildStdin; use std::process::ChildStdin;
use std::process::Stdio; use std::process::Stdio;
use std::str::FromStr;
use std::thread; use std::thread;
use std::thread::JoinHandle; use std::thread::JoinHandle;
@ -35,8 +34,6 @@ use itertools::Itertools as _;
use jj_lib::config::ConfigGetError; use jj_lib::config::ConfigGetError;
use jj_lib::config::StackedConfig; use jj_lib::config::StackedConfig;
use os_pipe::PipeWriter; use os_pipe::PipeWriter;
use serde::de::Deserialize as _;
use serde::de::IntoDeserializer as _;
use tracing::instrument; use tracing::instrument;
use crate::command_error::CommandError; use crate::command_error::CommandError;
@ -206,7 +203,7 @@ pub struct Ui {
output: UiOutput, output: UiOutput,
} }
#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize)] #[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, clap::ValueEnum)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub enum ColorChoice { pub enum ColorChoice {
Always, Always,
@ -215,17 +212,6 @@ pub enum ColorChoice {
Auto, Auto,
} }
impl FromStr for ColorChoice {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// serde::de::value::Error is Box<str> wrapper. Map it to String to hide
// the implementation detail.
Self::deserialize(s.into_deserializer())
.map_err(|err: serde::de::value::Error| err.to_string())
}
}
impl fmt::Display for ColorChoice { impl fmt::Display for ColorChoice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self { let s = match self {

View file

@ -1,6 +1,7 @@
--- ---
source: cli/tests/test_generate_md_cli_help.rs source: cli/tests/test_generate_md_cli_help.rs
description: "AUTO-GENERATED FILE, DO NOT EDIT. This cli reference is generated by a test as an `insta` snapshot. MkDocs includes this snapshot from docs/cli-reference.md." description: "AUTO-GENERATED FILE, DO NOT EDIT. This cli reference is generated by a test as an `insta` snapshot. MkDocs includes this snapshot from docs/cli-reference.md."
snapshot_kind: text
--- ---
<!-- BEGIN MARKDOWN--> <!-- BEGIN MARKDOWN-->
@ -187,7 +188,10 @@ To get started, see the tutorial at https://jj-vcs.github.io/jj/latest/tutorial/
It is possible to run mutating commands when loading the repo at an earlier operation. Doing that is equivalent to having run concurrent commands starting at the earlier operation. There's rarely a reason to do that, but it is possible. It is possible to run mutating commands when loading the repo at an earlier operation. Doing that is equivalent to having run concurrent commands starting at the earlier operation. There's rarely a reason to do that, but it is possible.
* `--debug` — Enable debug logging * `--debug` — Enable debug logging
* `--color <WHEN>` — When to colorize output (always, never, debug, auto) * `--color <WHEN>` — When to colorize output
Possible values: `always`, `never`, `debug`, `auto`
* `--quiet` — Silence non-primary command output * `--quiet` — Silence non-primary command output
For example, `jj file list` will still list files, but it won't tell you if the working copy was snapshotted or if descendants were rebased. For example, `jj file list` will still list files, but it won't tell you if the working copy was snapshotted or if descendants were rebased.

View file

@ -79,7 +79,7 @@ fn test_bookmark_names() {
--ignore-immutable Allow rewriting immutable commits --ignore-immutable Allow rewriting immutable commits
--at-operation Operation to load the repo at --at-operation Operation to load the repo at
--debug Enable debug logging --debug Enable debug logging
--color When to colorize output (always, never, debug, auto) --color When to colorize output
--quiet Silence non-primary command output --quiet Silence non-primary command output
--no-pager Disable the pager --no-pager Disable the pager
--config Additional configuration options (can be repeated) --config Additional configuration options (can be repeated)

View file

@ -458,7 +458,8 @@ fn test_color_config() {
// Invalid --color // Invalid --color
let stderr = test_env.jj_cmd_cli_error(&repo_path, &["log", "--color=foo"]); let stderr = test_env.jj_cmd_cli_error(&repo_path, &["log", "--color=foo"]);
insta::assert_snapshot!(stderr, @r" insta::assert_snapshot!(stderr, @r"
error: invalid value 'foo' for '--color <WHEN>': unknown variant `foo`, expected one of `always`, `never`, `debug`, `auto` error: invalid value 'foo' for '--color <WHEN>'
[possible values: always, never, debug, auto]
For more information, try '--help'. For more information, try '--help'.
"); ");
@ -935,7 +936,7 @@ fn test_help() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["diffedit", "-h"]); let stdout = test_env.jj_cmd_success(test_env.env_root(), &["diffedit", "-h"]);
insta::assert_snapshot!(stdout, @r###" insta::assert_snapshot!(stdout, @r"
Touch up the content changes in a revision with a diff editor Touch up the content changes in a revision with a diff editor
Usage: jj diffedit [OPTIONS] Usage: jj diffedit [OPTIONS]
@ -954,12 +955,13 @@ fn test_help() {
--ignore-immutable Allow rewriting immutable commits --ignore-immutable Allow rewriting immutable commits
--at-operation <AT_OPERATION> Operation to load the repo at [aliases: at-op] --at-operation <AT_OPERATION> Operation to load the repo at [aliases: at-op]
--debug Enable debug logging --debug Enable debug logging
--color <WHEN> When to colorize output (always, never, debug, auto) --color <WHEN> When to colorize output [possible values: always, never, debug,
auto]
--quiet Silence non-primary command output --quiet Silence non-primary command output
--no-pager Disable the pager --no-pager Disable the pager
--config <NAME=VALUE> Additional configuration options (can be repeated) --config <NAME=VALUE> Additional configuration options (can be repeated)
--config-file <PATH> Additional configuration files (can be repeated) --config-file <PATH> Additional configuration files (can be repeated)
"###); ");
} }
#[test] #[test]