From ecc2b2bb23594bd6be15934ec4665982e02d8c6d Mon Sep 17 00:00:00 2001 From: objz Date: Sat, 24 Jan 2026 18:35:42 +0100 Subject: [PATCH] feat(tui): rename tabs, add profile rename, fix editor spawning --- src/tui/layout.rs | 95 ++++++++++++++++++++++++++---- src/tui/widgets/account.rs | 2 +- src/tui/widgets/details.rs | 116 +++++++++++++++++++++++++++++++------ src/tui/widgets/status.rs | 4 +- 4 files changed, 186 insertions(+), 31 deletions(-) diff --git a/src/tui/layout.rs b/src/tui/layout.rs index c510f6e..e4bbcdf 100644 --- a/src/tui/layout.rs +++ b/src/tui/layout.rs @@ -47,6 +47,7 @@ pub struct App { throbber_state: throbber_widgets_tui::ThrobberState, throbber_tick: u8, error_effects: HashMap, + pending_editor: Option, } enum ErrorEffectState { @@ -61,9 +62,9 @@ pub enum FocusedArea { Profiles, Content, Account, - Details, - Status, - StatusExpanded, + Settings, + Overview, + OverviewExpanded, Popup, ErrorPopup, ConfirmDelete, @@ -118,6 +119,7 @@ impl App { throbber_state: throbber_widgets_tui::ThrobberState::default(), throbber_tick: 0, error_effects: HashMap::new(), + pending_editor: None, } } } @@ -151,6 +153,10 @@ impl App { terminal.draw(|frame| self.render_frame(frame))?; self.handle_events().wrap_err("handle events failed")?; + + if let Some(path) = self.pending_editor.take() { + Self::run_editor(terminal, &path); + } } Ok(()) } @@ -207,10 +213,16 @@ impl App { .split(main_chunks[2]); widgets::account::render(frame, bottom_chunks[0], self.focused, &mut self.account_state); - widgets::details::render(frame, bottom_chunks[1], self.focused); + widgets::details::render( + frame, + bottom_chunks[1], + self.focused, + self.profiles_state.selected_instance(), + &self.instance_manager.instances_dir, + ); widgets::status::render(frame, bottom_chunks[2], self.focused, &mut self.throbber_state); - if self.focused == FocusedArea::StatusExpanded { + if self.focused == FocusedArea::OverviewExpanded { self.render_log_overlay(frame); } @@ -266,7 +278,7 @@ impl App { } fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> { - if self.focused == FocusedArea::StatusExpanded { + if self.focused == FocusedArea::OverviewExpanded { if self.log_overlay_search.active { match key_event.code { KeyCode::Esc => { @@ -283,7 +295,7 @@ impl App { return Ok(()); } match key_event.code { - KeyCode::Char('S') | KeyCode::Esc => { + KeyCode::Char('O') | KeyCode::Esc => { self.focused = self.pre_overlay_focused; self.log_overlay_search.deactivate(); return Ok(()); @@ -387,6 +399,21 @@ impl App { } } + if self.focused == FocusedArea::Settings { + match widgets::details::handle_key( + &key_event, + self.profiles_state.selected_instance(), + &self.instance_manager.instances_dir, + ) { + widgets::details::SettingsAction::EditInstance(path) + | widgets::details::SettingsAction::EditGlobal(path) => { + self.pending_editor = Some(path); + return Ok(()); + } + widgets::details::SettingsAction::None => {} + } + } + match self.focused { FocusedArea::Popup => { new_instance::handle_key(&key_event, &mut self.profiles_state); @@ -438,10 +465,10 @@ impl App { KeyCode::Char('P') => self.focused = FocusedArea::Profiles, KeyCode::Char('C') => self.focused = FocusedArea::Content, KeyCode::Char('A') => self.focused = FocusedArea::Account, - KeyCode::Char('D') => self.focused = FocusedArea::Details, - KeyCode::Char('S') => { + KeyCode::Char('S') => self.focused = FocusedArea::Settings, + KeyCode::Char('O') => { self.pre_overlay_focused = self.focused; - self.focused = FocusedArea::StatusExpanded; + self.focused = FocusedArea::OverviewExpanded; } KeyCode::Tab | KeyCode::Char('l') | KeyCode::Right if self.focused == FocusedArea::Content => @@ -572,6 +599,52 @@ impl App { }); } + fn run_editor(terminal: &mut ratatui::DefaultTerminal, path: &std::path::Path) { + use ratatui::crossterm::{ + terminal::{disable_raw_mode, enable_raw_mode, LeaveAlternateScreen, EnterAlternateScreen}, + ExecutableCommand, + }; + use std::io::stdout; + + let editor = std::env::var("EDITOR") + .or_else(|_| std::env::var("VISUAL")) + .unwrap_or_else(|_| "vi".to_string()); + + let is_tui_editor = matches!( + editor.rsplit('/').next().unwrap_or(&editor), + "vi" | "vim" | "nvim" | "neovim" | "nano" | "micro" | "helix" | "hx" | "emacs" | "ne" | "joe" | "mcedit" + ); + + if is_tui_editor { + let _ = stdout().execute(LeaveAlternateScreen); + let _ = disable_raw_mode(); + + let result = std::process::Command::new(&editor) + .arg(path) + .stdin(std::process::Stdio::inherit()) + .stdout(std::process::Stdio::inherit()) + .stderr(std::process::Stdio::inherit()) + .status(); + + let _ = stdout().execute(EnterAlternateScreen); + let _ = enable_raw_mode(); + let _ = terminal.clear(); + + if let Err(e) = result { + tracing::error!("Failed to open editor: {}", e); + } + } else { + if let Err(e) = std::process::Command::new(&editor) + .arg(path) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + { + tracing::error!("Failed to open editor: {}", e); + } + } + } + fn spawn_launch(&self, instance: crate::instance::InstanceConfig) { use crate::instance::launch; use crate::running; @@ -678,7 +751,7 @@ impl App { .add_modifier(Modifier::BOLD), )) .title_bottom( - crate::tui::widgets::popups::keybind_line(&[("S", " close"), ("/", " search")]) + crate::tui::widgets::popups::keybind_line(&[("O", " close"), ("/", " search")]) .alignment(Alignment::Right), ) .border_type(BorderType::Rounded) diff --git a/src/tui/widgets/account.rs b/src/tui/widgets/account.rs index b0e9266..1cea9a4 100644 --- a/src/tui/widgets/account.rs +++ b/src/tui/widgets/account.rs @@ -220,7 +220,7 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut A }; let mut block = Block::default() - .title(styled_title("Account", true)) + .title(styled_title("Accounts", true)) .borders(Borders::ALL) .border_type(BorderType::Rounded) .border_style(Style::default().fg(color)); diff --git a/src/tui/widgets/details.rs b/src/tui/widgets/details.rs index 7a8b4de..bbfe859 100644 --- a/src/tui/widgets/details.rs +++ b/src/tui/widgets/details.rs @@ -1,41 +1,123 @@ use ratatui::{ layout::Rect, - style::Style, - widgets::{Block, BorderType, Borders}, + style::{Modifier, Style}, + text::{Line, Span}, + widgets::{Block, BorderType, Borders, Paragraph}, Frame, }; +use crate::instance::models::InstanceConfig; use crate::tui::layout::FocusedArea; use crate::tui::theme::THEME; use super::styled_title; -pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea) { - let color = if focused == FocusedArea::Details { +pub fn render( + frame: &mut Frame, + area: Rect, + focused: FocusedArea, + instance: Option<&InstanceConfig>, + _instances_dir: &std::path::Path, +) { + let color = if focused == FocusedArea::Settings { THEME.colors.border_focused } else { THEME.colors.border_unfocused }; let mut block = Block::default() - .title(styled_title("Details", true)) + .title(styled_title("Settings", true)) .borders(Borders::ALL) .border_type(BorderType::Rounded) .border_style(Style::default().fg(color)); - let lines = super::popups::keybind_lines_wrapped( - &[ - ("P", " profiles"), - ("C", " content"), - ("A", " account"), - ("S", " logs"), - ("q", " quit"), - ], - area.width.saturating_sub(2), - ); - for line in lines { - block = block.title_bottom(line); + if focused == FocusedArea::Settings { + let lines = super::popups::keybind_lines_wrapped( + &[("e", " edit instance"), ("g", " edit global")], + area.width.saturating_sub(2), + ); + for line in lines { + block = block.title_bottom(line); + } } + let inner = block.inner(area); frame.render_widget(block, area); + + let Some(inst) = instance else { + frame.render_widget( + Paragraph::new("No instance selected.") + .style(Style::default().fg(THEME.colors.text_idle)), + inner, + ); + return; + }; + + let label_style = Style::default().fg(THEME.colors.text_idle); + let value_style = Style::default() + .fg(THEME.colors.foreground) + .add_modifier(Modifier::BOLD); + let dim_style = Style::default().fg(THEME.colors.border_unfocused); + + let memory_min = inst.memory_min.as_deref().unwrap_or("512M"); + let memory_max = inst.memory_max.as_deref().unwrap_or("2G"); + let java_path = inst.java_path.as_deref().unwrap_or("system"); + let jvm_args = if inst.jvm_args.is_empty() { + "none".to_string() + } else { + inst.jvm_args.join(" ") + }; + let resolution = inst + .resolution + .map(|(w, h)| format!("{}x{}", w, h)) + .unwrap_or_else(|| "default".to_string()); + + let lines = vec![ + Line::from(vec![ + Span::styled("Memory ", label_style), + Span::styled(format!("{memory_min} - {memory_max}"), value_style), + ]), + Line::from(vec![ + Span::styled("Java ", label_style), + Span::styled(java_path, value_style), + ]), + Line::from(vec![ + Span::styled("JVM ", label_style), + Span::styled(jvm_args, dim_style), + ]), + Line::from(vec![ + Span::styled("Display ", label_style), + Span::styled(resolution, value_style), + ]), + ]; + + frame.render_widget(Paragraph::new(lines), inner); +} + +pub enum SettingsAction { + None, + EditInstance(std::path::PathBuf), + EditGlobal(std::path::PathBuf), +} + +pub fn handle_key( + key_event: &crossterm::event::KeyEvent, + instance: Option<&InstanceConfig>, + instances_dir: &std::path::Path, +) -> SettingsAction { + match key_event.code { + crossterm::event::KeyCode::Char('e') => { + if let Some(inst) = instance { + let path = instances_dir.join(&inst.name).join("instance.json"); + SettingsAction::EditInstance(path) + } else { + SettingsAction::None + } + } + crossterm::event::KeyCode::Char('g') => { + let path = crate::config::get_config_path().join("config.toml"); + SettingsAction::EditGlobal(path) + } + _ => SettingsAction::None, + } } diff --git a/src/tui/widgets/status.rs b/src/tui/widgets/status.rs index c31fa2a..9b5a958 100644 --- a/src/tui/widgets/status.rs +++ b/src/tui/widgets/status.rs @@ -19,14 +19,14 @@ pub fn render( focused: FocusedArea, throbber_state: &mut ThrobberState, ) { - let border_color = if focused == FocusedArea::Status { + let border_color = if focused == FocusedArea::Overview { THEME.colors.border_focused } else { THEME.colors.border_unfocused }; let block = Block::default() - .title(styled_title("Status", true)) + .title(styled_title("Overview", true)) .borders(Borders::ALL) .border_type(BorderType::Rounded) .border_style(Style::default().fg(border_color));