trackWork 0.15.0

A terminal-based time tracking application for managing work sessions
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use ratatui::{
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph},
    Frame,
};

use crate::app::{App, InputMode};
use crate::config::Config;
use crate::integrations::IntegrationKind;

/// Return the label style: Yellow+Bold when selected, Gray otherwise
fn label_style(is_selected: bool) -> Style {
    if is_selected {
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(Color::Gray)
    }
}

/// Parse color name string to ratatui Color
pub fn string_to_color(color_str: &str) -> Color {
    match color_str {
        "Blue" => Color::Blue,
        "Cyan" => Color::Cyan,
        "Green" => Color::Green,
        "Yellow" => Color::Yellow,
        "Magenta" => Color::Magenta,
        "Red" => Color::Red,
        "LightBlue" => Color::LightBlue,
        "LightCyan" => Color::LightCyan,
        "LightGreen" => Color::LightGreen,
        "LightYellow" => Color::LightYellow,
        "LightMagenta" => Color::LightMagenta,
        "LightRed" => Color::LightRed,
        "DarkGray" => Color::DarkGray,
        "Gray" => Color::Gray,
        "White" => Color::White,
        "Black" => Color::Black,
        _ => Color::White,
    }
}

pub fn draw_settings(f: &mut Frame, app: &App) {
    if let InputMode::Settings {
        integration,
        open_command,
        open_worklog_command,
        jira_url_setting,
        jira_email,
        jira_api_token,
        date_format,
        legacy_time_format,
        hide_eye_candy,
        colors,
        current_field,
        cursor_pos,
        debug_log_scroll_offset,
        ..
    } = &app.input_mode
    {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3),  // Header
                Constraint::Min(10),   // Settings form
                Constraint::Length(10), // Debug log
                Constraint::Length(3),  // Footer / Help
            ])
            .split(f.area());

        // Header
        let header_text = vec![Line::from(vec![Span::styled(
            "⚙ Settings",
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )])];
        let header = Paragraph::new(header_text).block(Block::default().borders(Borders::ALL));
        f.render_widget(header, chunks[0]);

        // Settings form
        let mut lines = vec![];

        // Field 0: Integration selector
        let integration_style = if *current_field == 0 {
            Style::default().bg(Color::DarkGray).fg(Color::White)
        } else {
            Style::default()
        };
        let mut integration_spans = vec![
            Span::styled("  Integration: ", label_style(*current_field == 0)),
        ];
        if *current_field == 0 {
            integration_spans.push(Span::styled(
                "",
                Style::default()
                    .fg(Color::White)
                    .add_modifier(Modifier::BOLD),
            ));
        } else {
            integration_spans.push(Span::raw("  "));
        }
        integration_spans.push(Span::styled(
            integration.display_name(),
            integration_style,
        ));
        if *current_field == 0 {
            integration_spans.push(Span::styled(
                "",
                Style::default()
                    .fg(Color::White)
                    .add_modifier(Modifier::BOLD),
            ));
        }
        lines.push(Line::from(integration_spans));
        lines.push(Line::from(""));

        // Render integration-specific fields
        match integration {
            IntegrationKind::CustomCommands => {
                draw_custom_commands_fields(
                    &mut lines,
                    open_command,
                    open_worklog_command,
                    date_format,
                    *legacy_time_format,
                    *hide_eye_candy,
                    colors,
                    *current_field,
                    *cursor_pos,
                );
            }
            IntegrationKind::Jira => {
                draw_jira_fields(
                    &mut lines,
                    jira_url_setting,
                    jira_email,
                    jira_api_token,
                    date_format,
                    *legacy_time_format,
                    *hide_eye_candy,
                    colors,
                    *current_field,
                    *cursor_pos,
                );
            }
        }

        let settings_form = Paragraph::new(lines).block(
            Block::default()
                .borders(Borders::ALL)
                .title("Configuration"),
        );
        f.render_widget(settings_form, chunks[1]);

        // Debug log with scrolling
        let log_height = chunks[2].height.saturating_sub(2) as usize;
        let total_logs = app.debug_log.len();
        let scroll_offset = *debug_log_scroll_offset;

        let log_lines: Vec<Line> = app.debug_log
            .iter()
            .rev()
            .skip(scroll_offset)
            .take(log_height)
            .map(|entry| {
                let (prefix, rest) = if let Some(idx) = entry.find(']') {
                    entry.split_at(idx + 1)
                } else {
                    ("", entry.as_str())
                };

                let color = if prefix.contains("ERROR") {
                    Color::Red
                } else if prefix.contains("JIRA") {
                    Color::Blue
                } else if prefix.contains("LOG WORK") {
                    Color::Cyan
                } else if prefix.contains("OPEN ISSUE") {
                    Color::Magenta
                } else if prefix.contains("CLIPBOARD") {
                    Color::Yellow
                } else {
                    Color::Green
                };

                Line::from(vec![
                    Span::styled(prefix, Style::default().fg(color).add_modifier(Modifier::BOLD)),
                    Span::styled(rest, Style::default().fg(Color::Gray)),
                ])
            })
            .collect();

        let title = if total_logs > log_height {
            format!("Debug Log ({}/{} - Shift+↑/↓ or Ctrl+↑/↓ to scroll)",
                    scroll_offset + 1.min(total_logs),
                    total_logs)
        } else {
            format!("Debug Log ({} entries)", total_logs)
        };

        let debug_log = Paragraph::new(log_lines).block(
            Block::default()
                .borders(Borders::ALL)
                .title(title),
        );
        f.render_widget(debug_log, chunks[2]);

        // Footer
        let help_text = vec![
            Line::from(vec![
                Span::styled("↑/↓/Tab", Style::default().fg(Color::Cyan)),
                Span::raw(": Navigate  "),
                Span::styled("←/→", Style::default().fg(Color::Cyan)),
                Span::raw(": Change Color  "),
                Span::styled("Enter", Style::default().fg(Color::Green)),
                Span::raw(": Save  "),
                Span::styled("Esc", Style::default().fg(Color::Red)),
                Span::raw(": Cancel"),
            ]),
        ];
        let footer = Paragraph::new(help_text).block(Block::default().borders(Borders::ALL));
        f.render_widget(footer, chunks[3]);
    }
}

fn draw_custom_commands_fields(
    lines: &mut Vec<Line<'_>>,
    open_command: &str,
    open_worklog_command: &str,
    date_format: &str,
    legacy_time_format: bool,
    hide_eye_candy: bool,
    colors: &[String; 6],
    current_field: usize,
    cursor_pos: usize,
) {
    use crate::cursor::render_with_cursor;

    // Field 1: Log Work Command
    let is_cmd = current_field == 1;
    let open_command_style = if is_cmd {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    lines.push(Line::from(vec![
        Span::styled("  Log Work Command (l): ", label_style(is_cmd)),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(render_with_cursor(open_command, cursor_pos, is_cmd), open_command_style),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "Variables: [[issue_key]] [[entry_started]] [[entry_ended]] [[task_duration]]",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "Press Ctrl++ to insert variable",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
    lines.push(Line::from(""));

    // Field 2: Open Issue Command
    let is_wl = current_field == 2;
    let open_worklog_command_style = if is_wl {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    lines.push(Line::from(vec![
        Span::styled("  Open Issue Command (o): ", label_style(is_wl)),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(render_with_cursor(open_worklog_command, cursor_pos, is_wl), open_worklog_command_style),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "Variables: [[issue_key]] [[entry_started]] [[entry_ended]] [[task_duration]]",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "Press Ctrl++ to insert variable",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
    lines.push(Line::from(""));

    // Field 3: Date Format
    draw_date_format_field(lines, date_format, current_field == 3, cursor_pos);
    lines.push(Line::from(""));

    // Field 4: Legacy Time Format
    draw_legacy_time_format_field(lines, legacy_time_format, current_field == 4);
    lines.push(Line::from(""));

    // Field 5: Change Passphrase button
    draw_passphrase_button(lines, current_field == 5);
    lines.push(Line::from(""));

    // Field 6: Triggers button
    draw_triggers_button(lines, current_field == 6);
    lines.push(Line::from(""));

    // Field 7: Hide Eye Candy toggle
    draw_hide_eye_candy_field(lines, hide_eye_candy, current_field == 7);
    lines.push(Line::from(""));

    // Fields 8-13: Colors
    draw_color_fields(lines, colors, 8, current_field);
}

fn draw_jira_fields(
    lines: &mut Vec<Line<'_>>,
    jira_url_setting: &str,
    jira_email: &str,
    jira_api_token: &str,
    date_format: &str,
    legacy_time_format: bool,
    hide_eye_candy: bool,
    colors: &[String; 6],
    current_field: usize,
    cursor_pos: usize,
) {
    use crate::cursor::render_with_cursor;

    // Field 1: Jira URL
    let is_url = current_field == 1;
    let jira_url_style = if is_url {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    lines.push(Line::from(vec![
        Span::styled("  Jira URL: ", label_style(is_url)),
        Span::styled(render_with_cursor(jira_url_setting, cursor_pos, is_url), jira_url_style),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "e.g., https://yourcompany.atlassian.net",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
    lines.push(Line::from(""));

    // Field 2: Jira Email
    let is_email = current_field == 2;
    let jira_email_style = if is_email {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    lines.push(Line::from(vec![
        Span::styled("  Jira Email: ", label_style(is_email)),
        Span::styled(render_with_cursor(jira_email, cursor_pos, is_email), jira_email_style),
    ]));
    lines.push(Line::from(""));

    // Field 3: API Token (masked)
    let is_token = current_field == 3;
    let token_style = if is_token {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    let masked_token = if jira_api_token.is_empty() && !is_token {
        "(not set)".to_string()
    } else if is_token {
        // Show masked with cursor
        let masked = "*".repeat(jira_api_token.chars().count());
        render_with_cursor(&masked, cursor_pos, true)
    } else {
        "*".repeat(jira_api_token.chars().count().min(20))
    };
    lines.push(Line::from(vec![
        Span::styled("  API Token: ", label_style(is_token)),
        Span::styled(masked_token, token_style),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "Generate at: https://id.atlassian.net/manage-profile/security/api-tokens",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
    lines.push(Line::from(""));

    // Field 4: Date Format
    draw_date_format_field(lines, date_format, current_field == 4, cursor_pos);
    lines.push(Line::from(""));

    // Field 5: Legacy Time Format
    draw_legacy_time_format_field(lines, legacy_time_format, current_field == 5);
    lines.push(Line::from(""));

    // Field 6: Change Passphrase button
    draw_passphrase_button(lines, current_field == 6);
    lines.push(Line::from(""));

    // Field 7: Triggers button
    draw_triggers_button(lines, current_field == 7);
    lines.push(Line::from(""));

    // Field 8: Hide Eye Candy toggle
    draw_hide_eye_candy_field(lines, hide_eye_candy, current_field == 8);
    lines.push(Line::from(""));

    // Fields 9-14: Colors
    draw_color_fields(lines, colors, 9, current_field);
}

fn draw_date_format_field(lines: &mut Vec<Line<'_>>, date_format: &str, is_selected: bool, cursor_pos: usize) {
    use crate::cursor::render_with_cursor;
    let style = if is_selected {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    lines.push(Line::from(vec![
        Span::styled("  Date Format: ", label_style(is_selected)),
        Span::styled(render_with_cursor(date_format, cursor_pos, is_selected), style),
        Span::styled(" (e.g., %d.%m.-%y)", Style::default().fg(Color::DarkGray)),
    ]));
}

fn draw_legacy_time_format_field(lines: &mut Vec<Line<'_>>, legacy_time_format: bool, is_selected: bool) {
    let style = if is_selected {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    let value = if legacy_time_format { "Yes" } else { "No" };
    lines.push(Line::from(vec![
        Span::styled("  Legacy Time Format: ", label_style(is_selected)),
        Span::styled(value, style),
        Span::styled(" (←/→ or Space to toggle)", Style::default().fg(Color::DarkGray)),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "Format: 2025-11-06T14:25:00.000+0000 (no colon in timezone)",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
}

fn draw_hide_eye_candy_field(lines: &mut Vec<Line<'_>>, hide_eye_candy: bool, is_selected: bool) {
    let style = if is_selected {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    };
    let value = if hide_eye_candy { "Yes" } else { "No" };
    lines.push(Line::from(vec![
        Span::styled("  Hide Eye Candy: ", label_style(is_selected)),
        Span::styled(value, style),
        Span::styled(" (←/→ or Space to toggle)", Style::default().fg(Color::DarkGray)),
    ]));
    lines.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            "Turn off the shimmer and breathe animations on the main view.",
            Style::default().fg(Color::DarkGray),
        ),
    ]));
}

fn draw_passphrase_button(lines: &mut Vec<Line<'_>>, is_selected: bool) {
    let style = if is_selected {
        Style::default()
            .bg(Color::DarkGray)
            .fg(Color::White)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default()
    };
    lines.push(Line::from(vec![
        Span::styled("  Passphrase: ", label_style(is_selected)),
        Span::styled("Change Passphrase [Enter]", style),
    ]));
}

fn draw_triggers_button(lines: &mut Vec<Line<'_>>, is_selected: bool) {
    let style = if is_selected {
        Style::default()
            .bg(Color::DarkGray)
            .fg(Color::White)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default()
    };
    lines.push(Line::from(vec![
        Span::styled("  Triggers: ", label_style(is_selected)),
        Span::styled("Configure Event Webhooks [Enter]", style),
    ]));
}

fn draw_color_fields(
    lines: &mut Vec<Line<'_>>,
    colors: &[String; 6],
    colors_start: usize,
    current_field: usize,
) {
    lines.push(Line::from(Span::styled(
        "  Color Palette:",
        Style::default()
            .fg(Color::Cyan)
            .add_modifier(Modifier::BOLD),
    )));
    lines.push(Line::from(""));

    for (idx, color_name) in Config::color_names().iter().enumerate() {
        let field_idx = colors_start + idx;
        let is_selected = current_field == field_idx;

        let color_value = &colors[idx];
        let actual_color = string_to_color(color_value);

        let name_style = if is_selected {
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::Gray)
        };
        let mut spans = vec![
            Span::raw("    "),
            Span::styled(format!("{:<10}", color_name), name_style),
            Span::raw(" "),
        ];

        if is_selected {
            spans.push(Span::styled(
                "",
                Style::default()
                    .fg(Color::White)
                    .add_modifier(Modifier::BOLD),
            ));
        } else {
            spans.push(Span::raw("  "));
        }

        spans.push(Span::styled(
            "███",
            Style::default()
                .fg(actual_color)
                .add_modifier(Modifier::BOLD),
        ));
        spans.push(Span::raw(" "));

        let value_style = if is_selected {
            Style::default()
                .bg(Color::DarkGray)
                .fg(Color::White)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(actual_color)
        };
        spans.push(Span::styled(format!("{:<15}", color_value), value_style));

        if is_selected {
            spans.push(Span::styled(
                "",
                Style::default()
                    .fg(Color::White)
                    .add_modifier(Modifier::BOLD),
            ));
        }

        lines.push(Line::from(spans));
    }
}