Skip to content

Commit

Permalink
[FEAT] - change method name
Browse files Browse the repository at this point in the history
  • Loading branch information
dehwyy committed Oct 8, 2024
1 parent 7b6bd1c commit d475f8d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
10 changes: 6 additions & 4 deletions src/core/ui/components/input.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -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 => {}
Expand Down
29 changes: 13 additions & 16 deletions src/core/ui/components/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -20,12 +23,6 @@ use super::{
StaticComponent,
};

macro_rules! build_padding {
($pad:ident) => {{
(0..$pad).map(|_| ' ').collect::<String>()
}};
}

pub struct SelectOption {
pub text: String,
pub decor: TextDecoration,
Expand Down Expand Up @@ -315,7 +312,7 @@ impl DynamicComponent<(), Vec<usize>> 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 {
Expand Down
57 changes: 30 additions & 27 deletions src/core/ui/components/text.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -53,13 +58,15 @@ impl TextPart {
pub struct TextBuilder {
flags: OutputGroupFlags,
parts: Vec<TextPart>,
pos: TerminalPosition,
}

impl Default for TextBuilder {
fn default() -> Self {
Self {
flags: OutputGroupFlags::empty(),
parts: Vec::new(),
pos: TerminalPosition::default(),
}
}
}
Expand All @@ -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 || {
Expand All @@ -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,
}
}
}
Expand Down

0 comments on commit d475f8d

Please sign in to comment.