2018-08-07 15:01:15 +03:00
|
|
|
/*
|
|
|
|
* meli - ui crate.
|
|
|
|
*
|
|
|
|
* Copyright 2017-2018 Manos Pitsidianakis
|
|
|
|
*
|
|
|
|
* This file is part of meli.
|
|
|
|
*
|
|
|
|
* meli is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* meli is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-18 10:42:52 +03:00
|
|
|
/*! Various useful components that can be used in a generic fashion.
|
2018-07-27 21:37:56 +03:00
|
|
|
*/
|
2018-07-20 12:15:06 +03:00
|
|
|
use super::*;
|
2018-07-11 17:07:51 +03:00
|
|
|
|
|
|
|
/// A horizontally split in half container.
|
|
|
|
pub struct HSplit {
|
|
|
|
top: Entity,
|
|
|
|
bottom: Entity,
|
2018-07-22 14:45:22 +03:00
|
|
|
show_divider: bool,
|
2018-07-11 17:07:51 +03:00
|
|
|
ratio: usize, // bottom/whole height * 100
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for HSplit {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display subject/info
|
|
|
|
self.top.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
impl HSplit {
|
2018-07-22 14:45:22 +03:00
|
|
|
pub fn new(top: Entity, bottom: Entity, ratio: usize, show_divider: bool) -> Self {
|
2018-07-11 17:07:51 +03:00
|
|
|
HSplit {
|
2018-08-07 15:01:15 +03:00
|
|
|
top,
|
|
|
|
bottom,
|
|
|
|
show_divider,
|
|
|
|
ratio,
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for HSplit {
|
2018-07-14 15:04:42 +03:00
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
2018-07-20 12:15:06 +03:00
|
|
|
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);
|
2018-07-27 21:37:56 +03:00
|
|
|
let bottom_entity_height = (self.ratio * total_rows) / 100;
|
2018-07-11 17:07:51 +03:00
|
|
|
let mid = get_y(upper_left) + total_rows - bottom_entity_height;
|
|
|
|
|
2018-07-22 14:45:22 +03:00
|
|
|
if self.show_divider {
|
|
|
|
for i in get_x(upper_left)..=get_x(bottom_right) {
|
|
|
|
grid[(i, mid)].set_ch('─');
|
|
|
|
}
|
2018-08-15 15:36:51 +03:00
|
|
|
context.dirty_areas.push_back(((get_x(upper_left), mid), (get_x(bottom_right), mid)));
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2018-08-15 15:36:51 +03:00
|
|
|
|
2018-08-07 15:01:15 +03:00
|
|
|
self.top.component.draw(
|
2018-07-27 21:37:56 +03:00
|
|
|
grid,
|
|
|
|
(
|
|
|
|
upper_left,
|
|
|
|
(get_x(bottom_right), get_y(upper_left) + mid - 1),
|
|
|
|
),
|
|
|
|
context,
|
|
|
|
);
|
2018-08-07 15:01:15 +03:00
|
|
|
self.bottom.component.draw(
|
2018-07-27 21:37:56 +03:00
|
|
|
grid,
|
|
|
|
((get_x(upper_left), get_y(upper_left) + mid), bottom_right),
|
|
|
|
context,
|
|
|
|
);
|
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) {
|
|
|
|
self.top.rcv_event(event, context);
|
|
|
|
self.bottom.rcv_event(event, context);
|
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
2018-07-27 21:37:56 +03:00
|
|
|
self.top.component.is_dirty() || self.bottom.component.is_dirty()
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {
|
|
|
|
self.top.component.set_dirty();
|
|
|
|
self.bottom.component.set_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-22 14:45:22 +03:00
|
|
|
show_divider: bool,
|
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
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for VSplit {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display focused entity
|
|
|
|
self.right.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
impl VSplit {
|
2018-07-22 14:45:22 +03:00
|
|
|
pub fn new(left: Entity, right: Entity, ratio: usize, show_divider: bool) -> Self {
|
2018-07-11 17:07:51 +03:00
|
|
|
VSplit {
|
2018-08-07 15:01:15 +03:00
|
|
|
left,
|
|
|
|
right,
|
|
|
|
show_divider,
|
|
|
|
ratio,
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for VSplit {
|
2018-07-14 15:04:42 +03:00
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
2018-07-20 12:15:06 +03:00
|
|
|
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);
|
2018-07-27 21:37:56 +03:00
|
|
|
let right_entity_width = (self.ratio * total_cols) / 100;
|
2018-07-11 17:07:51 +03:00
|
|
|
let mid = get_x(bottom_right) - right_entity_width;
|
2018-07-11 18:58:57 +03:00
|
|
|
|
2018-07-27 21:37:56 +03:00
|
|
|
if get_y(upper_left) > 1 {
|
2018-08-07 15:01:15 +03:00
|
|
|
let c = grid
|
|
|
|
.get(mid, get_y(upper_left) - 1)
|
2018-07-27 21:37:56 +03:00
|
|
|
.map(|a| a.ch())
|
|
|
|
.unwrap_or_else(|| ' ');
|
2018-08-07 15:01:15 +03:00
|
|
|
if let HORZ_BOUNDARY = c {
|
|
|
|
grid[(mid, get_y(upper_left) - 1)].set_ch(LIGHT_DOWN_AND_HORIZONTAL);
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 14:45:22 +03:00
|
|
|
if self.show_divider {
|
|
|
|
for i in get_y(upper_left)..=get_y(bottom_right) {
|
|
|
|
grid[(mid, i)].set_ch(VERT_BOUNDARY);
|
|
|
|
grid[(mid, i)].set_fg(Color::Default);
|
|
|
|
grid[(mid, i)].set_bg(Color::Default);
|
|
|
|
}
|
2018-07-27 21:37:56 +03:00
|
|
|
if get_y(bottom_right) > 1 {
|
2018-08-07 15:01:15 +03:00
|
|
|
let c = grid
|
|
|
|
.get(mid, get_y(bottom_right) - 1)
|
2018-07-27 21:37:56 +03:00
|
|
|
.map(|a| a.ch())
|
|
|
|
.unwrap_or_else(|| ' ');
|
2018-07-22 14:45:22 +03:00
|
|
|
match c {
|
|
|
|
HORZ_BOUNDARY => {
|
2018-07-27 21:37:56 +03:00
|
|
|
grid[(mid, get_y(bottom_right) + 1)].set_ch(LIGHT_UP_AND_HORIZONTAL);
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-07-22 14:45:22 +03:00
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2018-08-15 15:36:51 +03:00
|
|
|
context.dirty_areas.push_back(((mid, get_y(upper_left)), (mid, get_y(bottom_right))));
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2018-08-07 15:01:15 +03:00
|
|
|
self.left
|
|
|
|
.component
|
|
|
|
.draw(grid, (upper_left, (mid - 1, get_y(bottom_right))), context);
|
|
|
|
self.right
|
|
|
|
.component
|
|
|
|
.draw(grid, ((mid + 1, get_y(upper_left)), bottom_right), context);
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
|
|
|
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 {
|
2018-07-27 21:37:56 +03:00
|
|
|
self.left.component.is_dirty() || self.right.component.is_dirty()
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {
|
|
|
|
self.left.component.set_dirty();
|
|
|
|
self.right.component.set_dirty();
|
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:51:48 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum PagerMovement {
|
|
|
|
PageUp,
|
|
|
|
PageDown,
|
|
|
|
}
|
|
|
|
|
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-08-21 19:51:48 +03:00
|
|
|
#[derive(Default, Debug)]
|
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,
|
2018-08-21 19:51:48 +03:00
|
|
|
movement: Option<PagerMovement>,
|
2018-08-16 16:32:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for Pager {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display info
|
|
|
|
write!(f, "pager")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
impl Pager {
|
2018-08-16 16:32:47 +03:00
|
|
|
pub fn update_from_string(&mut self, text: String) -> () {
|
|
|
|
let lines: Vec<&str> = text.trim().split('\n').collect();
|
|
|
|
let height = lines.len() + 1;
|
|
|
|
let width = lines.iter().map(|l| l.len()).max().unwrap_or(0);
|
|
|
|
let mut content = CellBuffer::new(width, height, Cell::with_char(' '));
|
|
|
|
//interpret_format_flowed(&text);
|
|
|
|
Pager::print_string(&mut content, &text);
|
|
|
|
self.content = content;
|
|
|
|
self.height = height;
|
|
|
|
self.width = width;
|
|
|
|
self.dirty = true;
|
|
|
|
self.cursor_pos = 0;
|
|
|
|
}
|
2018-07-23 15:40:13 +03:00
|
|
|
pub fn from_string(mut text: String, context: &mut Context, cursor_pos: Option<usize>) -> Self {
|
2018-07-22 14:05:12 +03:00
|
|
|
let pager_filter: Option<&String> = context.settings.pager.filter.as_ref();
|
2018-07-22 23:11:07 +03:00
|
|
|
//let format_flowed: bool = context.settings.pager.format_flowed;
|
2018-07-17 17:16:16 +03:00
|
|
|
if let Some(bin) = pager_filter {
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
let mut filter_child = Command::new(bin)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("Failed to start pager filter process");
|
|
|
|
{
|
2018-07-27 21:37:56 +03:00
|
|
|
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");
|
2018-07-17 17:16:16 +03:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:37:56 +03:00
|
|
|
text = String::from_utf8_lossy(
|
|
|
|
&filter_child
|
|
|
|
.wait_with_output()
|
|
|
|
.expect("Failed to wait on filter")
|
|
|
|
.stdout,
|
|
|
|
).to_string();
|
2018-07-17 17:16:16 +03:00
|
|
|
}
|
2018-07-23 15:40:13 +03:00
|
|
|
|
2018-07-22 23:11:07 +03:00
|
|
|
let lines: Vec<&str> = text.trim().split('\n').collect();
|
2018-07-22 15:44:44 +03:00
|
|
|
let height = lines.len() + 1;
|
2018-07-14 18:03:43 +03:00
|
|
|
let width = lines.iter().map(|l| l.len()).max().unwrap_or(0);
|
|
|
|
let mut content = CellBuffer::new(width, height, Cell::with_char(' '));
|
2018-07-22 15:44:44 +03:00
|
|
|
//interpret_format_flowed(&text);
|
2018-07-17 17:16:16 +03:00
|
|
|
Pager::print_string(&mut content, &text);
|
|
|
|
Pager {
|
2018-07-23 15:40:13 +03:00
|
|
|
cursor_pos: cursor_pos.unwrap_or(0),
|
2018-07-17 17:16:16 +03:00
|
|
|
height: height,
|
|
|
|
width: width,
|
|
|
|
dirty: true,
|
|
|
|
content: content,
|
2018-08-21 19:51:48 +03:00
|
|
|
.. Default::default()
|
2018-07-17 17:16:16 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-24 11:34:44 +03:00
|
|
|
pub fn from_str(s: &str, cursor_pos: Option<usize>) -> Self {
|
2018-07-18 10:42:52 +03:00
|
|
|
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 {
|
2018-07-24 11:34:44 +03:00
|
|
|
cursor_pos: cursor_pos.unwrap_or(0),
|
2018-08-07 15:01:15 +03:00
|
|
|
height,
|
|
|
|
width,
|
2018-07-18 10:42:52 +03:00
|
|
|
dirty: true,
|
2018-08-07 15:01:15 +03:00
|
|
|
content,
|
2018-08-21 19:51:48 +03:00
|
|
|
.. Default::default()
|
2018-07-18 10:42:52 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 19:51:48 +03:00
|
|
|
pub fn from_buf(content: CellBuffer, cursor_pos: Option<usize>) -> Self {
|
|
|
|
let (width, height) = content.size();
|
2018-07-23 22:21:41 +03:00
|
|
|
Pager {
|
2018-07-24 11:34:44 +03:00
|
|
|
cursor_pos: cursor_pos.unwrap_or(0),
|
2018-08-07 15:01:15 +03:00
|
|
|
height,
|
|
|
|
width,
|
2018-07-23 22:21:41 +03:00
|
|
|
dirty: true,
|
2018-08-07 15:01:15 +03:00
|
|
|
content,
|
2018-08-21 19:51:48 +03:00
|
|
|
.. Default::default()
|
2018-07-23 22:21:41 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 17:16:16 +03:00
|
|
|
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() {
|
2018-07-27 21:37:56 +03:00
|
|
|
write_string_to_grid(
|
|
|
|
l,
|
|
|
|
content,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-07-23 15:40:13 +03:00
|
|
|
pub fn cursor_pos(&self) -> usize {
|
|
|
|
self.cursor_pos
|
|
|
|
}
|
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) {
|
2018-07-20 12:15:06 +03:00
|
|
|
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
|
|
|
|
2018-07-15 01:27:13 +03:00
|
|
|
self.dirty = false;
|
2018-08-21 19:51:48 +03:00
|
|
|
|
|
|
|
let height = height!(area);
|
|
|
|
if let Some(mvm) = self.movement.take() {
|
|
|
|
// TODO: Spend some time on this
|
|
|
|
match mvm {
|
|
|
|
PagerMovement::PageUp => {
|
|
|
|
self.cursor_pos = self.cursor_pos.saturating_sub(height);
|
|
|
|
},
|
|
|
|
PagerMovement::PageDown => {
|
|
|
|
if self.cursor_pos + 2*height + 1 < self.height {
|
|
|
|
self.cursor_pos += height;
|
|
|
|
} else {
|
|
|
|
self.cursor_pos = self.height.saturating_sub(height).saturating_sub(1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.height > height {
|
|
|
|
if self.cursor_pos > 0 && self.cursor_pos + height >= self.height {
|
|
|
|
self.cursor_pos = self.height.saturating_sub(height).saturating_sub(2);
|
|
|
|
//return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.cursor_pos = 0;
|
2018-08-10 11:06:47 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 15:26:06 +03:00
|
|
|
if self.height == 0 || self.height == self.cursor_pos || self.width == 0 {
|
2018-07-14 15:04:42 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-08-21 19:51:48 +03:00
|
|
|
|
2018-08-05 12:44:31 +03:00
|
|
|
clear_area(grid, area);
|
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;
|
2018-07-20 12:44:04 +03:00
|
|
|
//let rows = y(bottom_right) - y(upper_left);
|
2018-07-15 01:27:13 +03:00
|
|
|
//let page_length = rows / self.height;
|
2018-07-27 21:37:56 +03:00
|
|
|
copy_area_with_break(
|
|
|
|
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;
|
|
|
|
}
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
UIEventType::Input(Key::Char('j')) => {
|
2018-08-10 11:06:47 +03:00
|
|
|
if self.height > 0 && self.cursor_pos + 1 < self.height {
|
2018-07-11 17:07:51 +03:00
|
|
|
self.cursor_pos += 1;
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2018-08-21 19:51:48 +03:00
|
|
|
UIEventType::Input(Key::PageUp) => {
|
|
|
|
self.movement = Some(PagerMovement::PageUp);
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
|
|
|
UIEventType::Input(Key::PageDown) => {
|
|
|
|
self.movement = Some(PagerMovement::PageDown);
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2018-07-15 01:27:13 +03:00
|
|
|
UIEventType::ChangeMode(UIMode::Normal) => {
|
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2018-07-16 13:36:28 +03:00
|
|
|
UIEventType::Resize => {
|
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 15:04:42 +03:00
|
|
|
fn is_dirty(&self) -> bool {
|
|
|
|
self.dirty
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Status bar.
|
|
|
|
pub struct StatusBar {
|
|
|
|
container: Entity,
|
|
|
|
status: String,
|
2018-07-23 16:29:22 +03:00
|
|
|
notifications: VecDeque<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,
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for StatusBar {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display info
|
|
|
|
write!(f, "status bar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 15:04:42 +03:00
|
|
|
impl StatusBar {
|
|
|
|
pub fn new(container: Entity) -> Self {
|
|
|
|
StatusBar {
|
2018-08-07 15:01:15 +03:00
|
|
|
container,
|
2018-07-15 01:27:13 +03:00
|
|
|
status: String::with_capacity(256),
|
2018-07-23 16:29:22 +03:00
|
|
|
notifications: VecDeque::new(),
|
2018-07-15 01:27:13 +03:00
|
|
|
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-23 16:29:22 +03:00
|
|
|
if let Some(n) = self.notifications.pop_front() {
|
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
write_string_to_grid(&n, grid, Color::Byte(219), Color::Byte(88), area, false);
|
2018-07-23 16:29:22 +03:00
|
|
|
} else {
|
2018-07-27 21:37:56 +03:00
|
|
|
write_string_to_grid(
|
|
|
|
&self.status,
|
|
|
|
grid,
|
|
|
|
Color::Byte(123),
|
|
|
|
Color::Byte(26),
|
|
|
|
area,
|
|
|
|
false,
|
|
|
|
);
|
2018-07-23 16:29:22 +03:00
|
|
|
}
|
2018-07-17 17:16:16 +03:00
|
|
|
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);
|
2018-07-27 21:37:56 +03:00
|
|
|
write_string_to_grid(
|
|
|
|
&self.ex_buffer,
|
|
|
|
grid,
|
|
|
|
Color::Byte(219),
|
|
|
|
Color::Byte(88),
|
|
|
|
area,
|
|
|
|
false,
|
|
|
|
);
|
2018-07-17 17:16:16 +03:00
|
|
|
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) {
|
2018-07-20 12:15:06 +03:00
|
|
|
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
|
|
|
|
2018-08-07 15:01:15 +03:00
|
|
|
self.container.component.draw(
|
2018-07-27 21:37:56 +03:00
|
|
|
grid,
|
|
|
|
(
|
|
|
|
upper_left,
|
|
|
|
(get_x(bottom_right), get_y(bottom_right) - height),
|
|
|
|
),
|
|
|
|
context,
|
|
|
|
);
|
2018-07-16 11:08:04 +03:00
|
|
|
|
2018-07-15 01:27:13 +03:00
|
|
|
if !self.is_dirty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.dirty = false;
|
2018-07-27 21:37:56 +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 {
|
2018-07-27 21:37:56 +03:00
|
|
|
UIMode::Normal => {}
|
2018-07-15 01:27:13 +03:00
|
|
|
UIMode::Execute => {
|
2018-07-27 21:37:56 +03:00
|
|
|
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-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)) => {
|
2018-08-03 13:46:08 +03:00
|
|
|
match context.accounts[*idx_a].status(*idx_f) {
|
2018-08-15 15:32:30 +03:00
|
|
|
Ok(_) => {}
|
2018-08-03 13:46:08 +03:00
|
|
|
Err(_) => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 15:01:15 +03:00
|
|
|
let m = &context.accounts[*idx_a][*idx_f].as_ref().unwrap();
|
2018-07-27 21:37:56 +03:00
|
|
|
self.status = format!(
|
2018-08-04 20:40:20 +03:00
|
|
|
"{} | Mailbox: {}, Messages: {}, New: {}",
|
2018-07-27 21:37:56 +03:00
|
|
|
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-27 21:37:56 +03:00
|
|
|
}
|
2018-07-15 01:27:13 +03:00
|
|
|
UIEventType::ChangeMode(m) => {
|
2018-08-05 16:26:42 +03:00
|
|
|
let offset = self.status.find('|').unwrap_or_else(|| self.status.len());
|
2018-07-15 01:27:13 +03:00
|
|
|
self.status.replace_range(..offset, &format!("{} ", m));
|
|
|
|
self.dirty = true;
|
2018-08-07 15:01:15 +03:00
|
|
|
self.mode = *m;
|
2018-07-15 01:27:13 +03:00
|
|
|
match m {
|
|
|
|
UIMode::Normal => {
|
|
|
|
self.height = 1;
|
2018-08-07 16:14:06 +03:00
|
|
|
if !self.ex_buffer.is_empty() {
|
|
|
|
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()
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2018-07-15 01:27:13 +03:00
|
|
|
UIMode::Execute => {
|
|
|
|
self.height = 2;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2018-07-15 01:27:13 +03:00
|
|
|
};
|
2018-07-27 21:37:56 +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-27 21:37:56 +03:00
|
|
|
}
|
2018-08-07 16:14:06 +03:00
|
|
|
UIEventType::ExInput(Key::Ctrl('u')) => {
|
|
|
|
self.dirty = true;
|
|
|
|
self.ex_buffer.clear();
|
|
|
|
}
|
|
|
|
UIEventType::ExInput(Key::Backspace) | UIEventType::ExInput(Key::Ctrl('h')) => {
|
|
|
|
self.dirty = true;
|
|
|
|
self.ex_buffer.pop();
|
|
|
|
}
|
2018-07-16 13:36:28 +03:00
|
|
|
UIEventType::Resize => {
|
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2018-07-23 16:29:22 +03:00
|
|
|
UIEventType::StatusNotification(s) => {
|
|
|
|
self.notifications.push_back(s.clone());
|
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
|
|
|
self.dirty || self.container.component.is_dirty()
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
2018-07-20 12:15:06 +03:00
|
|
|
|
|
|
|
// A box with a text content.
|
|
|
|
pub struct TextBox {
|
2018-07-24 20:20:32 +03:00
|
|
|
_content: String,
|
2018-07-20 12:15:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TextBox {
|
|
|
|
pub fn new(s: String) -> Self {
|
2018-07-27 21:37:56 +03:00
|
|
|
TextBox { _content: s }
|
2018-07-20 12:15:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for TextBox {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display info
|
|
|
|
write!(f, "text box")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 12:15:06 +03:00
|
|
|
impl Component for TextBox {
|
2018-07-27 21:37:56 +03:00
|
|
|
fn draw(&mut self, _grid: &mut CellBuffer, _area: Area, _context: &mut Context) {}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn process_event(&mut self, _event: &UIEvent, _context: &mut Context) {}
|
|
|
|
fn set_dirty(&mut self) {}
|
2018-07-20 12:15:06 +03:00
|
|
|
}
|
2018-08-03 13:46:08 +03:00
|
|
|
|
|
|
|
pub struct Progress {
|
|
|
|
description: String,
|
|
|
|
total_work: usize,
|
|
|
|
finished: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Progress {
|
|
|
|
pub fn new(s: String, total_work: usize) -> Self {
|
|
|
|
Progress {
|
|
|
|
description: s,
|
2018-08-07 15:01:15 +03:00
|
|
|
total_work,
|
2018-08-03 13:46:08 +03:00
|
|
|
finished: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_work(&mut self, n: usize) -> () {
|
|
|
|
if self.finished >= self.total_work {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.finished += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn percentage(&self) -> usize {
|
|
|
|
if self.total_work > 0 {
|
|
|
|
100 * self.finished / self.total_work
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn description(&self) -> &str {
|
|
|
|
&self.description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for Progress {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display info
|
|
|
|
write!(f, "progress bar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 13:46:08 +03:00
|
|
|
impl Component for Progress {
|
|
|
|
fn draw(&mut self, _grid: &mut CellBuffer, _area: Area, _context: &mut Context) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn process_event(&mut self, _event: &UIEvent, _context: &mut Context) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Tabbed {
|
|
|
|
children: Vec<Box<Component>>,
|
|
|
|
cursor_pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Tabbed {
|
|
|
|
pub fn new(children: Vec<Box<Component>>) -> Self {
|
|
|
|
Tabbed {
|
|
|
|
children,
|
|
|
|
cursor_pos: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn draw_tabs(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
let mut x = get_x(upper_left!(area));
|
|
|
|
let mut y: usize = get_y(upper_left!(area));
|
|
|
|
for (idx, c) in self.children.iter().enumerate() {
|
|
|
|
let (fg, bg) = if idx == self.cursor_pos {
|
|
|
|
(Color::Default, Color::Default)
|
|
|
|
} else {
|
|
|
|
(Color::Byte(15), Color::Byte(8))
|
|
|
|
};
|
|
|
|
let (x_, _y_) = write_string_to_grid(
|
|
|
|
&format!(" {} ", c),
|
|
|
|
grid,
|
|
|
|
fg,
|
|
|
|
bg,
|
|
|
|
(set_x(upper_left!(area), x), bottom_right!(area)),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
x = x_ + 1;
|
|
|
|
if y != _y_ {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
y = _y_;
|
|
|
|
}
|
|
|
|
let (cols, _) = grid.size();
|
|
|
|
let cslice: &mut [Cell] = grid;
|
|
|
|
for c in cslice[(y * cols) + x..(y * cols) + cols].iter_mut() {
|
|
|
|
c.set_bg(Color::Byte(7));
|
|
|
|
}
|
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
|
|
|
pub fn add_component(&mut self, new: Box<Component>) {
|
|
|
|
self.children.push(new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Tabbed {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// TODO display info
|
|
|
|
write!(f, "tabs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Tabbed {
|
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
if self.children.len() > 1 {
|
|
|
|
self.draw_tabs(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
upper_left!(area),
|
|
|
|
set_x(upper_left!(area), get_x(bottom_right!(area))),
|
|
|
|
),
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
let y = get_y(upper_left!(area));
|
|
|
|
self.children[self.cursor_pos].draw(
|
|
|
|
grid,
|
|
|
|
(set_y(upper_left!(area), y + 1), bottom_right!(area)),
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.children[self.cursor_pos].draw(grid, area, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {
|
|
|
|
match &event.event_type {
|
|
|
|
UIEventType::Input(Key::Char('T')) => {
|
|
|
|
self.cursor_pos = (self.cursor_pos + 1) % self.children.len();
|
|
|
|
self.children[self.cursor_pos].set_dirty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
self.children[self.cursor_pos].process_event(event, context);
|
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
|
|
|
self.children[self.cursor_pos].is_dirty()
|
|
|
|
}
|
|
|
|
fn set_dirty(&mut self) {}
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|