summaryrefslogtreecommitdiffstats
path: root/src/opener.rs
diff options
context:
space:
mode:
authorquentin konieczko <konieczko@gmail.com>2022-10-14 13:11:35 +0200
committerquentin konieczko <konieczko@gmail.com>2022-10-14 13:11:35 +0200
commit129b33d23d01e5f2c33c274417c552d2d3daead8 (patch)
tree53cbe543f71363b60f92e95d050c599cd47a8b8b /src/opener.rs
parent49987eb15c5ed3a4ff2aee933ee1982beca08d8c (diff)
first draft opener
Diffstat (limited to 'src/opener.rs')
-rw-r--r--src/opener.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/opener.rs b/src/opener.rs
new file mode 100644
index 0000000..f58aac6
--- /dev/null
+++ b/src/opener.rs
@@ -0,0 +1,62 @@
+use std::collections::HashMap;
+use std::process::Command;
+
+#[derive(Clone)]
+struct OpenConfig {
+ opener: String,
+ use_term: bool,
+}
+
+impl OpenConfig {
+ pub fn new(opener: String, use_term: bool) -> Self {
+ OpenConfig { opener, use_term }
+ }
+}
+
+#[derive(Clone)]
+pub struct Opener {
+ terminal: String,
+ association: HashMap<String, OpenConfig>,
+}
+
+impl Opener {
+ pub fn new(terminal: String) -> Self {
+ let mut association = HashMap::new();
+ association.insert("md".to_owned(), OpenConfig::new("nvim".to_owned(), true));
+ Self {
+ terminal,
+ association,
+ }
+ }
+
+ pub fn open(&self, filepath: std::path::PathBuf) {
+ let extension = filepath.extension().unwrap().to_str().unwrap();
+ if let Some(open_config) = self.association.get(extension) {
+ if open_config.use_term {
+ self.open_terminal(
+ open_config.opener.clone(),
+ filepath.to_str().unwrap().to_owned(),
+ )
+ } else {
+ self.open_directly(
+ open_config.opener.clone(),
+ filepath.to_str().unwrap().to_owned(),
+ )
+ }
+ }
+ }
+
+ fn open_directly(&self, executable: String, filepath: String) {
+ execute_in_child(&executable, &vec![&filepath]);
+ }
+
+ fn open_terminal(&self, executable: String, filepath: String) {
+ execute_in_child(&self.terminal, &vec!["-e", &executable, &filepath]);
+ }
+}
+
+/// Execute the command in a fork.
+fn execute_in_child(exe: &str, args: &Vec<&str>) -> std::process::Child {
+ eprintln!("exec exe {}, args {:?}", exe, args);
+ Command::new(exe).args(args).spawn().unwrap()
+}