Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Add Select component. #117

Merged
merged 8 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ members = ["crates/*", "examples/*"]
async-trait = "0.1.83"
crossterm = "0.28.1"
dirs = "5.0.1"
iocraft = "0.5.0"
iocraft = "0.5.1"
# iocraft = { git = "https://github.com/ccbrown/iocraft", branch = "main" }
miette = "7.4.0"
regex = { version = "1.11.1", default-features = false }
relative-path = "1.9.3"
reqwest = { version = "0.12.9", default-features = false }
rustc-hash = "2.1.0"
serial_test = "3.2.0"
serde = { version = "1.0.215", features = ["derive"] }
serde = { version = "1.0.216", features = ["derive"] }
serde_json = "1.0.133"
serde_yaml = "0.9.34"
thiserror = "2.0.4"
thiserror = "2.0.8"
tokio = { version = "1.42.0", default-features = false, features = [
"io-util",
"rt",
Expand Down
22 changes: 10 additions & 12 deletions crates/console/src/components/confirm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::input_field::*;
use super::styled_text::*;
use crate::ui::ConsoleTheme;
use iocraft::prelude::*;

Expand All @@ -12,7 +11,7 @@ pub struct ConfirmProps<'a> {
pub no_char: char,
pub yes_label: String,
pub yes_char: char,
pub value: Option<&'a mut bool>,
pub on_confirm: Option<&'a mut bool>,
}

impl Default for ConfirmProps<'_> {
Expand All @@ -25,7 +24,7 @@ impl Default for ConfirmProps<'_> {
no_char: 'n',
yes_label: "Yes".into(),
yes_char: 'y',
value: None,
on_confirm: None,
}
}
}
Expand Down Expand Up @@ -74,9 +73,6 @@ pub fn Confirm<'a>(props: &mut ConfirmProps<'a>, mut hooks: Hooks) -> impl Into<
error.set(Some(format!("Please press [{yes}] or [{no}] to confirm")));
}
}
KeyCode::Esc => {
handle_confirm(false);
}
KeyCode::Left | KeyCode::Up | KeyCode::BackTab => {
set_focused(focused.get() - 1);
}
Expand All @@ -91,7 +87,7 @@ pub fn Confirm<'a>(props: &mut ConfirmProps<'a>, mut hooks: Hooks) -> impl Into<
});

if should_exit.get() {
if let Some(outer_value) = &mut props.value {
if let Some(outer_value) = &mut props.on_confirm {
**outer_value = confirmed.get();
}

Expand All @@ -113,10 +109,11 @@ pub fn Confirm<'a>(props: &mut ConfirmProps<'a>, mut hooks: Hooks) -> impl Into<
error: Some(error),
footer: props.legend.then(|| {
element! {
StyledText(
content: format!("<mutedlight>{yes}/{no}</mutedlight> confirm ⁃ <mutedlight>←/→</mutedlight> toggle ⁃ <mutedlight>ent/spc</mutedlight> select ⁃ <mutedlight>esc</mutedlight> cancel"),
style: Style::Muted
)
InputLegend(legend: vec![
(format!("{yes}/{no}"), "confirm".into()),
("↔".into(), "toggle".into()),
("↵".into(), "submit".into()),
])
}.into_any()
})
) {
Expand Down Expand Up @@ -162,5 +159,6 @@ pub fn Confirm<'a>(props: &mut ConfirmProps<'a>, mut hooks: Hooks) -> impl Into<
}
}
}
}.into_any()
}
.into_any()
}
44 changes: 16 additions & 28 deletions crates/console/src/components/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,50 @@ pub struct InputProps<'a> {
pub label: String,
pub prefix_symbol: Option<String>,
pub validate: Validator<'static, String>,
pub value: Option<&'a mut String>,
pub on_value: Option<&'a mut String>,
}

#[component]
pub fn Input<'a>(props: &mut InputProps<'a>, mut hooks: Hooks) -> impl Into<AnyElement<'a>> {
let theme = hooks.use_context::<ConsoleTheme>();
let mut system = hooks.use_context_mut::<SystemContext>();
let mut value = hooks.use_state(|| props.default_value.clone());
let mut submitted = hooks.use_state(|| false);
let mut should_exit = hooks.use_state(|| false);
let mut error = hooks.use_state(|| None);

let validate = props.validate.take();

hooks.use_local_terminal_events({
move |event| match event {
TerminalEvent::Key(KeyEvent { code, kind, .. }) if kind != KeyEventKind::Release => {
match code {
KeyCode::Enter => {
if let Some(msg) = validate(value.to_string()) {
error.set(Some(msg));
return;
} else {
error.set(None);
}

submitted.set(true);
should_exit.set(true);
}
KeyCode::Esc => {
should_exit.set(true);
}
_ => {}
TerminalEvent::Key(KeyEvent {
code: KeyCode::Enter,
kind,
..
}) if kind != KeyEventKind::Release => {
if let Some(msg) = validate(value.to_string()) {
error.set(Some(msg));
return;
} else {
error.set(None);
}

should_exit.set(true);
}
_ => {}
}
});

if should_exit.get() {
if submitted.get() {
if let Some(outer_value) = &mut props.value {
**outer_value = value.to_string();
}
if let Some(outer_value) = &mut props.on_value {
**outer_value = value.to_string();
}

system.exit();

return element! {
InputFieldValue(
label: &props.label,
value: if submitted.get() {
value.to_string()
} else {
String::new()
}
value: value.read().as_str(),
)
}
.into_any();
Expand Down
19 changes: 19 additions & 0 deletions crates/console/src/components/input_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,22 @@ pub fn InputFieldValue<'a>(
}
}
}

#[derive(Default, Props)]
pub struct InputLegendProps {
pub legend: Vec<(String, String)>,
}

#[component]
pub fn InputLegend<'a>(props: &InputLegendProps) -> impl Into<AnyElement<'a>> {
element! {
StyledText(
content: props.legend
.iter()
.map(|(key, label)| format!("<mutedlight>{key}</mutedlight> {label}"))
.collect::<Vec<_>>()
.join(" ⁃ "),
style: Style::Muted
)
}
}
2 changes: 2 additions & 0 deletions crates/console/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod map;
mod notice;
mod progress;
mod section;
mod select;
mod styled_text;
mod table;

Expand All @@ -20,6 +21,7 @@ pub use map::*;
pub use notice::*;
pub use progress::*;
pub use section::*;
pub use select::*;
pub use styled_text::*;
pub use table::*;

Expand Down
Loading
Loading