Skip to content

Commit

Permalink
clone/init repo: show directory picker if more than one is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
junglerobba committed Aug 14, 2024
1 parent a94fe14 commit 1e0e72c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,21 +601,33 @@ fn refresh_command(args: &RefreshCommand, tmux: &Tmux) -> Result<()> {
Ok(())
}

fn get_first_search_path(config: &Config) -> Result<PathBuf> {
fn pick_search_path(config: &Config, tmux: &Tmux) -> Result<Option<PathBuf>> {
let search_dirs = config
.search_dirs
.as_ref()
.ok_or(TmsError::ConfigError)
.attach_printable("No search path configured")?;
search_dirs
.first()
.ok_or(TmsError::ConfigError)
.attach_printable("No search path configured")
.map(|dir| dir.path.clone())
.attach_printable("No search path configured")?
.iter()
.map(|dir| dir.path.to_string())
.filter_map(|path| path.ok())
.collect::<Vec<String>>();

let path = if search_dirs.len() > 1 {
get_single_selection(&search_dirs, Preview::Directory, config, tmux)?
} else {
let first = search_dirs
.first()
.ok_or(TmsError::ConfigError)
.attach_printable("No search path configured")?;
Some(first.clone())
};
Ok(path.map(PathBuf::from))
}

fn clone_repo_command(args: &CloneRepoCommand, config: Config, tmux: &Tmux) -> Result<()> {
let mut path = get_first_search_path(&config)?;
let Some(mut path) = pick_search_path(&config, tmux)? else {
return Ok(());
};

let (_, repo_name) = args
.repository
Expand Down Expand Up @@ -674,8 +686,9 @@ fn git_credentials_callback(
}

fn init_repo_command(args: &InitRepoCommand, config: Config, tmux: &Tmux) -> Result<()> {
let mut path = get_first_search_path(&config)?;

let Some(mut path) = pick_search_path(&config, tmux)? else {
return Ok(());
};
path.push(&args.repository);

let repo = Repository::init(&path).change_context(TmsError::GitError)?;
Expand Down
8 changes: 8 additions & 0 deletions src/picker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io::{self, Stdout},
process,
rc::Rc,
sync::Arc,
};
Expand Down Expand Up @@ -36,6 +37,7 @@ use crate::{
pub enum Preview {
SessionPane,
None,
Directory,
}

pub struct Picker<'a> {
Expand Down Expand Up @@ -275,6 +277,12 @@ impl<'a> Picker<'a> {
if let Some(item) = snapshot.get_matched_item(index as u32) {
let output = match self.preview {
Preview::SessionPane => self.tmux.capture_pane(item.data),
Preview::Directory => process::Command::new("ls")
.args(["-1", item.data])
.output()
.unwrap_or_else(|_| {
panic!("Failed to execute the command for directory: {}", item.data)
}),
Preview::None => panic!("preview rendering should not have occured"),
};

Expand Down

0 comments on commit 1e0e72c

Please sign in to comment.