Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone/init repo: show directory picker if more than one is configured #118

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ Commands:
sessions Show running tmux sessions with asterisk on the current session
rename Rename the active session and the working directory
refresh Creates new worktree windows for the selected session
clone-repo Clone repository into the first search path and create a new session for it
clone-repo Clone repository and create a new session for it
init-repo Initialize empty repository
bookmark Bookmark a directory so it is available to select along with the Git repositories
help Print this message or the help of the given subcommand(s)

Options:
Expand Down
37 changes: 25 additions & 12 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ pub enum CliCommand {
Rename(RenameCommand),
/// Creates new worktree windows for the selected session
Refresh(RefreshCommand),
/// Clone repository into the first search path and create a new session for it
/// Clone repository and create a new session for it
CloneRepo(CloneRepoCommand),
/// Initialize empty repository at the first search path
/// Initialize empty repository
InitRepo(InitRepoCommand),
/// Bookmark a directory so it is available to select along with the Git repositories
Bookmark(BookmarkCommand),
Expand Down 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
Loading