feat: rewrite profiles list with fuzzy search and delete flow
This commit is contained in:
1 parent
86a18cc6e9
commit
cc24c0a1d8
2 files changed
+475
-76
No files matched your search
+312
-8
@@ -3,19 +3,33 @@ use super::{
|
|||||||
widgets::{self, profiles, WidgetKey},
|
widgets::{self, profiles, WidgetKey},
|
||||||
Tui,
|
Tui,
|
||||||
};
|
};
|
||||||
|
use crate::instance::{InstanceConfig, InstanceManager};
|
||||||
|
use crate::tui::error_buffer;
|
||||||
|
use crate::tui::progress;
|
||||||
|
use crate::tui::widgets::popups::confirm::{confirm_popup_area, ConfirmPopup};
|
||||||
|
use crate::tui::widgets::popups::confirm as confirm_popup;
|
||||||
|
use crate::tui::widgets::popups::error::{popup_area, ErrorPopup};
|
||||||
use color_eyre::eyre::Context;
|
use color_eyre::eyre::Context;
|
||||||
use crossterm::event::{self, Event};
|
use crossterm::event::{self, Event};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
crossterm::event::{KeyCode, KeyEvent, KeyEventKind},
|
crossterm::event::{KeyCode, KeyEvent, KeyEventKind},
|
||||||
layout::{Constraint, Direction, Layout},
|
layout::{Constraint, Direction, Layout},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
static PENDING_INSTANCES: Lazy<Arc<Mutex<Vec<InstanceConfig>>>> =
|
||||||
|
Lazy::new(|| Arc::new(Mutex::new(Vec::new())));
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
exit: bool,
|
exit: bool,
|
||||||
focused: FocusedArea,
|
focused: FocusedArea,
|
||||||
|
pre_overlay_focused: FocusedArea,
|
||||||
profiles_state: profiles::State,
|
profiles_state: profiles::State,
|
||||||
|
instance_manager: InstanceManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Copy, PartialEq)]
|
#[derive(Default, Debug, Clone, Copy, PartialEq)]
|
||||||
@@ -26,12 +40,47 @@ pub enum FocusedArea {
|
|||||||
Account,
|
Account,
|
||||||
Details,
|
Details,
|
||||||
Status,
|
Status,
|
||||||
|
StatusExpanded,
|
||||||
Popup,
|
Popup,
|
||||||
|
ErrorPopup,
|
||||||
|
ConfirmDelete,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for App {
|
||||||
|
fn default() -> Self {
|
||||||
|
let instances_dir = resolve_instances_dir();
|
||||||
|
|
||||||
|
match std::fs::create_dir_all(&instances_dir) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to create instances dir: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let manager = InstanceManager::new(instances_dir);
|
||||||
|
let instances = manager.load_all();
|
||||||
|
let profiles_state = profiles::State::with_instances(instances);
|
||||||
|
|
||||||
|
App {
|
||||||
|
exit: false,
|
||||||
|
focused: FocusedArea::default(),
|
||||||
|
pre_overlay_focused: FocusedArea::default(),
|
||||||
|
profiles_state,
|
||||||
|
instance_manager: manager,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn run(&mut self, terminal: &mut Tui) -> color_eyre::Result<()> {
|
pub async fn run(&mut self, terminal: &mut Tui) -> color_eyre::Result<()> {
|
||||||
while !self.exit {
|
while !self.exit {
|
||||||
|
if let Some(params) = new_instance::take_result() {
|
||||||
|
self.spawn_create(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.dismiss_expired_errors();
|
||||||
|
|
||||||
|
self.drain_pending_instances();
|
||||||
terminal.draw(|frame| self.render_frame(frame))?;
|
terminal.draw(|frame| self.render_frame(frame))?;
|
||||||
self.handle_events().wrap_err("handle events failed")?;
|
self.handle_events().wrap_err("handle events failed")?;
|
||||||
}
|
}
|
||||||
@@ -73,18 +122,97 @@ impl App {
|
|||||||
widgets::account::render(frame, bottom_chunks[0], self.focused);
|
widgets::account::render(frame, bottom_chunks[0], self.focused);
|
||||||
widgets::details::render(frame, bottom_chunks[1], self.focused);
|
widgets::details::render(frame, bottom_chunks[1], self.focused);
|
||||||
widgets::status::render(frame, bottom_chunks[2], self.focused);
|
widgets::status::render(frame, bottom_chunks[2], self.focused);
|
||||||
|
|
||||||
|
if self.focused == FocusedArea::StatusExpanded {
|
||||||
|
Self::render_log_overlay(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
let all_errors = error_buffer::peek_all_errors();
|
||||||
|
let mut next_y: u16 = 1;
|
||||||
|
for event in all_errors {
|
||||||
|
let elapsed_ms = event.pushed_at.elapsed().as_millis();
|
||||||
|
match popup_area(frame.area(), &event.message, next_y, elapsed_ms) {
|
||||||
|
Some(area) => {
|
||||||
|
next_y = next_y.saturating_add(area.height + 1);
|
||||||
|
frame.render_widget(ErrorPopup::new(event), area);
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.focused == FocusedArea::ConfirmDelete {
|
||||||
|
let name = confirm_popup::pending_delete_name();
|
||||||
|
if !name.is_empty() {
|
||||||
|
let area = confirm_popup_area(frame.area(), &name);
|
||||||
|
frame.render_widget(ConfirmPopup::new(&name), area);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_events(&mut self) -> color_eyre::Result<()> {
|
fn handle_events(&mut self) -> color_eyre::Result<()> {
|
||||||
match event::read()? {
|
match crossterm::event::poll(Duration::from_millis(16)) {
|
||||||
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => self
|
Ok(true) => {
|
||||||
.handle_key_event(key_event)
|
match event::read() {
|
||||||
.wrap_err_with(|| format!("handling key event failed:\n{key_event:#?}")),
|
Ok(Event::Key(key_event)) if key_event.kind == KeyEventKind::Press => self
|
||||||
_ => Ok(()),
|
.handle_key_event(key_event)
|
||||||
|
.wrap_err_with(|| format!("handling key event failed:\n{key_event:#?}")),
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Event read error: {}", e);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(false) => Ok(()),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Event poll error: {}", e);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> {
|
fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> {
|
||||||
|
if self.focused == FocusedArea::StatusExpanded {
|
||||||
|
match key_event.code {
|
||||||
|
KeyCode::Char('S') | KeyCode::Char('q') | KeyCode::Esc => {
|
||||||
|
self.focused = self.pre_overlay_focused;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.focused == FocusedArea::ConfirmDelete {
|
||||||
|
match key_event.code {
|
||||||
|
KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||||
|
let name = confirm_popup::pending_delete_name();
|
||||||
|
if !name.is_empty() {
|
||||||
|
match self.instance_manager.delete(&name) {
|
||||||
|
Ok(_) => {
|
||||||
|
self.profiles_state.remove_instance(&name);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to delete instance '{}': {}", name, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
confirm_popup::clear_pending();
|
||||||
|
self.focused = FocusedArea::Profiles;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||||
|
confirm_popup::clear_pending();
|
||||||
|
self.focused = FocusedArea::Profiles;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match self.focused {
|
match self.focused {
|
||||||
FocusedArea::Popup => {
|
FocusedArea::Popup => {
|
||||||
new_instance::handle_key(&key_event, &mut self.profiles_state);
|
new_instance::handle_key(&key_event, &mut self.profiles_state);
|
||||||
@@ -96,7 +224,17 @@ impl App {
|
|||||||
KeyCode::Char('C') => self.focused = FocusedArea::Content,
|
KeyCode::Char('C') => self.focused = FocusedArea::Content,
|
||||||
KeyCode::Char('A') => self.focused = FocusedArea::Account,
|
KeyCode::Char('A') => self.focused = FocusedArea::Account,
|
||||||
KeyCode::Char('D') => self.focused = FocusedArea::Details,
|
KeyCode::Char('D') => self.focused = FocusedArea::Details,
|
||||||
KeyCode::Char('S') => self.focused = FocusedArea::Status,
|
KeyCode::Char('S') => {
|
||||||
|
self.pre_overlay_focused = self.focused;
|
||||||
|
self.focused = FocusedArea::StatusExpanded;
|
||||||
|
}
|
||||||
|
KeyCode::Char('d') if self.focused == FocusedArea::Profiles => {
|
||||||
|
if let Some(instance) = self.profiles_state.selected_instance() {
|
||||||
|
let name = instance.name.clone();
|
||||||
|
confirm_popup::set_pending_delete(&name);
|
||||||
|
self.focused = FocusedArea::ConfirmDelete;
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,4 +252,170 @@ impl App {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_create(&self, params: new_instance::WizardParams) {
|
||||||
|
let instances_dir = self.instance_manager.instances_dir.clone();
|
||||||
|
let pending_instances = PENDING_INSTANCES.clone();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
progress::set_action(format!("Creating instance '{}'...", params.name));
|
||||||
|
progress::set_sub_action(format!("{} {}", params.game_version, params.loader));
|
||||||
|
|
||||||
|
let manager = InstanceManager::new(instances_dir);
|
||||||
|
match manager
|
||||||
|
.create(
|
||||||
|
¶ms.name,
|
||||||
|
¶ms.game_version,
|
||||||
|
params.loader,
|
||||||
|
params.loader_version.as_deref(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(config) => match pending_instances.lock() {
|
||||||
|
Ok(mut pending) => {
|
||||||
|
pending.push(config);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Pending instance queue lock poisoned: {}", e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_e) => {
|
||||||
|
progress::clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dismiss_expired_errors(&self) {
|
||||||
|
const DISMISS_MS: u128 = 5000;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match error_buffer::peek_error() {
|
||||||
|
Some(event) if event.pushed_at.elapsed().as_millis() >= DISMISS_MS => {
|
||||||
|
error_buffer::pop_error();
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drain_pending_instances(&mut self) {
|
||||||
|
match PENDING_INSTANCES.lock() {
|
||||||
|
Ok(mut pending) => {
|
||||||
|
for config in pending.drain(..) {
|
||||||
|
self.profiles_state.add_instance(config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Pending instance queue lock poisoned: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_log_overlay(frame: &mut Frame) {
|
||||||
|
use crate::tui::log_buffer;
|
||||||
|
use crate::tui::theme::THEME;
|
||||||
|
use ratatui::{
|
||||||
|
layout::Alignment,
|
||||||
|
style::{Color, Modifier, Style},
|
||||||
|
text::{Line, Span},
|
||||||
|
widgets::{Block, BorderType, Borders, Clear, List, ListItem},
|
||||||
|
};
|
||||||
|
|
||||||
|
let area = frame.area();
|
||||||
|
let overlay = ratatui::layout::Rect {
|
||||||
|
x: area.x + 1,
|
||||||
|
y: area.y + 1,
|
||||||
|
width: area.width.saturating_sub(2),
|
||||||
|
height: area.height.saturating_sub(2),
|
||||||
|
};
|
||||||
|
|
||||||
|
frame.render_widget(Clear, overlay);
|
||||||
|
|
||||||
|
let block = Block::default()
|
||||||
|
.title(Line::from(vec![Span::styled(
|
||||||
|
" Logs ",
|
||||||
|
Style::default()
|
||||||
|
.fg(THEME.colors.foreground)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
)]))
|
||||||
|
.title_bottom(
|
||||||
|
Line::from(vec![
|
||||||
|
Span::styled(
|
||||||
|
"[S]",
|
||||||
|
Style::default()
|
||||||
|
.fg(THEME.colors.border_focused)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
|
Span::styled(
|
||||||
|
"/",
|
||||||
|
Style::default().fg(THEME.colors.border_unfocused),
|
||||||
|
),
|
||||||
|
Span::styled(
|
||||||
|
"[Esc]",
|
||||||
|
Style::default()
|
||||||
|
.fg(THEME.colors.border_focused)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
|
Span::styled(
|
||||||
|
" close ",
|
||||||
|
Style::default().fg(THEME.colors.border_unfocused),
|
||||||
|
),
|
||||||
|
])
|
||||||
|
.alignment(Alignment::Right),
|
||||||
|
)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(Style::default().fg(THEME.colors.border_focused));
|
||||||
|
|
||||||
|
let inner = block.inner(overlay);
|
||||||
|
frame.render_widget(block, overlay);
|
||||||
|
|
||||||
|
let logs = log_buffer::get_logs();
|
||||||
|
let items: Vec<ListItem> = logs
|
||||||
|
.iter()
|
||||||
|
.map(|entry| {
|
||||||
|
let (level_str, level_color) = match entry.level {
|
||||||
|
tracing::Level::ERROR => ("ERROR", Color::Red),
|
||||||
|
tracing::Level::WARN => ("WARN ", Color::Yellow),
|
||||||
|
_ => ("INFO ", THEME.colors.border_unfocused),
|
||||||
|
};
|
||||||
|
ListItem::new(Line::from(vec![
|
||||||
|
Span::styled(
|
||||||
|
format!("{} ", level_str),
|
||||||
|
Style::default()
|
||||||
|
.fg(level_color)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
|
Span::styled(
|
||||||
|
entry.message.as_str(),
|
||||||
|
Style::default().fg(THEME.colors.foreground),
|
||||||
|
),
|
||||||
|
]))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let list = List::new(items);
|
||||||
|
frame.render_widget(list, inner);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_instances_dir() -> PathBuf {
|
||||||
|
let raw_dir = &crate::config::SETTINGS.paths.instances_dir;
|
||||||
|
|
||||||
|
if raw_dir == "~" {
|
||||||
|
return match dirs_next::home_dir() {
|
||||||
|
Some(home) => home,
|
||||||
|
None => PathBuf::from(raw_dir),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(stripped) = raw_dir.strip_prefix("~/") {
|
||||||
|
return match dirs_next::home_dir() {
|
||||||
|
Some(home) => home.join(stripped),
|
||||||
|
None => PathBuf::from(raw_dir),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
PathBuf::from(raw_dir)
|
||||||
}
|
}
|
||||||
+163
-68
@@ -1,75 +1,122 @@
|
|||||||
use crossterm::event::KeyCode;
|
use crossterm::event::KeyCode;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Constraint, Rect},
|
layout::{Constraint, Direction, Layout, Rect},
|
||||||
style::{Modifier, Style},
|
style::{Modifier, Style},
|
||||||
text::Text,
|
text::{Line, Span, Text},
|
||||||
widgets::{
|
widgets::{
|
||||||
Block, BorderType, Borders, Cell, Row, Scrollbar, ScrollbarOrientation, ScrollbarState,
|
Block, BorderType, Borders, List, ListItem, ListState, Paragraph, Scrollbar,
|
||||||
Table, TableState,
|
ScrollbarOrientation, ScrollbarState, Widget,
|
||||||
},
|
},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::instance::models::{InstanceConfig, InstanceState};
|
||||||
use crate::tui::{layout::FocusedArea, theme::THEME};
|
use crate::tui::{layout::FocusedArea, theme::THEME};
|
||||||
|
|
||||||
use super::{popups, styled_title, WidgetKey};
|
use super::{popups, styled_title, WidgetKey};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub profiles: Vec<Data>,
|
pub instances: Vec<InstanceConfig>,
|
||||||
pub table_state: TableState,
|
pub instance_states: Vec<InstanceState>,
|
||||||
|
pub list_state: ListState,
|
||||||
pub scrollbar_state: ScrollbarState,
|
pub scrollbar_state: ScrollbarState,
|
||||||
pub show_popup: bool,
|
pub show_popup: bool,
|
||||||
}
|
pub search_mode: bool,
|
||||||
|
pub search_query: String,
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct Data {
|
|
||||||
pub title: String,
|
|
||||||
pub id: String,
|
|
||||||
pub running: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
|
pub fn with_instances(instances: Vec<InstanceConfig>) -> Self {
|
||||||
|
let count = instances.len();
|
||||||
|
let mut s = State {
|
||||||
|
instances,
|
||||||
|
instance_states: vec![InstanceState::Ready; count],
|
||||||
|
list_state: ListState::default(),
|
||||||
|
scrollbar_state: ScrollbarState::default(),
|
||||||
|
show_popup: false,
|
||||||
|
search_mode: false,
|
||||||
|
search_query: String::new(),
|
||||||
|
};
|
||||||
|
if count > 0 {
|
||||||
|
s.list_state.select(Some(0));
|
||||||
|
}
|
||||||
|
s.update_scrollbar();
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn selected_instance(&self) -> Option<&InstanceConfig> {
|
||||||
|
let filtered = self.filtered_indices();
|
||||||
|
self.list_state
|
||||||
|
.selected()
|
||||||
|
.and_then(|i| filtered.get(i))
|
||||||
|
.and_then(|&idx| self.instances.get(idx))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn filtered_indices(&self) -> Vec<usize> {
|
||||||
|
if self.search_query.is_empty() {
|
||||||
|
(0..self.instances.len()).collect()
|
||||||
|
} else {
|
||||||
|
let q = self.search_query.to_lowercase();
|
||||||
|
self.instances
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, inst)| inst.name.to_lowercase().contains(&q))
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn next(&mut self) {
|
fn next(&mut self) {
|
||||||
let i = match self.table_state.selected() {
|
let count = self.filtered_indices().len();
|
||||||
|
if count == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let i = match self.list_state.selected() {
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
if i >= self.profiles.len().saturating_sub(1) {
|
if i >= count.saturating_sub(1) {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
i + 1
|
i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_none => 0,
|
None => 0,
|
||||||
};
|
};
|
||||||
self.table_state.select(Some(i));
|
self.list_state.select(Some(i));
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn previous(&mut self) {
|
fn previous(&mut self) {
|
||||||
let i = match self.table_state.selected() {
|
let count = self.filtered_indices().len();
|
||||||
|
if count == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let i = match self.list_state.selected() {
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
self.profiles.len().saturating_sub(1)
|
count.saturating_sub(1)
|
||||||
} else {
|
} else {
|
||||||
i - 1
|
i - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_none => 0,
|
None => 0,
|
||||||
};
|
};
|
||||||
self.table_state.select(Some(i));
|
self.list_state.select(Some(i));
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_scrollbar(&mut self) {
|
fn update_scrollbar(&mut self) {
|
||||||
let items = self.profiles.len().saturating_sub(1);
|
let filtered = self.filtered_indices();
|
||||||
let index = self.table_state.selected().unwrap_or(0);
|
let count = filtered.len();
|
||||||
|
let items = count.saturating_sub(1);
|
||||||
|
let index = self.list_state.selected().unwrap_or(0);
|
||||||
|
|
||||||
if self.profiles.is_empty() {
|
if count == 0 {
|
||||||
self.table_state.select(None);
|
self.list_state.select(None);
|
||||||
} else if self.table_state.selected().is_none() {
|
} else if self.list_state.selected().is_none() {
|
||||||
self.table_state.select(Some(0));
|
self.list_state.select(Some(0));
|
||||||
} else if index > items {
|
} else if index > items {
|
||||||
self.table_state.select(Some(items));
|
self.list_state.select(Some(items));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.scrollbar_state = ScrollbarState::new(items).position(index);
|
self.scrollbar_state = ScrollbarState::new(items).position(index);
|
||||||
@@ -78,18 +125,54 @@ impl State {
|
|||||||
pub fn wants_popup(&self) -> bool {
|
pub fn wants_popup(&self) -> bool {
|
||||||
self.show_popup
|
self.show_popup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove_instance(&mut self, name: &str) {
|
||||||
|
let before = self.instances.len();
|
||||||
|
self.instances.retain(|i| i.name != name);
|
||||||
|
let after = self.instances.len();
|
||||||
|
if after < before {
|
||||||
|
self.instance_states.truncate(after);
|
||||||
|
self.update_scrollbar();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_instance(&mut self, instance: InstanceConfig) {
|
||||||
|
self.instances.push(instance);
|
||||||
|
self.instance_states.push(InstanceState::Ready);
|
||||||
|
self.update_scrollbar();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WidgetKey for State {
|
impl WidgetKey for State {
|
||||||
fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) {
|
fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) {
|
||||||
match key_event.code {
|
match key_event.code {
|
||||||
|
KeyCode::Char('/') if !self.search_mode => {
|
||||||
|
self.search_mode = true;
|
||||||
|
self.list_state.select(Some(0));
|
||||||
|
self.update_scrollbar();
|
||||||
|
}
|
||||||
|
KeyCode::Esc if self.search_mode => {
|
||||||
|
self.search_mode = false;
|
||||||
|
self.search_query.clear();
|
||||||
|
self.list_state.select(Some(0));
|
||||||
|
self.update_scrollbar();
|
||||||
|
}
|
||||||
|
KeyCode::Backspace if self.search_mode => {
|
||||||
|
self.search_query.pop();
|
||||||
|
self.list_state.select(Some(0));
|
||||||
|
self.update_scrollbar();
|
||||||
|
}
|
||||||
|
KeyCode::Char(c) if self.search_mode && c != 'j' && c != 'k' => {
|
||||||
|
self.search_query.push(c);
|
||||||
|
self.list_state.select(Some(0));
|
||||||
|
self.update_scrollbar();
|
||||||
|
}
|
||||||
KeyCode::Char('a') => {
|
KeyCode::Char('a') => {
|
||||||
self.show_popup = true;
|
self.show_popup = true;
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
}
|
}
|
||||||
KeyCode::Char('d') => {
|
KeyCode::Char('d') => {
|
||||||
self.profiles.clear();
|
// Deletion handled by layout.rs which calls remove_instance
|
||||||
self.update_scrollbar();
|
|
||||||
}
|
}
|
||||||
KeyCode::Char('j') | KeyCode::Down => self.next(),
|
KeyCode::Char('j') | KeyCode::Down => self.next(),
|
||||||
KeyCode::Char('k') | KeyCode::Up => self.previous(),
|
KeyCode::Char('k') | KeyCode::Up => self.previous(),
|
||||||
@@ -120,65 +203,77 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
|||||||
height: area.height.saturating_sub(2),
|
height: area.height.saturating_sub(2),
|
||||||
};
|
};
|
||||||
|
|
||||||
let table_area = block.inner(area);
|
let inner_area = block.inner(area);
|
||||||
|
|
||||||
let rows = state.profiles.iter().enumerate().map(|(i, data)| {
|
let (list_area, search_area) = if state.search_mode {
|
||||||
let status = if data.running { "Running" } else { "Stopped" };
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([Constraint::Min(1), Constraint::Length(1)])
|
||||||
|
.split(inner_area);
|
||||||
|
(chunks[0], Some(chunks[1]))
|
||||||
|
} else {
|
||||||
|
(inner_area, None)
|
||||||
|
};
|
||||||
|
|
||||||
let background_color = if i % 2 == 0 {
|
let filtered = state.filtered_indices();
|
||||||
THEME.colors.row_background
|
let items: Vec<ListItem> = filtered
|
||||||
} else {
|
.iter()
|
||||||
THEME.colors.row_alternate_bg
|
.map(|&idx| {
|
||||||
};
|
let instance = &state.instances[idx];
|
||||||
|
|
||||||
Row::new(vec![
|
let name_line = Line::from(vec![
|
||||||
Cell::from(Text::from(format!("\n{}\n", data.title))),
|
Span::raw(" "),
|
||||||
Cell::from(Text::from(format!("\n{}\n", data.id))),
|
Span::styled(
|
||||||
Cell::from(Text::from(format!("\n{}\n", status))),
|
instance.name.as_str(),
|
||||||
])
|
Style::default()
|
||||||
.height(4)
|
.fg(THEME.colors.border_focused)
|
||||||
.style(Style::default().bg(background_color))
|
.add_modifier(Modifier::BOLD),
|
||||||
});
|
),
|
||||||
|
]);
|
||||||
|
let info_line = Line::from(vec![
|
||||||
|
Span::raw(" "),
|
||||||
|
Span::styled(
|
||||||
|
format!("{} \u{00b7} {}", instance.game_version, instance.loader),
|
||||||
|
Style::default().fg(THEME.colors.border_unfocused),
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
let spacer = Line::from("");
|
||||||
|
|
||||||
let widths = [
|
ListItem::new(Text::from(vec![name_line, info_line, spacer]))
|
||||||
Constraint::Percentage(40),
|
})
|
||||||
Constraint::Percentage(30),
|
.collect();
|
||||||
Constraint::Percentage(30),
|
|
||||||
];
|
|
||||||
|
|
||||||
let table = Table::new(rows, widths)
|
let list = List::new(items)
|
||||||
.row_highlight_style(
|
.highlight_style(Style::default().bg(THEME.colors.row_alternate_bg))
|
||||||
Style::default()
|
.highlight_symbol("");
|
||||||
.add_modifier(Modifier::REVERSED)
|
|
||||||
.fg(THEME.colors.row_highlight),
|
|
||||||
)
|
|
||||||
.highlight_spacing(ratatui::widgets::HighlightSpacing::Always);
|
|
||||||
|
|
||||||
frame.render_stateful_widget(table, table_area, &mut state.table_state);
|
frame.render_stateful_widget(list, list_area, &mut state.list_state);
|
||||||
|
|
||||||
|
if let Some(sa) = search_area {
|
||||||
|
let search_text = format!("/ {}\u{2588}", state.search_query);
|
||||||
|
let paragraph =
|
||||||
|
Paragraph::new(search_text).style(Style::default().fg(THEME.colors.border_focused));
|
||||||
|
paragraph.render(sa, frame.buffer_mut());
|
||||||
|
}
|
||||||
|
|
||||||
frame.render_stateful_widget(
|
frame.render_stateful_widget(
|
||||||
Scrollbar::default()
|
Scrollbar::default()
|
||||||
.orientation(ScrollbarOrientation::VerticalRight)
|
.orientation(ScrollbarOrientation::VerticalRight)
|
||||||
.begin_symbol(Some("▲"))
|
.begin_symbol(Some("\u{25b2}"))
|
||||||
.style(
|
.style(
|
||||||
Style::default()
|
Style::default()
|
||||||
.fg(THEME.colors.border_focused)
|
.fg(THEME.colors.border_focused)
|
||||||
.add_modifier(Modifier::BOLD),
|
.add_modifier(Modifier::BOLD),
|
||||||
)
|
)
|
||||||
.thumb_symbol("┃")
|
.thumb_symbol("\u{2503}")
|
||||||
.track_symbol(Some(""))
|
.track_symbol(Some(""))
|
||||||
.end_symbol(Some("▼")),
|
.end_symbol(Some("\u{25bc}")),
|
||||||
scrollbar_area,
|
scrollbar_area,
|
||||||
&mut state.scrollbar_state,
|
&mut state.scrollbar_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
if state.show_popup {
|
if state.show_popup {
|
||||||
let popup_area = Rect {
|
let popup_area = popups::new_instance::popup_rect(frame.area());
|
||||||
x: frame.area().width / 4,
|
|
||||||
y: frame.area().height / 3,
|
|
||||||
width: frame.area().width / 2,
|
|
||||||
height: frame.area().height / 3,
|
|
||||||
};
|
|
||||||
popups::new_instance::render(frame, popup_area, focused);
|
popups::new_instance::render(frame, popup_area, focused);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user