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
|
|
|
|
2019-02-25 11:11:56 +02:00
|
|
|
mod widgets;
|
|
|
|
|
|
|
|
pub use self::widgets::*;
|
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
/// A horizontally split in half container.
|
2018-08-23 14:39:54 +03:00
|
|
|
#[derive(Debug)]
|
2018-07-11 17:07:51 +03:00
|
|
|
pub struct HSplit {
|
2019-04-10 22:01:02 +03:00
|
|
|
top: Box<Component>,
|
|
|
|
bottom: Box<Component>,
|
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
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
|
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
|
2018-08-23 14:39:54 +03:00
|
|
|
Display::fmt(&self.top, f)
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
impl HSplit {
|
2019-04-10 22:01:02 +03:00
|
|
|
pub fn new(
|
|
|
|
top: Box<Component>,
|
|
|
|
bottom: Box<Component>,
|
|
|
|
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,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
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);
|
2019-04-10 22:01:02 +03:00
|
|
|
let bottom_component_height = (self.ratio * total_rows) / 100;
|
|
|
|
let mid = get_y(upper_left) + total_rows - bottom_component_height;
|
2018-07-11 17:07:51 +03:00
|
|
|
|
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-23 15:36:52 +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
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
self.top.draw(
|
2018-07-27 21:37:56 +03:00
|
|
|
grid,
|
|
|
|
(
|
|
|
|
upper_left,
|
|
|
|
(get_x(bottom_right), get_y(upper_left) + mid - 1),
|
|
|
|
),
|
|
|
|
context,
|
|
|
|
);
|
2019-04-10 22:01:02 +03:00
|
|
|
self.bottom.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
|
|
|
}
|
2019-02-26 17:50:47 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.top.process_event(event, context) || self.bottom.process_event(event, context)
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2018-07-14 15:04:42 +03:00
|
|
|
fn is_dirty(&self) -> bool {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.top.is_dirty() || self.bottom.is_dirty()
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.top.set_dirty();
|
|
|
|
self.bottom.set_dirty();
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2019-05-10 22:00:56 +03:00
|
|
|
fn get_shortcuts(&self, context: &Context) -> ShortcutMaps {
|
2019-03-04 10:15:11 +02:00
|
|
|
let mut top_map = self.top.get_shortcuts(context);
|
|
|
|
top_map.extend(self.bottom.get_shortcuts(context).into_iter());
|
2019-03-02 19:25:11 +02:00
|
|
|
top_map
|
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
|
2018-07-11 18:58:57 +03:00
|
|
|
/// A vertically split in half container.
|
2018-08-23 14:39:54 +03:00
|
|
|
#[derive(Debug)]
|
2018-07-11 17:07:51 +03:00
|
|
|
pub struct VSplit {
|
2019-04-10 22:01:02 +03:00
|
|
|
left: Box<Component>,
|
|
|
|
right: Box<Component>,
|
2018-07-22 14:45:22 +03:00
|
|
|
show_divider: bool,
|
2019-03-26 16:59:35 +02:00
|
|
|
prev_visibility: (bool, 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
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl fmt::Display for VSplit {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-04-10 22:01:02 +03:00
|
|
|
// TODO display focused component
|
2018-08-23 14:39:54 +03:00
|
|
|
Display::fmt(&self.right, f)
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
impl VSplit {
|
2019-04-10 22:01:02 +03:00
|
|
|
pub fn new(
|
|
|
|
left: Box<Component>,
|
|
|
|
right: Box<Component>,
|
|
|
|
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,
|
2019-03-26 16:59:35 +02:00
|
|
|
prev_visibility: (true, true),
|
2018-08-07 15:01:15 +03:00
|
|
|
ratio,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
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);
|
2019-03-26 16:59:35 +02:00
|
|
|
let visibility = (self.left.is_visible(), self.right.is_visible());
|
|
|
|
if visibility != self.prev_visibility {
|
|
|
|
self.set_dirty();
|
|
|
|
self.prev_visibility = visibility;
|
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
let right_component_width = match visibility {
|
2019-03-26 16:59:35 +02:00
|
|
|
(true, true) => (self.ratio * total_cols) / 100,
|
|
|
|
(false, true) => total_cols,
|
|
|
|
(true, false) => 0,
|
|
|
|
(false, false) => {
|
|
|
|
clear_area(grid, area);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2019-03-26 19:53:39 +02:00
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
let mid = get_x(bottom_right) - right_component_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)
|
2019-05-13 22:05:00 +03:00
|
|
|
.map(Cell::ch)
|
2018-07-27 21:37:56 +03:00
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:59:35 +02:00
|
|
|
if self.show_divider && mid != get_x(upper_left) {
|
2018-07-22 14:45:22 +03:00
|
|
|
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)
|
2019-05-13 22:05:00 +03:00
|
|
|
.map(Cell::ch)
|
2018-07-27 21:37:56 +03:00
|
|
|
.unwrap_or_else(|| ' ');
|
2018-08-23 15:36:52 +03:00
|
|
|
if let HORZ_BOUNDARY = c {
|
|
|
|
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-23 15:36:52 +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
|
|
|
}
|
2019-03-26 16:59:35 +02:00
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
if right_component_width == total_cols {
|
|
|
|
self.right.draw(grid, area, context);
|
|
|
|
} else if right_component_width == 0 {
|
|
|
|
self.left.draw(grid, area, context);
|
2019-03-26 16:59:35 +02:00
|
|
|
} else {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.left.draw(
|
2019-03-26 19:53:39 +02:00
|
|
|
grid,
|
2019-04-04 14:24:05 +03:00
|
|
|
(
|
|
|
|
upper_left,
|
|
|
|
(
|
|
|
|
if self.show_divider { mid - 1 } else { mid },
|
|
|
|
get_y(bottom_right),
|
|
|
|
),
|
|
|
|
),
|
2019-03-26 19:53:39 +02:00
|
|
|
context,
|
|
|
|
);
|
2019-03-26 16:59:35 +02:00
|
|
|
self.right
|
|
|
|
.draw(grid, (set_x(upper_left, mid + 1), bottom_right), context);
|
|
|
|
}
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2019-02-26 17:50:47 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
2019-04-10 22:01:02 +03:00
|
|
|
(self.left.process_event(event, context) || self.right.process_event(event, context))
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2018-07-14 15:04:42 +03:00
|
|
|
fn is_dirty(&self) -> bool {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.left.is_dirty() || self.right.is_dirty()
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.left.set_dirty();
|
|
|
|
self.right.set_dirty();
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2019-05-10 22:00:56 +03:00
|
|
|
fn get_shortcuts(&self, context: &Context) -> ShortcutMaps {
|
2019-03-04 10:15:11 +02:00
|
|
|
let mut right_map = self.right.get_shortcuts(context);
|
|
|
|
right_map.extend(self.left.get_shortcuts(context).into_iter());
|
2019-03-02 19:25:11 +02:00
|
|
|
right_map
|
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:51:48 +03:00
|
|
|
#[derive(Debug)]
|
2018-09-19 10:26:17 +03:00
|
|
|
pub enum PageMovement {
|
2019-05-16 00:58:40 +03:00
|
|
|
Home,
|
2018-08-21 19:51:48 +03:00
|
|
|
PageUp,
|
|
|
|
PageDown,
|
2019-05-16 00:58:40 +03:00
|
|
|
End,
|
2018-08-21 19:51:48 +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-08-21 19:51:48 +03:00
|
|
|
#[derive(Default, Debug)]
|
2018-07-11 17:07:51 +03:00
|
|
|
pub struct Pager {
|
2019-03-14 12:00:41 +02:00
|
|
|
text: String,
|
2018-07-11 17:07:51 +03:00
|
|
|
cursor_pos: usize,
|
2018-08-23 17:41:14 +03:00
|
|
|
max_cursor_pos: Option<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-09-19 10:26:17 +03:00
|
|
|
movement: Option<PageMovement>,
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
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
|
2019-05-10 22:00:56 +03:00
|
|
|
write!(f, "{}", Pager::DESCRIPTION)
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
impl Pager {
|
2019-05-10 22:00:56 +03:00
|
|
|
const DESCRIPTION: &'static str = "pager";
|
2019-03-14 12:00:41 +02:00
|
|
|
pub fn update_from_str(&mut self, text: &str, width: Option<usize>) {
|
|
|
|
let lines: Vec<&str> = if let Some(width) = width {
|
|
|
|
word_break_string(text, width)
|
|
|
|
} else {
|
|
|
|
text.trim().split('\n').collect()
|
|
|
|
};
|
|
|
|
|
2018-08-16 16:32:47 +03:00
|
|
|
let height = lines.len() + 1;
|
2019-03-14 12:00:41 +02:00
|
|
|
let width = width.unwrap_or_else(|| lines.iter().map(|l| l.len()).max().unwrap_or(0));
|
2018-08-16 16:32:47 +03:00
|
|
|
let mut content = CellBuffer::new(width, height, Cell::with_char(' '));
|
2019-03-14 12:00:41 +02:00
|
|
|
Pager::print_string(&mut content, lines);
|
|
|
|
self.text = text.to_string();
|
2018-08-16 16:32:47 +03:00
|
|
|
self.content = content;
|
|
|
|
self.height = height;
|
|
|
|
self.width = width;
|
|
|
|
self.dirty = true;
|
|
|
|
self.cursor_pos = 0;
|
2018-08-23 17:41:14 +03:00
|
|
|
self.max_cursor_pos = None;
|
2018-08-16 16:32:47 +03:00
|
|
|
}
|
2019-03-14 12:19:25 +02:00
|
|
|
pub fn from_string(
|
|
|
|
mut text: String,
|
2019-03-18 15:41:38 +02:00
|
|
|
context: Option<&Context>,
|
2019-03-14 12:19:25 +02:00
|
|
|
cursor_pos: Option<usize>,
|
|
|
|
width: Option<usize>,
|
|
|
|
) -> Self {
|
2019-03-18 15:41:38 +02:00
|
|
|
let pager_filter: Option<&String> = if let Some(context) = context {
|
|
|
|
context.settings.pager.filter.as_ref()
|
2018-07-22 23:11:07 +03:00
|
|
|
//let format_flowed: bool = context.settings.pager.format_flowed;
|
2019-03-18 15:41:38 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
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");
|
|
|
|
{
|
2019-06-18 21:13:58 +03:00
|
|
|
let stdin = filter_child.stdin.as_mut().expect("failed to open stdin");
|
2018-07-27 21:37:56 +03:00
|
|
|
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,
|
2019-03-14 12:19:25 +02:00
|
|
|
)
|
|
|
|
.to_string();
|
2018-07-17 17:16:16 +03:00
|
|
|
}
|
2018-07-23 15:40:13 +03:00
|
|
|
|
2019-03-14 12:00:41 +02:00
|
|
|
let content = {
|
|
|
|
let lines: Vec<&str> = if let Some(width) = width {
|
|
|
|
word_break_string(text.as_str(), width)
|
|
|
|
} else {
|
|
|
|
text.trim().split('\n').collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
let height = lines.len() + 1;
|
|
|
|
let width = width.unwrap_or_else(|| 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, lines);
|
|
|
|
content
|
|
|
|
};
|
2018-07-17 17:16:16 +03:00
|
|
|
Pager {
|
2019-03-18 12:45:33 +02:00
|
|
|
text,
|
2018-07-23 15:40:13 +03:00
|
|
|
cursor_pos: cursor_pos.unwrap_or(0),
|
2019-03-14 12:00:41 +02:00
|
|
|
height: content.size().1,
|
|
|
|
width: content.size().0,
|
2018-07-17 17:16:16 +03:00
|
|
|
dirty: true,
|
2018-08-23 15:36:52 +03:00
|
|
|
content,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2018-08-23 15:36:52 +03:00
|
|
|
..Default::default()
|
2018-07-17 17:16:16 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 12:00:41 +02:00
|
|
|
pub fn from_str(text: &str, cursor_pos: Option<usize>, width: Option<usize>) -> Self {
|
|
|
|
let lines: Vec<&str> = if let Some(width) = width {
|
|
|
|
word_break_string(text, width)
|
|
|
|
} else {
|
|
|
|
text.trim().split('\n').collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
let height = lines.len() + 1;
|
|
|
|
let width = width.unwrap_or_else(|| lines.iter().map(|l| l.len()).max().unwrap_or(0));
|
2018-07-18 10:42:52 +03:00
|
|
|
let mut content = CellBuffer::new(width, height, Cell::with_char(' '));
|
2019-03-14 12:19:25 +02:00
|
|
|
|
2019-03-14 12:00:41 +02:00
|
|
|
Pager::print_string(&mut content, lines);
|
2018-07-18 10:42:52 +03:00
|
|
|
Pager {
|
2019-03-14 12:00:41 +02:00
|
|
|
text: text.to_string(),
|
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,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2018-08-23 15:36:52 +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 {
|
2019-03-14 12:00:41 +02:00
|
|
|
text: String::new(),
|
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,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2018-08-23 15:36:52 +03:00
|
|
|
..Default::default()
|
2018-07-23 22:21:41 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 12:00:41 +02:00
|
|
|
pub fn print_string(content: &mut CellBuffer, lines: Vec<&str>) {
|
|
|
|
let width = content.size().0;
|
|
|
|
for (i, l) in lines.iter().enumerate() {
|
|
|
|
write_string_to_grid(
|
|
|
|
l,
|
|
|
|
content,
|
|
|
|
Color::Default,
|
|
|
|
Color::Default,
|
|
|
|
((0, i), (width - 1, i)),
|
|
|
|
true,
|
2019-03-14 12:19:25 +02: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() {
|
|
|
|
match mvm {
|
2018-09-19 10:26:17 +03:00
|
|
|
PageMovement::PageUp => {
|
2018-08-21 19:51:48 +03:00
|
|
|
self.cursor_pos = self.cursor_pos.saturating_sub(height);
|
2018-08-23 15:36:52 +03:00
|
|
|
}
|
2018-09-19 10:26:17 +03:00
|
|
|
PageMovement::PageDown => {
|
2019-05-12 00:40:44 +03:00
|
|
|
if self.cursor_pos + height < self.height {
|
2018-08-21 19:51:48 +03:00
|
|
|
self.cursor_pos += height;
|
|
|
|
}
|
2018-08-23 15:36:52 +03:00
|
|
|
}
|
2019-05-16 00:58:40 +03:00
|
|
|
PageMovement::Home => {
|
|
|
|
self.cursor_pos = 0;
|
|
|
|
}
|
|
|
|
PageMovement::End => {
|
|
|
|
self.cursor_pos = (self.height / height) * height;
|
|
|
|
}
|
2018-08-21 19:51:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 17:41:14 +03:00
|
|
|
if self.height == 0 || self.width == 0 {
|
|
|
|
return;
|
2018-08-10 11:06:47 +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;
|
2019-03-14 12:00:41 +02:00
|
|
|
let width = width!(area);
|
|
|
|
if width != self.width {
|
|
|
|
// Reflow text.
|
|
|
|
let lines: Vec<&str> = word_break_string(self.text.as_str(), width);
|
|
|
|
let height = lines.len() + 1;
|
|
|
|
self.width = width;
|
|
|
|
self.height = height;
|
|
|
|
self.content = CellBuffer::new(width, height, Cell::with_char(' '));
|
|
|
|
Pager::print_string(&mut self.content, lines);
|
|
|
|
}
|
2019-05-12 00:40:44 +03:00
|
|
|
if self.cursor_pos + height >= self.height {
|
|
|
|
self.cursor_pos = self.height.saturating_sub(height);
|
|
|
|
};
|
2019-05-13 22:05:00 +03:00
|
|
|
copy_area_with_break(
|
2018-07-27 21:37:56 +03:00
|
|
|
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
|
|
|
}
|
2019-03-04 10:15:11 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
2019-05-10 22:00:56 +03:00
|
|
|
let shortcuts = &self.get_shortcuts(context)[Self::DESCRIPTION];
|
2019-04-10 23:37:20 +03:00
|
|
|
match *event {
|
|
|
|
UIEvent::Input(ref key) if *key == shortcuts["scroll_up"] => {
|
2018-07-11 17:07:51 +03:00
|
|
|
if self.cursor_pos > 0 {
|
|
|
|
self.cursor_pos -= 1;
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Input(ref key) if *key == shortcuts["scroll_down"] => {
|
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
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Input(ref key) if *key == shortcuts["page_up"] => {
|
2018-09-19 10:26:17 +03:00
|
|
|
self.movement = Some(PageMovement::PageUp);
|
2018-08-21 19:51:48 +03:00
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Input(ref key) if *key == shortcuts["page_down"] => {
|
2018-09-19 10:26:17 +03:00
|
|
|
self.movement = Some(PageMovement::PageDown);
|
2018-08-21 19:51:48 +03:00
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::ChangeMode(UIMode::Normal) => {
|
2018-07-15 01:27:13 +03:00
|
|
|
self.dirty = true;
|
2018-08-23 14:39:54 +03:00
|
|
|
return false;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Resize => {
|
2018-07-16 13:36:28 +03:00
|
|
|
self.dirty = true;
|
2018-08-23 17:41:14 +03:00
|
|
|
self.max_cursor_pos = None;
|
2018-08-23 14:39:54 +03:00
|
|
|
return false;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2018-08-23 15:36:52 +03:00
|
|
|
_ => {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2018-08-23 14:39:54 +03:00
|
|
|
true
|
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;
|
|
|
|
}
|
2019-05-10 22:00:56 +03:00
|
|
|
fn get_shortcuts(&self, context: &Context) -> ShortcutMaps {
|
|
|
|
let config_map: FnvHashMap<&'static str, &Key> =
|
|
|
|
context.settings.shortcuts.pager.key_values();
|
|
|
|
[(
|
|
|
|
Pager::DESCRIPTION.to_string(),
|
|
|
|
[
|
|
|
|
("scroll_up", (*config_map["scroll_up"]).clone()),
|
|
|
|
("scroll_down", (*config_map["scroll_down"]).clone()),
|
|
|
|
("page_up", (*config_map["page_up"]).clone()),
|
|
|
|
("page_down", (*config_map["page_down"]).clone()),
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect::<ShortcutMap>(),
|
|
|
|
)]
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
2019-03-04 10:15:11 +02:00
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
|
|
|
}
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Status bar.
|
2018-08-23 14:39:54 +03:00
|
|
|
#[derive(Debug)]
|
2018-07-14 15:04:42 +03:00
|
|
|
pub struct StatusBar {
|
2019-04-10 22:01:02 +03:00
|
|
|
container: Box<Component>,
|
2018-07-14 15:04:42 +03:00
|
|
|
status: String,
|
2018-07-23 16:29:22 +03:00
|
|
|
notifications: VecDeque<String>,
|
2019-05-15 22:03:17 +03:00
|
|
|
ex_buffer: Field,
|
2018-08-26 19:29:12 +03:00
|
|
|
display_buffer: String,
|
2018-07-15 01:27:13 +03:00
|
|
|
mode: UIMode,
|
|
|
|
height: usize,
|
2018-07-14 15:04:42 +03:00
|
|
|
dirty: bool,
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2019-05-15 22:03:17 +03:00
|
|
|
|
|
|
|
auto_complete: AutoComplete,
|
|
|
|
cmd_history: Vec<String>,
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-04-10 22:01:02 +03:00
|
|
|
pub fn new(container: Box<Component>) -> Self {
|
2018-07-14 15:04:42 +03:00
|
|
|
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(),
|
2019-05-15 22:03:17 +03:00
|
|
|
ex_buffer: Field::Text(UText::new(String::with_capacity(256)), None),
|
2018-08-26 19:29:12 +03:00
|
|
|
display_buffer: String::with_capacity(8),
|
2018-07-14 15:04:42 +03:00
|
|
|
dirty: true,
|
2018-07-15 01:27:13 +03:00
|
|
|
mode: UIMode::Normal,
|
|
|
|
height: 1,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2019-05-15 22:03:17 +03:00
|
|
|
auto_complete: AutoComplete::new(Vec::new()),
|
|
|
|
|
|
|
|
cmd_history: Vec::new(),
|
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);
|
2019-05-12 15:10:39 +03:00
|
|
|
let (x, y) = write_string_to_grid(
|
|
|
|
&self.status,
|
|
|
|
grid,
|
|
|
|
Color::Byte(123),
|
|
|
|
Color::Byte(26),
|
|
|
|
area,
|
|
|
|
false,
|
|
|
|
);
|
2018-07-23 16:29:22 +03:00
|
|
|
if let Some(n) = self.notifications.pop_front() {
|
2018-07-27 21:37:56 +03:00
|
|
|
write_string_to_grid(
|
2019-05-12 15:10:39 +03:00
|
|
|
&n,
|
2018-07-27 21:37:56 +03:00
|
|
|
grid,
|
2019-05-12 15:10:39 +03:00
|
|
|
Color::Byte(219),
|
|
|
|
Color::Byte(88),
|
|
|
|
(
|
|
|
|
(std::cmp::max(x, width!(area).saturating_sub(n.len())), y),
|
|
|
|
bottom_right!(area),
|
|
|
|
),
|
2018-07-27 21:37:56 +03:00
|
|
|
false,
|
|
|
|
);
|
2018-07-23 16:29:22 +03:00
|
|
|
}
|
2018-08-26 19:29:12 +03:00
|
|
|
let (x, y) = bottom_right!(area);
|
|
|
|
for (idx, c) in self.display_buffer.chars().rev().enumerate() {
|
|
|
|
if let Some(cell) = grid.get_mut(x.saturating_sub(idx).saturating_sub(1), y) {
|
|
|
|
cell.set_ch(c);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
2019-05-15 22:03:17 +03:00
|
|
|
self.ex_buffer.as_str(),
|
2018-07-27 21:37:56 +03:00
|
|
|
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
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
self.container.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,
|
|
|
|
);
|
2019-05-15 22:03:17 +03:00
|
|
|
/* don't autocomplete for less than 3 characters */
|
|
|
|
if self.ex_buffer.as_str().split_graphemes().len() <= 2 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let suggestions: Vec<String> = self
|
|
|
|
.cmd_history
|
|
|
|
.iter()
|
|
|
|
.filter_map(|h| {
|
|
|
|
if h.starts_with(self.ex_buffer.as_str()) {
|
|
|
|
Some(h.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2019-06-19 19:39:23 +03:00
|
|
|
if suggestions.is_empty() && !self.auto_complete.suggestions().is_empty() {
|
|
|
|
self.auto_complete.set_suggestions(suggestions);
|
|
|
|
/* redraw self.container because we have got ridden of an autocomplete
|
2019-05-15 22:03:17 +03:00
|
|
|
* box, and it must be drawn over */
|
|
|
|
self.container.set_dirty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* redraw self.container because we have less suggestions than before */
|
|
|
|
if suggestions.len() < self.auto_complete.suggestions().len() {
|
|
|
|
self.container.set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.auto_complete.set_suggestions(suggestions) {
|
|
|
|
let len = self.auto_complete.suggestions().len() - 1;
|
|
|
|
self.auto_complete.set_cursor(len);
|
|
|
|
}
|
|
|
|
let hist_height = std::cmp::min(15, self.auto_complete.suggestions().len());
|
|
|
|
let hist_area = if height < self.auto_complete.suggestions().len() {
|
|
|
|
let mut scrollbar = ScrollBar::default();
|
|
|
|
scrollbar.set_show_arrows(false);
|
|
|
|
scrollbar.set_block_character(Some('▌'));
|
|
|
|
scrollbar.draw(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
(
|
|
|
|
get_x(upper_left),
|
|
|
|
get_y(bottom_right) - height - hist_height + 1,
|
|
|
|
),
|
|
|
|
set_y(bottom_right, get_y(bottom_right) - height),
|
|
|
|
),
|
|
|
|
self.auto_complete.cursor(),
|
|
|
|
hist_height,
|
|
|
|
self.auto_complete.suggestions().len(),
|
|
|
|
);
|
|
|
|
change_colors(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
(
|
|
|
|
get_x(upper_left),
|
|
|
|
get_y(bottom_right) - height - hist_height + 1,
|
|
|
|
),
|
|
|
|
set_y(bottom_right, get_y(bottom_right) - height),
|
|
|
|
),
|
|
|
|
Color::Byte(197), // DeepPink2,
|
|
|
|
Color::Byte(174), //LightPink3
|
|
|
|
);
|
|
|
|
context.dirty_areas.push_back((
|
|
|
|
(
|
|
|
|
get_x(upper_left),
|
|
|
|
get_y(bottom_right) - height - hist_height + 1,
|
|
|
|
),
|
|
|
|
set_y(bottom_right, get_y(bottom_right) - height),
|
|
|
|
));
|
|
|
|
(
|
|
|
|
(
|
|
|
|
get_x(upper_left) + 1,
|
|
|
|
get_y(bottom_right) - height - hist_height + 1,
|
|
|
|
),
|
|
|
|
set_y(bottom_right, get_y(bottom_right) - height),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
(
|
|
|
|
get_x(upper_left),
|
|
|
|
get_y(bottom_right) - height - hist_height + 1,
|
|
|
|
),
|
|
|
|
set_y(bottom_right, get_y(bottom_right) - height),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let offset = if hist_height
|
|
|
|
> (self.auto_complete.suggestions().len() - self.auto_complete.cursor())
|
|
|
|
{
|
|
|
|
self.auto_complete.suggestions().len() - hist_height
|
|
|
|
} else {
|
|
|
|
self.auto_complete.cursor()
|
|
|
|
};
|
|
|
|
clear_area(grid, hist_area);
|
|
|
|
change_colors(
|
|
|
|
grid,
|
|
|
|
hist_area,
|
|
|
|
Color::Byte(88), // DarkRed,
|
|
|
|
Color::Byte(174), //LightPink3
|
|
|
|
);
|
|
|
|
for (y_offset, s) in self
|
|
|
|
.auto_complete
|
|
|
|
.suggestions()
|
|
|
|
.iter()
|
|
|
|
.skip(offset)
|
|
|
|
.take(hist_height)
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
write_string_to_grid(
|
|
|
|
s.as_str(),
|
|
|
|
grid,
|
|
|
|
Color::Byte(88), // DarkRed,
|
|
|
|
Color::Byte(174), //LightPink3
|
|
|
|
(
|
|
|
|
set_y(
|
|
|
|
upper_left!(hist_area),
|
|
|
|
get_y(bottom_right!(hist_area)) - hist_height + y_offset + 1,
|
|
|
|
),
|
|
|
|
bottom_right!(hist_area),
|
|
|
|
),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
if y_offset + offset == self.auto_complete.cursor() {
|
|
|
|
change_colors(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
set_y(
|
|
|
|
upper_left!(hist_area),
|
|
|
|
get_y(bottom_right!(hist_area)) - hist_height + y_offset + 1,
|
|
|
|
),
|
|
|
|
set_y(
|
|
|
|
bottom_right!(hist_area),
|
|
|
|
get_y(bottom_right!(hist_area)) - hist_height + y_offset + 1,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Color::Byte(88), // DarkRed,
|
|
|
|
Color::Byte(173), //LightSalmon3
|
|
|
|
);
|
|
|
|
write_string_to_grid(
|
|
|
|
&s.as_str()[self.ex_buffer.as_str().len()..],
|
|
|
|
grid,
|
|
|
|
Color::Byte(97), // MediumPurple3,
|
|
|
|
Color::Byte(88), //LightPink3
|
|
|
|
(
|
|
|
|
(
|
|
|
|
get_y(upper_left)
|
|
|
|
+ self.ex_buffer.as_str().split_graphemes().len(),
|
|
|
|
get_y(bottom_right) - height + 1,
|
|
|
|
),
|
|
|
|
set_y(bottom_right, get_y(bottom_right) - height + 1),
|
|
|
|
),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.dirty_areas.push_back(hist_area);
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2018-07-15 01:27:13 +03:00
|
|
|
}
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
2019-02-26 17:50:47 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
2019-04-10 22:01:02 +03:00
|
|
|
if self.container.process_event(event, context) {
|
2018-08-23 14:39:54 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:37:20 +03:00
|
|
|
match &event {
|
|
|
|
UIEvent::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(_) => {
|
2018-08-23 14:39:54 +03:00
|
|
|
return false;
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 02:34:03 +03:00
|
|
|
let account = &context.accounts[*idx_a];
|
|
|
|
let m = &account[*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(),
|
2019-05-26 02:34:03 +03:00
|
|
|
m.envelopes.len(),
|
|
|
|
m.envelopes
|
|
|
|
.iter()
|
|
|
|
.map(|h| &account.collection[&h])
|
|
|
|
.filter(|e| !e.is_seen())
|
|
|
|
.count()
|
2018-07-27 21:37:56 +03:00
|
|
|
);
|
2018-07-14 15:04:42 +03:00
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::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() {
|
2019-04-10 23:37:20 +03:00
|
|
|
context
|
|
|
|
.replies
|
2019-05-15 22:03:17 +03:00
|
|
|
.push_back(UIEvent::Command(self.ex_buffer.as_str().to_string()));
|
|
|
|
}
|
|
|
|
if parse_command(&self.ex_buffer.as_str().as_bytes())
|
|
|
|
.to_full_result()
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
self.cmd_history.push(self.ex_buffer.as_str().to_string());
|
2018-08-07 16:14:06 +03:00
|
|
|
}
|
2019-05-15 22:03:17 +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
|
|
|
}
|
2019-05-15 22:03:17 +03:00
|
|
|
UIEvent::ExInput(Key::Char('\t')) => {
|
|
|
|
if let Some(suggestion) = self.auto_complete.get_suggestion() {
|
|
|
|
let mut utext = UText::new(suggestion);
|
|
|
|
let len = utext.as_str().len();
|
|
|
|
utext.set_cursor(len);
|
|
|
|
self.container.set_dirty();
|
|
|
|
self.set_dirty();
|
|
|
|
self.ex_buffer = Field::Text(utext, None);
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::ExInput(Key::Char(c)) => {
|
2018-07-15 01:27:13 +03:00
|
|
|
self.dirty = true;
|
2019-05-15 22:03:17 +03:00
|
|
|
self.ex_buffer
|
|
|
|
.process_event(&mut UIEvent::InsertInput(Key::Char(*c)), context);
|
2018-08-23 14:39:54 +03:00
|
|
|
return true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::ExInput(Key::Ctrl('u')) => {
|
2018-08-07 16:14:06 +03:00
|
|
|
self.dirty = true;
|
|
|
|
self.ex_buffer.clear();
|
2018-08-23 14:39:54 +03:00
|
|
|
return true;
|
2018-08-07 16:14:06 +03:00
|
|
|
}
|
2019-05-15 22:03:17 +03:00
|
|
|
UIEvent::ExInput(k @ Key::Backspace) | UIEvent::ExInput(k @ Key::Ctrl('h')) => {
|
2018-08-07 16:14:06 +03:00
|
|
|
self.dirty = true;
|
2019-05-15 22:03:17 +03:00
|
|
|
self.ex_buffer
|
|
|
|
.process_event(&mut UIEvent::InsertInput(k.clone()), context);
|
2018-08-23 14:39:54 +03:00
|
|
|
return true;
|
2018-08-07 16:14:06 +03:00
|
|
|
}
|
2019-05-15 22:03:17 +03:00
|
|
|
UIEvent::ExInput(Key::Up) => {
|
|
|
|
self.auto_complete.dec_cursor();
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
|
|
|
UIEvent::ExInput(Key::Down) => {
|
|
|
|
self.auto_complete.inc_cursor();
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Resize => {
|
2018-07-16 13:36:28 +03:00
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::StatusEvent(StatusEvent::DisplayMessage(s)) => {
|
2018-07-23 16:29:22 +03:00
|
|
|
self.notifications.push_back(s.clone());
|
|
|
|
self.dirty = true;
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::StatusEvent(StatusEvent::BufClear) => {
|
2018-08-26 19:29:12 +03:00
|
|
|
self.display_buffer.clear();
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::StatusEvent(StatusEvent::BufSet(s)) => {
|
2018-08-26 19:29:12 +03:00
|
|
|
self.display_buffer = s.clone();
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2018-07-27 21:37:56 +03:00
|
|
|
_ => {}
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
2018-08-23 15:36:52 +03:00
|
|
|
false
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.dirty || self.container.is_dirty()
|
2018-07-14 15:04:42 +03:00
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2019-05-10 22:00:56 +03:00
|
|
|
fn get_shortcuts(&self, context: &Context) -> ShortcutMaps {
|
2019-03-04 10:15:11 +02:00
|
|
|
self.container.get_shortcuts(context)
|
2019-03-02 19:25:11 +02:00
|
|
|
}
|
2018-07-20 12:15:06 +03:00
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
2018-08-23 15:36:52 +03:00
|
|
|
}
|
2018-07-20 12:15:06 +03:00
|
|
|
}
|
2018-08-03 13:46:08 +03:00
|
|
|
|
2018-08-23 14:39:54 +03:00
|
|
|
#[derive(Debug)]
|
2018-08-03 13:46:08 +03:00
|
|
|
pub struct Progress {
|
|
|
|
description: String,
|
|
|
|
total_work: usize,
|
|
|
|
finished: usize,
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:11:56 +02:00
|
|
|
pub fn add_work(&mut self, n: usize) {
|
2018-08-03 13:46:08 +03:00
|
|
|
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!()
|
|
|
|
}
|
2019-02-26 17:50:47 +02:00
|
|
|
fn process_event(&mut self, _event: &mut UIEvent, _context: &mut Context) -> bool {
|
2018-08-23 15:36:52 +03:00
|
|
|
false
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
fn set_dirty(&mut self) {}
|
2019-04-10 22:01:02 +03:00
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 14:39:54 +03:00
|
|
|
#[derive(Debug)]
|
2018-08-11 18:00:21 +03:00
|
|
|
pub struct Tabbed {
|
2019-03-25 13:59:29 +02:00
|
|
|
pinned: usize,
|
2019-04-10 22:01:02 +03:00
|
|
|
children: Vec<Box<Component>>,
|
2018-08-11 18:00:21 +03:00
|
|
|
cursor_pos: usize,
|
2019-03-02 19:25:11 +02:00
|
|
|
|
|
|
|
show_shortcuts: bool,
|
2019-03-14 12:10:29 +02:00
|
|
|
|
|
|
|
dirty: bool,
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Tabbed {
|
|
|
|
pub fn new(children: Vec<Box<Component>>) -> Self {
|
2019-03-25 13:59:29 +02:00
|
|
|
let pinned = children.len();
|
2018-08-11 18:00:21 +03:00
|
|
|
Tabbed {
|
2019-03-26 19:53:39 +02:00
|
|
|
pinned,
|
2019-04-10 22:01:02 +03:00
|
|
|
children,
|
2018-08-11 18:00:21 +03:00
|
|
|
cursor_pos: 0,
|
2019-03-02 19:25:11 +02:00
|
|
|
show_shortcuts: false,
|
2019-03-14 12:10:29 +02:00
|
|
|
dirty: true,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn draw_tabs(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
2019-03-25 13:59:29 +02:00
|
|
|
let upper_left = upper_left!(area);
|
|
|
|
|
2019-03-23 00:12:30 +02:00
|
|
|
if self.children.is_empty() {
|
|
|
|
clear_area(grid, area);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-25 13:59:29 +02:00
|
|
|
let mut x = get_x(upper_left);
|
|
|
|
let y: usize = get_y(upper_left);
|
2018-08-11 18:00:21 +03:00
|
|
|
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,
|
2019-03-25 13:59:29 +02:00
|
|
|
(set_x(upper_left, x), bottom_right!(area)),
|
2018-08-11 18:00:21 +03:00
|
|
|
false,
|
|
|
|
);
|
|
|
|
x = x_ + 1;
|
2019-03-25 13:59:29 +02:00
|
|
|
if idx == self.pinned.saturating_sub(1) {
|
|
|
|
x += 2;
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
if y != _y_ {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let (cols, _) = grid.size();
|
|
|
|
let cslice: &mut [Cell] = grid;
|
2019-03-14 12:00:41 +02:00
|
|
|
//TODO: bounds check
|
|
|
|
let cslice_len = cslice.len();
|
2019-03-14 12:19:25 +02:00
|
|
|
for c in cslice[(y * cols) + x.saturating_sub(1)
|
|
|
|
..std::cmp::min((y * cols) + x.saturating_sub(1), cslice_len)]
|
|
|
|
.iter_mut()
|
|
|
|
{
|
2018-08-11 18:00:21 +03:00
|
|
|
c.set_bg(Color::Byte(7));
|
2018-09-04 01:49:29 +03:00
|
|
|
c.set_ch(' ');
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
2018-09-04 01:49:29 +03:00
|
|
|
|
2019-03-23 00:12:30 +02:00
|
|
|
if self.cursor_pos == self.children.len() - 1 {
|
|
|
|
cslice[(y * cols) + x].set_ch('▍');
|
|
|
|
cslice[(y * cols) + x].set_fg(Color::Byte(8));
|
|
|
|
cslice[(y * cols) + x].set_bg(Color::Default);
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
|
|
|
pub fn add_component(&mut self, new: Box<Component>) {
|
2019-04-10 22:01:02 +03:00
|
|
|
self.children.push(new);
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-03-14 12:10:29 +02:00
|
|
|
if self.dirty {
|
2019-03-14 12:19:25 +02:00
|
|
|
clear_area(
|
|
|
|
grid,
|
|
|
|
(
|
|
|
|
upper_left!(area),
|
|
|
|
set_x(upper_left!(area), get_x(bottom_right!(area))),
|
|
|
|
),
|
|
|
|
);
|
2019-03-14 12:10:29 +02:00
|
|
|
self.dirty = false;
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
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);
|
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
|
|
|
|
if self.show_shortcuts {
|
|
|
|
let area = (
|
2019-03-14 12:19:25 +02:00
|
|
|
pos_inc(upper_left!(area), (2, 1)),
|
|
|
|
set_x(
|
|
|
|
bottom_right!(area),
|
|
|
|
get_x(bottom_right!(area)).saturating_sub(2),
|
|
|
|
),
|
|
|
|
);
|
2019-03-02 19:25:11 +02:00
|
|
|
clear_area(grid, area);
|
|
|
|
create_box(grid, area);
|
|
|
|
|
2019-05-10 22:00:56 +03:00
|
|
|
let mut idx = 0;
|
2019-03-02 19:25:11 +02:00
|
|
|
// TODO: print into a pager
|
2019-05-10 22:00:56 +03:00
|
|
|
for (desc, shortcuts) in self.children[self.cursor_pos]
|
2019-03-14 12:19:25 +02:00
|
|
|
.get_shortcuts(context)
|
|
|
|
.into_iter()
|
|
|
|
{
|
|
|
|
write_string_to_grid(
|
2019-05-10 22:00:56 +03:00
|
|
|
&desc,
|
2019-03-14 12:19:25 +02:00
|
|
|
grid,
|
|
|
|
Color::Default,
|
|
|
|
Color::Default,
|
|
|
|
(
|
2019-05-10 22:00:56 +03:00
|
|
|
pos_inc(upper_left!(area), (2, 1 + idx)),
|
2019-03-14 12:19:25 +02:00
|
|
|
set_x(
|
|
|
|
bottom_right!(area),
|
|
|
|
get_x(bottom_right!(area)).saturating_sub(2),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
false,
|
|
|
|
);
|
2019-05-10 22:00:56 +03:00
|
|
|
idx += 2;
|
|
|
|
let mut shortcuts = shortcuts.into_iter().collect::<Vec<_>>();
|
|
|
|
shortcuts.sort_unstable_by_key(|(ref k, _)| *k);
|
|
|
|
for (k, v) in shortcuts {
|
|
|
|
let (x, y) = write_string_to_grid(
|
|
|
|
&k,
|
|
|
|
grid,
|
|
|
|
Color::Byte(29),
|
|
|
|
Color::Default,
|
|
|
|
(
|
|
|
|
pos_inc(upper_left!(area), (2, 1 + idx)),
|
|
|
|
set_x(
|
|
|
|
bottom_right!(area),
|
|
|
|
get_x(bottom_right!(area)).saturating_sub(2),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
write_string_to_grid(
|
|
|
|
&format!("{}", v),
|
|
|
|
grid,
|
|
|
|
Color::Default,
|
|
|
|
Color::Default,
|
|
|
|
(
|
|
|
|
(x + 2, y),
|
|
|
|
set_x(
|
|
|
|
bottom_right!(area),
|
|
|
|
get_x(bottom_right!(area)).saturating_sub(2),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
idx += 1;
|
2019-03-14 12:19:25 +02:00
|
|
|
}
|
2019-03-02 19:25:11 +02:00
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
2019-02-26 17:50:47 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
2019-04-10 23:37:20 +03:00
|
|
|
match *event {
|
|
|
|
UIEvent::Input(Key::Char('T')) => {
|
2018-09-04 01:49:29 +03:00
|
|
|
self.cursor_pos = (self.cursor_pos + 1) % self.children.len();
|
|
|
|
self.set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Input(Key::Char('?')) => {
|
2019-03-02 19:25:11 +02:00
|
|
|
self.show_shortcuts = !self.show_shortcuts;
|
|
|
|
self.set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-18 22:13:54 +03:00
|
|
|
UIEvent::Action(Tab(NewDraft(account_idx, ref draft))) => {
|
|
|
|
let mut composer = Composer::new(account_idx);
|
|
|
|
if let Some(draft) = draft {
|
|
|
|
composer.set_draft(draft.clone());
|
|
|
|
}
|
|
|
|
self.add_component(Box::new(composer));
|
2018-09-04 02:10:37 +03:00
|
|
|
self.cursor_pos = self.children.len() - 1;
|
|
|
|
self.children[self.cursor_pos].set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Action(Tab(Reply(coordinates, msg))) => {
|
2018-09-04 01:49:29 +03:00
|
|
|
self.add_component(Box::new(Composer::with_context(coordinates, msg, context)));
|
|
|
|
self.cursor_pos = self.children.len() - 1;
|
|
|
|
self.children[self.cursor_pos].set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-26 02:34:03 +03:00
|
|
|
UIEvent::Action(Tab(Edit(account_pos, msg))) => {
|
|
|
|
self.add_component(Box::new(Composer::edit(account_pos, msg, context)));
|
2019-03-26 15:26:09 +02:00
|
|
|
self.cursor_pos = self.children.len() - 1;
|
|
|
|
self.children[self.cursor_pos].set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Action(Tab(TabOpen(ref mut e))) if e.is_some() => {
|
2019-02-26 17:50:47 +02:00
|
|
|
self.add_component(e.take().unwrap());
|
|
|
|
self.cursor_pos = self.children.len() - 1;
|
|
|
|
self.children[self.cursor_pos].set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Action(Tab(Close)) => {
|
2019-03-25 13:59:29 +02:00
|
|
|
if self.pinned > self.cursor_pos {
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
let id = self.children[self.cursor_pos].id();
|
2019-02-21 15:44:26 +02:00
|
|
|
self.children[self.cursor_pos].kill(id);
|
2019-03-14 12:10:29 +02:00
|
|
|
self.set_dirty();
|
2018-09-04 01:49:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Action(Tab(Kill(id))) => {
|
2019-03-25 13:59:29 +02:00
|
|
|
if self.pinned > self.cursor_pos {
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
if let Some(c_idx) = self.children.iter().position(|x| x.id() == id) {
|
2018-09-04 01:49:29 +03:00
|
|
|
self.children.remove(c_idx);
|
2019-04-11 00:04:28 +03:00
|
|
|
self.cursor_pos = 0;
|
2018-09-04 01:49:29 +03:00
|
|
|
self.set_dirty();
|
|
|
|
return true;
|
|
|
|
} else {
|
2019-05-01 19:20:33 +03:00
|
|
|
debug!(
|
2019-04-10 22:01:02 +03:00
|
|
|
"DEBUG: Child component with id {:?} not found.\nList: {:?}",
|
2019-02-21 15:44:26 +02:00
|
|
|
id, self.children
|
2018-09-04 01:49:29 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
2018-08-23 14:39:54 +03:00
|
|
|
self.children[self.cursor_pos].process_event(event, context)
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
2019-03-14 12:10:29 +02:00
|
|
|
self.dirty || self.children[self.cursor_pos].is_dirty()
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
2018-09-04 01:49:29 +03:00
|
|
|
fn set_dirty(&mut self) {
|
2019-03-14 12:10:29 +02:00
|
|
|
self.dirty = true;
|
2018-09-04 01:49:29 +03:00
|
|
|
self.children[self.cursor_pos].set_dirty();
|
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
|
|
|
}
|
2018-08-03 13:46:08 +03:00
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
|
|
|
|
type EntryIdentifier = Vec<u8>;
|
|
|
|
/// Shows selection to user
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct Selector {
|
2019-03-14 12:19:25 +02:00
|
|
|
single_only: bool,
|
|
|
|
/// allow only one selection
|
2019-02-15 09:06:42 +02:00
|
|
|
entries: Vec<(EntryIdentifier, bool)>,
|
|
|
|
selected_entry_count: u32,
|
|
|
|
content: CellBuffer,
|
|
|
|
|
|
|
|
cursor: usize,
|
|
|
|
|
|
|
|
dirty: bool,
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Selector {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
Display::fmt("Selector", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Selector {
|
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
let (width, height) = self.content.size();
|
2019-03-14 12:19:25 +02:00
|
|
|
copy_area_with_break(grid, &self.content, area, ((0, 0), (width, height)));
|
2019-02-15 09:06:42 +02:00
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
2019-03-03 22:11:15 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
|
2019-02-15 09:06:42 +02:00
|
|
|
let (width, height) = self.content.size();
|
2019-04-10 23:37:20 +03:00
|
|
|
match *event {
|
|
|
|
UIEvent::Input(Key::Char('\t')) => {
|
2019-03-14 12:19:25 +02:00
|
|
|
self.entries[self.cursor].1 = !self.entries[self.cursor].1;
|
2019-02-15 09:06:42 +02:00
|
|
|
if self.entries[self.cursor].1 {
|
|
|
|
write_string_to_grid(
|
|
|
|
"x",
|
|
|
|
&mut self.content,
|
|
|
|
Color::Default,
|
|
|
|
Color::Default,
|
|
|
|
((1, self.cursor), (width, self.cursor)),
|
|
|
|
false,
|
2019-03-14 12:19:25 +02:00
|
|
|
);
|
2019-02-15 09:06:42 +02:00
|
|
|
} else {
|
|
|
|
write_string_to_grid(
|
|
|
|
" ",
|
|
|
|
&mut self.content,
|
|
|
|
Color::Default,
|
|
|
|
Color::Default,
|
|
|
|
((1, self.cursor), (width, self.cursor)),
|
|
|
|
false,
|
2019-03-14 12:19:25 +02:00
|
|
|
);
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
2019-02-26 10:55:22 +02:00
|
|
|
self.dirty = true;
|
2019-02-15 09:06:42 +02:00
|
|
|
return true;
|
2019-03-14 12:19:25 +02:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Input(Key::Up) if self.cursor > 0 => {
|
2019-02-15 09:06:42 +02:00
|
|
|
self.cursor -= 1;
|
2019-02-26 10:55:22 +02:00
|
|
|
self.dirty = true;
|
2019-02-15 09:06:42 +02:00
|
|
|
return true;
|
2019-03-14 12:19:25 +02:00
|
|
|
}
|
2019-04-10 23:37:20 +03:00
|
|
|
UIEvent::Input(Key::Down) if self.cursor < height.saturating_sub(1) => {
|
2019-02-15 09:06:42 +02:00
|
|
|
self.cursor += 1;
|
2019-02-26 10:55:22 +02:00
|
|
|
self.dirty = true;
|
2019-02-15 09:06:42 +02:00
|
|
|
return true;
|
2019-03-14 12:19:25 +02:00
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
fn is_dirty(&self) -> bool {
|
|
|
|
self.dirty
|
|
|
|
}
|
|
|
|
fn set_dirty(&mut self) {
|
|
|
|
self.dirty = true;
|
|
|
|
}
|
2019-04-10 22:01:02 +03:00
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Selector {
|
|
|
|
pub fn new(mut entries: Vec<(EntryIdentifier, String)>, single_only: bool) -> Selector {
|
2019-03-14 12:19:25 +02:00
|
|
|
let width = entries
|
|
|
|
.iter()
|
|
|
|
.max_by_key(|e| e.1.len())
|
|
|
|
.map(|v| v.1.len())
|
|
|
|
.unwrap_or(0)
|
|
|
|
+ 4;
|
2019-02-15 09:06:42 +02:00
|
|
|
let height = entries.len();
|
|
|
|
let mut content = CellBuffer::new(width, height, Cell::with_char(' '));
|
2019-03-14 12:19:25 +02:00
|
|
|
let identifiers = entries
|
|
|
|
.iter_mut()
|
|
|
|
.map(|(id, _)| (std::mem::replace(&mut *id, Vec::new()), false))
|
|
|
|
.collect();
|
2019-02-15 09:06:42 +02:00
|
|
|
for (i, e) in entries.into_iter().enumerate() {
|
|
|
|
write_string_to_grid(
|
|
|
|
&format!("[ ] {}", e.1),
|
|
|
|
&mut content,
|
|
|
|
Color::Default,
|
|
|
|
Color::Default,
|
|
|
|
((0, i), (width - 1, i)),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Selector {
|
|
|
|
single_only,
|
|
|
|
entries: identifiers,
|
|
|
|
selected_entry_count: 0,
|
|
|
|
content,
|
|
|
|
cursor: 0,
|
|
|
|
dirty: true,
|
2019-04-11 00:04:17 +03:00
|
|
|
id: ComponentId::new_v4(),
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collect(self) -> Vec<EntryIdentifier> {
|
2019-03-14 12:19:25 +02:00
|
|
|
self.entries
|
|
|
|
.into_iter()
|
|
|
|
.filter(|v| v.1)
|
|
|
|
.map(|(id, _)| id)
|
|
|
|
.collect()
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
}
|