mirror of
https://codeberg.org/Cyborus/forgejo-cli.git
synced 2024-11-10 12:09:33 +01:00
Merge pull request 'add system for editor-specific flags' (#12) from editor-flags into main
Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/12
This commit is contained in:
commit
e532aacb6c
1 changed files with 16 additions and 1 deletions
17
src/main.rs
17
src/main.rs
|
@ -70,7 +70,9 @@ async fn readline(msg: &str) -> eyre::Result<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn editor(contents: &mut String, ext: Option<&str>) -> eyre::Result<()> {
|
async fn editor(contents: &mut String, ext: Option<&str>) -> eyre::Result<()> {
|
||||||
let editor = std::env::var_os("EDITOR").ok_or_else(|| eyre!("unable to locate editor"))?;
|
let editor = std::path::PathBuf::from(
|
||||||
|
std::env::var_os("EDITOR").ok_or_else(|| eyre!("unable to locate editor"))?,
|
||||||
|
);
|
||||||
|
|
||||||
let (mut file, path) = tempfile(ext).await?;
|
let (mut file, path) = tempfile(ext).await?;
|
||||||
file.write_all(contents.as_bytes()).await?;
|
file.write_all(contents.as_bytes()).await?;
|
||||||
|
@ -80,7 +82,9 @@ async fn editor(contents: &mut String, ext: Option<&str>) -> eyre::Result<()> {
|
||||||
// on errors
|
// on errors
|
||||||
let res = (|| async {
|
let res = (|| async {
|
||||||
eprint!("waiting on editor\r");
|
eprint!("waiting on editor\r");
|
||||||
|
let flags = get_editor_flags(&editor);
|
||||||
let status = tokio::process::Command::new(editor)
|
let status = tokio::process::Command::new(editor)
|
||||||
|
.args(flags)
|
||||||
.arg(&path)
|
.arg(&path)
|
||||||
.status()
|
.status()
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -100,6 +104,17 @@ async fn editor(contents: &mut String, ext: Option<&str>) -> eyre::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_editor_flags(editor_path: &std::path::Path) -> &'static [&'static str] {
|
||||||
|
let editor_name = match editor_path.file_stem().and_then(|s| s.to_str()) {
|
||||||
|
Some(name) => name,
|
||||||
|
None => return &[],
|
||||||
|
};
|
||||||
|
if editor_name == "code" {
|
||||||
|
return &["--wait"];
|
||||||
|
}
|
||||||
|
&[]
|
||||||
|
}
|
||||||
|
|
||||||
async fn tempfile(ext: Option<&str>) -> tokio::io::Result<(tokio::fs::File, std::path::PathBuf)> {
|
async fn tempfile(ext: Option<&str>) -> tokio::io::Result<(tokio::fs::File, std::path::PathBuf)> {
|
||||||
let filename = uuid::Uuid::new_v4();
|
let filename = uuid::Uuid::new_v4();
|
||||||
let mut path = std::env::temp_dir().join(filename.to_string());
|
let mut path = std::env::temp_dir().join(filename.to_string());
|
||||||
|
|
Loading…
Reference in a new issue