Skip to content
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
46 changes: 6 additions & 40 deletions crates/openshell-tui/src/ui/create_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,16 @@ fn draw_enter_key(

// Name field.
let name_placeholder = format!("optional (defaults to {selected_type})");
draw_text_field(
super::draw_text_field(
frame,
"Name",
&form.name,
&name_placeholder,
form.key_field == ProviderKeyField::Name,
chunks[idx],
t,
"_",
false,
);
idx += 1;

Expand All @@ -295,14 +297,16 @@ fn draw_enter_key(

if form.is_generic {
// Env var name field.
draw_text_field(
super::draw_text_field(
frame,
"Env var name",
&form.generic_env_name,
"e.g. MY_API_KEY",
form.key_field == ProviderKeyField::EnvVarName,
chunks[idx],
t,
"_",
false,
);
idx += 1;

Expand Down Expand Up @@ -773,44 +777,6 @@ pub fn draw_update(frame: &mut Frame<'_>, app: &App, area: Rect) {
// Helpers
// ---------------------------------------------------------------------------

fn draw_text_field(
frame: &mut Frame<'_>,
label: &str,
value: &str,
placeholder: &str,
focused: bool,
area: Rect,
theme: &crate::theme::Theme,
) {
let t = theme;
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1), // label
Constraint::Length(1), // input
])
.split(area);

let label_style = if focused { t.accent_bold } else { t.text };
let mut label_spans = vec![Span::styled(format!("{label}:"), label_style)];
if !placeholder.is_empty() {
label_spans.push(Span::styled(format!(" {placeholder}"), t.muted));
}
frame.render_widget(Paragraph::new(Line::from(label_spans)), chunks[0]);

let display = if value.is_empty() && !focused {
Line::from(Span::styled(" -", t.muted))
} else if focused {
Line::from(vec![
Span::styled(format!(" {value}"), t.accent),
Span::styled("_", t.accent),
])
} else {
Line::from(Span::styled(format!(" {value}"), t.text))
};
frame.render_widget(Paragraph::new(display), chunks[1]);
}

fn draw_secret_field(
frame: &mut Frame<'_>,
label: &str,
Expand Down
55 changes: 9 additions & 46 deletions crates/openshell-tui/src/ui/create_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,42 @@ fn draw_form(frame: &mut Frame<'_>, app: &App, area: Rect) {
.split(inner);

// --- Name ---
draw_text_field(
super::draw_text_field(
frame,
"Name",
&form.name,
"optional — auto-generated if empty",
form.focused_field == CreateFormField::Name,
chunks[0],
t,
"█",
true,
);

// --- Image ---
draw_text_field(
super::draw_text_field(
frame,
"Image",
&form.image,
"optional — server default if empty",
form.focused_field == CreateFormField::Image,
chunks[1],
t,
"█",
true,
);

// --- Command ---
draw_text_field(
super::draw_text_field(
frame,
"Command",
&form.command,
"optional — runs /bin/bash if empty",
form.focused_field == CreateFormField::Command,
chunks[2],
t,
"█",
true,
);

// --- Providers label ---
Expand Down Expand Up @@ -377,46 +383,3 @@ pub fn render_chase(

Line::from(spans)
}

// ---------------------------------------------------------------------------
// Form helpers
// ---------------------------------------------------------------------------

fn draw_text_field(
frame: &mut Frame<'_>,
label: &str,
value: &str,
placeholder: &str,
focused: bool,
area: Rect,
theme: &crate::theme::Theme,
) {
let t = theme;
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1), // label
Constraint::Length(1), // input
Constraint::Length(1), // gap
])
.split(area);

let label_style = if focused { t.accent_bold } else { t.text };
let mut label_spans = vec![Span::styled(format!("{label}:"), label_style)];
if !placeholder.is_empty() {
label_spans.push(Span::styled(format!(" {placeholder}"), t.muted));
}
frame.render_widget(Paragraph::new(Line::from(label_spans)), chunks[0]);

let display = if value.is_empty() && !focused {
Line::from(Span::styled(" -", t.muted))
} else if focused {
Line::from(vec![
Span::styled(format!(" {value}"), t.accent),
Span::styled("█", t.accent),
])
} else {
Line::from(Span::styled(format!(" {value}"), t.text))
};
frame.render_widget(Paragraph::new(display), chunks[1]);
}
27 changes: 3 additions & 24 deletions crates/openshell-tui/src/ui/global_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use ratatui::Frame;
use ratatui::layout::{Constraint, Rect};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Cell, Clear, Padding, Paragraph, Row, Table};
use ratatui::widgets::{Block, Borders, Cell, Padding, Row, Table};

use super::draw_empty_message;

Expand Down Expand Up @@ -133,10 +133,6 @@ fn draw_confirm_set(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) {
};
let new_value = app.setting_edit.as_ref().map_or("-", |e| e.input.as_str());

// 7 content lines + 2 border rows = 9 outer height.
let popup = centered_rect(60, 9, area);
frame.render_widget(Clear, popup);

let lines = vec![
Line::from(Span::styled(" Confirm Global Setting Change ", t.heading)),
Line::from(""),
Expand All @@ -161,12 +157,7 @@ fn draw_confirm_set(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) {
]),
];

let block = Block::default()
.borders(Borders::ALL)
.border_style(t.border_focused)
.padding(Padding::horizontal(1));

frame.render_widget(Paragraph::new(lines).block(block), popup);
super::draw_confirm_popup(frame, lines, t.border_focused, 60, area);
}

fn draw_confirm_delete(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) {
Expand Down Expand Up @@ -197,17 +188,5 @@ fn draw_confirm_delete(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect)
]),
];

// content lines + 2 for border
let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX);
let popup = centered_rect(60, popup_height, area);
frame.render_widget(Clear, popup);

let block = Block::default()
.borders(Borders::ALL)
.border_style(t.status_err)
.padding(Padding::horizontal(1));

frame.render_widget(Paragraph::new(lines).block(block), popup);
super::draw_confirm_popup(frame, lines, t.status_err, 60, area);
}

use super::centered_popup as centered_rect;
80 changes: 80 additions & 0 deletions crates/openshell-tui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,86 @@ fn draw_setting_edit_overlay(
frame.render_widget(Paragraph::new(lines).block(block), popup);
}

/// Draw a labeled text input field.
///
/// `cursor` is the cursor character shown when `focused` (e.g. `"█"` or `"_"`).
/// `show_gap` appends an extra blank row below the input line.
#[allow(clippy::too_many_arguments)]
fn draw_text_field(
frame: &mut Frame<'_>,
label: &str,
value: &str,
placeholder: &str,
focused: bool,
area: Rect,
theme: &Theme,
cursor: &str,
show_gap: bool,
) {
let t = theme;
let chunks = if show_gap {
Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1), // label
Constraint::Length(1), // input
Constraint::Length(1), // gap
])
.split(area)
} else {
Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1), // label
Constraint::Length(1), // input
])
.split(area)
};

let label_style = if focused { t.accent_bold } else { t.text };
let mut label_spans = vec![Span::styled(format!("{label}:"), label_style)];
if !placeholder.is_empty() {
label_spans.push(Span::styled(format!(" {placeholder}"), t.muted));
}
frame.render_widget(Paragraph::new(Line::from(label_spans)), chunks[0]);

let display = if value.is_empty() && !focused {
Line::from(Span::styled(" -", t.muted))
} else if focused {
Line::from(vec![
Span::styled(format!(" {value}"), t.accent),
Span::styled(cursor, t.accent),
])
} else {
Line::from(Span::styled(format!(" {value}"), t.text))
};
frame.render_widget(Paragraph::new(display), chunks[1]);
}

/// Render a bordered confirmation popup centred on `area`.
///
/// `lines` is the popup body; height is derived from the line count.
/// Use `t.border_focused` for confirmations and `t.status_err` for destructive
/// actions.
fn draw_confirm_popup(
frame: &mut Frame<'_>,
lines: Vec<Line<'_>>,
border_style: Style,
width: u16,
area: Rect,
) {
let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX);
let popup = centered_popup(width, popup_height, area);
frame.render_widget(Clear, popup);

let block = Block::default()
.borders(Borders::ALL)
.border_style(border_style)
.padding(Padding::horizontal(1));

frame.render_widget(Paragraph::new(lines).block(block), popup);
}

/// Center a popup rectangle within `area` using percentage-based width and
/// an absolute height (in rows).
pub fn centered_popup(percent_x: u16, height: u16, area: Rect) -> Rect {
Expand Down
26 changes: 3 additions & 23 deletions crates/openshell-tui/src/ui/sandbox_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use ratatui::Frame;
use ratatui::layout::{Constraint, Rect};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Cell, Clear, Padding, Paragraph, Row, Table};
use ratatui::widgets::{Block, Borders, Cell, Padding, Row, Table};

use super::draw_empty_message;
use crate::app::{App, SandboxPolicyTab, SettingScope};
Expand Down Expand Up @@ -166,16 +166,7 @@ fn draw_confirm_set(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) {
]),
];

let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX);
let popup = centered_rect(60, popup_height, area);
frame.render_widget(Clear, popup);

let block = Block::default()
.borders(Borders::ALL)
.border_style(t.border_focused)
.padding(Padding::horizontal(1));

frame.render_widget(Paragraph::new(lines).block(block), popup);
super::draw_confirm_popup(frame, lines, t.border_focused, 60, area);
}

fn draw_confirm_delete(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) {
Expand Down Expand Up @@ -204,16 +195,5 @@ fn draw_confirm_delete(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect)
]),
];

let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX);
let popup = centered_rect(55, popup_height, area);
frame.render_widget(Clear, popup);

let block = Block::default()
.borders(Borders::ALL)
.border_style(t.status_err)
.padding(Padding::horizontal(1));

frame.render_widget(Paragraph::new(lines).block(block), popup);
super::draw_confirm_popup(frame, lines, t.status_err, 55, area);
}

use super::centered_popup as centered_rect;
Loading