mirror of
https://github.com/veeso/termscp.git
synced 2026-07-24 08:37:24 +02:00
TextSpan instead of strings
This commit is contained in:
@@ -118,7 +118,7 @@ impl FileList {
|
||||
// Initialize states
|
||||
let mut states: OwnStates = OwnStates::default();
|
||||
// Set list length
|
||||
states.set_list_len(match &props.texts.body {
|
||||
states.set_list_len(match &props.texts.rows {
|
||||
Some(tokens) => tokens.len(),
|
||||
None => 0,
|
||||
});
|
||||
@@ -136,11 +136,11 @@ impl Component for FileList {
|
||||
false => None,
|
||||
true => {
|
||||
// Make list
|
||||
let list_item: Vec<ListItem> = match self.props.texts.body.as_ref() {
|
||||
let list_item: Vec<ListItem> = match self.props.texts.rows.as_ref() {
|
||||
None => vec![],
|
||||
Some(lines) => lines
|
||||
.iter()
|
||||
.map(|line: &String| ListItem::new(Span::from(line.to_string())))
|
||||
.map(|line| ListItem::new(Span::from(line.content.to_string())))
|
||||
.collect(),
|
||||
};
|
||||
let (fg, bg): (Color, Color) = match self.states.focus {
|
||||
@@ -186,7 +186,7 @@ impl Component for FileList {
|
||||
fn update(&mut self, props: Props) -> Msg {
|
||||
self.props = props;
|
||||
// re-Set list length
|
||||
self.states.set_list_len(match &self.props.texts.body {
|
||||
self.states.set_list_len(match &self.props.texts.rows {
|
||||
Some(tokens) => tokens.len(),
|
||||
None => 0,
|
||||
});
|
||||
@@ -287,7 +287,7 @@ impl Component for FileList {
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
use crate::ui::layout::props::TextParts;
|
||||
use crate::ui::layout::props::{TextParts, TextSpan};
|
||||
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
@@ -298,7 +298,7 @@ mod tests {
|
||||
PropsBuilder::default()
|
||||
.with_texts(TextParts::new(
|
||||
Some(String::from("filelist")),
|
||||
Some(vec![String::from("file1"), String::from("file2")]),
|
||||
Some(vec![TextSpan::from("file1"), TextSpan::from("file2")]),
|
||||
))
|
||||
.build(),
|
||||
);
|
||||
@@ -323,9 +323,9 @@ mod tests {
|
||||
.with_texts(TextParts::new(
|
||||
Some(String::from("filelist")),
|
||||
Some(vec![
|
||||
String::from("file1"),
|
||||
String::from("file2"),
|
||||
String::from("file3"),
|
||||
TextSpan::from("file1"),
|
||||
TextSpan::from("file2"),
|
||||
TextSpan::from("file3"),
|
||||
]),
|
||||
))
|
||||
.build(),
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
|
||||
// locals
|
||||
use super::super::props::TextSpan;
|
||||
use super::{Component, InputEvent, Msg, Payload, PropValue, Props, PropsBuilder, Render};
|
||||
// ext
|
||||
use crossterm::event::KeyCode;
|
||||
@@ -73,6 +74,13 @@ impl OwnStates {
|
||||
self.choice -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// ### make_choices
|
||||
///
|
||||
/// Set OwnStates choices from a vector of text spans
|
||||
pub fn make_choices(&mut self, spans: &Vec<TextSpan>) {
|
||||
self.choices = spans.iter().map(|x| x.content.clone()).collect();
|
||||
}
|
||||
}
|
||||
|
||||
// -- component
|
||||
@@ -92,8 +100,8 @@ impl RadioGroup {
|
||||
pub fn new(props: Props) -> Self {
|
||||
// Make states
|
||||
let mut states: OwnStates = OwnStates::default();
|
||||
// Update choices
|
||||
states.choices = props.texts.body.clone().unwrap_or(Vec::new());
|
||||
// Update choices (vec of TextSpan to String)
|
||||
states.make_choices(props.texts.rows.as_ref().unwrap_or(&Vec::new()));
|
||||
// Get value
|
||||
if let PropValue::Unsigned(choice) = props.value {
|
||||
states.choice = choice;
|
||||
@@ -164,7 +172,8 @@ impl Component for RadioGroup {
|
||||
/// Returns a Msg to the view
|
||||
fn update(&mut self, props: Props) -> Msg {
|
||||
// Reset choices
|
||||
self.states.choices = props.texts.body.clone().unwrap_or(Vec::new());
|
||||
self.states
|
||||
.make_choices(props.texts.rows.as_ref().unwrap_or(&Vec::new()));
|
||||
// Get value
|
||||
if let PropValue::Unsigned(choice) = props.value {
|
||||
self.states.choice = choice;
|
||||
@@ -256,7 +265,7 @@ impl Component for RadioGroup {
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
use crate::ui::layout::props::TextParts;
|
||||
use crate::ui::layout::props::{TextParts, TextSpan};
|
||||
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
@@ -268,9 +277,9 @@ mod tests {
|
||||
.with_texts(TextParts::new(
|
||||
Some(String::from("yes or no?")),
|
||||
Some(vec![
|
||||
String::from("Yes!"),
|
||||
String::from("No"),
|
||||
String::from("Maybe"),
|
||||
TextSpan::from("Yes!"),
|
||||
TextSpan::from("No"),
|
||||
TextSpan::from("Maybe"),
|
||||
]),
|
||||
))
|
||||
.with_value(PropValue::Unsigned(1))
|
||||
|
||||
Reference in New Issue
Block a user