-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(actions): add edit action (#22)
The new `edit` action. Default key binding is `e`. With this, otree can open current item in system editor to let user better view or copy contents. All the edited changes won't be saved. This is **readonly** to the original data.
- Loading branch information
Showing
14 changed files
with
285 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
[editor] | ||
program = "vim" | ||
args = ["{{file}}"] | ||
dir = "/tmp" | ||
|
||
[layout] | ||
direction = "horizontal" | ||
tree_size = 40 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use std::fs; | ||
use std::io::{self, Read, Write}; | ||
use std::path::PathBuf; | ||
use std::process::{Command, Stdio}; | ||
|
||
use anyhow::{bail, Context, Result}; | ||
|
||
use crate::config::Config; | ||
|
||
pub struct Edit { | ||
path: String, | ||
data: String, | ||
cmd: Command, | ||
} | ||
|
||
impl Edit { | ||
pub fn new(cfg: &Config, identify: String, data: String, extension: &'static str) -> Self { | ||
let mut cmd = Command::new(&cfg.editor.program); | ||
cmd.stdin(Stdio::inherit()); | ||
cmd.stdout(Stdio::inherit()); | ||
cmd.stderr(Stdio::inherit()); | ||
|
||
let name = identify.replace('/', "_"); | ||
let path = PathBuf::from(&cfg.editor.dir).join(format!("otree_{name}.{extension}")); | ||
let path = format!("{}", path.display()); | ||
|
||
for arg in cfg.editor.args.iter() { | ||
if !arg.contains("{file}") { | ||
cmd.arg(arg); | ||
continue; | ||
} | ||
|
||
let arg = arg.replace("{file}", &path); | ||
cmd.arg(arg); | ||
} | ||
|
||
Self { path, data, cmd } | ||
} | ||
|
||
pub fn run(mut self) { | ||
if let Err(err) = self._run() { | ||
eprintln!("Edit error: {err:#}"); | ||
eprintln!(); | ||
eprintln!("Press any key to continue..."); | ||
io::stdout().flush().unwrap(); | ||
|
||
// Wait for a single character input | ||
let mut buffer = [0; 1]; | ||
io::stdin().read_exact(&mut buffer).unwrap(); | ||
} | ||
} | ||
|
||
fn _run(&mut self) -> Result<()> { | ||
self.write_file().context("write edit file")?; | ||
|
||
let result = self.edit_file().context("edit file"); | ||
if result.is_err() { | ||
let _ = self.delete_file(); | ||
return result; | ||
} | ||
|
||
self.delete_file().context("delete edit file")?; | ||
Ok(()) | ||
} | ||
|
||
fn write_file(&self) -> Result<()> { | ||
let path = PathBuf::from(&self.path); | ||
if let Some(dir) = path.parent() { | ||
match fs::metadata(dir) { | ||
Ok(meta) => { | ||
if !meta.is_dir() { | ||
bail!("'{}' is not a directory", dir.display()); | ||
} | ||
} | ||
Err(err) if err.kind() == io::ErrorKind::NotFound => { | ||
fs::create_dir_all(dir) | ||
.with_context(|| format!("create directory '{}'", dir.display()))?; | ||
} | ||
Err(err) => { | ||
return Err(err) | ||
.with_context(|| format!("get metadata for '{}'", dir.display())) | ||
} | ||
} | ||
} | ||
|
||
fs::write(&path, &self.data) | ||
.with_context(|| format!("write data to file '{}'", path.display()))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn edit_file(&mut self) -> Result<()> { | ||
let status = self.cmd.status().context("execute editor command")?; | ||
if !status.success() { | ||
bail!("editor command exited with bad code"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn delete_file(&self) -> Result<()> { | ||
fs::remove_file(&self.path).with_context(|| format!("delete file '{}'", self.path)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.