void-focus 0.3.0-alpha.3

A feature-rich terminal focus timer with task tracking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use std::time::{Duration, Instant};

use anyhow::Result;
use crossterm::event::KeyModifiers;
use ratatui::widgets::ListState;

use crate::db::Database;
use crate::model::{
    AppData, EmptyQueueBehavior, EstimateCompleteBehavior, Priority, StoredSession, TimerMode,
    TimerState,
};
use crate::sound;
use crate::storage;
use crate::timer::{Timer, TimerConfig};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FocusTab {
    Dashboard,
    Tasks,
    Stats,
    Settings,
    Help,
}

impl FocusTab {
    pub const fn all() -> [FocusTab; 5] {
        [
            FocusTab::Dashboard,
            FocusTab::Tasks,
            FocusTab::Stats,
            FocusTab::Settings,
            FocusTab::Help,
        ]
    }
    pub fn label(&self) -> &'static str {
        match self {
            FocusTab::Dashboard => "Dashboard",
            FocusTab::Tasks => "Tasks",
            FocusTab::Stats => "Stats",
            FocusTab::Settings => "Settings",
            FocusTab::Help => "Help",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputMode {
    Normal,
    Editing,
}

#[derive(Debug, Clone)]
pub enum Popup {
    AddTask,
    EditTask(u64),
    ConfirmDelete(u64),
    EmptyQueueChoice,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputField {
    Title,
    Notes,
    Estimate,
    Priority,
    DueDate,
    Tags,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskFilter {
    All,
    Pending,
    Done,
    Today,
}

impl TaskFilter {
    pub fn label(&self) -> &'static str {
        match self {
            TaskFilter::All => "All",
            TaskFilter::Pending => "Open",
            TaskFilter::Done => "Done",
            TaskFilter::Today => "Today",
        }
    }

    pub fn next(self) -> Self {
        match self {
            TaskFilter::All => TaskFilter::Pending,
            TaskFilter::Pending => TaskFilter::Done,
            TaskFilter::Done => TaskFilter::Today,
            TaskFilter::Today => TaskFilter::All,
        }
    }
}

use crate::ui::{IconMode, IconSet};

pub mod theme;
pub use theme::*;

pub mod settings;
pub use settings::*;
pub mod keys;
pub mod popups;
pub mod task_ops;
pub mod timer_ops;

pub struct App {
    pub db: Database,
    pub data: AppData,
    pub timer: Timer,
    pub tab: FocusTab,
    pub input_mode: InputMode,
    pub input_buffer: String,
    pub input_buffer2: String,
    pub input_due_date: String,
    pub input_tags: String,
    pub input_number: u32,
    pub input_priority: Priority,
    pub input_field: InputField,
    pub popup: Option<Popup>,
    pub task_state: ListState,
    pub settings_state: SettingsState,
    pub status: Option<String>,
    pub status_error: bool,
    pub last_status_set: Instant,
    pub should_quit: bool,
    pub theme: Theme,
    pub icons: IconSet,
    pub active_task: Option<u64>,
    pub zen_mode: bool,
    pub task_filter: TaskFilter,
    pub active_tag_filter: Option<String>,
    pub task_search: String,
    pub searching: bool,
    pub weekly_chart: Vec<(String, u32)>,
    pub heatmap_data: Vec<(String, u32)>,
    pub session_counts: (u32, u32, u32),
    pub chart_dirty: bool,
    pub data_version: u64,
    pub recent_sessions: Vec<StoredSession>,
    pub stats_session_selected: usize,
    pub dashboard_task_selected: usize,
    pub calendar_date: chrono::NaiveDate,
}

impl App {
    pub fn new() -> Result<Self> {
        let db = Database::open()?;
        let mut data = db.load_app_data().unwrap_or_default();
        let _ = storage::ensure_today_reset(&db, &mut data);
        let config = TimerConfig::from_app_data(&data);
        let mut timer = Timer::new(config);
        let (completed, mode) = db.load_timer_state();
        timer.completed_focus_sessions = completed;
        timer.configure(mode);
        let recent_sessions = db.recent_sessions(15).unwrap_or_default();
        let mut task_state = ListState::default();
        if !data.tasks.is_empty() {
            task_state.select(Some(0));
        }
        let weekly_chart = storage::minutes_by_date(&db, 7).unwrap_or_default();
        let heatmap_data = storage::focus_heatmap(&db).unwrap_or_default();
        let session_counts = db.session_counts_by_mode().unwrap_or((0, 0, 0));
        let theme = Theme::from_variant(data.theme);
        let icons = IconSet::detect();
        let active_task = data.active_task_id.filter(|id| {
            data.tasks
                .iter()
                .find(|t| t.id == *id)
                .is_some_and(|t| t.status != crate::model::TaskStatus::Done)
        });
        if active_task != data.active_task_id {
            data.active_task_id = active_task;
        }
        let welcome = match icons.mode() {
            IconMode::Ascii => {
                "Welcome to Void! Using text icons (set VOID_USE_NERD_FONTS=1 if your terminal has a Nerd Font)."
            }
            IconMode::Nerd => "Welcome to Void! Press 5 or 'h' for help.",
        };
        Ok(Self {
            db,
            data,
            timer,
            tab: FocusTab::Dashboard,
            input_mode: InputMode::Normal,
            input_buffer: String::new(),
            input_buffer2: String::new(),
            input_due_date: String::new(),
            input_tags: String::new(),
            input_number: 25,
            input_priority: Priority::Medium,
            input_field: InputField::Title,
            popup: None,
            task_state,
            settings_state: SettingsState::new(),
            status: Some(welcome.into()),
            status_error: false,
            last_status_set: Instant::now(),
            should_quit: false,
            theme,
            icons,
            active_task,
            zen_mode: false,
            task_filter: TaskFilter::All,
            active_tag_filter: None,
            task_search: String::new(),
            searching: false,
            weekly_chart,
            heatmap_data,
            session_counts,
            chart_dirty: false,
            data_version: 0,
            recent_sessions,
            stats_session_selected: 0,
            dashboard_task_selected: 0,
            calendar_date: chrono::Local::now().date_naive(),
        })
    }

    pub fn queue_empty(&self) -> bool {
        storage::queue_empty(&self.data)
    }

    pub fn daily_goal_met(&self) -> bool {
        storage::today_focus_minutes(&self.data) >= self.data.daily_goal_minutes
    }

    fn persist_timer_state(&mut self) {
        let completed = self.timer.completed_focus_sessions;
        let mode = self.timer.mode;
        self.persist(|db| db.persist_timer_state(completed, mode));
    }

    fn refresh_recent_sessions(&mut self) {
        match self.db.recent_sessions(15) {
            Ok(sessions) => self.recent_sessions = sessions,
            Err(e) => self.set_status(format!("Error loading sessions: {e}"), true),
        }
        if self.stats_session_selected >= self.recent_sessions.len() {
            self.stats_session_selected = self.recent_sessions.len().saturating_sub(1);
        }
    }

    pub fn tick_rate(&self) -> Duration {
        match self.timer.state {
            TimerState::Running => Duration::from_millis(50),
            TimerState::Paused => Duration::from_millis(100),
            TimerState::Idle => Duration::from_millis(100),
            _ => Duration::from_millis(200),
        }
    }

    pub fn window_title(&self) -> String {
        let (main, tenths, _) = crate::canvas_timer::format_time_stack(&self.timer);
        let state = match self.timer.state {
            TimerState::Running => self.icons.play,
            TimerState::Paused => self.icons.pause,
            TimerState::Finished => self.icons.check,
            TimerState::Idle => self.icons.idle,
        };
        format!(
            "Void {} {}{} · {}",
            state,
            main,
            tenths,
            self.timer.mode.label()
        )
    }

    pub fn bump_data(&mut self) {
        self.data_version = self.data_version.wrapping_add(1);
        self.chart_dirty = true;
        self.refresh_recent_sessions();
        self.clamp_dashboard_task_selection();
    }

    fn persist<F>(&mut self, op: F)
    where
        F: FnOnce(&Database) -> anyhow::Result<()>,
    {
        if let Err(e) = op(&self.db) {
            self.set_status(format!("Save error: {e}"), true);
        }
    }

    fn persist_data<F>(&mut self, op: F)
    where
        F: FnOnce(&Database, &mut AppData) -> anyhow::Result<()>,
    {
        if let Err(e) = op(&self.db, &mut self.data) {
            self.set_status(format!("Save error: {e}"), true);
        }
    }

    fn persist_setting(&mut self, key: &str, value: impl AsRef<str>) {
        self.persist(|db| db.set_setting(key, value.as_ref()));
    }

    pub fn refresh_chart_if_needed(&mut self) {
        if self.chart_dirty {
            match storage::minutes_by_date(&self.db, 7) {
                Ok(data) => self.weekly_chart = data,
                Err(e) => self.set_status(format!("Chart error: {e}"), true),
            }
            match storage::focus_heatmap(&self.db) {
                Ok(data) => self.heatmap_data = data,
                Err(e) => self.set_status(format!("Heatmap error: {e}"), true),
            }
            match self.db.session_counts_by_mode() {
                Ok(counts) => self.session_counts = counts,
                Err(e) => self.set_status(format!("Stats error: {e}"), true),
            }
            self.chart_dirty = false;
        }
    }

    pub fn reload_heatmap(&mut self) {
        if let Ok(data) = storage::focus_heatmap(&self.db) {
            self.heatmap_data = data;
        }
    }

    fn sync_timer_config_to_data(&mut self) {
        self.data.focus_minutes = self.timer.config.focus_minutes;
        self.data.short_break_minutes = self.timer.config.short_break_minutes;
        self.data.long_break_minutes = self.timer.config.long_break_minutes;
        self.data.long_break_every = self.timer.config.long_break_every;
    }

    fn elapsed_minutes(&self, skipped: bool) -> u32 {
        let secs = self.timer.current_elapsed_seconds();
        if skipped {
            secs.div_ceil(60).max(1)
        } else {
            (secs / 60).max(1)
        }
    }

    pub fn hint(&self) -> String {
        match self.tab {
            FocusTab::Dashboard => "[j/k]tasks [f]ocus [Enter]status [x]done [s]tart [z]zen".into(),
            FocusTab::Tasks => {
                "[f]ocus [a]dd [e]dit [d]el [Enter]status [t]oday [g]filter [/]search".into()
            }
            FocusTab::Stats => "[j/k] sessions [d] delete [+/-] minutes [E] end session".into(),
            FocusTab::Settings => "[↑↓] nav [Enter] toggle/export [-/+] change values".into(),
            FocusTab::Help => "Press Tab or 1-5 to leave help".into(),
        }
    }

    pub fn set_status(&mut self, msg: impl Into<String>, error: bool) {
        self.status = Some(msg.into());
        self.status_error = error;
        self.last_status_set = Instant::now();
    }

    fn check_queue_empty(&mut self) {
        if self.queue_empty() {
            self.on_queue_empty();
        }
    }

    fn on_queue_empty(&mut self) {
        match self.data.empty_queue_behavior {
            EmptyQueueBehavior::FreeFocus => {
                self.set_status(
                    "All tasks done — free focus. Sessions log as general focus. [E] end session",
                    false,
                );
            }
            EmptyQueueBehavior::PauseTimer => {
                if self.timer.state == TimerState::Running {
                    self.pause_timer();
                } else if self.timer.state != TimerState::Paused {
                    self.timer.reset();
                }
                self.set_status("All tasks done — timer paused. [E] end session", false);
            }
            EmptyQueueBehavior::AskEachTime => {
                self.popup = Some(Popup::EmptyQueueChoice);
                self.set_status(
                    "All tasks done — [Enter] free focus  [p] pause  [a] add task",
                    false,
                );
            }
        }
    }

    pub fn export_backup(&mut self) {
        match self.db.export_json() {
            Ok(path) => self.set_status(format!("Exported backup to {}", path.display()), false),
            Err(e) => self.set_status(format!("Export failed: {e}"), true),
        }
    }

    pub fn open_add_task(&mut self) {
        self.input_buffer.clear();
        self.input_buffer2.clear();
        self.input_due_date.clear();
        self.input_tags.clear();
        self.input_number = 25;
        self.input_priority = Priority::Medium;
        self.input_field = InputField::Title;
        self.popup = Some(Popup::AddTask);
        self.input_mode = InputMode::Editing;
    }

    pub fn open_edit_task(&mut self) {
        let Some(id) = self.selected_task_id() else {
            self.set_status("No task selected.", true);
            return;
        };
        if let Some(t) = self.data.tasks.iter().find(|t| t.id == id).cloned() {
            self.input_buffer = t.title;
            self.input_buffer2 = t.notes;
            self.input_due_date = t.due_date.unwrap_or_default();
            self.input_tags = t.tags.join(", ");
            self.input_number = t.estimated_minutes;
            self.input_priority = t.priority;
            self.input_field = InputField::Title;
            self.popup = Some(Popup::EditTask(id));
            self.input_mode = InputMode::Editing;
        }
    }

    pub fn open_confirm_delete(&mut self) {
        if let Some(id) = self.selected_task_id() {
            self.popup = Some(Popup::ConfirmDelete(id));
        } else {
            self.set_status("No task to delete.", true);
        }
    }

    fn popup_due_date(&self) -> Result<Option<String>, String> {
        let allow_past = matches!(self.popup, Some(crate::app::Popup::EditTask(_)));
        storage::normalize_due_date(&self.input_due_date, allow_past)
    }

    pub fn cycle_tag_filter(&mut self) {
        let mut tags: Vec<String> = self
            .data
            .tasks
            .iter()
            .flat_map(|t| t.tags.clone())
            .collect();
        tags.sort();
        tags.dedup();

        if tags.is_empty() {
            self.set_status("No tags available to filter.", true);
            return;
        }

        self.active_tag_filter = match &self.active_tag_filter {
            None => Some(tags[0].clone()),
            Some(current) => {
                if let Some(idx) = tags.iter().position(|t| t == current) {
                    if idx + 1 < tags.len() {
                        Some(tags[idx + 1].clone())
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
        };

        let msg = match &self.active_tag_filter {
            Some(t) => format!("Filtered by tag: #{}", t),
            None => "Tag filter cleared.".to_string(),
        };
        self.set_status(msg, false);

        self.clamp_dashboard_task_selection();
        let len = self.filtered_task_indices().len();
        if len == 0 {
            self.task_state.select(None);
        } else {
            let sel = self.task_state.selected().unwrap_or(0).min(len - 1);
            self.task_state.select(Some(sel));
        }
    }

    fn popup_tags(&self) -> Vec<String> {
        storage::parse_tags(&self.input_tags)
    }

    pub fn selected_task_id(&self) -> Option<u64> {
        let indices = self.filtered_task_indices();
        self.task_state
            .selected()
            .and_then(|i| indices.get(i).copied())
            .and_then(|idx| self.data.tasks.get(idx).map(|t| t.id))
    }
}