kasl-cli 1.0.1

kasl is a comprehensive command-line utility 🛠️ designed to streamline the tracking of work activities 📊, including start times ⏰, pauses ⏸, and task completion
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Console display and table formatting system.
//!
//! Provides interface for rendering application data in well-formatted console tables.
//! Handles presentation layer for work reports, task lists, summaries, templates, and tags.
//!
//! ## Features
//!
//! - **Structured Data Display**: Converts complex data structures into readable tables
//! - **Consistent Formatting**: Maintains uniform appearance across all table types
//! - **Report Visualization**: Displays pre-calculated productivity and work metrics
//! - **Duration Formatting**: Handles time duration display in human-readable formats
//!
//! ## Usage
//!
//! ```rust,no_run
//! # fn f() -> anyhow::Result<()> {
//! use kasl::libs::view::View;
//! use kasl::libs::task::Task;
//! use kasl::db::workdays::Workday;
//! use kasl::libs::report::WorkInterval;
//! use chrono::{Local, TimeDelta};
//! use std::collections::HashMap;
//!
//! let tasks: Vec<Task> = vec![];
//! let workday = Workday {
//!     id: 1,
//!     date: Local::now().date_naive(),
//!     start: Local::now().naive_local(),
//!     end: None,
//! };
//! let intervals: Vec<WorkInterval> = vec![];
//! let filtered_duration = TimeDelta::zero();
//! let productivity = 0.0_f64;
//! let summary_data = (HashMap::new(), String::new(), String::new());
//!
//! View::tasks(&tasks)?;
//! View::report(&workday, &intervals, &filtered_duration, &productivity, &tasks)?;
//! View::sum(&summary_data)?;
//! # Ok(())
//! # }
//! ```

use super::task::Task;
use crate::db::templates::TaskTemplate;
use crate::db::workdays::Workday;
use crate::libs::formatter::{format_duration, terminal_cols, truncate_to_width};
use crate::libs::messages::Message;
use crate::libs::pause::Pause;
use crate::libs::report;
use crate::msg_print;
use anyhow::Result;
use chrono::{Duration, NaiveDate, TimeDelta};
use prettytable::{Cell, Row, Table, format, row};
use std::collections::HashMap;
use unicode_width::UnicodeWidthStr;

/// A utility struct for rendering application data to the console.
///
/// Serves as a namespace for various table rendering functions. All methods are static,
/// making it easy to call formatting functions without needing to instantiate the struct.
pub struct View {}

impl View {
    /// Displays a formatted table of tasks with comprehensive metadata.
    ///
    /// Renders a detailed table showing task information including identification numbers,
    /// names, completion status, comments, and associated tags.
    ///
    /// # Arguments
    ///
    /// * `tasks` - A slice of `Task` structs to display in the table
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on successful table rendering, or an error if
    /// the table cannot be displayed due to terminal or formatting issues.
    pub fn tasks(tasks: &[Task]) -> Result<()> {
        let show_task_id = tasks.iter().any(|t| t.task_id.is_some_and(|id| id != 0));
        let show_comment = tasks.iter().any(|t| !t.comment.trim().is_empty());
        let show_tags = tasks.iter().any(|t| !t.tags.is_empty());

        let idx_width = tasks.len().to_string().width().max("#".width());
        let id_width = tasks.iter().map(|t| t.id.unwrap_or(0).to_string().width()).max().unwrap_or(1).max("ID".width());
        let task_id_width = if show_task_id {
            tasks
                .iter()
                .map(|t| t.task_id.unwrap_or(0).to_string().width())
                .max()
                .unwrap_or(1)
                .max("TASK ID".width())
        } else {
            0
        };
        let done_width = "DONE".width().max("100%".width());

        // prettytable cell format: `| content |` → 3 chars overhead per column + 1 outer border
        let mut num_cols = 4; // #, ID, NAME, DONE
        if show_task_id {
            num_cols += 1;
        }
        if show_comment {
            num_cols += 1;
        }
        if show_tags {
            num_cols += 1;
        }

        let mut fixed_content = idx_width + id_width + done_width;
        if show_task_id {
            fixed_content += task_id_width;
        }

        let frame_overhead = 3 * num_cols + 1;
        let mut flexible = terminal_cols().saturating_sub(frame_overhead + fixed_content);

        // Reserve a modest slice for optional text columns; NAME gets the rest.
        let tags_width = if show_tags {
            let width = (flexible / 5).clamp(8, 20);
            flexible = flexible.saturating_sub(width);
            width
        } else {
            0
        };
        let comment_width = if show_comment {
            let width = (flexible / 3).clamp(12, 40);
            flexible = flexible.saturating_sub(width);
            width
        } else {
            0
        };
        let name_width = flexible.max(12);

        let mut table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);

        let mut titles = vec![Cell::new("#"), Cell::new("ID")];
        if show_task_id {
            titles.push(Cell::new("TASK ID"));
        }
        titles.push(Cell::new("NAME"));
        if show_comment {
            titles.push(Cell::new("COMMENT"));
        }
        titles.push(Cell::new("DONE"));
        if show_tags {
            titles.push(Cell::new("TAGS"));
        }
        table.set_titles(Row::new(titles));

        for (index, task) in tasks.iter().enumerate() {
            let mut cells = vec![Cell::new(&(index + 1).to_string()), Cell::new(&task.id.unwrap_or(0).to_string())];
            if show_task_id {
                cells.push(Cell::new(&task.task_id.unwrap_or(0).to_string()));
            }
            cells.push(Cell::new(&truncate_to_width(&task.name, name_width)));
            if show_comment {
                cells.push(Cell::new(&truncate_to_width(task.comment.trim(), comment_width)));
            }
            cells.push(Cell::new(&format!("{}%", task.completeness.unwrap_or(100))));
            if show_tags {
                let tags_str = task.tags.iter().map(|t| t.name.as_str()).collect::<Vec<_>>().join(", ");
                cells.push(Cell::new(&truncate_to_width(&tags_str, tags_width)));
            }
            table.add_row(Row::new(cells));
        }

        table.printstd();
        Ok(())
    }

    /// Displays a formatted daily work report using pre-calculated intervals.
    ///
    /// This method displays the core report data in a structured table format,
    /// including work intervals, total time, and productivity metrics calculated
    /// using the centralized Productivity module.
    ///
    /// ## Display Components
    ///
    /// 1. **Work Intervals**: Detailed breakdown of focused work periods
    /// 2. **Total Duration**: Sum of all work intervals (may be filtered)
    /// 3. **Productivity Percentage**: Calculated using comprehensive Productivity logic
    /// 4. **Associated Tasks**: Tasks completed during the workday for context
    ///
    /// The productivity value displayed here is calculated using the same centralized
    /// logic used throughout the application for consistency.
    ///
    /// # Arguments
    ///
    /// * `workday` - The workday record containing start/end times
    /// * `intervals` - Pre-calculated and optionally filtered work intervals for display
    /// * `filtered_duration` - Sum of displayed interval durations
    /// * `productivity` - Productivity percentage from centralized Productivity calculation
    /// * `tasks` - Tasks completed during the workday for context
    pub fn report(workday: &Workday, intervals: &[report::WorkInterval], filtered_duration: &TimeDelta, productivity: &f64, tasks: &[Task]) -> Result<()> {
        // Display formatted report header with readable date
        msg_print!(Message::ReportHeader(workday.date.format("%B %-d, %Y").to_string()), true);

        // Create and populate the work intervals table
        let mut table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
        table.set_titles(row!["ID", "START", "END", "DURATION"]);

        // Add each work interval as a table row with formatted times
        for (index, interval) in intervals.iter().enumerate() {
            table.add_row(row![
                index + 1,                           // Sequential numbering for easy reference
                interval.start.format("%H:%M"),      // Start time in HH:MM format
                interval.end.format("%H:%M"),        // End time in HH:MM format
                format_duration(&interval.duration)  // Human-readable duration
            ]);
        }

        // Add summary rows with total time and productivity metrics
        table.add_empty_row(); // Visual separator before summary
        table.add_row(row!["TOTAL", "", "", format_duration(filtered_duration)]);
        table.add_row(row!["PRODUCTIVITY", "", "", format!("{:.1}%", productivity)]);

        // Render the intervals table to console
        table.printstd();

        // Display associated tasks if any were completed during the day
        if !tasks.is_empty() {
            msg_print!(Message::TasksHeader, true);
            Self::tasks(tasks)?;
        }

        Ok(())
    }

    /// Displays a monthly summary of working hours with daily breakdowns.
    ///
    /// This method renders a comprehensive monthly view that shows daily work
    /// patterns, totals, and averages. It provides both detailed daily data
    /// and aggregate statistics to help users understand their work patterns
    /// over the entire month.
    ///
    /// ## Summary Structure
    ///
    /// The monthly summary includes:
    /// - **Daily Breakdown**: Each day with date, hours worked, and workday status
    /// - **Total Hours**: Cumulative time worked across all days in the month
    /// - **Average Hours**: Mean daily working time for better pattern analysis
    /// - **Work Days**: Count of days with recorded work activity
    ///
    /// ## Data Interpretation
    ///
    /// - **Workday Hours**: Actual time recorded for productive work days
    /// - **Rest Day Hours**: Default hours applied to weekends and holidays
    /// - **Missing Days**: Days without any recorded activity (shown as 0:00)
    ///
    /// # Arguments
    ///
    /// * `summary_data` - A tuple containing:
    ///   - `HashMap<NaiveDate, (String, String)>`: Daily durations and productivity data
    ///   - `String`: Total duration for the entire month
    ///   - `String`: Average daily duration across all days
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on successful summary display, or an error if
    /// table formatting or rendering fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn f() -> anyhow::Result<()> {
    /// use kasl::libs::view::View;
    /// use std::collections::HashMap;
    ///
    /// let daily_map = HashMap::new();
    /// let total_hours = String::new();
    /// let average_hours = String::new();
    ///
    /// let summary_data = (daily_map, total_hours, average_hours);
    /// View::sum(&summary_data)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn sum((daily_durations, total_duration, average_duration): &(HashMap<NaiveDate, (String, String)>, String, String)) -> Result<()> {
        // Initialize table with appropriate formatting for summary data
        let mut table: Table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
        table.set_titles(row!["DATE", "HOURS", "PRODUCTIVITY"]);

        // Sort dates chronologically for logical display order
        let mut sorted_dates: Vec<&NaiveDate> = daily_durations.keys().collect();
        sorted_dates.sort();

        // Add each day's data as a table row
        for date in sorted_dates {
            if let Some((duration, productivity)) = daily_durations.get(date) {
                table.add_row(row![
                    date.format("%Y-%m-%d"), // ISO date format for consistency
                    duration,                // Formatted duration string
                    productivity             // Productivity percentage or status
                ]);
            }
        }

        // Add summary statistics with visual separation
        table.add_empty_row(); // Visual separator before totals
        table.add_row(row!["TOTAL", total_duration, ""]);
        table.add_row(row!["AVERAGE", average_duration, ""]);

        // Render the summary table to console
        table.printstd();
        Ok(())
    }

    /// Displays a table of pauses for a given day with total pause time.
    ///
    /// # Arguments
    /// * `pauses` - A slice of `Pause` records to display.
    /// * `total_pause_time` - The total duration of all pauses.
    ///
    /// # Returns
    /// A `Result` indicating success.
    pub fn pauses(pauses: &[Pause], total_pause_time: Duration) -> Result<()> {
        let mut table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
        table.set_titles(row!["ID", "START", "END", "DURATION"]);

        for (i, b) in pauses.iter().enumerate() {
            table.add_row(row![
                i + 1,
                b.start.format("%H:%M"),
                b.end.map(|t| t.format("%H:%M").to_string()).unwrap_or_else(|| "-".to_string()),
                b.duration
                    .map(|duration: TimeDelta| format_duration(&duration))
                    .unwrap_or_else(|| "--:--".to_string())
            ]);
        }

        // Add total row
        if !pauses.is_empty() {
            table.add_empty_row();
            table.add_row(row!["TOTAL", "", "", format_duration(&total_pause_time)]);
        }

        table.printstd();
        Ok(())
    }

    /// Displays a formatted table of task templates for reusable task creation.
    ///
    /// This method renders a comprehensive view of all available task templates,
    /// showing their configuration and usage information. Templates provide a
    /// convenient way to create commonly used tasks with pre-filled parameters.
    ///
    /// ## Template Information
    ///
    /// The table displays essential template metadata:
    /// - **Template Name**: Unique identifier for template selection
    /// - **Task Name**: Default task title that will be used
    /// - **Comment**: Pre-configured task description or notes
    /// - **Completeness**: Default completion percentage for new tasks
    ///
    /// ## Usage Context
    ///
    /// Templates are particularly useful for:
    /// - Recurring tasks with standard parameters
    /// - Team workflows with consistent task structures
    /// - Quick task creation with minimal input required
    /// - Standardized task naming and completion patterns
    ///
    /// # Arguments
    ///
    /// * `templates` - A slice of `TaskTemplate` structs to display
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on successful table rendering, or an error if
    /// display operations fail.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn f() -> anyhow::Result<()> {
    /// use kasl::libs::view::View;
    /// use kasl::db::templates::TaskTemplate;
    ///
    /// let templates: Vec<TaskTemplate> = vec![/* template instances */];
    /// View::templates(&templates)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn templates(templates: &[TaskTemplate]) -> Result<()> {
        // Initialize table with clean formatting for template data
        let mut table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
        table.set_titles(row!["TEMPLATE NAME", "TASK NAME", "COMMENT", "COMPLETENESS"]);

        // Populate table with template information
        for template in templates {
            table.add_row(row![
                template.name,                         // Unique template identifier
                template.task_name,                    // Default task title
                template.comment,                      // Pre-configured description
                format!("{}%", template.completeness)  // Default completion with % symbol
            ]);
        }

        // Render the templates table to console
        table.printstd();
        Ok(())
    }

    /// Displays a formatted table of tags for task categorization and organization.
    ///
    /// This method provides a comprehensive view of all available tags that can
    /// be applied to tasks for organization and filtering purposes. The table
    /// shows both the functional and visual aspects of each tag.
    ///
    /// ## Tag Information
    ///
    /// The table displays key tag metadata:
    /// - **ID**: Unique database identifier for programmatic reference
    /// - **NAME**: Human-readable tag name used for categorization
    /// - **COLOR**: Optional color coding for visual organization (if supported)
    ///
    /// ## Organizational Benefits
    ///
    /// Tags provide several organizational advantages:
    /// - **Categorization**: Group related tasks by project, priority, or type
    /// - **Filtering**: Quickly find tasks based on specific criteria
    /// - **Visual Organization**: Color coding for rapid visual identification
    /// - **Reporting**: Generate reports filtered by specific tag categories
    ///
    /// ## Color Display
    ///
    /// Colors are displayed as text values (hex codes, names, etc.) since
    /// terminal color support varies. A dash (-) indicates no color assigned.
    ///
    /// # Arguments
    ///
    /// * `tags` - A slice of `Tag` structs to display in the table
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on successful table rendering, or an error if
    /// display operations fail.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn f() -> anyhow::Result<()> {
    /// use kasl::libs::view::View;
    /// use kasl::db::tags::Tag;
    ///
    /// let tags: Vec<Tag> = vec![/* tag instances */];
    /// View::tags(&tags)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn tags(tags: &[crate::db::tags::Tag]) -> Result<()> {
        // Initialize table with appropriate formatting for tag data
        let mut table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
        table.set_titles(row!["ID", "NAME", "COLOR"]);

        // Populate table with tag information
        for tag in tags {
            table.add_row(row![
                tag.id.unwrap_or(0),                 // Database ID, showing 0 for new tags
                tag.name,                            // Human-readable tag name
                tag.color.as_deref().unwrap_or("-")  // Color value or dash if none
            ]);
        }

        // Render the tags table to console
        table.printstd();
        Ok(())
    }

    /// Displays active Jira inbox items (pinned, score, then priority).
    ///
    /// SUMMARY is truncated to fit the terminal width (same approach as [`View::tasks`]).
    pub fn jira_inbox(items: &[crate::db::jira_inbox::JiraInboxItem]) -> Result<()> {
        let pin_width = "".width().max(1);
        let score_width = items
            .iter()
            .map(|i| i.sort_value.map(|v| format!("{}", v).width()).unwrap_or_else(|| "".width()))
            .max()
            .unwrap_or(1)
            .max("SCORE".width());
        let priority_width = items
            .iter()
            .map(|i| i.priority.as_deref().unwrap_or("").width())
            .max()
            .unwrap_or(1)
            .max("PRIORITY".width());
        let key_width = items.iter().map(|i| i.issue_key.width()).max().unwrap_or(1).max("KEY".width());
        let status_width = items
            .iter()
            .map(|i| {
                if i.status_name.is_empty() {
                    i.status_id.as_deref().unwrap_or("").width()
                } else {
                    i.status_name.width()
                }
            })
            .max()
            .unwrap_or(1)
            .max("STATUS".width())
            .min(18);

        // "", SCORE, PRIORITY, KEY, STATUS, SUMMARY
        let num_cols = 6;
        let frame_overhead = 3 * num_cols + 1;
        let fixed = pin_width + score_width + priority_width + key_width + status_width;
        let summary_width = terminal_cols().saturating_sub(frame_overhead + fixed).max(12);

        let mut table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
        table.set_titles(row!["", "SCORE", "PRIORITY", "KEY", "STATUS", "SUMMARY"]);

        for item in items {
            let pin = if item.pinned { "" } else { "" };
            let score = item.sort_value.map(|v| format!("{}", v)).unwrap_or_else(|| "".to_string());
            let status_raw = if item.status_name.is_empty() {
                item.status_id.as_deref().unwrap_or("")
            } else {
                item.status_name.as_str()
            };
            table.add_row(row![
                pin,
                score,
                item.priority.as_deref().unwrap_or(""),
                item.issue_key,
                truncate_to_width(status_raw, status_width),
                truncate_to_width(&item.summary, summary_width),
            ]);
        }

        table.printstd();
        Ok(())
    }
}