Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanjh committed Jun 30, 2024
1 parent d094824 commit f5a97e1
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 93 deletions.
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ ratatui = "0.26.2"
rusqlite = { version = "0.31.0", features = ["bundled"] }
sha3 = "0.10.8"
thiserror = "1.0.60"
time = "0.3.36"
unicode-width = "0.1.13"
wl-clipboard-rs = "0.9.0"
2 changes: 2 additions & 0 deletions export.sh → script/export-pass.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
# pass: the standard unix password manager
# https://www.passwordstore.org/
# export passwords to external file

shopt -s nullglob globstar
Expand Down
76 changes: 45 additions & 31 deletions src/tui/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
path::Path,
rc::Rc,
time::{SystemTime, UNIX_EPOCH},
};
use std::{path::Path, rc::Rc};

use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::{
Expand All @@ -23,7 +19,7 @@ use super::{
module::{
draw_account_table, draw_confirm, draw_form, draw_view, AccountTable, Confirm, Form, View,
},
util::copy_content,
util::{copy_content, current_millis},
};

enum AppMode {
Expand Down Expand Up @@ -67,8 +63,10 @@ impl App {
form: Form::default(),
to_del: Confirm::default().with_content("To delete the selected account?"),
};
let accounts = app.account_repo.all()?;
app.account_table.load(accounts);

app.change_mode(AppMode::Table);
app.load_accounts()?;

// app.change_mode(mode);
Ok(app)
}
Expand All @@ -83,8 +81,6 @@ impl App {
return Ok(());
}
match self.mode {
// AppMode::Login => self.login_on_key_event(key_event)?,
// AppMode::Reg => self.reg_on_key_envent(key_event)?,
AppMode::Table => self.table_on_key_envent(key_event)?,
AppMode::View => self.view_on_key_event(key_event)?,
AppMode::Add => self.add_on_key_event(key_event)?,
Expand Down Expand Up @@ -149,6 +145,7 @@ impl App {
}
}
KeyCode::Char('a') => {
self.form.reset();
self.change_mode(AppMode::Add);
}
KeyCode::Char('e') => {
Expand Down Expand Up @@ -203,7 +200,7 @@ impl App {
..
} => {
if self.form.validate() {
let current = current_timestamp();
let current = current_millis() as usize;
let acc = Account {
id: 0,
url: self.form.url().to_string(),
Expand All @@ -220,7 +217,7 @@ impl App {
};
self.pwd_repo.add(&pwd)?;

self.account_table.load(self.account_repo.all()?);
self.load_accounts()?;
self.account_table.select_by_aid(aid);

self.form.reset();
Expand Down Expand Up @@ -249,7 +246,7 @@ impl App {
..
} => {
if self.form.validate() {
let current = current_timestamp();
let current = current_millis() as usize;
if let Some(selected) = self.account_table.selected() {
let aid = selected.id;
let acc = Account {
Expand All @@ -268,7 +265,8 @@ impl App {
};
self.pwd_repo.add(&pwd)?;

self.account_table.load(self.account_repo.all()?);
// self.account_table.load(self.account_repo.all()?);
self.load_accounts()?;
self.account_table.select_by_aid(aid);

self.form.reset();
Expand Down Expand Up @@ -312,13 +310,23 @@ impl App {
fn change_mode(&mut self, mode: AppMode) {
self.mode = mode;
match self.mode {
// AppMode::Login => self.help_text = "login".to_owned(),
// AppMode::Reg => self.help_text = "reg".to_owned(),
AppMode::Table => self.help_text = "table".to_owned(),
AppMode::View => self.help_text = "view".to_owned(),
AppMode::Add => self.help_text = "add".to_owned(),
AppMode::Del => self.help_text = "delete".to_owned(),
AppMode::Edit => self.help_text = "edit".to_owned(),
AppMode::Table => {
self.help_text =
"/: filter, a: add, e: edit, d: delete, c: copy password, j: next, k: prev, l/enter: view, ctrl-c: quit"
.to_owned()
}
AppMode::View => {
self.help_text = "View Account - c: copy, x: show/hide passwords, q/esc: back".to_owned()
}
AppMode::Add => {
self.help_text =
"Eidt Account - ctrl-x: show/hide passwords, ctrl-v: paste, esc: back".to_owned()
}
AppMode::Del => self.help_text = "Delete Account - esc: back".to_owned(),
AppMode::Edit => {
self.help_text =
"Edit Account - ctrl-x: show/hide passwords, ctrl-v: paste, esc: back".to_owned()
}
}
}

Expand All @@ -331,17 +339,23 @@ impl App {
}
Ok(())
}

fn load_accounts(&mut self) -> TecResult<()> {
let accounts = self.account_repo.all()?;
self.account_table.load(accounts);
Ok(())
}
}

pub fn draw_app(f: &mut Frame, app: &mut App) {
let [main_area, search_area, help_area] = Layout::vertical([
let [main_area, help_area] = Layout::vertical([
Constraint::Min(3),
Constraint::Length(1),
// Constraint::Length(1),
Constraint::Length(1),
])
.areas(f.size());

draw_account_table(f, &mut app.account_table, main_area, search_area);
draw_account_table(f, &mut app.account_table, main_area);

let pop_rect = centered_rect(60, 30, main_area);
match app.mode {
Expand Down Expand Up @@ -388,10 +402,10 @@ fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
.split(popup_layout[1])[1] // Return the middle chunk
}

fn current_timestamp() -> usize {
let now = SystemTime::now();
let since_the_epoch = now
.duration_since(UNIX_EPOCH)
.expect("Clock may have gone backwards");
since_the_epoch.as_millis() as usize
}
// fn current_timestamp() -> usize {
// let now = SystemTime::now();
// let since_the_epoch = now
// .duration_since(UNIX_EPOCH)
// .expect("Clock may have gone backwards");
// since_the_epoch.as_millis() as usize
// }
10 changes: 3 additions & 7 deletions src/tui/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Auth {

impl Auth {
pub fn build(config_path: impl AsRef<Path>) -> TecResult<Self> {
let key_path = config_path.as_ref().join("tecpass.key");
let key_path = config_path.as_ref().join("tecpass.pub.key");

let key_store = KeyStore::new(key_path);
let mode = {
Expand All @@ -36,7 +36,6 @@ impl Auth {
}
};
let mut auth = Self {
// account_repo: AccountRepo::new(conn),
key_store,
mode: AuthMode::Login,
quiting: false,
Expand All @@ -47,7 +46,7 @@ impl Auth {
.with_min(8)
.with_max(32)
.with_active(),
reg: ConfirmPassword::default(),
reg: ConfirmPassword::default().with_title("Register password"),
};
auth.change_mode(mode);
Ok(auth)
Expand Down Expand Up @@ -86,7 +85,6 @@ impl Auth {
let res = self.key_store.get_key(pwd.as_bytes());
if let Ok(key) = res {
self.key = Some(key);
// self.change_mode(AppMode::Table);
} else {
self.login.set_msg("wrong password");
}
Expand Down Expand Up @@ -115,9 +113,7 @@ pub fn draw_auth(f: &mut Frame, auth: &mut Auth) {
let area = f.size();

match auth.mode {
AuthMode::Login => {
draw_input(f, &auth.login, area);
}
AuthMode::Login => draw_input(f, &auth.login, area),
AuthMode::Reg => draw_confirm_password(f, &auth.reg, area),
}
}
25 changes: 11 additions & 14 deletions src/tui/module/account_table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};
use ratatui::{
layout::{Constraint, Rect},
layout::{Constraint, Layout, Rect},
style::{Style, Stylize},
widgets::{Block, Row, Table, TableState},
Frame,
Expand Down Expand Up @@ -206,13 +206,13 @@ impl AccountTable {
// Ok(())
// }

pub fn help_text(&self) -> String {
if !self.query.is_active() {
"Down, j: next; Up, k: prev; /: filter".to_owned()
} else {
"Esc, Enter: quit filter".to_owned()
}
}
// pub fn help_text(&self) -> String {
// if !self.query.is_active() {
// "Down, j: next; Up, k: prev; /: filter".to_owned()
// } else {
// "Esc, Enter: quit filter".to_owned()
// }
// }

pub(crate) fn selected(&mut self) -> Option<&Account> {
// println!("xxxxxxxxxxxxxxxxxx {:?}", self.state.selected());
Expand All @@ -229,12 +229,9 @@ impl AccountTable {
}
}

pub fn draw_account_table(
f: &mut Frame,
at: &mut AccountTable,
main_area: Rect,
search_area: Rect,
) {
pub fn draw_account_table(f: &mut Frame, at: &mut AccountTable, area: Rect) {
let [main_area, search_area] =
Layout::vertical([Constraint::Min(3), Constraint::Length(1)]).areas(area);
let rows: Vec<Row> = at
.items
.iter()
Expand Down
Loading

0 comments on commit f5a97e1

Please sign in to comment.