use super::widgets::{centered_rect, tilde_path};
use crate::{logging, manager::GoManager, theme::Theme};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use ratatui::{
Frame, Terminal,
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Margin, Rect},
style::Style,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
};
use std::io;
pub(crate) fn run_setup_guide_if_needed<B: Backend>(
terminal: &mut Terminal<B>,
shim_path: &str,
shim_in_path: bool,
theme: &Theme,
manager: &GoManager,
) -> io::Result<bool> {
if shim_in_path {
logging::debug("setup: skipped reason=shim_in_path");
return Ok(true);
}
logging::info(&format!(
"setup: shown reason=shim_not_in_path shim_dir=\"{shim_path}\""
));
let mut local_notice: Option<Vec<Line<'static>>> = None;
loop {
terminal
.draw(|f| draw_setup_modal(f, f.area(), shim_path, theme, local_notice.as_deref()))?;
if let Event::Key(key) = event::read()?
&& key.kind == KeyEventKind::Press
{
if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
logging::info("setup: dismissed reason=interrupt note=app_not_started");
return Ok(false);
}
if key.code == KeyCode::Char('q') {
logging::info("setup: dismissed reason=quit note=app_not_started");
return Ok(false);
}
if key.code == KeyCode::Char('f') {
logging::debug("setup: fix requested via=guide");
match manager.fix_path_permanently() {
Ok(lines) => {
local_notice = Some(
lines
.into_iter()
.enumerate()
.map(|(i, line)| {
if i == 1 {
Line::from(Span::styled(line, theme.brand_bold()))
} else if i == 0 {
Line::from(Span::styled(line, theme.success()))
} else {
Line::from(Span::styled(line, theme.muted()))
}
})
.collect(),
);
}
Err(e) => {
logging::error(&format!("setup: fix failed error=\"{e}\""));
local_notice = Some(vec![
Line::from(Span::styled(
"Failed to fix PATH:".to_string(),
theme.error(),
)),
Line::from(Span::styled(format!(" {e}"), theme.muted())),
]);
}
}
logging::debug("setup: fix result shown");
continue; }
logging::info("setup: dismissed reason=continued");
return Ok(true);
}
}
}
pub(crate) fn draw_setup_modal(
frame: &mut Frame,
screen: Rect,
shim_path: &str,
theme: &Theme,
notice: Option<&[Line<'static>]>,
) {
let pct_x = if screen.width >= 90 { 78 } else { 92 };
let pct_y = if screen.height >= 32 { 68 } else { 88 };
let area = centered_rect(pct_x, pct_y, screen);
frame.render_widget(Block::default().style(Style::default().bg(theme.bg)), area);
let block = Block::default()
.title(Line::from(vec![
Span::styled(" 🔧 ", Style::default().fg(theme.brand)),
Span::styled(" GoVMR Setup ", theme.title()),
]))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(theme.border())
.style(Style::default().bg(theme.bg));
frame.render_widget(block, area);
let inner = area.inner(Margin {
horizontal: 2,
vertical: 1,
});
draw_setup_content(frame, inner, shim_path, theme, notice);
}
fn draw_setup_content(
frame: &mut Frame,
inner: Rect,
shim_path: &str,
theme: &Theme,
notice: Option<&[Line<'static>]>,
) {
let chunks = create_layout_chunks(inner);
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
"Add the GoVMR shim directory to your PATH so `go` resolves through GoVMR.",
theme.modal_body(),
))),
chunks[0],
);
#[cfg(unix)]
let session_cmd = format!("export PATH=\"{shim_path}:$PATH\"");
#[cfg(windows)]
let session_cmd = format!("$env:PATH+=\";{shim_path}\"");
let session_block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(theme.border())
.title(Span::styled(" This session ", theme.muted()));
#[cfg(unix)]
let session_display = format!(" $ {session_cmd} ");
#[cfg(windows)]
let session_display = format!(" PS> {session_cmd} ");
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
session_display,
theme.brand_bold(),
)))
.block(session_block),
chunks[1],
);
if let Some(notice) = notice {
let notice_block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(theme.success())
.title(Span::styled(" ✅ PATH Fix Result ", theme.success()));
frame.render_widget(
Paragraph::new(notice.to_vec())
.block(notice_block)
.wrap(ratatui::widgets::Wrap { trim: false }),
chunks[3],
);
} else {
frame.render_widget(
Paragraph::new(Line::from(vec![
Span::styled(" Press ", theme.muted()),
Span::styled("f", theme.key_hint()),
#[cfg(unix)]
Span::styled(
" to fix permanently (appends to shell profile).",
theme.muted(),
),
#[cfg(windows)]
Span::styled(" to fix permanently (updates User PATH).", theme.muted()),
])),
chunks[3],
);
}
let log_path = logging::default_log_path().map_or_else(
|| "~/.govmr/govmr.log".to_string(),
|p| tilde_path(&p.to_string_lossy()),
);
frame.render_widget(
Paragraph::new(Line::from(vec![
Span::styled(" Logs: ", theme.muted()),
Span::styled(log_path, theme.brand_bold()),
Span::styled(" — press ", theme.muted()),
Span::styled("L", theme.key_hint()),
Span::styled(" in the dashboard to view them live.", theme.muted()),
])),
chunks[4],
);
frame.render_widget(
Paragraph::new(Line::from(vec![
Span::styled(" Press ", theme.muted()),
Span::styled("any key", theme.key_hint()),
Span::styled(" to close ", theme.muted()),
]))
.alignment(Alignment::Center),
chunks[5],
);
}
fn create_layout_chunks(inner: Rect) -> Vec<Rect> {
Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(2), Constraint::Length(3), Constraint::Length(1), Constraint::Min(4), Constraint::Length(1), Constraint::Length(2), ])
.split(inner)
.to_vec()
}