Skip to content

Commit

Permalink
Merge pull request #103 from siketyan/feat/open-in-default-app
Browse files Browse the repository at this point in the history
feat: Open in default application (explorer, open)
  • Loading branch information
siketyan authored Mar 25, 2023
2 parents 76dfe63 + 3a7a9c1 commit f04e9b5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ impl Application {
}
}

#[cfg(windows)]
impl Default for Application {
fn default() -> Self {
Self {
cmd: "explorer.exe".to_string(),
args: vec!["%p".to_string()],
}
}
}

#[cfg(not(windows))]
impl Default for Application {
fn default() -> Self {
Self {
cmd: "open".to_string(),
args: vec!["%p".to_string()],
}
}
}

#[derive(Debug, Default, Deserialize)]
pub struct Applications {
#[serde(flatten)]
Expand All @@ -61,6 +81,16 @@ impl Applications {
self.open(name, &path)
.unwrap_or_else(|| Application::intermediate(name).open(&path))
}

pub fn open_or_intermediate_or_default<P>(&self, name: Option<&str>, path: P) -> Result<()>
where
P: AsRef<Path>,
{
match name {
Some(n) => self.open_or_intermediate(n, path),
_ => Application::default().open(path),
}
}
}

impl Deref for Applications {
Expand Down
9 changes: 6 additions & 3 deletions src/cmd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Cmd {

/// Opens the directory after cloned a repository.
#[clap(long)]
open: Option<String>,
open: Option<Option<String>>,
}

impl Cmd {
Expand Down Expand Up @@ -103,8 +103,11 @@ impl Cmd {
};

let open = if let Some(app) = &self.open {
config.applications.open_or_intermediate(app, &path)?;
Some(app.to_string())
config
.applications
.open_or_intermediate_or_default(app.as_deref(), &path)?;

Some(app.as_deref().unwrap_or("<default>").to_string())
} else {
None
};
Expand Down
8 changes: 5 additions & 3 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Cmd {

/// Opens the directory after cloned a repository.
#[clap(long)]
open: Option<String>,
open: Option<Option<String>>,
}

impl Cmd {
Expand Down Expand Up @@ -68,11 +68,13 @@ impl Cmd {
}

if let Some(app) = self.open {
config.applications.open_or_intermediate(&app, &path)?;
config
.applications
.open_or_intermediate_or_default(app.as_deref(), &path)?;

info!(
"Opened the repository in [{}] successfully.",
style(&app).bold(),
style(app.as_deref().unwrap_or("<default>")).bold(),
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Cmd {
repo: String,

/// Name of the application entry.
application: String,
application: Option<String>,
}

impl Cmd {
Expand All @@ -31,7 +31,7 @@ impl Cmd {

config
.applications
.open_or_intermediate(&self.application, path)?;
.open_or_intermediate_or_default(self.application.as_deref(), path)?;

Ok(())
}
Expand Down

0 comments on commit f04e9b5

Please sign in to comment.