Added checkboxes & radio buttons; Updated uninstall script

This commit is contained in:
Uber Veng
2026-05-22 19:04:52 +07:00
parent ab4ad186e6
commit 6a79fb2c54
8 changed files with 296 additions and 56 deletions

View File

@@ -61,6 +61,10 @@ pub enum Popup {
input_buffer: String,
/// Cursor position for Menu-type commands.
menu_selected_index: usize,
/// Cursor position for Form-type commands.
form_cursor: usize,
/// Checked/selected state for each Form field (parallel to fields vec).
form_values: Vec<bool>,
/// Index of the first visible line in log_buffer (0 = top of output).
log_scroll_pos: usize,
/// When true, keep position pinned to the bottom as new output arrives.
@@ -197,11 +201,20 @@ impl App {
if let Some(Popup::ExecutingStructured {
current_command,
menu_selected_index,
form_cursor,
form_values,
..
}) = &mut self.popup
{
if matches!(cmd, executor::StructuredCommand::Menu { .. }) {
*menu_selected_index = 0;
match &cmd {
executor::StructuredCommand::Menu { .. } => {
*menu_selected_index = 0;
}
executor::StructuredCommand::Form { fields, .. } => {
*form_cursor = 0;
*form_values = fields.iter().map(|f| f.default).collect();
}
_ => {}
}
*current_command = Some(cmd);
}
@@ -285,6 +298,8 @@ impl App {
input_buffer,
current_command,
menu_selected_index,
form_cursor,
form_values,
log_scroll_pos,
log_follow_bottom,
log_buffer,
@@ -298,6 +313,10 @@ impl App {
current_command,
Some(executor::StructuredCommand::Confirm { .. })
);
let is_form = matches!(
current_command,
Some(executor::StructuredCommand::Form { .. })
);
let has_command = current_command.is_some();
// Vim motions are disabled only when free text input is active.
let is_text_input = matches!(
@@ -319,6 +338,53 @@ impl App {
*log_scroll_pos + 1 >= log_buffer.len();
}
// ── Form: navigation and toggle ──────────────────
KeyCode::Up if is_form => {
if *form_cursor > 0 { *form_cursor -= 1; }
}
KeyCode::Down if is_form => {
let len = if let Some(
executor::StructuredCommand::Form { fields, .. }
) = current_command { fields.len() } else { 0 };
if *form_cursor + 1 < len { *form_cursor += 1; }
}
KeyCode::Char(' ') if is_form => {
let cursor = *form_cursor;
// Collect toggle info before mutating form_values
let toggle = if let Some(
executor::StructuredCommand::Form { fields, .. }
) = current_command {
fields.get(cursor).map(|f| {
let group_indices: Vec<usize> = fields.iter()
.enumerate()
.filter(|(_, ff)| {
ff.group.is_some()
&& ff.group == f.group
})
.map(|(i, _)| i)
.collect();
(f.field_type.clone(), group_indices)
})
} else { None };
if let Some((ftype, group_indices)) = toggle {
match ftype {
executor::FormFieldType::Checkbox => {
if let Some(v) = form_values.get_mut(cursor) {
*v = !*v;
}
}
executor::FormFieldType::Radio => {
for i in group_indices {
if let Some(v) = form_values.get_mut(i) {
*v = i == cursor;
}
}
}
}
}
}
// ── Arrow keys ────────────────────────────────────
KeyCode::Up if is_menu => {
if *menu_selected_index > 0 {
@@ -359,23 +425,41 @@ impl App {
}
// ── Text input ────────────────────────────────────
// Vim motion keys (j/k/l/h) are excluded here so they
// fall through to the vim-motion arms below.
// Excluded: vim motions (j/k/l/h) and form mode.
KeyCode::Char(c)
if !is_menu
&& !is_confirm
&& !is_form
&& (is_text_input
|| !matches!(c, 'j' | 'k' | 'l' | 'h')) =>
{
input_buffer.push(c);
}
KeyCode::Backspace if !is_menu => {
KeyCode::Backspace if !is_menu && !is_form => {
input_buffer.pop();
}
// ── Enter: commit response ────────────────────────
KeyCode::Enter => {
if let Some(cmd) = current_command.take() {
// Form submit: collect checked IDs
if is_form {
if let Some(executor::StructuredCommand::Form {
fields, ..
}) = current_command.take() {
let response = fields.iter().enumerate()
.filter_map(|(i, f)| {
form_values.get(i)
.copied()
.filter(|&v| v)
.map(|_| f.id.as_str())
})
.collect::<Vec<_>>()
.join(" ");
let _ = reply_tx.send(response);
form_values.clear();
*form_cursor = 0;
}
} else if let Some(cmd) = current_command.take() {
let response = match &cmd {
executor::StructuredCommand::Input { .. } => {
input_buffer.clone()
@@ -393,7 +477,6 @@ impl App {
.get(*menu_selected_index)
.map(|opt| opt.id.clone())
.unwrap_or_default(),
// Message / Progress: send empty ack.
_ => String::new(),
};
let _ = reply_tx.send(response);
@@ -418,9 +501,9 @@ impl App {
// ── Vim motions (off during text input) ───────────
KeyCode::Char('k') if !is_text_input => {
if is_menu {
if *menu_selected_index > 0 {
*menu_selected_index -= 1;
}
if *menu_selected_index > 0 { *menu_selected_index -= 1; }
} else if is_form {
if *form_cursor > 0 { *form_cursor -= 1; }
} else if !has_command {
*log_scroll_pos = log_scroll_pos.saturating_sub(1);
*log_follow_bottom = false;
@@ -430,12 +513,16 @@ impl App {
if is_menu {
if let Some(executor::StructuredCommand::Menu {
options, ..
}) = current_command
{
}) = current_command {
if *menu_selected_index + 1 < options.len() {
*menu_selected_index += 1;
}
}
} else if is_form {
let len = if let Some(
executor::StructuredCommand::Form { fields, .. }
) = current_command { fields.len() } else { 0 };
if *form_cursor + 1 < len { *form_cursor += 1; }
} else if !has_command {
*log_scroll_pos = log_scroll_pos.saturating_add(1);
*log_follow_bottom =
@@ -443,18 +530,31 @@ impl App {
}
}
KeyCode::Char('l') if !is_text_input => {
// Forward / confirm — same logic as Enter.
if let Some(cmd) = current_command.take() {
// Form submit
if is_form {
if let Some(executor::StructuredCommand::Form {
fields, ..
}) = current_command.take() {
let response = fields.iter().enumerate()
.filter_map(|(i, f)| {
form_values.get(i).copied()
.filter(|&v| v).map(|_| f.id.as_str())
})
.collect::<Vec<_>>().join(" ");
let _ = reply_tx.send(response);
form_values.clear();
*form_cursor = 0;
}
} else if let Some(cmd) = current_command.take() {
let response = match &cmd {
executor::StructuredCommand::Confirm { .. } => {
"y".to_string()
}
executor::StructuredCommand::Menu {
options, ..
} => options
.get(*menu_selected_index)
.map(|o| o.id.clone())
.unwrap_or_default(),
executor::StructuredCommand::Menu { options, .. } => {
options.get(*menu_selected_index)
.map(|o| o.id.clone())
.unwrap_or_default()
}
_ => String::new(),
};
let _ = reply_tx.send(response);
@@ -691,6 +791,8 @@ impl App {
current_command: None,
input_buffer: String::new(),
menu_selected_index: 0,
form_cursor: 0,
form_values: Vec::new(),
log_scroll_pos: 0,
log_follow_bottom: false,
finished: None,

View File

@@ -1,4 +1,4 @@
pub mod structured;
pub use structured::{StructuredCommand, MessageLevel};
pub use structured::{StructuredCommand, MessageLevel, FormField, FormFieldType};
// Здесь могут быть функции для download, http и т.д.

View File

@@ -31,15 +31,34 @@ pub enum StructuredCommand {
percent: u8,
message: Option<String>,
},
/// A list of checkboxes and radio buttons.
/// Response: space-separated IDs of all checked/selected fields.
Form {
prompt: String,
fields: Vec<FormField>,
},
/// Run an interactive program that needs the real terminal.
/// The client suspends ratatui, inherits stdin/stdout/stderr, waits for
/// the process to exit, then restores the TUI.
/// The script receives the exit code as the response.
Exec {
shell: String,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum FormFieldType {
Checkbox,
Radio,
}
#[derive(Debug, Clone)]
pub struct FormField {
pub id: String,
pub label: String,
pub field_type: FormFieldType,
pub default: bool,
/// Radio buttons with the same group are mutually exclusive.
pub group: Option<String>,
}
#[derive(Debug)]
pub struct MenuOption {
pub id: String,
@@ -203,6 +222,25 @@ fn parse_command(json_str: &str, cmd_tx: UnboundedSender<StructuredCommand>) ->
let message = v["message"].as_str().map(String::from);
StructuredCommand::Progress { percent, message }
}
"form" => {
let prompt = v["prompt"].as_str().unwrap_or("").to_string();
let fields = v["fields"].as_array()
.map(|arr| {
arr.iter().filter_map(|f| {
let id = f["id"].as_str()?.to_string();
let label = f["label"].as_str()?.to_string();
let field_type = match f["field_type"].as_str().unwrap_or("checkbox") {
"radio" => FormFieldType::Radio,
_ => FormFieldType::Checkbox,
};
let default = f["default"].as_bool().unwrap_or(false);
let group = f["group"].as_str().map(String::from);
Some(FormField { id, label, field_type, default, group })
}).collect()
})
.unwrap_or_default();
StructuredCommand::Form { prompt, fields }
}
"exec" => {
let shell = v["shell"].as_str().unwrap_or("").to_string();
StructuredCommand::Exec { shell }

View File

@@ -132,6 +132,8 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
current_command,
input_buffer,
menu_selected_index,
form_cursor,
form_values,
log_scroll_pos,
log_follow_bottom,
finished,
@@ -140,6 +142,9 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
Some(executor::StructuredCommand::Menu { options, .. }) => {
(options.len() as u16 + 2).min(14)
}
Some(executor::StructuredCommand::Form { fields, .. }) => {
(fields.len() as u16 + 2).min(16)
}
Some(_) => 5,
None => 0,
};
@@ -229,7 +234,12 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
// ── Interactive command area ─────────────────────────────────────
if cmd_height > 0 {
if let Some(cmd) = current_command {
render_command(f, cmd, input_buffer, *menu_selected_index, chunks[1]);
render_command(
f, cmd, input_buffer,
*menu_selected_index,
*form_cursor, form_values,
chunks[1],
);
}
}
}
@@ -364,6 +374,8 @@ fn render_command(
cmd: &executor::StructuredCommand,
input_buffer: &str,
menu_selected_index: usize,
form_cursor: usize,
form_values: &[bool],
area: Rect,
) {
match cmd {
@@ -492,6 +504,47 @@ fn render_command(
f.render_widget(paragraph, area);
}
executor::StructuredCommand::Form { prompt, fields } => {
let items: Vec<ListItem> = fields.iter().enumerate().map(|(i, field)| {
let checked = form_values.get(i).copied().unwrap_or(false);
let icon = match field.field_type {
executor::FormFieldType::Checkbox => {
if checked { "[✓]" } else { "[ ]" }
}
executor::FormFieldType::Radio => {
if checked { "(●)" } else { "( )" }
}
};
let is_cursor = i == form_cursor;
let prefix = if is_cursor { "" } else { " " };
let style = if is_cursor {
Style::default().bg(Color::Blue).add_modifier(Modifier::BOLD)
} else {
Style::default()
};
ListItem::new(Line::from(vec![
Span::raw(format!("{}{} {}", prefix, icon, field.label)),
])).style(style)
}).collect();
let mut state = ListState::default();
state.select(Some(form_cursor));
let list = List::new(items).block(
Block::default()
.borders(Borders::ALL)
.title(format!(" {} ", prompt))
.title_bottom(
Line::from(Span::styled(
" Space — переключить Enter/l — применить Esc — отмена ",
Style::default().fg(Color::DarkGray),
)).alignment(Alignment::Right),
)
.border_style(Style::default().fg(Color::Cyan)),
);
f.render_stateful_widget(list, area, &mut state);
}
// Exec is handled by the main loop before rendering; this arm is
// never reached in practice but satisfies the exhaustiveness check.
executor::StructuredCommand::Exec { shell } => {