Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
patch: Add --json flag
Erik Kundt committed 2 years ago
commit a3c67ff95d252d017930af0a3a9f531cac34d060
parent 30dfa8acb2a1270cbd3fc16a474f14daea11664c
4 files changed +54 -10
modified Cargo.toml
@@ -27,7 +27,3 @@ thiserror = { version = "1" }
tuirealm = { version = "1.9.0", default-features = false, features = [ "with-termion" ] }
tui-realm-stdlib = { version = "1.2.0", default-features = false, features = [ "with-termion" ] }
tui-realm-textarea = { git = "https://github.com/erak/tui-realm-textarea.git", default-features = false, features = [ "with-termion", "clipboard" ] }
-

-
# tuirealm = { version = "1.8.0", default-features = false, features = [ "with-crossterm" ] }
-
# tui-realm-stdlib = { version = "1.2.0", default-features = false, features = [ "with-crossterm" ] }
-
# tui-realm-textarea = { git = "https://github.com/erak/tui-realm-textarea.git", default-features = false, features = [ "with-crossterm", "clipboard" ] }
modified bin/commands/patch.rs
@@ -34,12 +34,14 @@ Select options

Other options

+
    --json              Output is JSON (default: false)
    --help              Print help
"#,
};

pub struct Options {
    op: Operation,
+
    json: bool,
}

pub enum Operation {
@@ -68,6 +70,7 @@ impl Args for Options {

        let mut parser = lexopt::Parser::from_args(args);
        let mut op: Option<OperationName> = None;
+
        let mut json = false;
        let mut select_opts: Option<SelectOptions> = None;

        #[allow(clippy::never_loop)]
@@ -76,6 +79,9 @@ impl Args for Options {
                Long("help") | Short('h') => {
                    return Err(Error::Help.into());
                }
+
                Long("json") | Short('j') => {
+
                    json = true;
+
                }

                // Select options.
                Long("operation") | Short('o') if op == Some(OperationName::Select) => {
@@ -104,7 +110,7 @@ impl Args for Options {
                opts: select_opts.unwrap_or_default(),
            },
        };
-
        Ok((Options { op }, vec![]))
+
        Ok((Options { op, json }, vec![]))
    }
}

@@ -121,11 +127,17 @@ pub fn run(options: Options, _ctx: impl terminal::Context) -> anyhow::Result<()>
            let mut app = select::App::new(context, opts.subject);
            let output = Window::default().run(&mut app, 1000 / FPS)?;

-
            let json = output
-
                .map(|output| serde_json::to_string(&output).unwrap_or_default())
-
                .unwrap_or_default();
-

-
            eprint!("{json}");
+
            let output = if options.json {
+
                output
+
                    .map(|o| serde_json::to_string(&o).unwrap_or_default())
+
                    .unwrap_or_default()
+
            } else {
+
                output
+
                    .map(|o| format!("rad patch {}", o))
+
                    .unwrap_or_default()
+
            };
+

+
            eprint!("{output}");
        }
    }

modified bin/commands/patch/select.rs
@@ -5,6 +5,7 @@ mod page;
#[path = "select/ui.rs"]
mod ui;

+
use std::fmt::Display;
use std::hash::Hash;

use anyhow::Result;
@@ -34,6 +35,12 @@ impl From<radicle::cob::patch::PatchId> for PatchId {
    }
}

+
impl Display for PatchId {
+
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+
        write!(f, "{}", self.0)
+
    }
+
}
+

impl Serialize for PatchId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
@@ -63,6 +70,22 @@ pub enum PatchOperation {
    Checkout,
}

+
impl Display for PatchOperation {
+
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+
        match self {
+
            PatchOperation::Show => {
+
                write!(f, "show")
+
            }
+
            PatchOperation::Edit => {
+
                write!(f, "edit")
+
            }
+
            PatchOperation::Checkout => {
+
                write!(f, "checkout")
+
            }
+
        }
+
    }
+
}
+

/// The application's output that depends on the application's
/// subject.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
@@ -71,6 +94,15 @@ pub struct Output {
    id: PatchId,
}

+
impl Display for Output {
+
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+
        match &self.operation {
+
            Some(op) => write!(f, "{} {}", op, self.id),
+
            None => write!(f, "{}", self.id),
+
        }
+
    }
+
}
+

#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub enum ListCid {
    Header,
added out.txt
@@ -0,0 +1,4 @@
+
   Compiling radicle-tui v0.1.0 (/home/erikli/projects/radicle/dev/radicle-tui)
+
    Finished dev [unoptimized + debuginfo] target(s) in 4.38s
+
     Running `target/debug/rad-tui patch select --operation`
+
rad patch checkout 534e768883ac507f3ec1f4f9da67623486cd0657