Skip to content

Commit e8b278a

Browse files
committed
add cli flag to open files tab with selected file #2510
1 parent 47283f2 commit e8b278a

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/app.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
accessors,
3+
args::CliArgs,
34
cmdbar::CommandBar,
45
components::{
56
command_pump, event_pump, CommandInfo, Component,
@@ -150,14 +151,14 @@ impl App {
150151
///
151152
#[allow(clippy::too_many_lines)]
152153
pub fn new(
153-
repo: RepoPathRef,
154+
cliargs: CliArgs,
154155
sender_git: Sender<AsyncGitNotification>,
155156
sender_app: Sender<AsyncAppNotification>,
156157
input: Input,
157158
theme: Theme,
158-
select_file: Option<PathBuf>,
159159
key_config: KeyConfig,
160160
) -> Result<Self> {
161+
let repo = RefCell::new(cliargs.repo_path.clone());
161162
log::trace!("open repo at: {:?}", &repo);
162163

163164
let repo_path_text =
@@ -231,7 +232,7 @@ impl App {
231232
popup_stack: PopupStack::default(),
232233
};
233234

234-
if let Some(file) = select_file {
235+
if let Some(file) = cliargs.select_file {
235236
app.set_tab(2)?;
236237
// convert to relative git path
237238
if let Ok(abs) = file.canonicalize() {

src/args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const WATCHER_FLAG_ID: &str = "watcher";
2323
const DEFAULT_THEME: &str = "theme.ron";
2424
const DEFAULT_GIT_DIR: &str = ".";
2525

26+
#[derive(Clone)]
2627
pub struct CliArgs {
2728
pub theme: PathBuf,
2829
pub select_file: Option<PathBuf>,

src/main.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ mod tabs;
7979
mod ui;
8080
mod watcher;
8181

82-
use crate::{app::App, args::process_cmdline};
82+
use crate::{
83+
app::App,
84+
args::{process_cmdline, CliArgs},
85+
};
8386
use anyhow::{anyhow, bail, Result};
8487
use app::QuitState;
8588
use asyncgit::{
@@ -102,10 +105,9 @@ use scopeguard::defer;
102105
use scopetime::scope_time;
103106
use spinner::Spinner;
104107
use std::{
105-
cell::RefCell,
106108
io::{self, Stdout},
107109
panic,
108-
path::{Path, PathBuf},
110+
path::Path,
109111
time::{Duration, Instant},
110112
};
111113
use ui::style::Theme;
@@ -163,7 +165,7 @@ macro_rules! log_eprintln {
163165
fn main() -> Result<()> {
164166
let app_start = Instant::now();
165167

166-
let cliargs = process_cmdline()?;
168+
let mut cliargs = process_cmdline()?;
167169

168170
asyncgit::register_tracing_logging();
169171
ensure_valid_path(&cliargs.repo_path)?;
@@ -180,9 +182,8 @@ fn main() -> Result<()> {
180182

181183
set_panic_handler()?;
182184

183-
let mut repo_path = cliargs.repo_path;
184-
let mut select_file = cliargs.select_file;
185-
let mut terminal = start_terminal(io::stdout(), &repo_path)?;
185+
let mut terminal =
186+
start_terminal(io::stdout(), &cliargs.repo_path)?;
186187
let input = Input::new();
187188

188189
let updater = if cliargs.notify_watcher {
@@ -194,9 +195,8 @@ fn main() -> Result<()> {
194195
loop {
195196
let quit_state = run_app(
196197
app_start,
197-
repo_path.clone(),
198+
cliargs.clone(),
198199
theme.clone(),
199-
select_file.clone(),
200200
key_config.clone(),
201201
&input,
202202
updater,
@@ -205,8 +205,12 @@ fn main() -> Result<()> {
205205

206206
match quit_state {
207207
QuitState::OpenSubmodule(p) => {
208-
repo_path = p;
209-
select_file = None;
208+
cliargs = CliArgs {
209+
repo_path: p,
210+
select_file: None,
211+
theme: cliargs.theme,
212+
notify_watcher: cliargs.notify_watcher,
213+
}
210214
}
211215
_ => break,
212216
}
@@ -218,9 +222,8 @@ fn main() -> Result<()> {
218222
#[allow(clippy::too_many_arguments)]
219223
fn run_app(
220224
app_start: Instant,
221-
repo: RepoPath,
225+
cliargs: CliArgs,
222226
theme: Theme,
223-
select_file: Option<PathBuf>,
224227
key_config: KeyConfig,
225228
input: &Input,
226229
updater: Updater,
@@ -233,8 +236,9 @@ fn run_app(
233236

234237
let (rx_ticker, rx_watcher) = match updater {
235238
Updater::NotifyWatcher => {
236-
let repo_watcher =
237-
RepoWatcher::new(repo_work_dir(&repo)?.as_str());
239+
let repo_watcher = RepoWatcher::new(
240+
repo_work_dir(&cliargs.repo_path)?.as_str(),
241+
);
238242

239243
(never(), repo_watcher.receiver())
240244
}
@@ -244,12 +248,11 @@ fn run_app(
244248
let spinner_ticker = tick(SPINNER_INTERVAL);
245249

246250
let mut app = App::new(
247-
RefCell::new(repo),
251+
cliargs,
248252
tx_git,
249253
tx_app,
250254
input.clone(),
251255
theme,
252-
select_file,
253256
key_config,
254257
)?;
255258

0 commit comments

Comments
 (0)