use ratatui_action::id::ActionId;
use ratatui_action::input::{ActionChoice, ActionInput};
use ratatui_action::spec::{ActionSpec, Availability};
use crate::event::PaletteMode;
use crate::shortcut::ShortcutLabels;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PaletteRow {
action_id: ActionId,
title: String,
subtitle: Option<String>,
category: Option<String>,
shortcut: Option<String>,
availability: Availability,
}
impl PaletteRow {
pub(crate) fn from_action_with_shortcuts(
action: &ActionSpec,
shortcuts: &ShortcutLabels,
) -> Self {
Self {
action_id: action.id().clone(),
title: action.title().to_string(),
subtitle: action.description().map(str::to_string),
category: action.category().map(str::to_string),
shortcut: shortcuts.get(action.id()).map(str::to_string),
availability: action.availability().clone(),
}
}
pub(crate) fn from_choice(
action_id: &ActionId,
input: &ActionInput,
choice: &ActionChoice,
) -> Self {
Self {
action_id: action_id.clone(),
title: choice.label().to_string(),
subtitle: choice.description().map(str::to_string),
category: Some(input.label().to_string()),
shortcut: None,
availability: Availability::Enabled,
}
}
pub(crate) fn from_bool(action_id: &ActionId, input: &ActionInput, value: bool) -> Self {
Self {
action_id: action_id.clone(),
title: if value { "Yes" } else { "No" }.into(),
subtitle: Some(value.to_string()),
category: Some(input.label().to_string()),
shortcut: None,
availability: Availability::Enabled,
}
}
pub fn action_id(&self) -> &ActionId {
&self.action_id
}
pub fn title(&self) -> &str {
&self.title
}
pub fn subtitle(&self) -> Option<&str> {
self.subtitle.as_deref()
}
pub fn category(&self) -> Option<&str> {
self.category.as_deref()
}
pub fn shortcut(&self) -> Option<&str> {
self.shortcut.as_deref()
}
pub fn availability(&self) -> &Availability {
&self.availability
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PaletteView {
pub(crate) prompt: String,
pub(crate) query: String,
pub(crate) rows: Vec<PaletteRow>,
pub(crate) selected: Option<usize>,
pub(crate) mode: PaletteMode,
}
impl PaletteView {
pub fn prompt(&self) -> &str {
&self.prompt
}
pub fn query(&self) -> &str {
&self.query
}
pub fn rows(&self) -> &[PaletteRow] {
&self.rows
}
pub fn selected(&self) -> Option<usize> {
self.selected
}
pub fn mode(&self) -> &PaletteMode {
&self.mode
}
}