Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
576 changes: 453 additions & 123 deletions .rot/snapshot.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ percentage distribution.
* Find transactions quickly using partial or specific information.
* Organize transactions with custom tags for easy filtering.
* Add recurring transactions with flexible interval rules.
* Privacy mode for hiding balances and amounts.
* Password protection option for the DB with SQLite cipher.
* Works fully offline.

<h2>Installation</h2>
Expand Down
41 changes: 8 additions & 33 deletions tui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub struct Config {
pub new_location: Option<PathBuf>,
pub theme_index: Option<usize>,
pub migration_v1_done: Option<bool>,
/// Whether the database is encrypted and a password is asked on startup
pub db_encrypted: Option<bool>,
pub privacy_mode: Option<bool>,
}

impl Config {
Expand All @@ -42,6 +42,7 @@ impl Config {
theme_index: Some(0),
migration_v1_done: None,
db_encrypted: None,
privacy_mode: None,
});
}

Expand Down Expand Up @@ -168,6 +169,11 @@ impl Config {
self.save_config()
}

pub fn set_privacy_mode(&mut self, privacy_mode: bool) -> Result<()> {
self.privacy_mode = Some(privacy_mode);
self.save_config()
}

pub fn set_migration_v1_done(&mut self) -> Result<()> {
self.migration_v1_done = Some(true);
self.save_config()
Expand All @@ -182,6 +188,7 @@ pub fn migrate_config(config_path: &PathBuf) -> Result<()> {
theme_index: Some(0),
migration_v1_done: None,
db_encrypted: None,
privacy_mode: None,
};

let mut backup_path = config_path.to_owned();
Expand Down Expand Up @@ -246,35 +253,3 @@ pub fn migrate_config(config_path: &PathBuf) -> Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn config_from_before_encryption_loads_as_not_encrypted() {
let config: Config = serde_json::from_str(
r#"{"backup_db_path":null,"new_location":null,"theme_index":1,"migration_v1_done":true}"#,
)
.unwrap();

assert_eq!(config.db_encrypted, None);
assert_eq!(config.migration_v1_done, Some(true));
}

#[test]
fn encrypted_flag_is_saved_and_loaded() {
let dir = std::env::temp_dir().join("rex_config_encrypted_test");
fs::create_dir_all(&dir).unwrap();

let mut config = Config::get_config(&dir.join("rex.sqlite")).unwrap();
assert_eq!(config.db_encrypted, None);

config.set_db_encrypted(true).unwrap();

let loaded = Config::get_config(&dir.join("rex.sqlite")).unwrap();
assert_eq!(loaded.db_encrypted, Some(true));

fs::remove_dir_all(dir).unwrap();
}
}
1 change: 1 addition & 0 deletions tui/src/key_checker/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn activity_keys(handler: &mut InputKeyHandler) -> Result<Option<HandlingOut
KeyCode::Char('u') => handler.go_recurring(),
KeyCode::Char('v') => handler.show_activity_tx_details()?,
KeyCode::Char('t') => handler.next_theme()?,
KeyCode::Char('P') => handler.toggle_privacy()?,
KeyCode::Right => handler.handle_right_arrow()?,
KeyCode::Left => handler.handle_left_arrow()?,
KeyCode::Up => handler.handle_up_arrow(),
Expand Down
1 change: 1 addition & 0 deletions tui/src/key_checker/add_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn add_tx_keys(handler: &mut InputKeyHandler) -> Result<Option<HandlingOutpu
KeyCode::Char('y') => handler.go_activity(),
KeyCode::Char('u') => handler.go_recurring(),
KeyCode::Char('t') => handler.next_theme()?,
KeyCode::Char('P') => handler.toggle_privacy()?,
KeyCode::Enter => handler.select_date_field(),
KeyCode::Char(c) if c.is_numeric() => {
handler.handle_number_press();
Expand Down
1 change: 1 addition & 0 deletions tui/src/key_checker/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn chart_keys(handler: &mut InputKeyHandler) -> Result<Option<HandlingOutput
KeyCode::Char('y') => handler.go_activity(),
KeyCode::Char('u') => handler.go_recurring(),
KeyCode::Char('t') => handler.next_theme()?,
KeyCode::Char('P') => handler.toggle_privacy()?,
KeyCode::Right => handler.handle_right_arrow()?,
KeyCode::Left => handler.handle_left_arrow()?,
KeyCode::Up => handler.handle_up_arrow(),
Expand Down
1 change: 1 addition & 0 deletions tui/src/key_checker/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn home_keys(handler: &mut InputKeyHandler) -> Result<Option<HandlingOutput>
KeyCode::Char('.') => handler.switch_tx_position_down()?,
KeyCode::Char('v') => handler.show_home_tx_details(),
KeyCode::Char('t') => handler.next_theme()?,
KeyCode::Char('P') => handler.toggle_privacy()?,
KeyCode::Right => handler.handle_right_arrow()?,
KeyCode::Left => handler.handle_left_arrow()?,
KeyCode::Up => handler.handle_up_arrow(),
Expand Down
112 changes: 6 additions & 106 deletions tui/src/key_checker/key_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,12 @@ impl<'a> InputKeyHandler<'a> {
self.popup_status.next();
}

/// Turns privacy mode on or off and saves it so it carries over to the next launch
pub fn toggle_privacy(&mut self) -> Result<()> {
let enabled = !self.config.privacy_mode.unwrap_or(false);
self.config.set_privacy_mode(enabled)
}

pub fn next_theme(&mut self) -> Result<()> {
let new_index = self.theme.next();
self.config.theme_index = Some(new_index);
Expand Down Expand Up @@ -2976,109 +2982,3 @@ fn recurring_number_jump_tabs(tx_type: TxType, frequency: RecurrenceFrequency) -
fn digit_to_tab_index(digit: u32) -> usize {
if digit == 0 { 9 } else { digit as usize - 1 }
}

#[cfg(test)]
mod number_jump_tests {
use super::*;

#[test]
fn digit_to_tab_index_maps_1_through_9_to_0_through_8() {
for digit in 1..=9 {
assert_eq!(digit_to_tab_index(digit), digit as usize - 1);
}
}

#[test]
fn digit_to_tab_index_maps_0_to_the_tenth_field() {
assert_eq!(digit_to_tab_index(0), 9);
}

#[test]
fn income_expense_daily_has_nine_fields_both_end_date_and_tags_reachable() {
let tabs = recurring_number_jump_tabs(TxType::IncomeExpense, RecurrenceFrequency::Daily);
assert_eq!(
tabs,
vec![
TxTab::Date,
TxTab::Details,
TxTab::TxType,
TxTab::FromMethod,
TxTab::Amount,
TxTab::Frequency,
TxTab::RecurInterval,
TxTab::EndDate,
TxTab::Tags,
]
);
assert_eq!(tabs[digit_to_tab_index(8)], TxTab::EndDate);
assert_eq!(tabs[digit_to_tab_index(9)], TxTab::Tags);
// No 10th field in this combination, so key 0 has nothing to jump to
assert!(tabs.get(digit_to_tab_index(0)).is_none());
}

#[test]
fn income_expense_weekly_or_monthly_has_ten_fields_both_end_date_and_tags_reachable() {
let tabs = recurring_number_jump_tabs(TxType::IncomeExpense, RecurrenceFrequency::Weekly);
assert_eq!(tabs.len(), 10);
assert_eq!(tabs[digit_to_tab_index(8)], TxTab::RecurValue);
assert_eq!(tabs[digit_to_tab_index(9)], TxTab::EndDate);
assert_eq!(tabs[digit_to_tab_index(0)], TxTab::Tags);
}

#[test]
fn income_expense_yearly_has_eleven_fields_tags_has_no_key_left() {
let tabs = recurring_number_jump_tabs(TxType::IncomeExpense, RecurrenceFrequency::Yearly);
assert_eq!(tabs.len(), 11);
assert_eq!(tabs[digit_to_tab_index(8)], TxTab::RecurValue);
assert_eq!(tabs[digit_to_tab_index(9)], TxTab::RecurMonth);
assert_eq!(tabs[digit_to_tab_index(0)], TxTab::EndDate);
// Tags is the 11th field - out of digits (1-9 then 0 only covers 10)
assert!(tabs.get(10).is_some());
assert_eq!(tabs[10], TxTab::Tags);
}

#[test]
fn transfer_daily_has_ten_fields_both_end_date_and_tags_reachable() {
let tabs = recurring_number_jump_tabs(TxType::Transfer, RecurrenceFrequency::Daily);
assert_eq!(tabs.len(), 10);
assert_eq!(tabs[4], TxTab::ToMethod);
assert_eq!(tabs[digit_to_tab_index(9)], TxTab::EndDate);
assert_eq!(tabs[digit_to_tab_index(0)], TxTab::Tags);
}

#[test]
fn transfer_weekly_or_monthly_has_eleven_fields_tags_has_no_key_left() {
let tabs = recurring_number_jump_tabs(TxType::Transfer, RecurrenceFrequency::Monthly);
assert_eq!(tabs.len(), 11);
assert_eq!(tabs[digit_to_tab_index(9)], TxTab::RecurValue);
assert_eq!(tabs[digit_to_tab_index(0)], TxTab::EndDate);
assert_eq!(tabs[10], TxTab::Tags);
}

#[test]
fn transfer_yearly_has_twelve_fields_end_date_and_tags_have_no_key_left() {
let tabs = recurring_number_jump_tabs(TxType::Transfer, RecurrenceFrequency::Yearly);
assert_eq!(tabs.len(), 12);
assert_eq!(
&tabs[..10],
[
TxTab::Date,
TxTab::Details,
TxTab::TxType,
TxTab::FromMethod,
TxTab::ToMethod,
TxTab::Amount,
TxTab::Frequency,
TxTab::RecurInterval,
TxTab::RecurValue,
TxTab::RecurMonth,
]
);
assert_eq!(tabs[digit_to_tab_index(9)], TxTab::RecurValue);
assert_eq!(tabs[digit_to_tab_index(0)], TxTab::RecurMonth);
// End Date and Tags are the 11th/12th fields here - genuinely out of digits,
// only reachable via sequential Enter in this one combination
assert_eq!(tabs[10], TxTab::EndDate);
assert_eq!(tabs[11], TxTab::Tags);
}
}
1 change: 1 addition & 0 deletions tui/src/key_checker/recurring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn recurring_keys(handler: &mut InputKeyHandler) -> Result<Option<HandlingOu
KeyCode::Char('w') => handler.go_search(),
KeyCode::Char('y') => handler.go_activity(),
KeyCode::Char('t') => handler.next_theme()?,
KeyCode::Char('P') => handler.toggle_privacy()?,
KeyCode::Up => handler.handle_up_arrow(),
KeyCode::Down => handler.handle_down_arrow(),
KeyCode::Enter => handler.select_date_field(),
Expand Down
1 change: 1 addition & 0 deletions tui/src/key_checker/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn search_keys(handler: &mut InputKeyHandler) -> Result<Option<HandlingOutpu
KeyCode::Char('y') => handler.go_activity(),
KeyCode::Char('u') => handler.go_recurring(),
KeyCode::Char('t') => handler.next_theme()?,
KeyCode::Char('P') => handler.toggle_privacy()?,
KeyCode::Up => handler.handle_up_arrow(),
KeyCode::Down => handler.handle_down_arrow(),
KeyCode::Enter => handler.select_date_field(),
Expand Down
1 change: 1 addition & 0 deletions tui/src/key_checker/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn summary_keys(handler: &mut InputKeyHandler) -> Result<Option<HandlingOutp
KeyCode::Char('y') => handler.go_activity(),
KeyCode::Char('u') => handler.go_recurring(),
KeyCode::Char('t') => handler.next_theme()?,
KeyCode::Char('P') => handler.toggle_privacy()?,
KeyCode::Right => handler.handle_right_arrow()?,
KeyCode::Left => handler.handle_left_arrow()?,
KeyCode::Up => handler.handle_up_arrow(),
Expand Down
9 changes: 9 additions & 0 deletions tui/src/page_handler/ui_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ pub fn start_app<B: Backend>(
popup_status = PopupType::new_choice_config_forced(&theme);
}

let privacy = config.privacy_mode.unwrap_or(false);

// Passing out relevant data to the UI function
terminal
.draw(|f| {
Expand All @@ -223,6 +225,7 @@ pub fn start_app<B: Backend>(
&mut lerp_state,
&mut home_txs,
&theme,
privacy,
conn,
),

Expand All @@ -233,6 +236,7 @@ pub fn start_app<B: Backend>(
&add_tx_tab,
&mut lerp_state,
&theme,
privacy,
conn,
),

Expand All @@ -251,6 +255,7 @@ pub fn start_app<B: Backend>(
&mut lerp_state,
&chart_view,
&theme,
privacy,
conn,
),

Expand All @@ -266,6 +271,7 @@ pub fn start_app<B: Backend>(
&mut lerp_state,
&full_summary,
&theme,
privacy,
conn,
),
CurrentUi::Search => search_ui(
Expand All @@ -276,6 +282,7 @@ pub fn start_app<B: Backend>(
search_date_type,
&mut lerp_state,
&theme,
privacy,
),
CurrentUi::Activity => activity_ui(
f,
Expand All @@ -286,6 +293,7 @@ pub fn start_app<B: Backend>(
&mut activity_table,
&mut lerp_state,
&theme,
privacy,
),
CurrentUi::Recurring => recurring_ui(
f,
Expand All @@ -294,6 +302,7 @@ pub fn start_app<B: Backend>(
&mut recurring_table,
&mut lerp_state,
&theme,
privacy,
),
}

Expand Down
51 changes: 0 additions & 51 deletions tui/src/page_handler/unlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,54 +109,3 @@ fn show_unlock_ui(f: &mut Frame, theme: &Theme, password_len: usize, error: Opti
chunks[3],
);
}

#[cfg(test)]
mod tests {
use ratatui::Terminal;
use ratatui::backend::TestBackend;

use super::*;
use crate::theme::ThemeVariant;

fn render(password_len: usize, error: Option<&str>) -> String {
let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap();
let theme = Theme::new(ThemeVariant::Dracula);

terminal
.draw(|f| show_unlock_ui(f, &theme, password_len, error))
.unwrap();

terminal
.backend()
.buffer()
.content()
.iter()
.map(|cell| cell.symbol())
.collect()
}

#[test]
fn typed_characters_are_masked() {
let screen = render(6, None);

assert!(screen.contains("******"));
assert!(!screen.contains("*******"));
assert!(screen.contains("Rex - Locked"));
}

#[test]
fn wrong_password_message_is_shown() {
assert!(render(0, Some("Wrong password")).contains("Wrong password"));
assert!(!render(0, None).contains("Wrong password"));
}

#[test]
fn fits_a_small_terminal_without_panicking() {
let mut terminal = Terminal::new(TestBackend::new(30, 8)).unwrap();
let theme = Theme::new(ThemeVariant::Light);

terminal
.draw(|f| show_unlock_ui(f, &theme, 40, Some("Wrong password")))
.unwrap();
}
}
Loading
Loading