Skip to content

Commit e182cd9

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

File tree

5 files changed

+39
-32
lines changed

5 files changed

+39
-32
lines changed

src/app.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,20 @@ impl App {
174174
sender_app,
175175
};
176176

177-
let tab = env.options.borrow().current_tab();
177+
let mut select_file: Option<PathBuf> = None;
178+
let tab = if let Some(file) = cliargs.select_file {
179+
// convert to relative git path
180+
if let Ok(abs) = file.canonicalize() {
181+
if let Ok(path) = abs.strip_prefix(
182+
env.repo.borrow().gitpath().canonicalize()?,
183+
) {
184+
select_file = Some(Path::new(".").join(path));
185+
}
186+
}
187+
2
188+
} else {
189+
env.options.borrow().current_tab()
190+
};
178191

179192
let mut app = Self {
180193
input,
@@ -219,7 +232,7 @@ impl App {
219232
status_tab: Status::new(&env),
220233
stashing_tab: Stashing::new(&env),
221234
stashlist_tab: StashList::new(&env),
222-
files_tab: FilesTab::new(&env),
235+
files_tab: FilesTab::new(&env, select_file),
223236
tab: 0,
224237
queue: env.queue,
225238
theme: env.theme,
@@ -232,21 +245,7 @@ impl App {
232245
popup_stack: PopupStack::default(),
233246
};
234247

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

251250
Ok(app)
252251
}
@@ -787,9 +786,6 @@ impl App {
787786
InternalEvent::SelectBranch => {
788787
self.select_branch_popup.open()?;
789788
}
790-
InternalEvent::SelectFile { path } => {
791-
self.files_tab.find_file(&path);
792-
}
793789
InternalEvent::ViewSubmodules => {
794790
self.submodule_popup.open()?;
795791
}

src/components/revision_files.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ use ratatui::{
3030
Frame,
3131
};
3232
use std::{borrow::Cow, fmt::Write};
33-
use std::{collections::BTreeSet, path::Path};
33+
use std::{
34+
collections::BTreeSet,
35+
path::{Path, PathBuf},
36+
};
3437
use unicode_truncate::UnicodeTruncateStr;
3538
use unicode_width::UnicodeWidthStr;
3639

@@ -53,11 +56,15 @@ pub struct RevisionFilesComponent {
5356
revision: Option<CommitInfo>,
5457
focus: Focus,
5558
key_config: SharedKeyConfig,
59+
select_file: Option<PathBuf>,
5660
}
5761

5862
impl RevisionFilesComponent {
5963
///
60-
pub fn new(env: &Environment) -> Self {
64+
pub fn new(
65+
env: &Environment,
66+
select_file: Option<PathBuf>,
67+
) -> Self {
6168
Self {
6269
queue: env.queue.clone(),
6370
tree: FileTree::default(),
@@ -72,6 +79,7 @@ impl RevisionFilesComponent {
7279
focus: Focus::Tree,
7380
key_config: env.key_config.clone(),
7481
repo: env.repo.clone(),
82+
select_file: select_file,
7583
visible: false,
7684
}
7785
}
@@ -134,6 +142,12 @@ impl RevisionFilesComponent {
134142
self.tree.collapse_but_root();
135143

136144
self.files = Some(last);
145+
146+
let select_file = self.select_file.clone();
147+
self.select_file = None;
148+
if let Some(file) = select_file {
149+
self.find_file(file.as_path())
150+
}
137151
}
138152
} else if let Some(rev) = &self.revision {
139153
self.request_files(rev.id);

src/popups/revision_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl RevisionFilesPopup {
3838
///
3939
pub fn new(env: &Environment) -> Self {
4040
Self {
41-
files: RevisionFilesComponent::new(env),
41+
files: RevisionFilesComponent::new(env, None),
4242
visible: false,
4343
key_config: env.key_config.clone(),
4444
open_request: None,

src/queue.rs

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

src/tabs/files.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::Path;
1+
use std::path::{Path, PathBuf};
22

33
use crate::{
44
app::Environment,
@@ -19,10 +19,13 @@ pub struct FilesTab {
1919

2020
impl FilesTab {
2121
///
22-
pub fn new(env: &Environment) -> Self {
22+
pub fn new(
23+
env: &Environment,
24+
select_file: Option<PathBuf>,
25+
) -> Self {
2326
Self {
2427
visible: false,
25-
files: RevisionFilesComponent::new(env),
28+
files: RevisionFilesComponent::new(env, select_file),
2629
repo: env.repo.clone(),
2730
}
2831
}
@@ -58,10 +61,6 @@ impl FilesTab {
5861
pub fn file_finder_update(&mut self, file: &Path) {
5962
self.files.find_file(file);
6063
}
61-
62-
pub fn find_file(&mut self, file: &Path) {
63-
self.files.find_file(file);
64-
}
6564
}
6665

6766
impl DrawableComponent for FilesTab {

0 commit comments

Comments
 (0)