gn_cli/commands/
shortcuts.rs1use anyhow::Result;
2use clap::Args;
3use serde::{Deserialize, Serialize};
4use std::collections::BTreeMap;
5use std::fs;
6use std::path::PathBuf;
7
8#[derive(Args, Debug)]
9pub struct ShortcutsArgs {
10 #[arg(short, long)]
12 pub list: bool,
13
14 #[arg(short, long)]
16 pub set: Option<String>,
17
18 #[arg(long)]
20 pub reset: bool,
21}
22
23#[derive(Serialize, Deserialize, Debug, Clone)]
24pub struct ShortcutConfig {
25 pub cli_aliases: BTreeMap<String, String>,
26 pub tui_keybindings: BTreeMap<String, String>,
27 pub custom_user_shortcuts: BTreeMap<String, String>,
28}
29
30impl Default for ShortcutConfig {
31 fn default() -> Self {
32 let mut cli_aliases = BTreeMap::new();
33 cli_aliases.insert("gn a".into(), "Add a new note to file and line range [alias: add]".into());
34 cli_aliases.insert("gn r".into(), "Reply to an existing note thread by ID or number [alias: reply]".into());
35 cli_aliases.insert("gn l / gn ls".into(), "List notes with numbered quick-indexes [alias: list]".into());
36 cli_aliases.insert("gn s".into(), "Interactive arrow-key note thread picker [alias: show]".into());
37 cli_aliases.insert("gn ok / gn close".into(), "Resolve or approve a note by index # or ID [alias: resolve]".into());
38 cli_aliases.insert("gn d".into(), "View git diff with inline notes attached to hunks [alias: diff]".into());
39 cli_aliases.insert("gn b".into(), "View git blame with inline notes annotations [alias: blame]".into());
40 cli_aliases.insert("gn push / gn pull".into(), "Sync notes bidirectionally with remote refs/notes/* [alias: sync]".into());
41 cli_aliases.insert("gn sum".into(), "AI summary of open discussion threads via Gemini [alias: summarize]".into());
42 cli_aliases.insert("gn doc".into(), "Check repo health, refspecs, and stale notes [alias: doctor]".into());
43 cli_aliases.insert("gn heal".into(), "Auto-heal & re-anchor notes after git rebase / amend [alias: rebase-heal]".into());
44 cli_aliases.insert("gn p2p / gn sync --p2p".into(), "Offline LAN peer-to-peer sync / USB bundle export & import".into());
45 cli_aliases.insert("gn imp".into(), "Import reviews from GitLab MR, Bitbucket PR, Jira, or GitHub [alias: import]".into());
46 cli_aliases.insert("gn i".into(), "1-second setup: configure fetch refspec & auto-sync hooks [alias: init]".into());
47 cli_aliases.insert("gn check / gn gate".into(), "CI quality gating and merge-readiness check [alias: gate]".into());
48 cli_aliases.insert("gn verify / gn sig".into(), "Verify cryptographic signatures of notes [alias: sig]".into());
49
50 let mut tui_keybindings = BTreeMap::new();
51 tui_keybindings.insert("↑ / ↓ or k / j".into(), "Navigate files and notes list with vertical scroll".into());
52 tui_keybindings.insert("Enter / →".into(), "Expand file / focus diff & note thread panel".into());
53 tui_keybindings.insert("Esc / ←".into(), "Navigate back to previous pane".into());
54 tui_keybindings.insert("r".into(), "Open quick reply bar for highlighted thread".into());
55 tui_keybindings.insert("a".into(), "Mark highlighted note as [✔ Approved]".into());
56 tui_keybindings.insert("x / ok".into(), "Mark highlighted note as [✓ Resolved]".into());
57 tui_keybindings.insert("q".into(), "Quit git-notes TUI".into());
58
59 let custom_user_shortcuts = BTreeMap::new();
60
61 Self {
62 cli_aliases,
63 tui_keybindings,
64 custom_user_shortcuts,
65 }
66 }
67}
68
69impl ShortcutConfig {
70 fn config_path() -> Option<PathBuf> {
71 dirs_fallback().map(|p| p.join("shortcuts.json"))
72 }
73
74 pub fn load() -> Self {
75 Self::config_path()
76 .and_then(|p| fs::read_to_string(p).ok())
77 .and_then(|s| serde_json::from_str(&s).ok())
78 .unwrap_or_default()
79 }
80
81 pub fn save(&self) -> Result<()> {
82 if let Some(path) = Self::config_path() {
83 if let Some(parent) = path.parent() {
84 let _ = fs::create_dir_all(parent);
85 }
86 let data = serde_json::to_string_pretty(self)?;
87 fs::write(path, data)?;
88 }
89 Ok(())
90 }
91}
92
93pub fn run(args: &ShortcutsArgs) -> Result<()> {
94 let mut config = ShortcutConfig::load();
95
96 if args.reset {
97 config = ShortcutConfig::default();
98 config.save()?;
99 println!("\x1b[32m✓ Reset all shortcuts and quickies to default configuration.\x1b[0m");
100 return Ok(());
101 }
102
103 if let Some(ref set_binding) = args.set {
104 if let Some((key, val)) = set_binding.split_once('=') {
105 config
106 .custom_user_shortcuts
107 .insert(key.trim().to_string(), val.trim().to_string());
108 config.save()?;
109 println!(
110 "\x1b[32m✓ Saved custom shortcut:\x1b[0m \x1b[36m{}\x1b[0m ➜ \x1b[1m{}\x1b[0m",
111 key.trim(),
112 val.trim()
113 );
114 return Ok(());
115 } else {
116 eprintln!("\x1b[31m✗ Invalid format. Use --set KEY=COMMAND (e.g., gn shortcuts --set c=\"add -m\")\x1b[0m");
117 return Ok(());
118 }
119 }
120
121 println!("\n\x1b[1;36m⚡ git-notes Quickies & Keyboard Shortcuts Cheat Sheet\x1b[0m\n");
122
123 println!("\x1b[1;33mCLI Quickies & Abbreviations (gn):\x1b[0m");
124 println!("┌───────────────────────┬────────────────────────────────────────────────────────────────┐");
125 println!("│ Quickie / Command │ Action Description │");
126 println!("├───────────────────────┼────────────────────────────────────────────────────────────────┤");
127 for (k, v) in &config.cli_aliases {
128 println!("│ \x1b[36m{:<21}\x1b[0m │ {:<62} │", k, v);
129 }
130 println!("└───────────────────────┴────────────────────────────────────────────────────────────────┘");
131
132 println!("\n\x1b[1;33mTUI & Terminal Keybindings:\x1b[0m");
133 println!("┌───────────────────────┬────────────────────────────────────────────────────────────────┐");
134 println!("│ Key / Keystroke │ TUI Navigation & Review Action │");
135 println!("├───────────────────────┼────────────────────────────────────────────────────────────────┤");
136 for (k, v) in &config.tui_keybindings {
137 println!("│ \x1b[35m{:<21}\x1b[0m │ {:<62} │", k, v);
138 }
139 println!("└───────────────────────┴────────────────────────────────────────────────────────────────┘");
140
141 if !config.custom_user_shortcuts.is_empty() {
142 println!("\n\x1b[1;33mCustom User Shortcuts (~/.git-notes/shortcuts.json):\x1b[0m");
143 for (k, v) in &config.custom_user_shortcuts {
144 println!(" • \x1b[36m{}\x1b[0m ➜ {}", k, v);
145 }
146 } else {
147 println!("\n\x1b[90m💡 Customize shortcuts anytime: \x1b[36mgn shortcuts --set <alias>=<command>\x1b[0m");
148 }
149
150 println!("\n┌─────────────────────────────────────────────────────────────┐");
151 println!("│ \x1b[1;36m💬 Enjoying git-notes? Help us grow!\x1b[0m │");
152 println!("│ \x1b[33m⭐ Star on GitHub:\x1b[0m https://github.com/isaim0011/git-notes │");
153 println!("│ \x1b[35m★ Review on Open VSX:\x1b[0m https://open-vsx.org/extension/isaim0011/vscode-git-notes │");
154 println!("└─────────────────────────────────────────────────────────────┘\n");
155
156 Ok(())
157}
158
159fn dirs_fallback() -> Option<PathBuf> {
160 std::env::var_os("USERPROFILE")
161 .or_else(|| std::env::var_os("HOME"))
162 .map(|h| PathBuf::from(h).join(".git-notes"))
163}