Skip to content

Commit de87a44

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

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
* 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))
1616
* dx: `make check` checks Cargo.toml dependency ordering using `cargo sort` [[@naseschwarz](https://github.com/naseschwarz)]
1717
* 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))
18+
* 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))
1819

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

src/app.rs

Lines changed: 17 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,19 @@ 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+
let abs = file.canonicalize()?;
238+
let repo = app.repo.borrow().gitpath().canonicalize()?;
239+
if let Ok(path) = abs.strip_prefix(repo) {
240+
app.queue.push(InternalEvent::SelectFile {
241+
path: Path::new(".").join(path),
242+
});
243+
}
244+
} else {
245+
app.set_tab(tab)?;
246+
}
234247

235248
Ok(app)
236249
}
@@ -771,6 +784,9 @@ impl App {
771784
InternalEvent::SelectBranch => {
772785
self.select_branch_popup.open()?;
773786
}
787+
InternalEvent::SelectFile { path } => {
788+
self.files_tab.find_file(&path);
789+
}
774790
InternalEvent::ViewSubmodules => {
775791
self.submodule_popup.open()?;
776792
}

src/args.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414

1515
pub struct CliArgs {
1616
pub theme: PathBuf,
17+
pub select_file: Option<PathBuf>,
1718
pub repo_path: RepoPath,
1819
pub notify_watcher: bool,
1920
}
@@ -38,6 +39,9 @@ pub fn process_cmdline() -> Result<CliArgs> {
3839
.get_one::<String>("directory")
3940
.map_or_else(|| PathBuf::from("."), PathBuf::from);
4041

42+
let select_file =
43+
arg_matches.get_one::<String>("file").map(PathBuf::from);
44+
4145
let repo_path = if let Some(w) = workdir {
4246
RepoPath::Workdir { gitdir, workdir: w }
4347
} else {
@@ -62,6 +66,7 @@ pub fn process_cmdline() -> Result<CliArgs> {
6266

6367
Ok(CliArgs {
6468
theme,
69+
select_file,
6570
repo_path,
6671
notify_watcher,
6772
})
@@ -116,6 +121,13 @@ fn app() -> ClapApp {
116121
.long("bugreport")
117122
.action(clap::ArgAction::SetTrue),
118123
)
124+
.arg(
125+
Arg::new("file")
126+
.help("Select the file in the file tab")
127+
.short('f')
128+
.long("file")
129+
.num_args(1),
130+
)
119131
.arg(
120132
Arg::new("directory")
121133
.help("Set the git directory")

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use std::{
103103
cell::RefCell,
104104
io::{self, Stdout},
105105
panic,
106-
path::Path,
106+
path::{Path, PathBuf},
107107
time::{Duration, Instant},
108108
};
109109
use ui::style::Theme;
@@ -193,6 +193,7 @@ fn main() -> Result<()> {
193193
app_start,
194194
repo_path.clone(),
195195
theme.clone(),
196+
cliargs.select_file.clone(),
196197
key_config.clone(),
197198
&input,
198199
updater,
@@ -214,6 +215,7 @@ fn run_app(
214215
app_start: Instant,
215216
repo: RepoPath,
216217
theme: Theme,
218+
select_file: Option<PathBuf>,
217219
key_config: KeyConfig,
218220
input: &Input,
219221
updater: Updater,
@@ -242,6 +244,7 @@ fn run_app(
242244
tx_app,
243245
input.clone(),
244246
theme,
247+
select_file,
245248
key_config,
246249
)?;
247250

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)