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
48 let mut tui_keybindings = BTreeMap::new();
49 tui_keybindings.insert("↑ / ↓ or k / j".into(), "Navigate files and notes list with vertical scroll".into());
50 tui_keybindings.insert("Enter / →".into(), "Expand file / focus diff & note thread panel".into());
51 tui_keybindings.insert("Esc / ←".into(), "Navigate back to previous pane".into());
52 tui_keybindings.insert("r".into(), "Open quick reply bar for highlighted thread".into());
53 tui_keybindings.insert("a".into(), "Mark highlighted note as [✔ Approved]".into());
54 tui_keybindings.insert("x / ok".into(), "Mark highlighted note as [✓ Resolved]".into());
55 tui_keybindings.insert("q".into(), "Quit git-notes TUI".into());
56
57 let custom_user_shortcuts = BTreeMap::new();
58
59 Self {
60 cli_aliases,
61 tui_keybindings,
62 custom_user_shortcuts,
63 }
64 }
65}
66
67impl ShortcutConfig {
68 fn config_path() -> Option<PathBuf> {
69 dirs_fallback().map(|p| p.join("shortcuts.json"))
70 }
71
72 pub fn load() -> Self {
73 Self::config_path()
74 .and_then(|p| fs::read_to_string(p).ok())
75 .and_then(|s| serde_json::from_str(&s).ok())
76 .unwrap_or_default()
77 }
78
79 pub fn save(&self) -> Result<()> {
80 if let Some(path) = Self::config_path() {
81 if let Some(parent) = path.parent() {
82 let _ = fs::create_dir_all(parent);
83 }
84 let data = serde_json::to_string_pretty(self)?;
85 fs::write(path, data)?;
86 }
87 Ok(())
88 }
89}
90
91pub fn run(args: &ShortcutsArgs) -> Result<()> {
92 let mut config = ShortcutConfig::load();
93
94 if args.reset {
95 config = ShortcutConfig::default();
96 config.save()?;
97 println!("\x1b[32m✓ Reset all shortcuts and quickies to default configuration.\x1b[0m");
98 return Ok(());
99 }
100
101 if let Some(ref set_binding) = args.set {
102 if let Some((key, val)) = set_binding.split_once('=') {
103 config
104 .custom_user_shortcuts
105 .insert(key.trim().to_string(), val.trim().to_string());
106 config.save()?;
107 println!(
108 "\x1b[32m✓ Saved custom shortcut:\x1b[0m \x1b[36m{}\x1b[0m ➜ \x1b[1m{}\x1b[0m",
109 key.trim(),
110 val.trim()
111 );
112 return Ok(());
113 } else {
114 eprintln!("\x1b[31m✗ Invalid format. Use --set KEY=COMMAND (e.g., gn shortcuts --set c=\"add -m\")\x1b[0m");
115 return Ok(());
116 }
117 }
118
119 println!("\n\x1b[1;36m⚡ git-notes Quickies & Keyboard Shortcuts Cheat Sheet\x1b[0m\n");
120
121 println!("\x1b[1;33mCLI Quickies & Abbreviations (gn):\x1b[0m");
122 println!("┌───────────────────────┬────────────────────────────────────────────────────────────────┐");
123 println!("│ Quickie / Command │ Action Description │");
124 println!("├───────────────────────┼────────────────────────────────────────────────────────────────┤");
125 for (k, v) in &config.cli_aliases {
126 println!("│ \x1b[36m{:<21}\x1b[0m │ {:<62} │", k, v);
127 }
128 println!("└───────────────────────┴────────────────────────────────────────────────────────────────┘");
129
130 println!("\n\x1b[1;33mTUI & Terminal Keybindings:\x1b[0m");
131 println!("┌───────────────────────┬────────────────────────────────────────────────────────────────┐");
132 println!("│ Key / Keystroke │ TUI Navigation & Review Action │");
133 println!("├───────────────────────┼────────────────────────────────────────────────────────────────┤");
134 for (k, v) in &config.tui_keybindings {
135 println!("│ \x1b[35m{:<21}\x1b[0m │ {:<62} │", k, v);
136 }
137 println!("└───────────────────────┴────────────────────────────────────────────────────────────────┘");
138
139 if !config.custom_user_shortcuts.is_empty() {
140 println!("\n\x1b[1;33mCustom User Shortcuts (~/.git-notes/shortcuts.json):\x1b[0m");
141 for (k, v) in &config.custom_user_shortcuts {
142 println!(" • \x1b[36m{}\x1b[0m ➜ {}", k, v);
143 }
144 } else {
145 println!("\n\x1b[90m💡 Customize shortcuts anytime: \x1b[36mgn shortcuts --set <alias>=<command>\x1b[0m");
146 }
147
148 println!("\n┌─────────────────────────────────────────────────────────────┐");
149 println!("│ \x1b[1;36m💬 Enjoying git-notes? Help us grow!\x1b[0m │");
150 println!("│ \x1b[33m⭐ Star on GitHub:\x1b[0m https://github.com/isaim0011/git-notes │");
151 println!("│ \x1b[35m★ Review on Open VSX:\x1b[0m https://open-vsx.org/extension/isaim0011/vscode-git-notes │");
152 println!("└─────────────────────────────────────────────────────────────┘\n");
153
154 Ok(())
155}
156
157fn dirs_fallback() -> Option<PathBuf> {
158 std::env::var_os("USERPROFILE")
159 .or_else(|| std::env::var_os("HOME"))
160 .map(|h| PathBuf::from(h).join(".git-notes"))
161}