Skip to content

Commit ec4f38d

Browse files
committed
add cli flag to open files tab with selected file #2510
1 parent 5849096 commit ec4f38d

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
* new command-line option to override the default log file path (`--logfile`) [[@acuteenvy](https://github.com/acuteenvy)] ([#2539](https://github.com/gitui-org/gitui/pull/2539))
1818
* dx: `make check` checks Cargo.toml dependency ordering using `cargo sort` [[@naseschwarz](https://github.com/naseschwarz)]
1919
* add `use_selection_fg` to theme file to allow customizing selection foreground color [[@Upsylonbare](https://github.com/Upsylonbare)] ([#2515](https://github.com/gitui-org/gitui/pull/2515))
20+
* add `--file` cli flag to open the files tab with the given file already selected [[@laktak](https://github.com/laktak)] ([#2510](https://github.com/gitui-org/gitui/issues/2510))
2021

2122
### Changed
2223
* improve error messages [[@acuteenvy](https://github.com/acuteenvy)] ([#2617](https://github.com/gitui-org/gitui/pull/2617))

src/app.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl App {
155155
sender_app: Sender<AsyncAppNotification>,
156156
input: Input,
157157
theme: Theme,
158+
select_file: Option<PathBuf>,
158159
key_config: KeyConfig,
159160
) -> Result<Self> {
160161
log::trace!("open repo at: {:?}", &repo);
@@ -230,7 +231,21 @@ impl App {
230231
popup_stack: PopupStack::default(),
231232
};
232233

233-
app.set_tab(tab)?;
234+
if let Some(file) = select_file {
235+
app.set_tab(2)?;
236+
// convert to relative git path
237+
if let Ok(abs) = file.canonicalize() {
238+
let repo =
239+
app.repo.borrow().gitpath().canonicalize()?;
240+
if let Ok(path) = abs.strip_prefix(repo) {
241+
app.queue.push(InternalEvent::SelectFile {
242+
path: Path::new(".").join(path),
243+
});
244+
}
245+
}
246+
} else {
247+
app.set_tab(tab)?;
248+
}
234249

235250
Ok(app)
236251
}
@@ -771,6 +786,9 @@ impl App {
771786
InternalEvent::SelectBranch => {
772787
self.select_branch_popup.open()?;
773788
}
789+
InternalEvent::SelectFile { path } => {
790+
self.files_tab.find_file(&path);
791+
}
774792
InternalEvent::ViewSubmodules => {
775793
self.submodule_popup.open()?;
776794
}

src/args.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ const LOG_FILE_FLAG_ID: &str = "logfile";
1717
const LOGGING_FLAG_ID: &str = "logging";
1818
const THEME_FLAG_ID: &str = "theme";
1919
const WORKDIR_FLAG_ID: &str = "workdir";
20+
const FILE_FLAG_ID: &str = "file";
2021
const GIT_DIR_FLAG_ID: &str = "directory";
2122
const WATCHER_FLAG_ID: &str = "watcher";
2223
const DEFAULT_THEME: &str = "theme.ron";
2324
const DEFAULT_GIT_DIR: &str = ".";
2425

2526
pub struct CliArgs {
2627
pub theme: PathBuf,
28+
pub select_file: Option<PathBuf>,
2729
pub repo_path: RepoPath,
2830
pub notify_watcher: bool,
2931
}
@@ -51,6 +53,10 @@ pub fn process_cmdline() -> Result<CliArgs> {
5153
PathBuf::from,
5254
);
5355

56+
let select_file = arg_matches
57+
.get_one::<String>(FILE_FLAG_ID)
58+
.map(PathBuf::from);
59+
5460
let repo_path = if let Some(w) = workdir {
5561
RepoPath::Workdir { gitdir, workdir: w }
5662
} else {
@@ -75,6 +81,7 @@ pub fn process_cmdline() -> Result<CliArgs> {
7581

7682
Ok(CliArgs {
7783
theme,
84+
select_file,
7885
repo_path,
7986
notify_watcher,
8087
})
@@ -129,6 +136,13 @@ fn app() -> ClapApp {
129136
.long("bugreport")
130137
.action(clap::ArgAction::SetTrue),
131138
)
139+
.arg(
140+
Arg::new(FILE_FLAG_ID)
141+
.help("Select the file in the file tab")
142+
.short('f')
143+
.long("file")
144+
.num_args(1),
145+
)
132146
.arg(
133147
Arg::new(GIT_DIR_FLAG_ID)
134148
.help("Set the git directory")

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ use std::{
105105
cell::RefCell,
106106
io::{self, Stdout},
107107
panic,
108-
path::Path,
108+
path::{Path, PathBuf},
109109
time::{Duration, Instant},
110110
};
111111
use ui::style::Theme;
@@ -195,6 +195,7 @@ fn main() -> Result<()> {
195195
app_start,
196196
repo_path.clone(),
197197
theme.clone(),
198+
cliargs.select_file.clone(),
198199
key_config.clone(),
199200
&input,
200201
updater,
@@ -212,10 +213,12 @@ fn main() -> Result<()> {
212213
Ok(())
213214
}
214215

216+
#[allow(clippy::too_many_arguments)]
215217
fn run_app(
216218
app_start: Instant,
217219
repo: RepoPath,
218220
theme: Theme,
221+
select_file: Option<PathBuf>,
219222
key_config: KeyConfig,
220223
input: &Input,
221224
updater: Updater,
@@ -244,6 +247,7 @@ fn run_app(
244247
tx_app,
245248
input.clone(),
246249
theme,
250+
select_file,
247251
key_config,
248252
)?;
249253

src/queue.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ pub enum InternalEvent {
120120
///
121121
SelectBranch,
122122
///
123+
SelectFile { path: PathBuf },
124+
///
123125
OpenExternalEditor(Option<String>),
124126
///
125127
Push(String, PushType, bool, bool),

src/tabs/files.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ impl FilesTab {
5858
pub fn file_finder_update(&mut self, file: &Path) {
5959
self.files.find_file(file);
6060
}
61+
62+
pub fn find_file(&mut self, file: &Path) {
63+
self.files.find_file(file);
64+
}
6165
}
6266

6367
impl DrawableComponent for FilesTab {

0 commit comments

Comments
 (0)