Skip to main content

gitu/
cli.rs

1use std::io;
2use std::path::PathBuf;
3
4use clap::{CommandFactory, Parser, Subcommand};
5use clap_complete::Shell;
6
7#[derive(Default, Debug, Parser)]
8#[command(name = "gitu")]
9#[command(flatten_help = true)]
10pub struct Args {
11    #[command(subcommand)]
12    pub command: Option<Commands>,
13
14    /// Send keys on startup (eg: `gitu -k ll`).
15    ///     It is possible to send:
16    ///     - single char-keys: a, b, c, ...
17    ///     - special keys: <backspace>, <enter>, <up>, <tab>, <delete>, <esc>, ...
18    ///     - modifiers: <ctrl+a>, <ctrl+shift+alt+a>, <shift+delete>
19    #[clap(short, long, verbatim_doc_comment)]
20    pub keys: Option<String>,
21
22    /// Print one frame and exit. Useful for debugging.
23    #[clap(long, action)]
24    pub print: bool,
25
26    /// Read a patch on stdin and browse it, for use as git's pager:
27    ///     GIT_PAGER='gitu --pager' git show
28    #[clap(long, action, verbatim_doc_comment)]
29    pub pager: bool,
30
31    /// Enable logging to 'gitu.log'
32    #[clap(long, action)]
33    pub log: bool,
34
35    #[clap(long, action)]
36    /// Print version
37    pub version: bool,
38
39    /// Config file to use
40    #[clap(short, long)]
41    pub config: Option<PathBuf>,
42}
43
44#[derive(Debug, Subcommand)]
45pub enum Commands {
46    Show {
47        reference: String,
48    },
49    /// Interactively rebase onto <upstream>, editing the instruction list in Gitu.
50    Rebase {
51        upstream: String,
52    },
53    /// Edit a `git rebase -i` instruction list, for use as GIT_SEQUENCE_EDITOR:
54    ///     GIT_SEQUENCE_EDITOR='gitu sequence-editor' git rebase -i <upstream>
55    #[clap(verbatim_doc_comment)]
56    SequenceEditor {
57        file: PathBuf,
58    },
59    Blame {
60        file: String,
61        #[clap(short, long)]
62        rev: Option<String>,
63    },
64    /// Print a shell completion script to stdout.
65    ///
66    /// Example (bash): `gitu completion bash > ~/.local/share/bash-completion/completions/gitu`
67    Completion {
68        /// The shell to generate a completion script for
69        shell: Shell,
70    },
71}
72
73/// Write a shell completion script for `gitu` to the given writer.
74pub fn completions(shell: Shell, out: &mut impl io::Write) {
75    let mut cmd = Args::command();
76    let bin_name = cmd.get_name().to_string();
77    clap_complete::generate(shell, &mut cmd, bin_name, out);
78}