Skip to content

Commit

Permalink
Timetracker Print GUI - Fix week number command line parsing
Browse files Browse the repository at this point in the history
Passing --last-week and --relative-week now works as expected.
  • Loading branch information
david-cattermole committed Mar 1, 2024
1 parent f8faf9e commit 61f149a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
9 changes: 6 additions & 3 deletions print-gui-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ fn main() -> Result<()> {
.application_id(constants::APPLICATION_ID)
.build();

let global_state: GlobalStateRcRefCell =
Rc::new(RefCell::new(GlobalState::new_with_settings(settings)));
let global_state: GlobalStateRcRefCell = Rc::new(RefCell::new(GlobalState::new_with_settings(
settings, &args,
)));
let global_entries: GlobalEntriesRcRefCell = Rc::new(RefCell::new(GlobalEntries::new()));

application.connect_activate(clone!(
Expand All @@ -52,7 +53,9 @@ fn main() -> Result<()> {
}
));

let exit_code = application.run();
// All argument parsing is handled by our own parser, not GTK.
let args: &[&str] = &[];
let exit_code = application.run_with_args(args);
if exit_code != glib::ExitCode::SUCCESS {
bail!("GtkApplication exited with failure: {:?}", exit_code);
}
Expand Down
36 changes: 26 additions & 10 deletions print-gui-bin/src/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::utils::duration_format_as_id;
use crate::utils::get_absolute_week_start_end;
use crate::utils::id_as_datetime_format;
use crate::utils::id_as_duration_format;
use crate::CommandArguments;

use anyhow::Result;
use chrono::Datelike;
Expand Down Expand Up @@ -73,7 +74,10 @@ pub struct GlobalState {
pub type GlobalStateRcRefCell = Rc<RefCell<GlobalState>>;

impl GlobalState {
pub fn new_with_settings(settings: PrintGuiAppSettings) -> GlobalState {
pub fn new_with_settings(
settings: PrintGuiAppSettings,
args: &CommandArguments,
) -> GlobalState {
let text_buffer = TextBuffer::builder().build();

let mut preset_states = MapStringPresetState::new();
Expand Down Expand Up @@ -103,6 +107,24 @@ impl GlobalState {
preset_states.insert(preset_name.clone(), PresetState::Disable);
}

// Get the current week as the default value.
let today_local_timezone = chrono::Local::now();

// Set the default week based on command line argument flag
// logic, and ensure the week number does not go below 1, or
// above 52.
let current_week = today_local_timezone.iso_week().week();
let week_number: u32 = if args.last_week {
assert!(current_week != 0);
if current_week == 1 {
52
} else {
current_week.checked_sub(1).unwrap()
}
} else {
((current_week as i32) + args.relative_week).wrapping_rem_euclid(52) as u32
};

GlobalState {
settings: settings,
all_preset_names: all_preset_names,
Expand All @@ -115,7 +137,7 @@ impl GlobalState {
date_range_label: None,
preset_buttons_layout: None,
text_view: None,
week_number: 1,
week_number: week_number,
text_buffer: text_buffer,
}
}
Expand Down Expand Up @@ -518,7 +540,6 @@ fn build_preset_buttons(

/// Create the window, and all the widgets in the window.
fn construct_window(
week_number: u32,
global_state: GlobalStateRcRefCell,
global_entries: GlobalEntriesRcRefCell,
) -> ApplicationWindow {
Expand All @@ -542,8 +563,7 @@ fn construct_window(
.expect("Couldn't get 'week_number_spin_button' widget."),
);
let week_number_spin_button = borrowed_state.week_number_spin_button.as_ref().unwrap();
week_number_spin_button.set_value(week_number as f64);
borrowed_state.week_number = week_number;
week_number_spin_button.set_value(borrowed_state.week_number as f64);

borrowed_state.text_view = Some(
builder
Expand Down Expand Up @@ -658,11 +678,7 @@ pub fn build_ui(
global_state: GlobalStateRcRefCell,
global_entries: GlobalEntriesRcRefCell,
) {
// Get the current week as the default value.
let today_local_timezone = chrono::Local::now();
let today_week = today_local_timezone.iso_week().week();

let window = construct_window(today_week, global_state.clone(), global_entries.clone());
let window = construct_window(global_state.clone(), global_entries.clone());
window.set_application(Some(app));

setup_signals(global_state.clone(), global_entries.clone());
Expand Down

0 comments on commit 61f149a

Please sign in to comment.