File size: 8,931 Bytes
e5034c3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | use std::io::IsTerminal;
use anyhow::Result;
use console::strip_ansi_codes;
use crate::preview::{PreviewLayout, PreviewPlacement, SelectMode, SelectRow, SelectUiOptions};
/// Builder for select prompts with fuzzy search.
pub struct SelectBuilder<T> {
pub(crate) message: String,
pub(crate) options: Vec<T>,
pub(crate) starting_cursor: Option<usize>,
pub(crate) default: Option<bool>,
pub(crate) help_message: Option<&'static str>,
pub(crate) initial_text: Option<String>,
pub(crate) header_lines: usize,
pub(crate) preview: Option<String>,
pub(crate) preview_window: Option<String>,
}
impl<T: 'static> SelectBuilder<T> {
/// Set starting cursor position.
pub fn with_starting_cursor(mut self, cursor: usize) -> Self {
self.starting_cursor = Some(cursor);
self
}
/// Set a preview command shown in a side panel as the user navigates items.
pub fn with_preview(mut self, command: impl Into<String>) -> Self {
self.preview = Some(command.into());
self
}
/// Set the layout of the preview panel.
pub fn with_preview_window(mut self, layout: impl Into<String>) -> Self {
self.preview_window = Some(layout.into());
self
}
/// Set default for confirm prompts using bool options.
pub fn with_default(mut self, default: bool) -> Self {
self.default = Some(default);
self
}
/// Set help message displayed as a header above the list.
pub fn with_help_message(mut self, message: &'static str) -> Self {
self.help_message = Some(message);
self
}
/// Set initial search text for fuzzy search.
pub fn with_initial_text(mut self, text: impl Into<String>) -> Self {
self.initial_text = Some(text.into());
self
}
/// Set the number of header lines treated as non-selectable options.
pub fn with_header_lines(mut self, n: usize) -> Self {
self.header_lines = n;
self
}
/// Execute select prompt with fuzzy search.
///
/// # Returns
///
/// - `Ok(Some(T))` when the user selects an option.
/// - `Ok(None)` when no options are available or the user cancels.
///
/// # Errors
///
/// Returns an error if the picker cannot set up terminal interaction,
/// render, process events, or run a preview command.
pub fn prompt(self) -> Result<Option<T>>
where
T: std::fmt::Display + Clone,
{
if !std::io::stderr().is_terminal() {
return Ok(None);
}
if std::any::TypeId::of::<T>() == std::any::TypeId::of::<bool>() {
return prompt_confirm_as(&self.message, self.default);
}
if self.options.is_empty() {
return Ok(None);
}
let rows = self
.options
.iter()
.enumerate()
.map(|(index, item)| {
let display = strip_ansi_codes(&item.to_string()).trim().to_string();
if index < self.header_lines {
SelectRow::header(display)
} else {
SelectRow::new(index.to_string(), display.clone()).search(display)
}
})
.collect::<Vec<_>>();
let header_count = self.header_lines.min(rows.len());
if rows.len() == header_count {
return Ok(None);
}
let mut selector = SelectUiOptions::new(format!("{} ❯ ", self.message), rows)
.header_lines(header_count)
.mode(SelectMode::Single)
.preview_layout(parse_preview_layout(self.preview_window.as_deref()));
if let Some(query) = self.initial_text {
selector = selector.query(Some(query));
}
if let Some(preview) = self.preview {
selector = selector.preview(Some(preview));
}
if let Some(cursor) = self.starting_cursor {
selector = selector.initial_raw(Some(cursor.to_string()));
}
if let Some(help) = self.help_message {
selector.rows.insert(0, SelectRow::header(help));
selector.header_lines = selector.header_lines.saturating_add(1);
}
let selected = selector.prompt()?;
Ok(selected.and_then(|row| {
row.raw
.parse::<usize>()
.ok()
.and_then(|index| self.options.get(index).cloned())
}))
}
}
fn parse_preview_layout(layout: Option<&str>) -> PreviewLayout {
let Some(layout) = layout else {
return PreviewLayout::default();
};
let placement = if layout.contains("down") || layout.contains("bottom") {
PreviewPlacement::Bottom
} else {
PreviewPlacement::Right
};
let percent = layout
.split(|ch: char| !ch.is_ascii_digit())
.find_map(|part| part.parse::<u16>().ok())
.unwrap_or_else(|| PreviewLayout::default().percent)
.clamp(1, 99);
PreviewLayout { placement, percent }
}
/// Runs a yes/no confirmation prompt.
///
/// Returns `Ok(Some(true))` for Yes, `Ok(Some(false))` for No, and `Ok(None)`
/// if cancelled.
fn prompt_confirm(message: &str, default: Option<bool>) -> Result<Option<bool>> {
let rows = if default == Some(false) {
vec![SelectRow::new("no", "No"), SelectRow::new("yes", "Yes")]
} else {
vec![SelectRow::new("yes", "Yes"), SelectRow::new("no", "No")]
};
let selected = SelectUiOptions::new(format!("{} ❯ ", message), rows).prompt()?;
Ok(selected.and_then(|row| match row.raw.as_str() {
"yes" => Some(true),
"no" => Some(false),
_ => None,
}))
}
/// Wrapper around [`prompt_confirm`] that safely converts the `bool` result
/// into the generic type `T`.
///
/// This must only be called when `T` is known to be `bool`.
fn prompt_confirm_as<T: 'static + Clone>(
message: &str,
default: Option<bool>,
) -> Result<Option<T>> {
let result = prompt_confirm(message, default)?;
Ok(result.and_then(|value| {
let any_value: Box<dyn std::any::Any> = Box::new(value);
any_value.downcast::<T>().ok().map(|boxed| *boxed)
}))
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::*;
use crate::ForgeWidget;
#[test]
fn test_select_builder_creates() {
let builder = ForgeWidget::select("Test", vec!["a", "b", "c"]);
assert_eq!(builder.message, "Test");
assert_eq!(builder.options, vec!["a", "b", "c"]);
}
#[test]
fn test_confirm_builder_creates() {
let builder = ForgeWidget::confirm("Confirm?");
assert_eq!(builder.message, "Confirm?");
}
#[test]
fn test_select_builder_with_initial_text() {
let builder =
ForgeWidget::select("Test", vec!["apple", "banana", "cherry"]).with_initial_text("app");
assert_eq!(builder.initial_text, Some("app".to_string()));
}
#[test]
fn test_select_owned_builder_with_initial_text() {
let builder =
ForgeWidget::select("Test", vec!["apple", "banana", "cherry"]).with_initial_text("ban");
assert_eq!(builder.initial_text, Some("ban".to_string()));
}
#[test]
fn test_ansi_stripping() {
let fixture = ["\x1b[1mBold\x1b[0m", "\x1b[31mRed\x1b[0m"];
let actual: Vec<String> = fixture
.iter()
.map(|value| strip_ansi_codes(value).to_string())
.collect();
let expected = vec!["Bold", "Red"];
assert_eq!(actual, expected);
}
#[test]
fn test_display_options_are_trimmed() {
let fixture = [
" openai [empty]",
"✓ anthropic [api.anthropic.com]",
];
let actual: Vec<String> = fixture
.iter()
.map(|value| strip_ansi_codes(value).trim().to_string())
.collect();
let expected = vec![
"openai [empty]".to_string(),
"✓ anthropic [api.anthropic.com]".to_string(),
];
assert_eq!(actual, expected);
}
#[test]
fn test_with_starting_cursor() {
let builder = ForgeWidget::select("Test", vec!["a", "b", "c"]).with_starting_cursor(2);
assert_eq!(builder.starting_cursor, Some(2));
}
#[test]
fn test_parse_preview_layout_defaults_to_right() {
let fixture = None;
let actual = parse_preview_layout(fixture);
let expected = PreviewLayout { placement: PreviewPlacement::Right, percent: 50 };
assert_eq!(actual, expected);
}
#[test]
fn test_parse_preview_layout_supports_bottom_percent() {
let fixture = Some("down,60%");
let actual = parse_preview_layout(fixture);
let expected = PreviewLayout { placement: PreviewPlacement::Bottom, percent: 60 };
assert_eq!(actual, expected);
}
}
|