[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::{
|
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},
|
util::file_util::{load_file, save_file},
|
||||||
};
|
};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
@ -8,20 +9,20 @@ pub struct ConsentManager;
|
||||||
impl ConsentManager {
|
impl ConsentManager {
|
||||||
pub async fn check() -> (bool, bool) {
|
pub async fn check() -> (bool, bool) {
|
||||||
let file = load_file("", "agreements");
|
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 {
|
let final_state = if existing.eula {
|
||||||
existing
|
existing
|
||||||
} else {
|
} else {
|
||||||
let choice = run_consent_ui().await;
|
let choice = terms_checker::run_consent_ui().await;
|
||||||
let state = match choice {
|
let state = match choice {
|
||||||
UserChoice::Deny => ConsentUiState::denied(),
|
UserChoice::Deny => ConsentState::denied(),
|
||||||
UserChoice::AcceptEULA => ConsentUiState {
|
UserChoice::AcceptEULA => ConsentState {
|
||||||
eula: true,
|
eula: true,
|
||||||
tos: false,
|
tos: false,
|
||||||
pp: false,
|
pp: false,
|
||||||
},
|
},
|
||||||
UserChoice::AcceptAll => ConsentUiState {
|
UserChoice::AcceptAll => ConsentState {
|
||||||
eula: true,
|
eula: true,
|
||||||
tos: true,
|
tos: true,
|
||||||
pp: true,
|
pp: true,
|
||||||
|
|
@ -46,13 +47,13 @@ pub enum UserChoice {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct ConsentUiState {
|
pub struct ConsentState {
|
||||||
pub eula: bool,
|
pub eula: bool,
|
||||||
pub tos: bool,
|
pub tos: bool,
|
||||||
pub pp: bool,
|
pub pp: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConsentUiState {
|
impl ConsentState {
|
||||||
fn denied() -> Self {
|
fn denied() -> Self {
|
||||||
Self {
|
Self {
|
||||||
eula: false,
|
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 consent_state;
|
||||||
pub mod doc;
|
pub mod doc;
|
||||||
|
pub mod focus;
|
||||||
pub mod md_viewer;
|
pub mod md_viewer;
|
||||||
pub mod terms_checker;
|
pub mod terms_checker;
|
||||||
pub mod terms_getter;
|
pub mod terms_getter;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::terms::{
|
use crate::terms::{
|
||||||
consent_state::{ConsentUiState, UserChoice},
|
consent_state::{ConsentState, UserChoice},
|
||||||
|
focus::Focus,
|
||||||
md_viewer::FileViewer,
|
md_viewer::FileViewer,
|
||||||
terms_getter::{Type, get_link, get_terms},
|
terms_getter::{Type, get_link, get_terms},
|
||||||
};
|
};
|
||||||
|
|
@ -12,63 +13,10 @@ use ratatui::{
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
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 {
|
pub async fn run_consent_ui() -> UserChoice {
|
||||||
let mut terminal = ratatui::init();
|
let mut terminal = ratatui::init();
|
||||||
|
|
||||||
let mut state = ConsentUiState {
|
let mut state = ConsentState {
|
||||||
eula: false,
|
eula: false,
|
||||||
tos: false,
|
tos: false,
|
||||||
pp: 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));
|
.block(Block::default().borders(Borders::ALL));
|
||||||
f.render_widget(p, area);
|
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![
|
let buttons = vec![
|
||||||
("[Q] Cancel", Focus::Cancel),
|
("[Q] Cancel", Focus::Cancel),
|
||||||
("Continue", Focus::Continue),
|
("Continue", Focus::Continue),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue