[Add] Split Focus From UI
This commit is contained in:
parent
6b957f8eaa
commit
a81f80b191
4 changed files with 68 additions and 64 deletions
|
|
@ -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,
|
||||
|
|
|
|||
54
src/terms/focus.rs
Normal file
54
src/terms/focus.rs
Normal file
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
Loading…
Reference in a new issue