summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorJon Grythe Stødle <jonstodle@outlook.com>2020-07-06 02:06:19 +0200
committerStephan Dilly <dilly.stephan@gmail.com>2020-07-07 12:42:01 +0200
commit52f31adb9870a41743008eca1156673881348811 (patch)
tree8b3dbc7b7c0abace012e6fec8e3e0012ccb1fea5 /src/main.rs
parentb9ec27d89c26bcf4776d93061267df4e34db5cd5 (diff)
Add command to edit selected file in editor
Pressing `e` while looking at a file in the _Status_ view will launch an external editor with the current file opened. The editor chosen is determined by the default logic introduced in #114. An improvement to this in the future could be launching at the specific line at which the _Diff_ view is focused, but that seems to require a change in `FileDiff` which is a change bigger than this PR. Fixes #166
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index 60ed171d..61dd7a37 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -43,12 +43,13 @@ use scopeguard::defer;
use scopetime::scope_time;
use simplelog::{Config, LevelFilter, WriteLogger};
use spinner::Spinner;
+use std::process::Command;
use std::{
env, fs,
fs::File,
io::{self, Write},
panic,
- path::PathBuf,
+ path::{Path, PathBuf},
process,
time::{Duration, Instant},
};
@@ -241,6 +242,28 @@ fn migrate_config() -> Result<()> {
Ok(())
}
+fn open_file_in_editor(path: &Path) -> Result<()> {
+ let mut editor = env::var("GIT_EDITOR")
+ .ok()
+ .or_else(|| env::var("VISUAL").ok())
+ .or_else(|| env::var("EDITOR").ok())
+ .unwrap_or_else(|| String::from("vi"));
+ editor.push_str(&format!(" {}", path.to_string_lossy()));
+
+ let mut editor = editor.split_whitespace();
+
+ let command = editor
+ .next()
+ .ok_or_else(|| anyhow!("unable to read editor command"))?;
+
+ Command::new(command)
+ .args(editor)
+ .status()
+ .map_err(|e| anyhow!("\"{}\": {}", command, e))?;
+
+ Ok(())
+}
+
fn get_app_cache_path() -> Result<PathBuf> {
let mut path = dirs::cache_dir()
.ok_or_else(|| anyhow!("failed to find os cache dir."))?;