use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
layout::{Margin, Rect},
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, List, ListItem, ListState, Scrollbar, ScrollbarOrientation, ScrollbarState},
Frame,
};
use super::Component;
use crate::action::Action;
use crate::types::KeyMetadata;
use crate::ui::theme::{self, AnimationState};
pub struct KeyBrowserProps<'a> {
pub keys: &'a [Option<KeyMetadata>],
pub total_count: Option<u64>,
pub is_loading: bool,
pub active_search_query: Option<&'a str>,
pub is_active: bool,
pub backend_type: Option<crate::types::BackendType>,
pub is_searching: bool,
pub search_results_local: &'a [usize],
pub search_results_server: &'a [KeyMetadata],
pub is_server_searching: bool,
pub search_selection_index: Option<usize>,
pub animation: &'a AnimationState,
}
pub struct KeyBrowser {
pub state: ListState,
pub scrollbar_state: ScrollbarState,
pub viewport_height: usize,
scroll_top: usize,
}
impl KeyBrowser {
const EDGE_SCROLL_GUARD: usize = 3;
pub fn new() -> Self {
Self {
state: ListState::default(),
scrollbar_state: ScrollbarState::default(),
viewport_height: 20,
scroll_top: 0,
}
}
pub fn select(&mut self, index: Option<usize>) {
self.state.select(index);
if index.is_none() {
self.scroll_top = 0;
}
}
pub fn selected_index(&self) -> Option<usize> {
self.state.selected()
}
pub fn viewport_height(&self) -> usize {
self.viewport_height
}
pub fn view_bounds(&self, total_count: usize) -> Option<(usize, usize)> {
if total_count == 0 {
return None;
}
let visible_len = self.viewport_height.min(total_count.max(1));
if visible_len == 0 {
return None;
}
let max_start = total_count.saturating_sub(visible_len);
let start_index = self.scroll_top.min(max_start);
Some((start_index, visible_len))
}
}
impl Default for KeyBrowser {
fn default() -> Self {
Self::new()
}
}
impl Component for KeyBrowser {
type Props<'a> = KeyBrowserProps<'a>;
type Msg = Action;
fn render(&mut self, f: &mut Frame, area: Rect, props: Self::Props<'_>) {
let is_active = props.is_active;
self.viewport_height = area.height.saturating_sub(2) as usize;
let content_width = area.width.saturating_sub(4) as usize;
let has_search = props
.active_search_query
.map(|q| !q.is_empty())
.unwrap_or(false);
let (key_items, display_count, selected_display_idx) = if has_search {
self.build_search_results_list(&props, content_width)
} else {
self.build_normal_list(&props, content_width, is_active)
};
let title = self.build_title(&props, has_search, display_count);
let block = Self::themed_block(&title, is_active, props.animation, props.is_loading);
let keys_list = List::new(key_items)
.block(block)
.highlight_style(theme::list_selected().add_modifier(Modifier::BOLD))
.highlight_symbol("▸ ");
let mut view_state = ListState::default();
view_state.select(selected_display_idx);
f.render_stateful_widget(keys_list, area, &mut view_state);
if display_count > 0 {
let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("▲"))
.end_symbol(Some("▼"))
.track_symbol(Some("│"))
.thumb_symbol("█")
.style(Style::default().fg(if is_active {
theme::NEON_CYAN()
} else {
Color::Rgb(60, 70, 90)
}));
let scrollbar_area = area.inner(Margin {
vertical: 1,
horizontal: 0,
});
self.scrollbar_state = self
.scrollbar_state
.content_length(display_count)
.position(selected_display_idx.unwrap_or(0));
f.render_stateful_widget(scrollbar, scrollbar_area, &mut self.scrollbar_state);
}
}
fn handle_input(&mut self, key: KeyEvent, props: Self::Props<'_>) -> Option<Self::Msg> {
let total_count = props
.total_count
.map(|t| t as usize)
.unwrap_or_else(|| props.keys.len());
match key.code {
KeyCode::Down | KeyCode::Char('j') => {
if total_count == 0 {
return None;
}
let i = match self.state.selected() {
Some(i) => {
if i >= total_count - 1 {
0
} else {
i + 1
}
}
None => 0,
};
self.state.select(Some(i));
if self.needs_loading_around(i, props.keys, total_count) {
return Some(Action::LoadMoreKeys(i));
}
Some(Action::SelectKey(i))
}
KeyCode::Up | KeyCode::Char('k') => {
if total_count == 0 {
return None;
}
let i = match self.state.selected() {
Some(i) => {
if i == 0 {
total_count - 1
} else {
i - 1
}
}
None => 0,
};
self.state.select(Some(i));
if self.needs_loading_around(i, props.keys, total_count) {
return Some(Action::LoadMoreKeys(i));
}
Some(Action::SelectKey(i))
}
_ => None,
}
}
}
impl KeyBrowser {
fn build_search_results_list(
&self,
props: &KeyBrowserProps<'_>,
content_width: usize,
) -> (Vec<ListItem<'static>>, usize, Option<usize>) {
let mut items: Vec<ListItem<'static>> = Vec::new();
for &key_idx in props.search_results_local.iter() {
if let Some(Some(key)) = props.keys.get(key_idx) {
items.push(Self::build_key_item(key, content_width, props.backend_type));
}
}
let local_key_names: std::collections::HashSet<&str> = props
.search_results_local
.iter()
.filter_map(|&idx| {
props
.keys
.get(idx)
.and_then(|k| k.as_ref())
.map(|k| k.name.as_str())
})
.collect();
for server_key in props.search_results_server.iter() {
if !local_key_names.contains(server_key.name.as_str()) {
items.push(Self::build_key_item_with_indicator(
server_key,
content_width,
props.backend_type,
"◦", ));
}
}
if items.is_empty() {
if props.is_server_searching {
let spinner = theme::spinner(props.animation);
items.push(ListItem::new(Span::styled(
format!("{} Searching...", spinner),
Style::default()
.fg(theme::NEON_AMBER())
.add_modifier(Modifier::ITALIC),
)));
} else if props
.active_search_query
.map(|q| !q.is_empty())
.unwrap_or(false)
{
items.push(ListItem::new(Span::styled(
" No matches found",
Style::default()
.fg(theme::TEXT_DIM())
.add_modifier(Modifier::ITALIC),
)));
}
}
let items_len = items.len();
let result_count = props.search_results_local.len();
(
items,
result_count.max(items_len),
props.search_selection_index,
)
}
fn build_normal_list(
&mut self,
props: &KeyBrowserProps<'_>,
content_width: usize,
is_active: bool,
) -> (Vec<ListItem<'static>>, usize, Option<usize>) {
let total_count = props
.total_count
.map(|t| t as usize)
.unwrap_or_else(|| props.keys.len());
let selected_abs = self
.state
.selected()
.unwrap_or(0)
.min(total_count.saturating_sub(1));
let (start_index, visible_len) = if is_active {
self.compute_view_window(selected_abs, total_count)
} else {
let visible_len = self.viewport_height.min(total_count.max(1));
let max_start = total_count.saturating_sub(visible_len);
let start = self.scroll_top.min(max_start);
(start, visible_len)
};
let mut key_items: Vec<ListItem<'static>> = Vec::with_capacity(visible_len);
for offset in 0..visible_len {
let abs = start_index + offset;
if let Some(Some(k)) = props.keys.get(abs) {
key_items.push(Self::build_key_item(k, content_width, props.backend_type));
} else {
let spinner = theme::spinner_dots(props.animation);
key_items.push(ListItem::new(Span::styled(
format!(" {} loading...", spinner),
Style::default().fg(theme::TEXT_DIM()),
)));
}
}
let rel_selection = if total_count > 0 {
Some(selected_abs.saturating_sub(start_index))
} else {
None
};
(key_items, total_count, rel_selection)
}
fn build_title(
&self,
props: &KeyBrowserProps<'_>,
has_search: bool,
display_count: usize,
) -> String {
if props.is_searching {
if let Some(query) = props.active_search_query {
if query.is_empty() {
"Keys │ ▍".to_string()
} else {
let server_indicator = if props.is_server_searching {
format!(" {}", theme::spinner(props.animation))
} else {
String::new()
};
format!(
"Keys │ {}▍{} ({} found)",
query, server_indicator, display_count
)
}
} else {
"Keys │ ▍".to_string()
}
} else if has_search {
if let Some(query) = props.active_search_query {
format!("Keys │ /{} ({} results)", query, display_count)
} else {
"Keys".to_string()
}
} else {
let total_count = props
.total_count
.map(|t| t as usize)
.unwrap_or(props.keys.len());
let position = self
.state
.selected()
.map(|i| format!(" [{}/{}]", i.saturating_add(1), total_count))
.unwrap_or_else(|| " [—]".to_string());
let loading_indicator = if props.is_loading {
format!(" {}", theme::spinner(props.animation))
} else {
String::new()
};
format!("Keys{}{}", position, loading_indicator)
}
}
fn compute_view_window(&mut self, selected_abs: usize, total_count: usize) -> (usize, usize) {
let visible_len = self.viewport_height.min(total_count.max(1));
if visible_len == 0 {
self.scroll_top = 0;
return (0, 0);
}
let max_start = total_count.saturating_sub(visible_len);
self.scroll_top = self.scroll_top.min(max_start);
if selected_abs < self.scroll_top {
self.scroll_top = selected_abs;
} else {
let last_visible = self
.scroll_top
.saturating_add(visible_len.saturating_sub(1));
if selected_abs > last_visible {
self.scroll_top = selected_abs.saturating_sub(visible_len.saturating_sub(1));
}
}
let guard = Self::edge_guard_for(visible_len);
if guard > 0 {
let top_threshold = self.scroll_top.saturating_add(guard);
let bottom_anchor = visible_len.saturating_sub(guard + 1);
let bottom_threshold = self.scroll_top.saturating_add(bottom_anchor);
if selected_abs < top_threshold {
let new_top = selected_abs.saturating_sub(guard);
self.scroll_top = new_top.min(max_start);
} else if selected_abs > bottom_threshold {
let new_top = selected_abs.saturating_sub(bottom_anchor);
self.scroll_top = new_top.min(max_start);
}
}
(self.scroll_top, visible_len)
}
fn edge_guard_for(visible_len: usize) -> usize {
if visible_len <= 1 {
return 0;
}
let max_guard = (visible_len - 1) / 2;
Self::EDGE_SCROLL_GUARD.min(max_guard)
}
fn needs_loading_around(
&self,
index: usize,
keys: &[Option<KeyMetadata>],
total: usize,
) -> bool {
let n = self.viewport_height;
if total == 0 {
return false;
}
let start = index.saturating_sub(n);
let end = (index + n).min(total);
for i in start..end {
if keys.get(i).and_then(|k| k.as_ref()).is_none() {
return true;
}
}
if index < n {
let wrap_start = total.saturating_sub(n);
for i in wrap_start..total {
if keys.get(i).and_then(|k| k.as_ref()).is_none() {
return true;
}
}
}
if index + n >= total {
for i in 0..n.min(total) {
if keys.get(i).and_then(|k| k.as_ref()).is_none() {
return true;
}
}
}
false
}
fn build_key_item(
key: &KeyMetadata,
content_width: usize,
backend_type: Option<crate::types::BackendType>,
) -> ListItem<'static> {
let show_type = !matches!(
backend_type,
Some(crate::types::BackendType::Memcached | crate::types::BackendType::Etcd)
);
if show_type {
let type_label = key.value_type.to_string();
let type_width = type_label.chars().count();
let min_gap = if content_width > type_width { 1 } else { 0 };
let available_for_name = content_width.saturating_sub(type_width + min_gap);
let name_display = Self::truncate_to_fit(&key.name, available_for_name);
let name_width = name_display.chars().count();
let spacer_width = content_width.saturating_sub(name_width + type_width);
let spacer = if spacer_width > 0 {
" ".repeat(spacer_width)
} else {
String::new()
};
let type_color = match key.value_type {
crate::types::ValueType::String => theme::NEON_CYAN(),
crate::types::ValueType::Hash => theme::NEON_PURPLE(),
crate::types::ValueType::List => theme::NEON_GREEN(),
crate::types::ValueType::Set => theme::NEON_AMBER(),
crate::types::ValueType::SortedSet => theme::NEON_PINK(),
crate::types::ValueType::Binary => theme::TEXT_SECONDARY(),
crate::types::ValueType::Json => theme::ELECTRIC_BLUE(),
crate::types::ValueType::Integer => theme::NEON_GREEN(),
crate::types::ValueType::Float => theme::NEON_AMBER(),
crate::types::ValueType::Unknown => theme::TEXT_DIM(),
};
let line = Line::from(vec![
Span::styled(name_display, Style::default().fg(theme::TEXT_PRIMARY())),
Span::raw(spacer),
Span::styled(
type_label,
Style::default().fg(type_color).add_modifier(Modifier::DIM),
),
]);
ListItem::new(line)
} else {
let name_display = Self::truncate_to_fit(&key.name, content_width);
let line = Line::from(vec![Span::styled(
name_display,
Style::default().fg(theme::TEXT_PRIMARY()),
)]);
ListItem::new(line)
}
}
fn build_key_item_with_indicator(
key: &KeyMetadata,
content_width: usize,
backend_type: Option<crate::types::BackendType>,
indicator: &str,
) -> ListItem<'static> {
let show_type = !matches!(
backend_type,
Some(crate::types::BackendType::Memcached | crate::types::BackendType::Etcd)
);
let indicator_width = indicator.chars().count() + 1;
if show_type {
let type_label = key.value_type.to_string();
let type_width = type_label.chars().count();
let min_gap = if content_width > type_width + indicator_width {
1
} else {
0
};
let available_for_name =
content_width.saturating_sub(type_width + min_gap + indicator_width);
let name_display = Self::truncate_to_fit(&key.name, available_for_name);
let name_width = name_display.chars().count();
let spacer_width =
content_width.saturating_sub(name_width + type_width + indicator_width);
let spacer = if spacer_width > 0 {
" ".repeat(spacer_width)
} else {
String::new()
};
let line = Line::from(vec![
Span::styled(
format!("{} ", indicator),
Style::default().fg(theme::NEON_AMBER()),
),
Span::styled(name_display, Style::default().fg(theme::TEXT_PRIMARY())),
Span::raw(spacer),
Span::styled(type_label, Style::default().fg(theme::TEXT_DIM())),
]);
ListItem::new(line)
} else {
let available_for_name = content_width.saturating_sub(indicator_width);
let name_display = Self::truncate_to_fit(&key.name, available_for_name);
let line = Line::from(vec![
Span::styled(
format!("{} ", indicator),
Style::default().fg(theme::NEON_AMBER()),
),
Span::styled(name_display, Style::default().fg(theme::TEXT_PRIMARY())),
]);
ListItem::new(line)
}
}
fn truncate_to_fit(text: &str, max_chars: usize) -> String {
if max_chars == 0 {
return String::new();
}
let char_count = text.chars().count();
if char_count <= max_chars {
return text.to_string();
}
if max_chars <= 3 {
return text.chars().take(max_chars).collect();
}
let mut truncated: String = text.chars().take(max_chars - 3).collect();
truncated.push_str("...");
truncated
}
fn themed_block(
title: &str,
is_active: bool,
_animation: &AnimationState,
_is_loading: bool,
) -> Block<'static> {
theme::panel_block(title, is_active)
}
}