mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 13:17:55 +08:00
improve folder struct + msg management (#217)
* make imap list and search return msg instead of fetch * move imap logouts to main fn * improve list command * improve search command * improve flags command * improve template reply * improve tpl forward command * refactor tpl and msg reply/forward * refactor copy, move and write commands * refactor attachment command * fix attachment part of copy and move commands * fix send, save, read and mbox * put back notify and watch commands * fix msg encoding * refactor edit choices, clean dead code * fix attachment for write, reply and forward commands * refactor config mod struct * refactor project folder struct * fix vim plugin (#215)
This commit is contained in:
+7
-7
@@ -1,5 +1,5 @@
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::debug;
|
||||
use log::{debug, error};
|
||||
use std::io::{self, Write};
|
||||
|
||||
pub enum PreEditChoice {
|
||||
@@ -32,11 +32,11 @@ pub fn pre_edit() -> Result<PreEditChoice> {
|
||||
Ok(PreEditChoice::Quit)
|
||||
}
|
||||
Some(choice) => {
|
||||
debug!("invalid choice `{}`", choice);
|
||||
Err(anyhow!("invalid choice `{}`", choice))
|
||||
error!(r#"invalid choice "{}""#, choice);
|
||||
Err(anyhow!(r#"invalid choice "{}""#, choice))
|
||||
}
|
||||
None => {
|
||||
debug!("empty choice");
|
||||
error!("empty choice");
|
||||
Err(anyhow!("empty choice"))
|
||||
}
|
||||
}
|
||||
@@ -81,11 +81,11 @@ pub fn post_edit() -> Result<PostEditChoice> {
|
||||
Ok(PostEditChoice::Discard)
|
||||
}
|
||||
Some(choice) => {
|
||||
debug!("invalid choice `{}`", choice);
|
||||
Err(anyhow!("invalid choice `{}`", choice))
|
||||
error!(r#"invalid choice "{}""#, choice);
|
||||
Err(anyhow!(r#"invalid choice "{}""#, choice))
|
||||
}
|
||||
None => {
|
||||
debug!("empty choice");
|
||||
error!("empty choice");
|
||||
Err(anyhow!("empty choice"))
|
||||
}
|
||||
}
|
||||
|
||||
+17
-55
@@ -1,70 +1,32 @@
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::{debug, error};
|
||||
use std::{
|
||||
env,
|
||||
fs::File,
|
||||
io::{Read, Write},
|
||||
process::Command,
|
||||
};
|
||||
use anyhow::{Context, Result};
|
||||
use log::debug;
|
||||
use std::{env, fs, process::Command};
|
||||
|
||||
use crate::{
|
||||
domain::msg,
|
||||
ui::choice::{self, PreEditChoice},
|
||||
};
|
||||
use crate::domain::msg::{msg_utils, Tpl};
|
||||
|
||||
pub fn open_editor_with_tpl(tpl: &[u8]) -> Result<String> {
|
||||
let path = msg::utils::draft_path();
|
||||
if path.exists() {
|
||||
debug!("draft found");
|
||||
loop {
|
||||
match choice::pre_edit() {
|
||||
Ok(choice) => match choice {
|
||||
PreEditChoice::Edit => return open_editor_with_draft(),
|
||||
PreEditChoice::Discard => break,
|
||||
PreEditChoice::Quit => return Err(anyhow!("edition aborted")),
|
||||
},
|
||||
Err(err) => error!("{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn open_with_tpl(tpl: Tpl) -> Result<Tpl> {
|
||||
let path = msg_utils::local_draft_path();
|
||||
|
||||
debug!("create draft");
|
||||
File::create(&path)
|
||||
.context(format!("cannot create draft file `{:?}`", path))?
|
||||
.write(tpl)
|
||||
.context(format!("cannot write draft file `{:?}`", path))?;
|
||||
fs::write(&path, tpl.as_bytes()).context(format!("cannot write local draft at {:?}", path))?;
|
||||
|
||||
debug!("open editor");
|
||||
Command::new(env::var("EDITOR").context("cannot find `$EDITOR` env var")?)
|
||||
Command::new(env::var("EDITOR").context(r#"cannot find "$EDITOR" env var"#)?)
|
||||
.arg(&path)
|
||||
.status()
|
||||
.context("cannot launch editor")?;
|
||||
|
||||
debug!("read draft");
|
||||
let mut draft = String::new();
|
||||
File::open(&path)
|
||||
.context(format!("cannot open draft file `{:?}`", path))?
|
||||
.read_to_string(&mut draft)
|
||||
.context(format!("cannot read draft file `{:?}`", path))?;
|
||||
let content =
|
||||
fs::read_to_string(&path).context(format!("cannot read local draft at {:?}", path))?;
|
||||
|
||||
Ok(draft)
|
||||
Ok(Tpl(content))
|
||||
}
|
||||
|
||||
pub fn open_editor_with_draft() -> Result<String> {
|
||||
let path = msg::utils::draft_path();
|
||||
|
||||
// Opens editor and saves user input to draft file
|
||||
Command::new(env::var("EDITOR").context("cannot find `EDITOR` env var")?)
|
||||
.arg(&path)
|
||||
.status()
|
||||
.context("cannot launch editor")?;
|
||||
|
||||
// Extracts draft file content
|
||||
let mut draft = String::new();
|
||||
File::open(&path)
|
||||
.context(format!("cannot open file `{:?}`", path))?
|
||||
.read_to_string(&mut draft)
|
||||
.context(format!("cannot read file `{:?}`", path))?;
|
||||
|
||||
Ok(draft)
|
||||
pub fn open_with_draft() -> Result<Tpl> {
|
||||
let path = msg_utils::local_draft_path();
|
||||
let content =
|
||||
fs::read_to_string(&path).context(format!("cannot read local draft at {:?}", path))?;
|
||||
let tpl = Tpl(content);
|
||||
open_with_tpl(tpl)
|
||||
}
|
||||
|
||||
@@ -2,4 +2,6 @@
|
||||
|
||||
pub mod choice;
|
||||
pub mod editor;
|
||||
|
||||
pub mod table;
|
||||
pub use table::*;
|
||||
|
||||
+2
-2
@@ -11,11 +11,11 @@ use unicode_width::UnicodeWidthStr;
|
||||
|
||||
/// Define the default terminal size.
|
||||
/// It is used when the size cannot be determined by the `terminal_size` crate.
|
||||
const DEFAULT_TERM_WIDTH: usize = 80;
|
||||
pub const DEFAULT_TERM_WIDTH: usize = 80;
|
||||
|
||||
/// Define the minimum size of a shrinked cell.
|
||||
/// TODO: make this customizable.
|
||||
const MAX_SHRINK_WIDTH: usize = 5;
|
||||
pub const MAX_SHRINK_WIDTH: usize = 5;
|
||||
|
||||
/// Wrapper around [ANSI escape codes] for styling cells.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user