~aodag/maildirview

416c3da428f9306feba36a26a59deac6b5fbfd1a — Atsushi Odagiri 8 months ago ed445b3 master
display subject list
2 files changed, 41 insertions(+), 7 deletions(-)

M viewer/Cargo.toml
M viewer/src/main.rs
M viewer/Cargo.toml => viewer/Cargo.toml +2 -0
@@ 7,5 7,7 @@ edition = "2021"

[dependencies]
clap = {version="3.0.12", features=["derive"]}
crossterm = "0.22.1"
maildir = "0.5.0"
mailparse = "0.13.8"
tui = "0.17.0"

M viewer/src/main.rs => viewer/src/main.rs +39 -7
@@ 1,6 1,16 @@
use clap::Parser;
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
use crossterm::execute;
use crossterm::terminal::{
    disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use maildir::*;
use mailparse::*;
use std::time::Duration;
use std::{io, thread};
use tui::backend::CrosstermBackend;
use tui::widgets::{Block, Borders, List, ListItem};
use tui::Terminal;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]


@@ 17,13 27,35 @@ fn get_subject(headers: Vec<MailHeader>) -> Result<String, ()> {
    Err(())
}

fn main() {
fn main() -> Result<(), io::Error> {
    let cli = Cli::parse();
    let maildir = Maildir::from(cli.maildir);
    for entry in maildir.list_new() {
        match get_subject(entry.unwrap().headers().unwrap()) {
            Ok(s) => println!("Subject: {}", s),
            Err(_) => println!("failed"),
        }
    }
    let entries = maildir.list_new();
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;
    let items: Vec<ListItem> = entries.into_iter().map(|e| {
        let subject = get_subject(e.unwrap().headers().unwrap()).unwrap();
        ListItem::new(subject)
    }).collect();
    terminal.draw(|f| {
        let size = f.size();
        let list = List::new(items)
            .block(Block::default().title("List").borders(Borders::ALL))
            //.style(Style::default().fg(Color::White))
            //.highlight_style(Style::default().add_modifier(Modifier::ITALIC))
            .highlight_symbol(">>");
        f.render_widget(list, size);
    })?;
    thread::sleep(Duration::from_millis(5000));
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture,
    )?;
    disable_raw_mode()?;
    terminal.show_cursor()?;
    Ok(())
}