diff --git a/src/terms/consent_state.rs b/src/terms/consent_state.rs index 5e394bd..5ada255 100644 --- a/src/terms/consent_state.rs +++ b/src/terms/consent_state.rs @@ -1,5 +1,6 @@ use crate::{ - terms::{terms_checker::run_consent_ui, terms_getter::get_current_docs}, + terms::terms_checker, + terms::terms_getter::get_current_docs, util::file_util::{load_file, save_file}, }; use std::time::{SystemTime, UNIX_EPOCH}; @@ -8,20 +9,20 @@ pub struct ConsentManager; impl ConsentManager { pub async fn check() -> (bool, bool) { let file = load_file("", "agreements"); - let existing = ConsentUiState::from_str(&file).sanitize(); + let existing = ConsentState::from_str(&file).sanitize(); let final_state = if existing.eula { existing } else { - let choice = run_consent_ui().await; + let choice = terms_checker::run_consent_ui().await; let state = match choice { - UserChoice::Deny => ConsentUiState::denied(), - UserChoice::AcceptEULA => ConsentUiState { + UserChoice::Deny => ConsentState::denied(), + UserChoice::AcceptEULA => ConsentState { eula: true, tos: false, pp: false, }, - UserChoice::AcceptAll => ConsentUiState { + UserChoice::AcceptAll => ConsentState { eula: true, tos: true, pp: true, @@ -46,13 +47,13 @@ pub enum UserChoice { } #[derive(Debug, Clone, Copy)] -pub struct ConsentUiState { +pub struct ConsentState { pub eula: bool, pub tos: bool, pub pp: bool, } -impl ConsentUiState { +impl ConsentState { fn denied() -> Self { Self { eula: false, diff --git a/src/terms/focus.rs b/src/terms/focus.rs new file mode 100644 index 0000000..3ac4140 --- /dev/null +++ b/src/terms/focus.rs @@ -0,0 +1,54 @@ +use crate::terms::consent_state::ConsentState; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Focus { + Eula, + Tos, + Pp, + Cancel, + Continue, + ContinueAll, +} +impl Focus { + pub fn next(&mut self, state: ConsentState) { + *self = match self { + Focus::Eula => Focus::Tos, + Focus::Tos => Focus::Pp, + Focus::Pp => Focus::Cancel, + Focus::Cancel => { + if state.can_continue() { + Focus::Continue + } else { + Focus::Eula + } + } + Focus::Continue => { + if state.can_continue_all() { + Focus::ContinueAll + } else { + Focus::Eula + } + } + Focus::ContinueAll => Focus::Eula, + }; + } + + pub fn prev(&mut self, state: ConsentState) { + *self = match self { + Focus::Eula => { + if state.can_continue_all() { + Focus::ContinueAll + } else if state.can_continue() { + Focus::Continue + } else { + Focus::Cancel + } + } + Focus::Tos => Focus::Eula, + Focus::Pp => Focus::Tos, + Focus::Cancel => Focus::Pp, + Focus::Continue => Focus::Cancel, + Focus::ContinueAll => Focus::Continue, + }; + } +} diff --git a/src/terms/mod.rs b/src/terms/mod.rs index 6177949..1c71e7e 100644 --- a/src/terms/mod.rs +++ b/src/terms/mod.rs @@ -1,5 +1,6 @@ pub mod consent_state; pub mod doc; +pub mod focus; pub mod md_viewer; pub mod terms_checker; pub mod terms_getter; diff --git a/src/terms/terms_checker.rs b/src/terms/terms_checker.rs index 571d2a7..29d45d4 100644 --- a/src/terms/terms_checker.rs +++ b/src/terms/terms_checker.rs @@ -1,5 +1,6 @@ use crate::terms::{ - consent_state::{ConsentUiState, UserChoice}, + consent_state::{ConsentState, UserChoice}, + focus::Focus, md_viewer::FileViewer, terms_getter::{Type, get_link, get_terms}, }; @@ -12,63 +13,10 @@ use ratatui::{ }; use std::time::Duration; -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Focus { - Eula, - Tos, - Pp, - Cancel, - Continue, - ContinueAll, -} -impl Focus { - fn next(&mut self, state: ConsentUiState) { - *self = match self { - Focus::Eula => Focus::Tos, - Focus::Tos => Focus::Pp, - Focus::Pp => Focus::Cancel, - Focus::Cancel => { - if state.can_continue() { - Focus::Continue - } else { - Focus::Eula - } - } - Focus::Continue => { - if state.can_continue_all() { - Focus::ContinueAll - } else { - Focus::Eula - } - } - Focus::ContinueAll => Focus::Eula, - }; - } - - fn prev(&mut self, state: ConsentUiState) { - *self = match self { - Focus::Eula => { - if state.can_continue_all() { - Focus::ContinueAll - } else if state.can_continue() { - Focus::Continue - } else { - Focus::Cancel - } - } - Focus::Tos => Focus::Eula, - Focus::Pp => Focus::Tos, - Focus::Cancel => Focus::Pp, - Focus::Continue => Focus::Cancel, - Focus::ContinueAll => Focus::Continue, - }; - } -} - pub async fn run_consent_ui() -> UserChoice { let mut terminal = ratatui::init(); - let mut state = ConsentUiState { + let mut state = ConsentState { eula: false, tos: false, pp: false, @@ -369,7 +317,7 @@ fn draw_button(f: &mut ratatui::Frame, area: Rect, label: &str, style: Style) { .block(Block::default().borders(Borders::ALL)); f.render_widget(p, area); } -fn draw_buttons(f: &mut ratatui::Frame, area: Rect, state: &ConsentUiState) { +fn draw_buttons(f: &mut ratatui::Frame, area: Rect, state: &ConsentState) { let buttons = vec![ ("[Q] Cancel", Focus::Cancel), ("Continue", Focus::Continue),