From d475f8dc8b07456805612a38b9b5cd38945ac18e Mon Sep 17 00:00:00 2001 From: dehwyy Date: Wed, 9 Oct 2024 00:18:28 +0300 Subject: [PATCH] [FEAT] - change method name --- src/core/ui/components/input.rs | 10 +++--- src/core/ui/components/select.rs | 29 ++++++++-------- src/core/ui/components/text.rs | 57 +++++++++++++++++--------------- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/core/ui/components/input.rs b/src/core/ui/components/input.rs index c88517b..ca86de6 100644 --- a/src/core/ui/components/input.rs +++ b/src/core/ui/components/input.rs @@ -1,12 +1,13 @@ use std::fmt::Display; +use futures::stream; use tokio::io::{AsyncWriteExt, Stdout}; use crate::{ core::{ cell::color::{Color, Rgb}, event::key::{Key, KeyChar}, - geom::align::Align, + geom::{align::Align, stretch::Stretch}, io::{ ansi, out::{ @@ -98,7 +99,7 @@ impl Input { ((self.placeholder.len() + self.value.len()) as u16, 1) } - async fn destroy(&mut self, stdout: &mut Stdout) -> String { + async fn destroy(&mut self) -> String { self.value.trim().to_string() } } @@ -135,7 +136,8 @@ impl DynamicComponent<(), String> for Input { text_builder .add_part(TextPart::new(&self.value).decor(self.value_decor)) - .build_with_align(self.inner.pos) + .pos(self.inner.pos) + .build() .render(stdout) .await; @@ -159,7 +161,7 @@ impl DynamicComponent<(), String> for Input { } // Enter -> Destroy component Key::Enter(_) => { - return ComponentRenderOutput::Destroyed(self.destroy(stdout).await); + return ComponentRenderOutput::Destroyed(self.destroy().await); } // TODO: handle _ev => {} diff --git a/src/core/ui/components/select.rs b/src/core/ui/components/select.rs index c080ee3..6bb7c70 100644 --- a/src/core/ui/components/select.rs +++ b/src/core/ui/components/select.rs @@ -2,16 +2,19 @@ use std::{fmt::Display, process::exit}; use tokio::io::Stdout; -use crate::core::{ - cell::color::Color, - event::{key::Key, Event, EventReceiver}, - geom::align::Align, - io::{ - ansi::def as ansi, - out::{flags::OutputGroupFlags, group::OutputGroup}, - text_decor::TextDecoration, +use crate::{ + build_padding, + core::{ + cell::color::Color, + event::{key::Key, Event, EventReceiver}, + geom::align::Align, + io::{ + ansi::def as ansi, + out::{flags::OutputGroupFlags, group::OutputGroup}, + text_decor::TextDecoration, + }, + terminal::Terminal, }, - terminal::Terminal, }; use super::{ @@ -20,12 +23,6 @@ use super::{ StaticComponent, }; -macro_rules! build_padding { - ($pad:ident) => {{ - (0..$pad).map(|_| ' ').collect::() - }}; -} - pub struct SelectOption { pub text: String, pub decor: TextDecoration, @@ -315,7 +312,7 @@ impl DynamicComponent<(), Vec> for Select { b = self.apply_option_style(i, choice, b); } - b.build_with_align(self.inner.pos).render(stdout).await; + b.pos(self.inner.pos).build().render(stdout).await; while let Ok(ev) = rx.try_recv() { if let Event::Key(key) = ev { diff --git a/src/core/ui/components/text.rs b/src/core/ui/components/text.rs index 3faff44..ab263aa 100644 --- a/src/core/ui/components/text.rs +++ b/src/core/ui/components/text.rs @@ -1,21 +1,26 @@ +use std::process::exit; + use tokio::io::Stdout; -use crate::core::{ - event::EventReceiver, - geom::align::Align, - io::{ - ansi::{ - def as ansi, - global::AnsiGlobal, - sequence::{AnsiSequence, AnsiSequenceType}, +use crate::{ + build_padding, + core::{ + event::EventReceiver, + geom::align::Align, + io::{ + ansi::{ + def as ansi, + global::AnsiGlobal, + sequence::{AnsiSequence, AnsiSequenceType}, + }, + out::{flags::OutputGroupFlags, group::OutputGroup, Output}, + text_decor::TextDecoration, }, - out::{flags::OutputGroupFlags, group::OutputGroup, Output}, - text_decor::TextDecoration, + terminal::{Terminal, TerminalPosition}, }, - terminal::{Terminal, TerminalPosition}, }; -use super::{Component, ComponentInner, ComponentRenderOutput, StaticComponent}; +use super::{Component, ComponentInner, ComponentRenderOutput, ComponentSize, StaticComponent}; #[derive(Default, Clone)] pub struct TextPart { @@ -53,6 +58,7 @@ impl TextPart { pub struct TextBuilder { flags: OutputGroupFlags, parts: Vec, + pos: TerminalPosition, } impl Default for TextBuilder { @@ -60,6 +66,7 @@ impl Default for TextBuilder { Self { flags: OutputGroupFlags::empty(), parts: Vec::new(), + pos: TerminalPosition::default(), } } } @@ -79,17 +86,15 @@ impl TextBuilder { self } - pub fn build(self) -> Text { - Text { - inner: ComponentInner::default(), - flags: self.flags, - parts: self.parts, - } + pub fn pos(mut self, c_pos: TerminalPosition) -> Self { + self.pos = c_pos; + self } - pub fn build_with_align(self, (x, y): TerminalPosition) -> Text { - // move to base position. + pub fn build(self) -> Text { + let (x, y) = self.pos; + // move to base position. let mut reset_position_sequence = { let mut line = 1u16; move || { @@ -100,19 +105,17 @@ impl TextBuilder { let mut parts = vec![reset_position_sequence()]; - for part in self.parts { + for (i, part) in self.parts.iter().enumerate() { parts.push(part.clone()); - part.ln.then(|| parts.push(reset_position_sequence())); + // TODO: do not call if `next` elem doesn't exist + (part.ln && i < self.parts.len() - 1).then(|| parts.push(reset_position_sequence())); } Text { - inner: ComponentInner { - pos: (x, y), - alignment: Align::default(), - }, + inner: ComponentInner::default(), flags: self.flags, - parts: parts, + parts, } } }