utilities/widgets.rs: move text field to its own module

pull/223/head
Manos Pitsidianakis 2023-06-03 14:43:00 +03:00
parent daf42fd456
commit f537c24909
4 changed files with 382 additions and 251 deletions

View File

@ -205,7 +205,7 @@ impl Component for ContactManager {
(
s.to_string(),
match v {
Field::Text(v, _) => v.as_str().to_string(),
Field::Text(v) => v.as_str().to_string(),
Field::Choice(mut v, c) => v.remove(c).to_string(),
},
)

View File

@ -23,11 +23,16 @@
*/
use text_processing::Reflow;
pub type AutoCompleteFn = Box<dyn Fn(&Context, &str) -> Vec<AutoCompleteEntry> + Send + Sync>;
use super::*;
mod pager;
pub use self::pager::*;
mod text;
pub use self::text::*;
mod widgets;
pub use self::widgets::*;
@ -58,7 +63,7 @@ pub struct StatusBar {
status: String,
status_message: String,
substatus_message: String,
ex_buffer: Field,
ex_buffer: TextField,
ex_buffer_cmd_history_pos: Option<usize>,
display_buffer: String,
mode: UIMode,
@ -102,7 +107,7 @@ impl StatusBar {
status: String::with_capacity(256),
status_message: String::with_capacity(256),
substatus_message: String::with_capacity(256),
ex_buffer: Field::Text(UText::new(String::with_capacity(256)), None),
ex_buffer: TextField::new(UText::new(String::with_capacity(256)), None),
ex_buffer_cmd_history_pos: None,
display_buffer: String::with_capacity(8),
dirty: true,
@ -595,7 +600,7 @@ impl Component for StatusBar {
utext.set_cursor(len);
self.container.set_dirty(true);
self.set_dirty(true);
self.ex_buffer = Field::Text(utext, None);
self.ex_buffer = TextField::new(utext, None);
}
}
UIEvent::CmdInput(Key::Char(c)) => {
@ -622,27 +627,15 @@ impl Component for StatusBar {
}
UIEvent::CmdInput(Key::Down) => {
self.auto_complete.inc_cursor();
self.dirty = true;
self.set_dirty(true);
}
UIEvent::CmdInput(Key::Left) => {
if let Field::Text(ref mut utext, _) = self.ex_buffer {
utext.cursor_dec();
} else {
unsafe {
std::hint::unreachable_unchecked();
}
}
self.dirty = true;
self.ex_buffer.cursor_dec();
self.set_dirty(true);
}
UIEvent::CmdInput(Key::Right) => {
if let Field::Text(ref mut utext, _) = self.ex_buffer {
utext.cursor_inc();
} else {
unsafe {
std::hint::unreachable_unchecked();
}
}
self.dirty = true;
self.ex_buffer.cursor_inc();
self.set_dirty(true);
}
UIEvent::CmdInput(Key::Ctrl('p')) => {
if self.cmd_history.is_empty() {
@ -659,7 +652,7 @@ impl Component for StatusBar {
utext.set_cursor(len);
self.container.set_dirty(true);
self.set_dirty(true);
self.ex_buffer = Field::Text(utext, None);
self.ex_buffer = TextField::new(utext, None);
self.ex_buffer_cmd_history_pos = pos;
self.dirty = true;
}
@ -684,7 +677,7 @@ impl Component for StatusBar {
utext.set_cursor(len);
self.container.set_dirty(true);
self.set_dirty(true);
self.ex_buffer = Field::Text(utext, None);
self.ex_buffer = TextField::new(utext, None);
self.ex_buffer_cmd_history_pos = pos;
self.dirty = true;
}
@ -778,11 +771,15 @@ impl Component for StatusBar {
}
fn is_dirty(&self) -> bool {
self.dirty || self.container.is_dirty() || self.progress_spinner.is_dirty()
self.dirty
|| self.container.is_dirty()
|| self.ex_buffer.is_dirty()
|| self.progress_spinner.is_dirty()
}
fn set_dirty(&mut self, value: bool) {
self.dirty = value;
self.ex_buffer.set_dirty(value);
self.progress_spinner.set_dirty(value);
}

View File

@ -0,0 +1,322 @@
/*
* meli
*
* Copyright 2017-2020 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/>.
*/
use super::*;
pub struct TextField {
inner: UText,
autocomplete: Option<(AutoCompleteFn, Box<AutoComplete>)>,
}
impl Debug for TextField {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct(stringify!(TextField))
.field("inner", &self.inner)
.field("has AutoComplete", &self.autocomplete.is_some())
.finish()
}
}
impl Default for TextField {
fn default() -> TextField {
Self {
inner: UText::new(String::with_capacity(256)),
autocomplete: None,
}
}
}
impl TextField {
pub fn new(inner: UText, autocomplete: Option<(AutoCompleteFn, Box<AutoComplete>)>) -> Self {
Self {
inner,
autocomplete,
}
}
pub fn as_str(&self) -> &str {
self.inner.as_str()
}
pub fn cursor(&self) -> usize {
self.inner.grapheme_pos()
}
pub fn cursor_inc(&mut self) {
self.inner.cursor_inc();
}
pub fn cursor_dec(&mut self) {
self.inner.cursor_dec();
}
pub fn is_empty(&self) -> bool {
self.as_str().is_empty()
}
pub fn into_string(self) -> String {
self.inner.into_string()
}
pub fn clear(&mut self) {
self.inner.clear()
}
pub fn draw_cursor(
&mut self,
grid: &mut CellBuffer,
area: Area,
secondary_area: Area,
context: &mut Context,
) {
let upper_left = upper_left!(area);
let width = width!(area);
let pos = if width < self.inner.grapheme_pos() {
width
} else {
self.inner.grapheme_pos()
};
change_colors(
grid,
(pos_inc(upper_left, (pos, 0)), pos_inc(upper_left, (pos, 0))),
crate::conf::value(context, "theme_default").fg,
crate::conf::value(context, "highlight").bg,
);
if self.inner.grapheme_len() <= 2 {
return;
}
if let Some((autocomplete_fn, autocomplete)) = self.autocomplete.as_mut() {
let entries = autocomplete_fn(context, self.inner.as_str());
autocomplete.set_suggestions(entries);
autocomplete.draw(grid, secondary_area, context);
}
}
}
impl Component for TextField {
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
let theme_attr = crate::conf::value(context, "widgets.form.field");
let width = width!(area);
let str = self.as_str();
/* Calculate which part of the str is visible
* ##########################################
*
* Example:
* For the string "The quick brown fox jumps over the lazy dog" with visible
* width of field of 10 columns
*
*
* Cursor <= width
* =================
* Cursor at:
*
* The quick brown fox jumps over the lazy dog
*
* cursor
*
*
* The quick brown fox jumps over the lazy dog
*
*
* No skip.
*
* Cursor at the end
* =================
* Cursor at:
*
* The quick brown fox jumps over the lazy dog
*
* remainder cursor
*
* ++
* |The| quick brown fox jumps over the lazy dog
* ++
* ++++++
* skip offset
*
* Intermediate cursor
* ===================
* Cursor at:
*
* The quick brown fox jumps over the lazy dog
*
* remainder cursor
*
* +
* T|he quick brown fox jumps over the lazy dog
* +
* +++
* skip offset
*/
write_string_to_grid(
if width < self.inner.grapheme_pos() {
str.trim_left_at_boundary(
width
* self
.inner
.grapheme_pos()
.wrapping_div(width)
.saturating_sub(1)
+ self.inner.grapheme_pos().wrapping_rem(width),
)
} else {
str
},
grid,
theme_attr.fg,
theme_attr.bg,
theme_attr.attrs,
area,
None,
);
}
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
match *event {
UIEvent::InsertInput(Key::Char('\t')) => {
if let Some(suggestion) = self
.autocomplete
.as_mut()
.and_then(|a| a.1.get_suggestion())
{
self.inner = UText::new(suggestion);
let len = self.inner.as_str().len();
self.inner.set_cursor(len);
} else {
self.inner.insert_char(' ');
}
}
UIEvent::InsertInput(Key::Char('\n')) => {
if let Some(suggestion) = self
.autocomplete
.as_mut()
.and_then(|a| a.1.get_suggestion())
{
self.inner = UText::new(suggestion);
let len = self.inner.as_str().len();
self.inner.set_cursor(len);
}
context
.replies
.push_back(UIEvent::ChangeMode(UIMode::Normal));
}
UIEvent::InsertInput(Key::Up) => {
if let Some(ac) = self.autocomplete.as_mut() {
ac.1.dec_cursor();
} else {
return false;
}
}
UIEvent::InsertInput(Key::Down) => {
if let Some(ac) = self.autocomplete.as_mut() {
ac.1.inc_cursor();
} else {
return false;
}
}
UIEvent::InsertInput(Key::Right) => {
self.inner.cursor_inc();
}
UIEvent::InsertInput(Key::Left) => {
self.inner.cursor_dec();
}
UIEvent::InsertInput(Key::Char(k)) => {
self.inner.insert_char(k);
}
UIEvent::InsertInput(Key::Paste(ref p)) => {
for c in p.chars() {
self.inner.insert_char(c);
}
}
UIEvent::InsertInput(Key::Alt('b')) => { /* Meta+B Backward one word */ }
UIEvent::InsertInput(Key::Backspace) | UIEvent::InsertInput(Key::Ctrl('h')) => {
/* Ctrl+H Delete previous character */
self.inner.backspace();
if let Some(ac) = self.autocomplete.as_mut() {
ac.1.set_suggestions(Vec::new());
}
}
UIEvent::InsertInput(Key::Ctrl('t')) => {
/* Ctrl+T Transpose characters */
self.inner.set_cursor(0);
}
UIEvent::InsertInput(Key::Ctrl('a')) => {
/* Beginning of line */
self.inner.set_cursor(0);
}
UIEvent::InsertInput(Key::Ctrl('b')) => {
/* Backward one character */
self.inner.cursor_dec();
}
UIEvent::InsertInput(Key::Ctrl('d')) => {
/* Delete one character */
self.inner.cursor_dec();
}
UIEvent::InsertInput(Key::Ctrl('f')) => {
/* Ctrl+F / → Forward one character */
self.inner.cursor_inc();
}
UIEvent::InsertInput(Key::Ctrl('w')) => {
/* Cut previous word */
while self.inner.as_str()[..self.inner.cursor_pos()]
.last_grapheme()
.map(|(_, graph)| !graph.is_empty() && graph.trim().is_empty())
.unwrap_or(false)
{
self.inner.backspace();
}
while self.inner.as_str()[..self.inner.cursor_pos()]
.last_grapheme()
.map(|(_, graph)| !graph.is_empty() && !graph.trim().is_empty())
.unwrap_or(false)
{
self.inner.backspace();
}
}
UIEvent::InsertInput(Key::Ctrl('u')) => self.inner.cut_left(),
UIEvent::InsertInput(Key::Ctrl('e')) => {
/* Ctrl+E End of line */
self.inner.set_cursor(self.inner.as_str().len());
}
/* TODO: add rest of readline shortcuts */
_ => {
return false;
}
}
self.set_dirty(true);
true
}
fn is_dirty(&self) -> bool {
false
}
fn set_dirty(&mut self, _value: bool) {}
fn id(&self) -> ComponentId {
ComponentId::nil()
}
fn set_id(&mut self, _id: ComponentId) {}
}
impl fmt::Display for TextField {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_str(),)
}
}

View File

@ -23,8 +23,6 @@ use std::{borrow::Cow, collections::HashMap, time::Duration};
use super::*;
type AutoCompleteFn = Box<dyn Fn(&Context, &str) -> Vec<AutoCompleteEntry> + Send + Sync>;
#[derive(Debug, PartialEq, Eq, Default)]
enum FormFocus {
#[default]
@ -36,32 +34,30 @@ enum FormFocus {
type Cursor = usize;
pub enum Field {
Text(UText, Option<(AutoCompleteFn, Box<AutoComplete>)>),
Text(TextField),
Choice(Vec<Cow<'static, str>>, Cursor),
}
impl Debug for Field {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Text(s, _) => fmt::Debug::fmt(s, f),
Self::Text(ref t) => fmt::Debug::fmt(t, f),
k => fmt::Debug::fmt(k, f),
}
}
}
use crate::Field::*;
impl Default for Field {
fn default() -> Field {
Field::Text(UText::new(String::with_capacity(256)), None)
Field::Text(TextField::default())
}
}
impl Field {
pub fn as_str(&self) -> &str {
match self {
Text(ref s, _) => s.as_str(),
Choice(ref v, cursor) => {
Self::Text(ref s) => s.as_str(),
Self::Choice(ref v, cursor) => {
if v.is_empty() {
""
} else {
@ -73,8 +69,8 @@ impl Field {
pub fn cursor(&self) -> usize {
match self {
Text(ref s, _) => s.grapheme_pos(),
Choice(_, ref cursor) => *cursor,
Self::Text(ref s) => s.cursor(),
Self::Choice(_, ref cursor) => *cursor,
}
}
@ -84,15 +80,15 @@ impl Field {
pub fn into_string(self) -> String {
match self {
Text(s, _) => s.into_string(),
Choice(mut v, cursor) => v.remove(cursor).to_string(),
Self::Text(s) => s.into_string(),
Self::Choice(mut v, cursor) => v.remove(cursor).to_string(),
}
}
pub fn clear(&mut self) {
match self {
Text(s, _) => s.clear(),
Choice(_, _) => {}
Self::Text(s) => s.clear(),
Self::Choice(_, _) => {}
}
}
@ -103,31 +99,11 @@ impl Field {
secondary_area: Area,
context: &mut Context,
) {
let upper_left = upper_left!(area);
match self {
Text(ref term, auto_complete_fn) => {
let width = width!(area);
let pos = if width < term.grapheme_pos() {
width
} else {
term.grapheme_pos()
};
change_colors(
grid,
(pos_inc(upper_left, (pos, 0)), pos_inc(upper_left, (pos, 0))),
crate::conf::value(context, "theme_default").fg,
crate::conf::value(context, "highlight").bg,
);
if term.grapheme_len() <= 2 {
return;
}
if let Some((auto_complete_fn, auto_complete)) = auto_complete_fn {
let entries = auto_complete_fn(context, term.as_str());
auto_complete.set_suggestions(entries);
auto_complete.draw(grid, secondary_area, context);
}
Self::Text(ref mut text_field) => {
text_field.draw_cursor(grid, area, secondary_area, context);
}
Choice(_, _cursor) => {}
Self::Choice(_, _cursor) => {}
}
}
}
@ -135,78 +111,12 @@ impl Field {
impl Component for Field {
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
let theme_attr = crate::conf::value(context, "widgets.form.field");
let width = width!(area);
let str = self.as_str();
match self {
Text(ref term, _) => {
/* Calculate which part of the str is visible
* ##########################################
*
* Example:
* For the string "The quick brown fox jumps over the lazy dog" with visible
* width of field of 10 columns
*
*
* Cursor <= width
* =================
* Cursor at:
*
* The quick brown fox jumps over the lazy dog
*
* cursor
*
*
* The quick brown fox jumps over the lazy dog
*
*
* No skip.
*
* Cursor at the end
* =================
* Cursor at:
*
* The quick brown fox jumps over the lazy dog
*
* remainder cursor
*
* ++
* |The| quick brown fox jumps over the lazy dog
* ++
* ++++++
* skip offset
*
* Intermediate cursor
* ===================
* Cursor at:
*
* The quick brown fox jumps over the lazy dog
*
* remainder cursor
*
* +
* T|he quick brown fox jumps over the lazy dog
* +
* +++
* skip offset
*/
write_string_to_grid(
if width < term.grapheme_pos() {
str.trim_left_at_boundary(
width * term.grapheme_pos().wrapping_div(width).saturating_sub(1)
+ term.grapheme_pos().wrapping_rem(width),
)
} else {
str
},
grid,
theme_attr.fg,
theme_attr.bg,
theme_attr.attrs,
area,
None,
);
Self::Text(ref mut text_field) => {
text_field.draw(grid, area, context);
}
Choice(_, _) => {
Self::Choice(_, _) => {
write_string_to_grid(
str,
grid,
@ -220,53 +130,16 @@ impl Component for Field {
}
}
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
if let Self::Text(ref mut t) = self {
return t.process_event(event, context);
}
match *event {
UIEvent::InsertInput(Key::Char('\t')) => {
if let Text(ref mut s, Some((_, auto_complete))) = self {
if let Some(suggestion) = auto_complete.get_suggestion() {
*s = UText::new(suggestion);
let len = s.as_str().len();
s.set_cursor(len);
return true;
}
}
if let Text(ref mut s, _) = self {
s.insert_char(' ');
}
return true;
}
UIEvent::InsertInput(Key::Char('\n')) => {
if let Text(ref mut s, Some((_, auto_complete))) = self {
if let Some(suggestion) = auto_complete.get_suggestion() {
*s = UText::new(suggestion);
let len = s.as_str().len();
s.set_cursor(len);
}
}
context
.replies
.push_back(UIEvent::ChangeMode(UIMode::Normal));
return true;
}
UIEvent::InsertInput(Key::Up) => {
if let Text(_, Some((_, auto_complete))) = self {
auto_complete.dec_cursor();
} else {
return false;
}
}
UIEvent::InsertInput(Key::Down) => {
if let Text(_, Some((_, auto_complete))) = self {
auto_complete.inc_cursor();
} else {
return false;
}
}
UIEvent::InsertInput(Key::Right) => match self {
Text(ref mut s, _) => {
s.cursor_inc();
Self::Text(_) => {
return false;
}
Choice(ref vec, ref mut cursor) => {
Self::Choice(ref vec, ref mut cursor) => {
*cursor = if *cursor == vec.len().saturating_sub(1) {
0
} else {
@ -275,10 +148,10 @@ impl Component for Field {
}
},
UIEvent::InsertInput(Key::Left) => match self {
Text(ref mut s, _) => {
s.cursor_dec();
Self::Text(_) => {
return false;
}
Choice(_, ref mut cursor) => {
Self::Choice(_, ref mut cursor) => {
if *cursor == 0 {
return false;
} else {
@ -286,73 +159,6 @@ impl Component for Field {
}
}
},
UIEvent::InsertInput(Key::Char(k)) => {
if let Text(ref mut s, _) = self {
s.insert_char(k);
}
}
UIEvent::InsertInput(Key::Paste(ref p)) => {
if let Text(ref mut s, _) = self {
for c in p.chars() {
s.insert_char(c);
}
}
}
UIEvent::InsertInput(Key::Backspace) | UIEvent::InsertInput(Key::Ctrl('h')) => {
if let Text(ref mut s, auto_complete) = self {
s.backspace();
if let Some(ac) = auto_complete.as_mut() {
ac.1.set_suggestions(Vec::new());
}
}
}
UIEvent::InsertInput(Key::Ctrl('a')) => {
if let Text(ref mut s, _) = self {
s.set_cursor(0);
}
}
UIEvent::InsertInput(Key::Ctrl('b')) => {
/* Backward one character */
if let Text(ref mut s, _) = self {
s.cursor_dec();
}
}
UIEvent::InsertInput(Key::Ctrl('f')) => {
/* Forward one character */
if let Text(ref mut s, _) = self {
s.cursor_inc();
}
}
UIEvent::InsertInput(Key::Ctrl('w')) => {
/* Cut previous word */
if let Text(ref mut s, _) = self {
while s.as_str()[..s.cursor_pos()]
.last_grapheme()
.map(|(_, graph)| !graph.is_empty() && graph.trim().is_empty())
.unwrap_or(false)
{
s.backspace();
}
while s.as_str()[..s.cursor_pos()]
.last_grapheme()
.map(|(_, graph)| !graph.is_empty() && !graph.trim().is_empty())
.unwrap_or(false)
{
s.backspace();
}
}
}
UIEvent::InsertInput(Key::Ctrl('u')) => {
if let Text(ref mut s, _) = self {
s.cut_left()
}
}
UIEvent::InsertInput(Key::Ctrl('e')) => {
if let Text(ref mut s, _) = self {
s.set_cursor(s.as_str().len());
}
}
/* TODO: add rest of readline shortcuts */
_ => {
return false;
}
@ -360,14 +166,17 @@ impl Component for Field {
self.set_dirty(true);
true
}
fn is_dirty(&self) -> bool {
false
}
fn set_dirty(&mut self, _value: bool) {}
fn id(&self) -> ComponentId {
ComponentId::nil()
}
fn set_id(&mut self, _id: ComponentId) {}
}
@ -377,8 +186,8 @@ impl fmt::Display for Field {
f,
"{}",
match self {
Text(ref s, _) => s.as_str(),
Choice(ref v, ref cursor) => v[*cursor].as_ref(),
Self::Text(ref s) => s.as_str(),
Self::Choice(ref v, ref cursor) => v[*cursor].as_ref(),
}
)
}
@ -456,23 +265,26 @@ impl<T: 'static + std::fmt::Debug + Copy + Default + Send + Sync> FormWidget<T>
pub fn push_choices(&mut self, value: (Cow<'static, str>, Vec<Cow<'static, str>>)) {
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
self.layout.push(value.0.clone());
self.fields.insert(value.0, Choice(value.1, 0));
self.fields.insert(value.0, Field::Choice(value.1, 0));
}
pub fn push_cl(&mut self, value: (Cow<'static, str>, String, AutoCompleteFn)) {
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
self.layout.push(value.0.clone());
self.fields.insert(
value.0,
Text(
Field::Text(TextField::new(
UText::new(value.1),
Some((value.2, AutoComplete::new(Vec::new()))),
),
)),
);
}
pub fn push(&mut self, value: (Cow<'static, str>, String)) {
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
self.layout.push(value.0.clone());
self.fields.insert(value.0, Text(UText::new(value.1), None));
self.fields.insert(
value.0,
Field::Text(TextField::new(UText::new(value.1), None)),
);
}
pub fn insert(&mut self, index: usize, value: (Cow<'static, str>, Field)) {