refactoring
This commit is contained in:
563
src/ui.rs
563
src/ui.rs
@@ -2,6 +2,7 @@ use crate::ansi;
|
||||
use crate::config::Config;
|
||||
use crate::executor;
|
||||
use crate::app::{App, MessageLevel, Popup, UpdatingStatus};
|
||||
use crate::text_input::TextInput;
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::{Color, Modifier, Style},
|
||||
@@ -38,7 +39,6 @@ fn render_menu(f: &mut Frame, app: &mut App, area: Rect) {
|
||||
let inner_h = area.height.saturating_sub(2) as usize;
|
||||
let sel = if total > 0 { app.selected_index.min(total - 1) } else { 0 };
|
||||
|
||||
// Wrap each item title into visual lines
|
||||
let item_lines: Vec<Vec<String>> = items.iter().map(|item| {
|
||||
let prefix = match &item.kind {
|
||||
crate::menu::MenuItemKind::Category { .. } => "📁 ",
|
||||
@@ -47,7 +47,6 @@ fn render_menu(f: &mut Frame, app: &mut App, area: Rect) {
|
||||
wrap_to_lines(&format!("{}{}", prefix, item.title), inner_w)
|
||||
}).collect();
|
||||
|
||||
// How many items fit starting from offset
|
||||
let vis_from = |off: usize| -> usize {
|
||||
let mut rows = 0usize;
|
||||
let mut n = 0usize;
|
||||
@@ -60,20 +59,14 @@ fn render_menu(f: &mut Frame, app: &mut App, area: Rect) {
|
||||
n
|
||||
};
|
||||
|
||||
// Clamp scroll offset for scrolloff margin
|
||||
let offset = &mut app.menu_scroll_offset;
|
||||
if total == 0 {
|
||||
*offset = 0;
|
||||
} else {
|
||||
// Ensure sel is not before offset
|
||||
if *offset > sel {
|
||||
*offset = sel;
|
||||
}
|
||||
// Scroll up: sel must not be within top SCROLLOFF items
|
||||
if *offset > sel { *offset = sel; }
|
||||
if sel < offset.saturating_add(SCROLLOFF) && *offset > 0 {
|
||||
*offset = sel.saturating_sub(SCROLLOFF);
|
||||
}
|
||||
// Scroll down: sel must not be within bottom SCROLLOFF items
|
||||
loop {
|
||||
let vis = vis_from(*offset);
|
||||
if vis == 0 { break; }
|
||||
@@ -84,7 +77,6 @@ fn render_menu(f: &mut Frame, app: &mut App, area: Rect) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Clamp to valid range
|
||||
let max_off = total.saturating_sub(1);
|
||||
*offset = (*offset).min(max_off);
|
||||
}
|
||||
@@ -162,7 +154,6 @@ fn render_description(f: &mut Frame, app: &App, area: Rect) {
|
||||
fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
let area = f.area();
|
||||
|
||||
// Use a smaller area for update-related popups
|
||||
let popup_area = match popup {
|
||||
Popup::UpdateConfirm { .. } | Popup::Updating { .. } => centered_rect(50, 40, area),
|
||||
_ => centered_rect(80, 90, area),
|
||||
@@ -171,11 +162,7 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
f.render_widget(Clear, popup_area);
|
||||
|
||||
match popup {
|
||||
Popup::Confirming {
|
||||
action: _,
|
||||
item_title,
|
||||
confirm_message,
|
||||
} => {
|
||||
Popup::Confirming { action: _, item_title, confirm_message } => {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title("Подтверждение")
|
||||
@@ -199,11 +186,10 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
)),
|
||||
]
|
||||
};
|
||||
let paragraph = Paragraph::new(text)
|
||||
.block(block)
|
||||
.alignment(Alignment::Center)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, popup_area);
|
||||
f.render_widget(
|
||||
Paragraph::new(text).block(block).alignment(Alignment::Center).wrap(Wrap { trim: true }),
|
||||
popup_area,
|
||||
);
|
||||
}
|
||||
|
||||
Popup::ExecutingStructured {
|
||||
@@ -211,8 +197,7 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
kill_tx: _,
|
||||
log_buffer,
|
||||
current_command,
|
||||
input_buffer,
|
||||
input_cursor,
|
||||
input,
|
||||
menu_selected_index,
|
||||
form_cursor,
|
||||
form_values,
|
||||
@@ -222,12 +207,8 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
finished,
|
||||
} => {
|
||||
let cmd_height: u16 = match current_command {
|
||||
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(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,
|
||||
};
|
||||
@@ -238,11 +219,9 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
.constraints([Constraint::Min(3), Constraint::Length(cmd_height)].as_ref())
|
||||
.split(popup_area);
|
||||
|
||||
// ── Scrollable log ──────────────────────────────────────────────
|
||||
let visible = chunks[0].height.saturating_sub(2) as usize;
|
||||
let total = log_buffer.len();
|
||||
|
||||
// Clamp pos so we never show empty lines past the end.
|
||||
let max_pos = total.saturating_sub(visible);
|
||||
if *log_follow_bottom {
|
||||
*log_scroll_pos = max_pos;
|
||||
@@ -274,9 +253,7 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
let at_bottom = pos >= max_pos;
|
||||
let at_top = pos == 0;
|
||||
|
||||
// Reusable scroll hint based on current position
|
||||
let scroll_hint = if total <= visible {
|
||||
// All content fits — nothing to scroll
|
||||
String::new()
|
||||
} else if at_top && at_bottom {
|
||||
String::new()
|
||||
@@ -289,36 +266,19 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
};
|
||||
|
||||
let (title, border_color): (String, Color) = match finished {
|
||||
Some(0) => (
|
||||
format!(" ✓ Завершено — Esc закрыть{} ", scroll_hint),
|
||||
Color::Green,
|
||||
),
|
||||
Some(code) => (
|
||||
format!(" ✗ Ошибка (код {}) — Esc закрыть{} ", code, scroll_hint),
|
||||
Color::Red,
|
||||
),
|
||||
None if at_top && at_bottom => (
|
||||
" Выполняется… ".to_string(),
|
||||
Color::DarkGray,
|
||||
),
|
||||
None if at_bottom => (
|
||||
" Выполняется… │ PgUp/↑ прокрутить вверх ".to_string(),
|
||||
Color::DarkGray,
|
||||
),
|
||||
None => (
|
||||
format!(" Выполняется… │ {}/{} │ PgDn/↓ вниз ", pos + 1, total),
|
||||
Color::Yellow,
|
||||
),
|
||||
Some(0) => (format!(" ✓ Завершено — Esc закрыть{} ", scroll_hint), Color::Green),
|
||||
Some(code) => (format!(" ✗ Ошибка (код {}) — Esc закрыть{} ", code, scroll_hint), Color::Red),
|
||||
None if at_top && at_bottom => (" Выполняется… ".to_string(), Color::DarkGray),
|
||||
None if at_bottom => (" Выполняется… │ PgUp/↑ прокрутить вверх ".to_string(), Color::DarkGray),
|
||||
None => (format!(" Выполняется… │ {}/{} │ PgDn/↓ вниз ", pos + 1, total), Color::Yellow),
|
||||
};
|
||||
|
||||
let log_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(title.as_str())
|
||||
.border_style(Style::default().fg(border_color));
|
||||
|
||||
f.render_widget(List::new(log_items).block(log_block), chunks[0]);
|
||||
|
||||
// ── Scrollbar ───────────────────────────────────────────────────
|
||||
if total > visible {
|
||||
let scrollbar = Scrollbar::default()
|
||||
.orientation(ScrollbarOrientation::VerticalRight)
|
||||
@@ -335,7 +295,6 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
f.render_stateful_widget(scrollbar, sb_area, &mut sb_state);
|
||||
}
|
||||
|
||||
// ── Horizontal scrollbar ─────────────────────────────────────────
|
||||
if max_content_w > viewport_w {
|
||||
let mut h_state = ScrollbarState::new(max_scroll_x).position(scroll_x);
|
||||
let hscroll_area = Rect {
|
||||
@@ -355,15 +314,9 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Interactive command area ─────────────────────────────────────
|
||||
if cmd_height > 0 {
|
||||
if let Some(cmd) = current_command {
|
||||
render_command(
|
||||
f, cmd, input_buffer, *input_cursor,
|
||||
*menu_selected_index,
|
||||
*form_cursor, form_values,
|
||||
chunks[1],
|
||||
);
|
||||
render_command(f, cmd, input, *menu_selected_index, *form_cursor, form_values, chunks[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -378,20 +331,16 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
.borders(Borders::ALL)
|
||||
.title("Сообщение")
|
||||
.border_style(Style::default().fg(color));
|
||||
let paragraph = Paragraph::new(text.as_str())
|
||||
.block(block)
|
||||
.alignment(Alignment::Center)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, popup_area);
|
||||
f.render_widget(
|
||||
Paragraph::new(text.as_str()).block(block).alignment(Alignment::Center).wrap(Wrap { trim: true }),
|
||||
popup_area,
|
||||
);
|
||||
}
|
||||
|
||||
Popup::UpdateConfirm { info } => {
|
||||
let mut lines = vec![
|
||||
Line::from(""),
|
||||
Line::from(Span::raw(format!(
|
||||
" {} → {}",
|
||||
info.current_version, info.new_version
|
||||
))),
|
||||
Line::from(Span::raw(format!(" {} → {}", info.current_version, info.new_version))),
|
||||
];
|
||||
if info.size > 0 {
|
||||
let mb = info.size as f64 / 1_048_576.0;
|
||||
@@ -407,17 +356,10 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
.borders(Borders::ALL)
|
||||
.title(" Доступно обновление ")
|
||||
.border_style(Style::default().fg(Color::Cyan));
|
||||
let paragraph = Paragraph::new(lines)
|
||||
.block(block)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, popup_area);
|
||||
f.render_widget(Paragraph::new(lines).block(block).wrap(Wrap { trim: true }), popup_area);
|
||||
}
|
||||
|
||||
Popup::Updating {
|
||||
info,
|
||||
downloaded,
|
||||
status,
|
||||
} => {
|
||||
Popup::Updating { info, downloaded, status } => {
|
||||
let (title, border_color) = match status {
|
||||
UpdatingStatus::Downloading => (" Загрузка обновления… ", Color::Cyan),
|
||||
UpdatingStatus::Applying => (" Применение обновления… ", Color::Yellow),
|
||||
@@ -428,23 +370,14 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
let bar_width = popup_area.width.saturating_sub(4) as usize;
|
||||
|
||||
let progress_line = if info.size > 0 {
|
||||
let percent = ((*downloaded as f64 / info.size as f64) * 100.0)
|
||||
.min(100.0) as usize;
|
||||
let percent = ((*downloaded as f64 / info.size as f64) * 100.0).min(100.0) as usize;
|
||||
let filled = (percent * bar_width) / 100;
|
||||
format!(
|
||||
"[{}{}] {}%",
|
||||
"█".repeat(filled),
|
||||
"░".repeat(bar_width.saturating_sub(filled)),
|
||||
percent
|
||||
)
|
||||
format!("[{}{}] {}%", "█".repeat(filled), "░".repeat(bar_width.saturating_sub(filled)), percent)
|
||||
} else {
|
||||
// Indeterminate: animate based on downloaded bytes
|
||||
let pos = ((*downloaded / 4096) as usize) % bar_width.max(1);
|
||||
let thumb = 4.min(bar_width);
|
||||
let mut bar = vec!['░'; bar_width];
|
||||
for i in pos..((pos + thumb).min(bar_width)) {
|
||||
bar[i] = '█';
|
||||
}
|
||||
for i in pos..((pos + thumb).min(bar_width)) { bar[i] = '█'; }
|
||||
format!("[{}]", bar.iter().collect::<String>())
|
||||
};
|
||||
|
||||
@@ -455,8 +388,7 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
let mb_total = info.size as f64 / 1_048_576.0;
|
||||
format!(" {:.1} / {:.1} МБ", mb_done, mb_total)
|
||||
} else {
|
||||
let kb = *downloaded / 1024;
|
||||
format!(" {} КБ загружено", kb)
|
||||
format!(" {} КБ загружено", *downloaded / 1024)
|
||||
}
|
||||
}
|
||||
UpdatingStatus::Applying => " Применяется…".to_string(),
|
||||
@@ -473,20 +405,14 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
|
||||
if matches!(status, UpdatingStatus::Failed(_)) {
|
||||
lines.push(Line::from(""));
|
||||
lines.push(Line::from(Span::styled(
|
||||
" [Esc] Закрыть",
|
||||
Style::default().fg(Color::Yellow),
|
||||
)));
|
||||
lines.push(Line::from(Span::styled(" [Esc] Закрыть", Style::default().fg(Color::Yellow))));
|
||||
}
|
||||
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(title)
|
||||
.border_style(Style::default().fg(border_color));
|
||||
let paragraph = Paragraph::new(lines)
|
||||
.block(block)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, popup_area);
|
||||
f.render_widget(Paragraph::new(lines).block(block).wrap(Wrap { trim: true }), popup_area);
|
||||
}
|
||||
|
||||
Popup::EndpointSelector { selected, scroll_offset } => {
|
||||
@@ -497,14 +423,12 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
let total = endpoints.len();
|
||||
let inner_h = popup_area.height.saturating_sub(2) as usize;
|
||||
|
||||
// Compute name column width
|
||||
let name_col_w = endpoints.iter()
|
||||
.map(|ep| ep.name.chars().count())
|
||||
.max()
|
||||
.unwrap_or(8)
|
||||
.max(8);
|
||||
|
||||
// Clamp scroll_offset to keep selected visible
|
||||
if *selected < *scroll_offset {
|
||||
*scroll_offset = *selected;
|
||||
} else if inner_h > 0 && *selected >= *scroll_offset + inner_h {
|
||||
@@ -523,13 +447,8 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
let is_active = ep.url == config.active_endpoint;
|
||||
|
||||
let prefix = if is_selected { "▶ " } else { " " };
|
||||
|
||||
let n = ep.name.chars().count();
|
||||
let pad = if n < name_col_w {
|
||||
" ".repeat(name_col_w - n)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let pad = if n < name_col_w { " ".repeat(name_col_w - n) } else { String::new() };
|
||||
|
||||
let (bg, name_fg, url_fg) = if is_selected {
|
||||
(Color::Blue, Color::White, Color::Gray)
|
||||
@@ -561,11 +480,8 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
.borders(Borders::ALL)
|
||||
.title(format!(" Эндпоинты [активен: {}] ", active_name))
|
||||
.title(
|
||||
Line::from(Span::styled(
|
||||
" [Esc] ",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))
|
||||
.alignment(Alignment::Right),
|
||||
Line::from(Span::styled(" [Esc] ", Style::default().fg(Color::DarkGray)))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.title_bottom(
|
||||
Line::from(Span::styled(
|
||||
@@ -596,14 +512,7 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
}
|
||||
}
|
||||
|
||||
Popup::AddEndpoint {
|
||||
url_buf,
|
||||
name_buf,
|
||||
active_field,
|
||||
cursor,
|
||||
error,
|
||||
..
|
||||
} => {
|
||||
Popup::UpsertEndpoint { edit_index, fields, active_field, error, .. } => {
|
||||
let popup_area = centered_rect(60, 50, area);
|
||||
f.render_widget(Clear, popup_area);
|
||||
|
||||
@@ -618,27 +527,8 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
])
|
||||
.split(popup_area);
|
||||
|
||||
let name_color = if *active_field == 0 { Color::Cyan } else { Color::DarkGray };
|
||||
let url_color = if *active_field == 1 { Color::Cyan } else { Color::DarkGray };
|
||||
|
||||
f.render_widget(
|
||||
Paragraph::new(name_buf.as_str()).block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Название ")
|
||||
.border_style(Style::default().fg(name_color)),
|
||||
),
|
||||
chunks[0],
|
||||
);
|
||||
f.render_widget(
|
||||
Paragraph::new(url_buf.as_str()).block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" URL ")
|
||||
.border_style(Style::default().fg(url_color)),
|
||||
),
|
||||
chunks[1],
|
||||
);
|
||||
render_text_field(f, " Название ", &fields[0], *active_field == 0, chunks[0]);
|
||||
render_text_field(f, " URL ", &fields[1], *active_field == 1, chunks[1]);
|
||||
|
||||
if let Some(err) = error {
|
||||
f.render_widget(
|
||||
@@ -649,149 +539,75 @@ fn render_popup(f: &mut Frame, popup: &mut Popup, config: &Config) {
|
||||
);
|
||||
}
|
||||
|
||||
// Position terminal cursor in the active field
|
||||
let active_chunk = if *active_field == 0 { chunks[0] } else { chunks[1] };
|
||||
let cx = (active_chunk.x + 1 + *cursor as u16)
|
||||
.min(active_chunk.x + active_chunk.width.saturating_sub(2));
|
||||
f.set_cursor_position((cx, active_chunk.y + 1));
|
||||
// Real terminal cursor in the active field
|
||||
let af = *active_field;
|
||||
let cx = (chunks[af].x + 1 + fields[af].cursor as u16)
|
||||
.min(chunks[af].x + chunks[af].width.saturating_sub(2));
|
||||
f.set_cursor_position((cx, chunks[af].y + 1));
|
||||
|
||||
let outer_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Добавить эндпоинт ")
|
||||
.title(
|
||||
Line::from(Span::styled(
|
||||
" [Esc] ",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.title_bottom(
|
||||
Line::from(Span::styled(
|
||||
" Enter — далее / добавить ",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.border_style(Style::default().fg(Color::Cyan));
|
||||
f.render_widget(outer_block, popup_area);
|
||||
let (title, hint) = if edit_index.is_some() {
|
||||
(" Редактировать эндпоинт ", " Enter — далее / сохранить ")
|
||||
} else {
|
||||
(" Добавить эндпоинт ", " Enter — далее / добавить ")
|
||||
};
|
||||
|
||||
f.render_widget(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(title)
|
||||
.title(
|
||||
Line::from(Span::styled(" [Esc] ", Style::default().fg(Color::DarkGray)))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.title_bottom(
|
||||
Line::from(Span::styled(hint, Style::default().fg(Color::DarkGray)))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.border_style(Style::default().fg(Color::Cyan)),
|
||||
popup_area,
|
||||
);
|
||||
}
|
||||
|
||||
Popup::ConfirmDeleteEndpoint { index, .. } => {
|
||||
let popup_area = centered_rect(50, 30, area);
|
||||
f.render_widget(Clear, popup_area);
|
||||
|
||||
let name = config.endpoints.get(*index)
|
||||
.map(|ep| ep.name.as_str())
|
||||
.unwrap_or("?");
|
||||
|
||||
let name = config.endpoints.get(*index).map(|ep| ep.name.as_str()).unwrap_or("?");
|
||||
let text = vec![
|
||||
Line::from(""),
|
||||
Line::from(Span::raw(format!(" Удалить эндпоинт «{}»?", name))),
|
||||
Line::from(""),
|
||||
Line::from(Span::styled(
|
||||
" [Y] Да [N / Esc] Нет",
|
||||
Style::default().fg(Color::Yellow),
|
||||
)),
|
||||
Line::from(Span::styled(" [Y] Да [N / Esc] Нет", Style::default().fg(Color::Yellow))),
|
||||
];
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Подтверждение ")
|
||||
.border_style(Style::default().fg(Color::Yellow));
|
||||
f.render_widget(
|
||||
Paragraph::new(text).block(block).wrap(Wrap { trim: true }),
|
||||
Paragraph::new(text)
|
||||
.block(Block::default().borders(Borders::ALL).title(" Подтверждение ")
|
||||
.border_style(Style::default().fg(Color::Yellow)))
|
||||
.wrap(Wrap { trim: true }),
|
||||
popup_area,
|
||||
);
|
||||
}
|
||||
|
||||
Popup::EditEndpoint {
|
||||
name_buf,
|
||||
url_buf,
|
||||
active_field,
|
||||
cursor,
|
||||
error,
|
||||
..
|
||||
} => {
|
||||
let popup_area = centered_rect(60, 50, area);
|
||||
f.render_widget(Clear, popup_area);
|
||||
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.margin(1)
|
||||
.constraints([
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(0),
|
||||
])
|
||||
.split(popup_area);
|
||||
|
||||
let name_color = if *active_field == 0 { Color::Cyan } else { Color::DarkGray };
|
||||
let url_color = if *active_field == 1 { Color::Cyan } else { Color::DarkGray };
|
||||
|
||||
f.render_widget(
|
||||
Paragraph::new(name_buf.as_str()).block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Название ")
|
||||
.border_style(Style::default().fg(name_color)),
|
||||
),
|
||||
chunks[0],
|
||||
);
|
||||
f.render_widget(
|
||||
Paragraph::new(url_buf.as_str()).block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" URL ")
|
||||
.border_style(Style::default().fg(url_color)),
|
||||
),
|
||||
chunks[1],
|
||||
);
|
||||
|
||||
if let Some(err) = error {
|
||||
f.render_widget(
|
||||
Paragraph::new(err.as_str())
|
||||
.style(Style::default().fg(Color::Red))
|
||||
.wrap(Wrap { trim: true }),
|
||||
chunks[2],
|
||||
);
|
||||
}
|
||||
|
||||
// Position terminal cursor in the active field
|
||||
let active_chunk = if *active_field == 0 { chunks[0] } else { chunks[1] };
|
||||
let cx = (active_chunk.x + 1 + *cursor as u16)
|
||||
.min(active_chunk.x + active_chunk.width.saturating_sub(2));
|
||||
f.set_cursor_position((cx, active_chunk.y + 1));
|
||||
|
||||
let outer_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Редактировать эндпоинт ")
|
||||
.title(
|
||||
Line::from(Span::styled(
|
||||
" [Esc] ",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.title_bottom(
|
||||
Line::from(Span::styled(
|
||||
" Enter — далее / сохранить ",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.border_style(Style::default().fg(Color::Cyan));
|
||||
f.render_widget(outer_block, popup_area);
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders a labelled text field with colored border for active/inactive state.
|
||||
fn render_text_field(f: &mut Frame, label: &str, input: &TextInput, is_active: bool, area: Rect) {
|
||||
let color = if is_active { Color::Cyan } else { Color::DarkGray };
|
||||
f.render_widget(
|
||||
Paragraph::new(input.buf.as_str()).block(
|
||||
Block::default().borders(Borders::ALL).title(label)
|
||||
.border_style(Style::default().fg(color)),
|
||||
),
|
||||
area,
|
||||
);
|
||||
}
|
||||
|
||||
fn render_command(
|
||||
f: &mut Frame,
|
||||
cmd: &executor::StructuredCommand,
|
||||
input_buffer: &str,
|
||||
input_cursor: usize,
|
||||
input: &TextInput,
|
||||
menu_selected_index: usize,
|
||||
form_cursor: usize,
|
||||
form_values: &[bool],
|
||||
@@ -800,19 +616,18 @@ fn render_command(
|
||||
match cmd {
|
||||
executor::StructuredCommand::Input { prompt, secret, .. } => {
|
||||
let display = if *secret {
|
||||
"•".repeat(input_buffer.chars().count())
|
||||
"•".repeat(input.buf.chars().count())
|
||||
} else {
|
||||
input_buffer.to_string()
|
||||
input.buf.clone()
|
||||
};
|
||||
let paragraph = Paragraph::new(display.as_str()).block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(format!(" {} ", prompt))
|
||||
.border_style(Style::default().fg(Color::Cyan)),
|
||||
f.render_widget(
|
||||
Paragraph::new(display.as_str()).block(
|
||||
Block::default().borders(Borders::ALL).title(format!(" {} ", prompt))
|
||||
.border_style(Style::default().fg(Color::Cyan)),
|
||||
),
|
||||
area,
|
||||
);
|
||||
f.render_widget(paragraph, area);
|
||||
let cursor_x = (area.x + 1 + input_cursor as u16)
|
||||
.min(area.x + area.width.saturating_sub(2));
|
||||
let cursor_x = (area.x + 1 + input.cursor as u16).min(area.x + area.width.saturating_sub(2));
|
||||
f.set_cursor_position((cursor_x, area.y + 1));
|
||||
}
|
||||
|
||||
@@ -824,12 +639,7 @@ fn render_command(
|
||||
if i == menu_selected_index {
|
||||
ListItem::new(Line::from(vec![
|
||||
Span::styled(" ▶ ", Style::default().fg(Color::Cyan)),
|
||||
Span::styled(
|
||||
opt.label.as_str(),
|
||||
Style::default()
|
||||
.bg(Color::Blue)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::styled(opt.label.as_str(), Style::default().bg(Color::Blue).add_modifier(Modifier::BOLD)),
|
||||
]))
|
||||
} else {
|
||||
ListItem::new(Line::from(vec![
|
||||
@@ -843,40 +653,32 @@ fn render_command(
|
||||
let mut state = ListState::default();
|
||||
state.select(Some(menu_selected_index));
|
||||
|
||||
let list = List::new(items).block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(format!(" {} ", prompt))
|
||||
.title_bottom(
|
||||
Line::from(Span::styled(
|
||||
" ↑↓ навигация Enter выбор ",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.border_style(Style::default().fg(Color::Cyan)),
|
||||
f.render_stateful_widget(
|
||||
List::new(items).block(
|
||||
Block::default().borders(Borders::ALL).title(format!(" {} ", prompt))
|
||||
.title_bottom(
|
||||
Line::from(Span::styled(" ↑↓ навигация Enter выбор ", Style::default().fg(Color::DarkGray)))
|
||||
.alignment(Alignment::Right),
|
||||
)
|
||||
.border_style(Style::default().fg(Color::Cyan)),
|
||||
),
|
||||
area,
|
||||
&mut state,
|
||||
);
|
||||
f.render_stateful_widget(list, area, &mut state);
|
||||
}
|
||||
|
||||
executor::StructuredCommand::Confirm { prompt } => {
|
||||
let text = vec![
|
||||
Line::from(Span::raw(prompt.as_str())),
|
||||
Line::from(""),
|
||||
Line::from(Span::styled(
|
||||
" [Y] Да [N / Esc] Нет",
|
||||
Style::default().fg(Color::Yellow),
|
||||
)),
|
||||
];
|
||||
let paragraph = Paragraph::new(text)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Подтверждение ")
|
||||
.border_style(Style::default().fg(Color::Yellow)),
|
||||
)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, area);
|
||||
f.render_widget(
|
||||
Paragraph::new(vec![
|
||||
Line::from(Span::raw(prompt.as_str())),
|
||||
Line::from(""),
|
||||
Line::from(Span::styled(" [Y] Да [N / Esc] Нет", Style::default().fg(Color::Yellow))),
|
||||
])
|
||||
.block(Block::default().borders(Borders::ALL).title(" Подтверждение ")
|
||||
.border_style(Style::default().fg(Color::Yellow)))
|
||||
.wrap(Wrap { trim: true }),
|
||||
area,
|
||||
);
|
||||
}
|
||||
|
||||
executor::StructuredCommand::Message { level, text } => {
|
||||
@@ -885,23 +687,17 @@ fn render_command(
|
||||
executor::MessageLevel::Warn => Color::Yellow,
|
||||
executor::MessageLevel::Error => Color::Red,
|
||||
};
|
||||
let lines = vec![
|
||||
Line::from(Span::styled(text.as_str(), Style::default().fg(color))),
|
||||
Line::from(""),
|
||||
Line::from(Span::styled(
|
||||
" Нажмите Enter для продолжения",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
)),
|
||||
];
|
||||
let paragraph = Paragraph::new(lines)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Сообщение ")
|
||||
.border_style(Style::default().fg(color)),
|
||||
)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, area);
|
||||
f.render_widget(
|
||||
Paragraph::new(vec![
|
||||
Line::from(Span::styled(text.as_str(), Style::default().fg(color))),
|
||||
Line::from(""),
|
||||
Line::from(Span::styled(" Нажмите Enter для продолжения", Style::default().fg(Color::DarkGray))),
|
||||
])
|
||||
.block(Block::default().borders(Borders::ALL).title(" Сообщение ")
|
||||
.border_style(Style::default().fg(color)))
|
||||
.wrap(Wrap { trim: true }),
|
||||
area,
|
||||
);
|
||||
}
|
||||
|
||||
executor::StructuredCommand::Progress { percent, message } => {
|
||||
@@ -916,36 +712,26 @@ fn render_command(
|
||||
"░".repeat(bar_width.saturating_sub(filled)),
|
||||
pct
|
||||
);
|
||||
let paragraph = Paragraph::new(vec![
|
||||
Line::from(bar),
|
||||
Line::from(msg),
|
||||
])
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Прогресс ")
|
||||
.border_style(Style::default().fg(Color::Cyan)),
|
||||
f.render_widget(
|
||||
Paragraph::new(vec![Line::from(bar), Line::from(msg)])
|
||||
.block(Block::default().borders(Borders::ALL).title(" Прогресс ")
|
||||
.border_style(Style::default().fg(Color::Cyan))),
|
||||
area,
|
||||
);
|
||||
f.render_widget(paragraph, area);
|
||||
}
|
||||
None => {
|
||||
// Indeterminate: spinning braille dots
|
||||
const SPINNER: &[&str] =
|
||||
&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
||||
const SPINNER: &[&str] = &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
||||
let frame = (std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_millis()
|
||||
/ 120) as usize;
|
||||
.as_millis() / 120) as usize;
|
||||
let spinner = SPINNER[frame % SPINNER.len()];
|
||||
let line = format!("{} {}", spinner, msg);
|
||||
let paragraph = Paragraph::new(line).block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Прогресс ")
|
||||
.border_style(Style::default().fg(Color::Cyan)),
|
||||
f.render_widget(
|
||||
Paragraph::new(format!("{} {}", spinner, msg))
|
||||
.block(Block::default().borders(Borders::ALL).title(" Прогресс ")
|
||||
.border_style(Style::default().fg(Color::Cyan))),
|
||||
area,
|
||||
);
|
||||
f.render_widget(paragraph, area);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -954,12 +740,8 @@ fn render_command(
|
||||
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 { "( )" }
|
||||
}
|
||||
executor::FormFieldType::Checkbox => if checked { "[✓]" } else { "[ ]" },
|
||||
executor::FormFieldType::Radio => if checked { "(●)" } else { "( )" },
|
||||
};
|
||||
let is_cursor = i == form_cursor;
|
||||
let prefix = if is_cursor { "▶ " } else { " " };
|
||||
@@ -976,33 +758,30 @@ fn render_command(
|
||||
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::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)),
|
||||
),
|
||||
area,
|
||||
&mut state,
|
||||
);
|
||||
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 } => {
|
||||
let paragraph = Paragraph::new(format!("Запуск: {}", shell))
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(" Внешнее приложение ")
|
||||
.border_style(Style::default().fg(Color::Magenta)),
|
||||
)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, area);
|
||||
f.render_widget(
|
||||
Paragraph::new(format!("Запуск: {}", shell))
|
||||
.block(Block::default().borders(Borders::ALL).title(" Внешнее приложение ")
|
||||
.border_style(Style::default().fg(Color::Magenta)))
|
||||
.wrap(Wrap { trim: true }),
|
||||
area,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1010,29 +789,22 @@ fn render_command(
|
||||
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
||||
let popup_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(
|
||||
[
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
Constraint::Percentage(percent_y),
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
]
|
||||
.as_ref(),
|
||||
)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
Constraint::Percentage(percent_y),
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
].as_ref())
|
||||
.split(r);
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints(
|
||||
[
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
Constraint::Percentage(percent_x),
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
]
|
||||
.as_ref(),
|
||||
)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
Constraint::Percentage(percent_x),
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
].as_ref())
|
||||
.split(popup_layout[1])[1]
|
||||
}
|
||||
|
||||
/// Wrap `text` into lines of at most `max_width` chars (word-wrap, hard-break on long words).
|
||||
fn wrap_to_lines(text: &str, max_width: usize) -> Vec<String> {
|
||||
if max_width == 0 || text.is_empty() {
|
||||
return vec![text.to_string()];
|
||||
@@ -1072,7 +844,6 @@ fn wrap_to_lines(text: &str, max_width: usize) -> Vec<String> {
|
||||
lines
|
||||
}
|
||||
|
||||
/// Skip `offset` display chars from the start of a parsed ANSI span list.
|
||||
fn h_scroll(parsed: Vec<(ratatui::style::Style, String)>, offset: usize) -> Vec<Span<'static>> {
|
||||
let mut skip = offset;
|
||||
let mut result = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user