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 /// With --pager, print what fits on one screen and exit, as less's --quit-if-one-screen.
32 #[clap(long, action, requires = "pager")]
33 pub quit_if_one_screen: bool,
34
35 /// Enable logging to 'gitu.log'
36 #[clap(long, action)]
37 pub log: bool,
38
39 /// Show external command output in the message area.
40 #[clap(long, action)]
41 pub debug: bool,
42
43 #[clap(long, action)]
44 /// Print version
45 pub version: bool,
46
47 /// Config file to use
48 #[clap(short, long)]
49 pub config: Option<PathBuf>,
50}
51
52#[derive(Debug, Subcommand)]
53pub enum Commands {
54 Show {
55 reference: String,
56 },
57 /// Interactively rebase onto <upstream>, editing the instruction list in Gitu.
58 Rebase {
59 upstream: String,
60 },
61 /// Edit a `git rebase -i` instruction list, for use as GIT_SEQUENCE_EDITOR:
62 /// GIT_SEQUENCE_EDITOR='gitu sequence-editor' git rebase -i <upstream>
63 #[clap(verbatim_doc_comment)]
64 SequenceEditor {
65 file: PathBuf,
66 },
67 Blame {
68 file: String,
69 #[clap(short, long)]
70 rev: Option<String>,
71 },
72 /// Print a shell completion script to stdout.
73 ///
74 /// Example (bash): `gitu completion bash > ~/.local/share/bash-completion/completions/gitu`
75 Completion {
76 /// The shell to generate a completion script for
77 shell: Shell,
78 },
79}
80
81/// Write a shell completion script for `gitu` to the given writer.
82pub fn completions(shell: Shell, out: &mut impl io::Write) {
83 let mut cmd = Args::command();
84 let bin_name = cmd.get_name().to_string();
85 clap_complete::generate(shell, &mut cmd, bin_name, out);
86}