meli/ui/src/components/utilities.rs

407 lines
14 KiB
Rust
Raw Normal View History

2018-07-18 10:42:52 +03:00
/*! Various useful components that can be used in a generic fashion.
*/
use super::*;
2018-07-11 17:07:51 +03:00
use melib::mailbox::email::interpret_format_flowed;
2018-07-11 17:07:51 +03:00
/// A horizontally split in half container.
pub struct HSplit {
top: Entity,
bottom: Entity,
ratio: usize, // bottom/whole height * 100
}
impl HSplit {
pub fn new(top: Entity, bottom: Entity, ratio: usize) -> Self {
HSplit {
top: top,
bottom: bottom,
ratio: ratio,
}
}
}
impl Component for HSplit {
2018-07-14 15:04:42 +03:00
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
if !is_valid_area!(area) {
return;
}
2018-07-14 15:04:42 +03:00
let upper_left = upper_left!(area);
let bottom_right = bottom_right!(area);
2018-07-11 17:07:51 +03:00
let total_rows = get_y(bottom_right) - get_y(upper_left);
let bottom_entity_height = (self.ratio*total_rows )/100;
let mid = get_y(upper_left) + total_rows - bottom_entity_height;
for i in get_x(upper_left)..=get_x(bottom_right) {
2018-07-11 17:07:51 +03:00
grid[(i, mid)].set_ch('─');
}
2018-07-14 15:04:42 +03:00
let _ = self.top.component.draw(grid,
(upper_left, (get_x(bottom_right), get_y(upper_left) + mid-1)),
context);
let _ = self.bottom.component.draw(grid,
((get_x(upper_left), get_y(upper_left) + mid), bottom_right),
context);
2018-07-11 17:07:51 +03:00
grid[bottom_right].set_ch('b');
}
2018-07-14 15:04:42 +03:00
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {
self.top.rcv_event(event, context);
self.bottom.rcv_event(event, context);
}
fn is_dirty(&self) -> bool {
self.top.component.is_dirty() || self.bottom.component.is_dirty()
2018-07-11 17:07:51 +03:00
}
}
2018-07-11 18:58:57 +03:00
/// A vertically split in half container.
2018-07-11 17:07:51 +03:00
pub struct VSplit {
left: Entity,
right: Entity,
2018-07-11 18:58:57 +03:00
/// This is the width of the right container to the entire width.
2018-07-11 17:07:51 +03:00
ratio: usize, // right/(container width) * 100
}
impl VSplit {
pub fn new(left: Entity, right: Entity, ratio: usize) -> Self {
VSplit {
left: left,
right: right,
ratio: ratio,
}
}
}
impl Component for VSplit {
2018-07-14 15:04:42 +03:00
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
if !is_valid_area!(area) {
return;
}
2018-07-14 15:04:42 +03:00
let upper_left = upper_left!(area);
let bottom_right = bottom_right!(area);
2018-07-11 17:07:51 +03:00
let total_cols = get_x(bottom_right) - get_x(upper_left);
let right_entity_width = (self.ratio*total_cols )/100;
let mid = get_x(bottom_right) - right_entity_width;
2018-07-11 18:58:57 +03:00
2018-07-11 17:07:51 +03:00
if get_y(upper_left)> 1 {
let c = grid.get(mid, get_y(upper_left)-1).map(|a| a.ch()).unwrap_or_else(|| ' ');
match c {
HORZ_BOUNDARY => {
grid[(mid, get_y(upper_left)-1)].set_ch(LIGHT_DOWN_AND_HORIZONTAL);
},
_ => {},
}
}
2018-07-14 15:04:42 +03:00
for i in get_y(upper_left)..=get_y(bottom_right) {
2018-07-11 17:07:51 +03:00
grid[(mid, i)].set_ch(VERT_BOUNDARY);
2018-07-15 01:27:13 +03:00
grid[(mid, i)].set_fg(Color::Default);
grid[(mid, i)].set_bg(Color::Default);
2018-07-11 17:07:51 +03:00
}
if get_y(bottom_right)> 1 {
let c = grid.get(mid, get_y(bottom_right)-1).map(|a| a.ch()).unwrap_or_else(|| ' ');
match c {
HORZ_BOUNDARY => {
grid[(mid, get_y(bottom_right)+1)].set_ch(LIGHT_UP_AND_HORIZONTAL);
},
_ => {},
}
}
2018-07-14 15:04:42 +03:00
let _ = self.left.component.draw(grid,
(upper_left, (mid-1, get_y(bottom_right))),
context);
let _ = self.right.component.draw(grid,
((mid+1, get_y(upper_left)), bottom_right),
context);
}
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {
self.left.rcv_event(event, context);
self.right.rcv_event(event, context);
2018-07-11 17:07:51 +03:00
}
2018-07-14 15:04:42 +03:00
fn is_dirty(&self) -> bool {
self.left.component.is_dirty() || self.right.component.is_dirty()
2018-07-11 17:07:51 +03:00
}
}
2018-07-11 18:58:57 +03:00
/// A pager for text.
/// `Pager` holds its own content in its own `CellBuffer` and when `draw` is called, it draws the
/// current view of the text. It is responsible for scrolling etc.
2018-07-11 17:07:51 +03:00
pub struct Pager {
cursor_pos: usize,
2018-07-14 18:03:43 +03:00
height: usize,
width: usize,
2018-07-14 15:04:42 +03:00
dirty: bool,
2018-07-11 17:07:51 +03:00
content: CellBuffer,
}
// TODO: Make a `new` method that is content agnostic.
2018-07-11 17:07:51 +03:00
impl Pager {
pub fn from_envelope(mailbox_idx: (usize, usize), envelope_idx: usize, context: &mut Context) -> Self {
let mailbox = &mut context.accounts[mailbox_idx.0][mailbox_idx.1].as_ref().unwrap().as_ref().unwrap();
let envelope : &Envelope = &mailbox.collection[envelope_idx];
let pager_filter: Option<&String> = context.settings.pager.filter.as_ref();
let format_flowed: bool = context.settings.pager.format_flowed;
let mut text = envelope.body().text();
if let Some(bin) = pager_filter {
use std::io::Write;
use std::process::{Command, Stdio};
eprintln!("{}", bin);
let mut filter_child = Command::new(bin)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Failed to start pager filter process");
{
let mut stdin =
filter_child.stdin.as_mut().expect("failed to open stdin");
stdin.write_all(text.as_bytes()).expect("Failed to write to stdin");
}
text = String::from_utf8_lossy(&filter_child.wait_with_output().expect("Failed to wait on filter").stdout).to_string();
}
2018-07-11 17:07:51 +03:00
let lines: Vec<&str> = text.trim().split('\n').collect();
2018-07-14 18:03:43 +03:00
let height = lines.len();
let width = lines.iter().map(|l| l.len()).max().unwrap_or(0);
let mut content = CellBuffer::new(width, height, Cell::with_char(' '));
if false {
interpret_format_flowed(&text);
}
Pager::print_string(&mut content, &text);
Pager {
cursor_pos: 0,
height: height,
width: width,
dirty: true,
content: content,
}
}
2018-07-18 10:42:52 +03:00
pub fn new_from_str(s: &str) -> Self {
let lines: Vec<&str> = s.trim().split('\n').collect();
let height = lines.len();
let width = lines.iter().map(|l| l.len()).max().unwrap_or(0);
let mut content = CellBuffer::new(width, height, Cell::with_char(' '));
Pager::print_string(&mut content, s);
Pager {
cursor_pos: 0,
height: height,
width: width,
dirty: true,
content: content,
}
}
pub fn print_string(content: &mut CellBuffer, s: &str) {
let lines: Vec<&str> = s.trim().split('\n').collect();
let width = lines.iter().map(|l| l.len()).max().unwrap_or(0);
2018-07-15 01:27:13 +03:00
if width > 0 {
for (i, l) in lines.iter().enumerate() {
write_string_to_grid(l,
content,
2018-07-15 01:27:13 +03:00
Color::Default,
Color::Default,
((0, i), (width -1, i)),
true);
2018-07-15 01:27:13 +03:00
}
2018-07-11 17:07:51 +03:00
}
}
}
impl Component for Pager {
2018-07-14 15:04:42 +03:00
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
if !is_valid_area!(area) {
return;
}
2018-07-15 01:27:13 +03:00
if !self.is_dirty() {
2018-07-11 17:07:51 +03:00
return;
2018-07-11 18:58:57 +03:00
}
2018-07-14 15:04:42 +03:00
let upper_left = upper_left!(area);
let bottom_right = bottom_right!(area);
2018-07-15 01:27:13 +03:00
self.dirty = false;
if self.height == 0 || self.height == self.cursor_pos || self.width == 0 {
2018-07-14 15:04:42 +03:00
return;
}
2018-07-15 01:27:13 +03:00
clear_area(grid,
(upper_left, bottom_right));
context.dirty_areas.push_back((upper_left, bottom_right));
2018-07-14 15:04:42 +03:00
2018-07-15 01:27:13 +03:00
//let pager_context: usize = context.settings.pager.pager_context;
//let pager_stop: bool = context.settings.pager.pager_stop;
//let rows = y(bottom_right) - y(upper_left);
2018-07-15 01:27:13 +03:00
//let page_length = rows / self.height;
2018-07-15 15:35:57 +03:00
copy_area(grid, &self.content, area, ((0, self.cursor_pos), (self.width - 1, self.height - 1)));
2018-07-14 18:03:43 +03:00
context.dirty_areas.push_back(area);
2018-07-11 17:07:51 +03:00
}
2018-07-14 15:04:42 +03:00
fn process_event(&mut self, event: &UIEvent, _context: &mut Context) {
2018-07-11 17:07:51 +03:00
match event.event_type {
UIEventType::Input(Key::Char('k')) => {
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
self.dirty = true;
}
},
UIEventType::Input(Key::Char('j')) => {
2018-07-14 18:03:43 +03:00
if self.height > 0 && self.cursor_pos < self.height - 1 {
2018-07-11 17:07:51 +03:00
self.cursor_pos += 1;
self.dirty = true;
}
},
2018-07-15 01:27:13 +03:00
UIEventType::ChangeMode(UIMode::Normal) => {
self.dirty = true;
},
2018-07-16 13:36:28 +03:00
UIEventType::Resize => {
self.dirty = true;
},
2018-07-11 17:07:51 +03:00
_ => {
},
}
}
2018-07-14 15:04:42 +03:00
fn is_dirty(&self) -> bool {
self.dirty
}
}
/// Status bar.
pub struct StatusBar {
container: Entity,
status: String,
2018-07-15 01:27:13 +03:00
ex_buffer: String,
mode: UIMode,
height: usize,
2018-07-14 15:04:42 +03:00
dirty: bool,
}
impl StatusBar {
pub fn new(container: Entity) -> Self {
StatusBar {
container: container,
2018-07-15 01:27:13 +03:00
status: String::with_capacity(256),
ex_buffer: String::with_capacity(256),
2018-07-14 15:04:42 +03:00
dirty: true,
2018-07-15 01:27:13 +03:00
mode: UIMode::Normal,
height: 1,
2018-07-14 15:04:42 +03:00
}
}
fn draw_status_bar(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
clear_area(grid, area);
2018-07-15 01:27:13 +03:00
write_string_to_grid(&self.status,
2018-07-14 15:04:42 +03:00
grid,
Color::Byte(123),
Color::Byte(26),
area, false);
change_colors(grid, area, Color::Byte(123), Color::Byte(26));
2018-07-14 15:04:42 +03:00
context.dirty_areas.push_back(area);
}
2018-07-15 01:27:13 +03:00
fn draw_execute_bar(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
clear_area(grid, area);
write_string_to_grid(&self.ex_buffer,
grid,
Color::Byte(219),
Color::Byte(88),
area, false);
change_colors(grid, area, Color::Byte(219), Color::Byte(88));
2018-07-15 01:27:13 +03:00
context.dirty_areas.push_back(area);
}
2018-07-11 17:07:51 +03:00
}
2018-07-14 15:04:42 +03:00
impl Component for StatusBar {
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
if !is_valid_area!(area) {
return;
}
2018-07-14 15:04:42 +03:00
let upper_left = upper_left!(area);
let bottom_right = bottom_right!(area);
2018-07-15 01:27:13 +03:00
2018-07-14 15:04:42 +03:00
let total_rows = get_y(bottom_right) - get_y(upper_left);
2018-07-15 01:27:13 +03:00
if total_rows <= self.height {
2018-07-14 15:04:42 +03:00
return;
}
2018-07-15 01:27:13 +03:00
let height = self.height;
2018-07-14 15:04:42 +03:00
let _ = self.container.component.draw(grid,
2018-07-15 01:27:13 +03:00
(upper_left, (get_x(bottom_right), get_y(bottom_right) - height)),
2018-07-14 15:04:42 +03:00
context);
2018-07-15 01:27:13 +03:00
if !self.is_dirty() {
return;
}
self.dirty = false;
2018-07-14 15:04:42 +03:00
self.draw_status_bar(grid, (set_y(upper_left, get_y(bottom_right)), bottom_right), context);
2018-07-15 01:27:13 +03:00
match self.mode {
UIMode::Normal => {
},
UIMode::Execute => {
self.draw_execute_bar(grid,
(set_y(upper_left, get_y(bottom_right) - height + 1), set_y(bottom_right, get_y(bottom_right) - height+1)),
context);
},
2018-07-21 11:20:13 +03:00
_ => {},
2018-07-15 01:27:13 +03:00
}
2018-07-14 15:04:42 +03:00
}
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {
self.container.rcv_event(event, context);
2018-07-21 11:20:13 +03:00
match &event.event_type {
UIEventType::RefreshMailbox((ref idx_a, ref idx_f)) => {
let m = &context.accounts[*idx_a][*idx_f].as_ref().unwrap().as_ref().unwrap();
self.status = format!("{} |Mailbox: {}, Messages: {}, New: {}", self.mode, m.folder.name(), m.collection.len(), m.collection.iter().filter(|e| !e.is_seen()).count());
2018-07-14 15:04:42 +03:00
self.dirty = true;
},
2018-07-15 01:27:13 +03:00
UIEventType::ChangeMode(m) => {
let offset = self.status.find('|').unwrap_or(self.status.len());
self.status.replace_range(..offset, &format!("{} ", m));
self.dirty = true;
2018-07-21 11:20:13 +03:00
self.mode = m.clone();
2018-07-15 01:27:13 +03:00
match m {
UIMode::Normal => {
self.height = 1;
context.replies.push_back(UIEvent { id: 0, event_type: UIEventType::Command(self.ex_buffer.clone())});
2018-07-15 01:27:13 +03:00
self.ex_buffer.clear()
},
UIMode::Execute => {
self.height = 2;
},
2018-07-21 11:20:13 +03:00
_ => {},
2018-07-15 01:27:13 +03:00
};
},
UIEventType::ExInput(Key::Char(c)) => {
self.dirty = true;
2018-07-21 11:20:13 +03:00
self.ex_buffer.push(*c);
2018-07-15 01:27:13 +03:00
},
2018-07-16 13:36:28 +03:00
UIEventType::Resize => {
self.dirty = true;
},
2018-07-14 15:04:42 +03:00
_ => {},
}
}
fn is_dirty(&self) -> bool {
self.dirty || self.container.component.is_dirty()
}
}
// A box with a text content.
pub struct TextBox {
content: String,
}
impl TextBox {
pub fn new(s: String) -> Self {
TextBox {
content: s,
}
}
}
impl Component for TextBox {
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
}
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {
return;
}
}