From 09ec573753eef123c52c4c6106e63be707675b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 25 Apr 2021 18:03:25 +0200 Subject: termion, show inbox --- Cargo.toml | 4 ++-- src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 007c8c7..adcfe8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -crossterm = "0.19" # terminal control (cursor, style) -termimad = "0.10" # markdown renderer +termion = "1" # terminal control (cursor, style) +# termimad = "0.10" # markdown renderer notmuch = { path = "notmuch" } diff --git a/src/main.rs b/src/main.rs index 1b30c67..ea7830c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,52 @@ +use std::io::{Stdout, Write, stdin, stdout}; +use termion::color; +use termion::event::Key; +use termion::input::TermRead; +use termion::raw::{IntoRawMode, RawTerminal}; + fn main() { + let stdin = stdin(); + let mut stdout = stdout().into_raw_mode().unwrap(); + + // open database let db = notmuch::Database::open(&"/home/gustav/.mail", notmuch::DatabaseMode::ReadOnly).unwrap(); - let query = db.create_query("tag:inbox and tag:unread").unwrap(); + + // get threads + let query = db.create_query("tag:inbox").unwrap(); let threads = query.search_threads().unwrap(); + let threads = threads + .map(|t| format!("thread {:?}, {:?}", + t.subject(), + t.authors())) + .collect(); + + let mut i: isize = 0; + show_threads(&mut stdout, &threads, i as usize); + + for c in stdin.keys() { + match c.unwrap() { + Key::Char('j') => i += 1, + Key::Char('k') => i -= 1, + Key::Char('q') => break, + _ => () + } + i = i.rem_euclid(threads.len() as isize); + show_threads(&mut stdout, &threads, i as usize); + } +} + +fn show_threads(stdout: &mut RawTerminal, threads: &Vec, highlight: usize) { + write!(stdout, "{}", termion::clear::All).unwrap(); - for thread in threads { - println!("thread {:?}, {:?}", thread.subject(), thread.authors()); + for (i, thread) in threads.iter().enumerate() { + write!(stdout, "{}", termion::cursor::Goto(1, (i + 1) as u16)).unwrap(); + let highlight = highlight == i; + if highlight { + write!(stdout, "{}", color::Fg(color::Red)).unwrap(); + } + writeln!(stdout, "{}", thread).unwrap(); + if highlight { + write!(stdout, "{}", color::Fg(color::Reset)).unwrap(); + } } } -- cgit v1.2.1