use crate::{
theme::{Theme, ThemeName, ThemePickerView},
version::GoVersion,
};
use ratatui::{text::Line, widgets::ListState};
use std::time::Instant;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum ActiveTab {
Available,
Installed,
}
impl ActiveTab {
#[must_use]
pub(crate) fn toggle(self) -> Self {
match self {
ActiveTab::Available => ActiveTab::Installed,
ActiveTab::Installed => ActiveTab::Available,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum MsgKind {
Success,
Error,
Info,
}
#[derive(Clone)]
pub(crate) struct StatusMessage {
pub(crate) text: String,
pub(crate) kind: MsgKind,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Phase {
Downloading,
Extracting,
}
#[derive(Clone)]
pub(crate) enum BusyState {
Refreshing,
Switching(String),
Deleting(String),
Installing {
version: String,
phase: Phase,
downloaded: u64,
total: u64,
speed: f64,
started_at: Instant,
},
Updating {
version: String,
phase: Phase,
downloaded: u64,
total: u64,
speed: f64,
started_at: Instant,
},
}
impl BusyState {
#[must_use]
pub(crate) fn target(&self) -> Option<&str> {
match self {
BusyState::Refreshing => None,
BusyState::Switching(v)
| BusyState::Deleting(v)
| BusyState::Installing { version: v, .. }
| BusyState::Updating { version: v, .. } => Some(v),
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ThemePickerState {
pub(crate) view: ThemePickerView,
pub(crate) family_cursor: usize,
pub(crate) theme_cursor: usize,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum SystemPrompt {
Update,
UninstallKeep,
UninstallPurge,
}
#[derive(Clone)]
pub(crate) struct VisualAddition {
pub(crate) text: String,
pub(crate) anchor: usize,
}
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct AppState {
pub(crate) versions: Vec<GoVersion>,
pub(crate) list_state: ListState,
pub(crate) active_tab: ActiveTab,
pub(crate) busy: Option<BusyState>,
pub(crate) status_message: Option<StatusMessage>,
pub(crate) confirming_delete: Option<String>,
pub(crate) is_shim_in_path: bool,
pub(crate) shim_path: String,
pub(crate) filter: String,
pub(crate) filter_mode: bool,
pub(crate) show_help: bool,
pub(crate) show_command_help: bool,
pub(crate) command_help_scroll: usize,
pub(crate) show_theme_picker: bool,
pub(crate) theme_picker: ThemePickerState,
pub(crate) theme: Theme,
pub(crate) tick_count: u64,
pub(crate) path_fix_notice: Option<Vec<Line<'static>>>,
pub(crate) show_logs: bool,
pub(crate) log_lines: Vec<String>,
pub(crate) log_scroll: usize,
pub(crate) log_follow: bool,
pub(crate) log_refreshed: Option<Instant>,
pub(crate) log_focus: bool,
pub(crate) log_wrap: bool,
pub(crate) log_visual_skip: usize,
pub(crate) log_visual_additions: Vec<VisualAddition>,
pub(crate) cancel_install: Option<tokio::sync::watch::Sender<bool>>,
pub(crate) system_prompt: Option<SystemPrompt>,
}
impl AppState {
#[allow(dead_code)]
pub(crate) fn from_versions(versions: Vec<GoVersion>, is_shim_in_path: bool) -> Self {
let mut list_state = ListState::default();
if !versions.is_empty() {
list_state.select(Some(0));
}
let theme = Theme::for_name(ThemeName::default());
Self {
versions,
list_state,
active_tab: ActiveTab::Available,
busy: None,
status_message: None,
confirming_delete: None,
is_shim_in_path,
shim_path: String::from("~/.govmr/shim"),
filter: String::new(),
filter_mode: false,
show_help: false,
show_command_help: false,
command_help_scroll: 0,
show_theme_picker: false,
theme_picker: ThemePickerState::default(),
theme,
tick_count: 0,
path_fix_notice: None,
show_logs: false,
log_lines: Vec::new(),
log_scroll: 0,
log_follow: true,
log_refreshed: None,
log_focus: false,
log_wrap: false,
cancel_install: None,
system_prompt: None,
log_visual_skip: 0,
log_visual_additions: Vec::new(),
}
}
#[must_use]
pub(crate) fn visible_indices(&self) -> Vec<usize> {
visible_indices(self)
}
pub(crate) fn clamp_selection(&mut self) {
let len = self.visible_indices().len();
match self.list_state.selected() {
Some(i) if len > 0 && i >= len => self.list_state.select(Some(len - 1)),
None if len > 0 => self.list_state.select(Some(0)),
Some(_) | None => {}
}
}
pub(crate) fn next_item(&mut self) {
let len = self.visible_indices().len();
if len == 0 {
return;
}
let i = match self.list_state.selected() {
Some(i) => (i + 1) % len,
None => 0,
};
self.list_state.select(Some(i));
}
pub(crate) fn previous_item(&mut self) {
let len = self.visible_indices().len();
if len == 0 {
return;
}
let i = match self.list_state.selected() {
Some(0) => len - 1,
Some(i) => i - 1,
None => 0,
};
self.list_state.select(Some(i));
}
}
#[must_use]
pub(crate) fn visible_indices(state: &AppState) -> Vec<usize> {
let query = state.filter.to_lowercase();
state
.versions
.iter()
.enumerate()
.filter(|(_, v)| match state.active_tab {
ActiveTab::Available => true,
ActiveTab::Installed => v.installed,
})
.filter(|(_, v)| {
query.is_empty()
|| v.raw_version.to_lowercase().contains(&query)
|| v.display_name.to_lowercase().contains(&query)
})
.map(|(i, _)| i)
.collect()
}