~aodag/maildirview

ref: 416c3da428f9306feba36a26a59deac6b5fbfd1a maildirview/viewer/src/main.rs -rw-r--r-- 1.8 KiB
416c3da4Atsushi Odagiri display subject list 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)]
struct Cli {
    maildir: String,
}

fn get_subject(headers: Vec<MailHeader>) -> Result<String, ()> {
    for h in headers {
        if h.get_key() == "Subject" {
            return Ok(h.get_value());
        }
    }
    Err(())
}

fn main() -> Result<(), io::Error> {
    let cli = Cli::parse();
    let maildir = Maildir::from(cli.maildir);
    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(())
}