oli-tui 0.1.3

A simple, blazingly fast TUI based AI coding assistant
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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
#![allow(clippy::needless_borrow)]

use crate::app::agent::AgentManager;
use crate::app::commands::CommandHandler;
use crate::app::models::ModelManager;
use crate::app::permissions::PermissionHandler;
use crate::app::state::{App, AppState};
use crate::app::utils::Scrollable;
use crate::ui::draw::ui;
use crate::ui::guards::TerminalGuard;
use crate::ui::messages::{initialize_setup_messages, process_message};
use anyhow::Result;
use crossterm::event::{Event, KeyCode, KeyModifiers};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::{io, sync::mpsc, time::Duration};
use tui_textarea::{Input, Key};

/// Main application run loop
pub fn run_app() -> Result<()> {
    // Initialize terminal
    let _guard = TerminalGuard::new()?;
    let mut terminal = Terminal::new(CrosstermBackend::new(io::stdout()))?;

    // Initialize application state
    let mut app = App::new();

    // Set up welcome messages
    initialize_setup_messages(&mut app);
    app.messages
        .push("DEBUG: Application started. Press Enter to begin setup.".into());

    // Create channel for events
    let (tx, rx) = mpsc::channel::<String>();

    // Initial UI draw
    terminal.draw(|f| ui(f, &mut app))?;

    // Track when we last redrew the screen to control framerate
    let mut last_redraw = std::time::Instant::now();
    let min_redraw_interval = std::time::Duration::from_millis(100);

    // Main event loop
    while app.state != AppState::Error("quit".into()) {
        // Process messages without forcing screen redraws
        process_channel_messages(&mut app, &rx, &mut terminal)?;
        process_agent_messages(&mut app, &mut terminal)?;
        process_auto_scroll(&mut app, &mut terminal)?;

        // Determine if we need to redraw based on application state
        let need_animation = app.agent_progress_rx.is_some()
            || app.permission_required
            || app.tool_execution_in_progress;

        // Throttle redraws to prevent flickering and allow scrolling to work
        let should_redraw = need_animation && last_redraw.elapsed() >= min_redraw_interval;

        // Only redraw at controlled intervals when animations are needed
        if should_redraw {
            terminal.draw(|f| ui(f, &mut app))?;
            last_redraw = std::time::Instant::now();
        }

        // Check for command mode before handling events
        if let AppState::Chat = app.state {
            if app.input.starts_with('/') {
                app.check_command_mode();
            }
        }

        // Process user input with short timeout to keep processing messages
        // This shorter poll timeout makes the UI more responsive during tool execution
        if crossterm::event::poll(Duration::from_millis(25))? {
            if let Event::Key(key) = crossterm::event::read()? {
                // Pass both the key code and the modifiers to the process_key_event function
                process_key_event(&mut app, key.code, key.modifiers, &tx, &mut terminal)?;
            }
        } else {
            // Use a very short sleep to keep checking messages frequently
            std::thread::sleep(Duration::from_millis(5));
        }
    }

    Ok(())
}

/// Process messages from the message channel without forcing redraws
fn process_channel_messages(
    app: &mut App,
    rx: &mpsc::Receiver<String>,
    _terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    let mut received_message = false;

    while let Ok(msg) = rx.try_recv() {
        received_message = true;

        if app.debug_messages {
            app.messages
                .push(format!("DEBUG: Received message: {}", msg));
        }
        process_message(app, &msg)?;
        // Don't redraw here - let the main loop control the redraw timing
    }

    // If we received any messages, add auto-scroll marker
    if received_message {
        app.messages.push("_AUTO_SCROLL_".into());
    }

    Ok(())
}

/// Process messages from the agent progress channel without forcing redraws
fn process_agent_messages(
    app: &mut App,
    _terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    // Collect messages first to avoid borrow checker issues
    let mut messages_to_process = Vec::new();
    let mut any_tool_executed = false;

    // Check for agent progress messages and collect them
    if let Some(ref agent_rx) = &app.agent_progress_rx {
        // Drain all available messages into our collection
        while let Ok(msg) = agent_rx.try_recv() {
            if msg == "[TOOL_EXECUTED]" {
                any_tool_executed = true;
            } else {
                messages_to_process.push(msg);
            }
        }
    }

    // Process tool execution counter first
    if any_tool_executed {
        app.add_tool_use();
        app.last_message_time = std::time::Instant::now();
    }

    // Process all collected messages
    let has_messages = !messages_to_process.is_empty();
    let mut any_completion = false;

    for msg in &messages_to_process {
        // Check for tool completion message
        let is_tool_completion =
            msg.contains("Result:") || (msg.contains("tool") && msg.contains("completed"));

        if is_tool_completion {
            any_completion = true;
        }

        // Add debug message if debug is enabled
        if app.debug_messages {
            app.messages
                .push(format!("DEBUG: Received agent message: {}", msg));
        }

        // Handle ANSI escape sequences by stripping them for storage but preserving their meaning
        let processed_msg = if msg.contains("\x1b[") {
            // Store a version without the ANSI codes for message matching but preserve the styling
            let clean_msg = msg.replace("\x1b[32m", "").replace("\x1b[0m", "");
            if app.debug_messages {
                app.messages.push(format!("[ansi_styled] {}", clean_msg));
            }
            clean_msg
        } else {
            msg.clone()
        };

        // Process the message
        process_message(app, &processed_msg)?;
    }

    // Update animation timestamp for tool completions
    if any_completion {
        app.last_message_time = std::time::Instant::now();
    }

    // Add auto-scroll marker if we processed any messages, but don't force redraw
    if has_messages || any_tool_executed {
        app.messages.push("_AUTO_SCROLL_".into());
        // Don't force redraw - let the main loop handle it based on timing
    }

    Ok(())
}

/// Process auto-scroll markers in messages without forcing redraws
fn process_auto_scroll(
    app: &mut App,
    _terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    // Check if we need to auto-scroll after processing messages
    let needs_scroll = app.messages.iter().any(|m| m == "_AUTO_SCROLL_");

    // Always remove any auto-scroll markers first
    app.messages.retain(|m| m != "_AUTO_SCROLL_");

    if needs_scroll {
        // Only auto-scroll when explicitly requested via auto-scroll marker
        app.auto_scroll_to_bottom();

        // Don't force a redraw here - let the main loop handle it when appropriate
        // This prevents unnecessary screen updates that block scrolling
    }

    Ok(())
}

/// Handle Left arrow key for cursor movement
fn handle_left_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Chat | AppState::ApiKeyInput => {
            // Use textarea's built-in input method with proper conversion
            app.textarea.input(Input {
                key: Key::Left,
                ctrl: false,
                alt: false,
                shift: false,
            });

            // Update legacy cursor position for compatibility
            app.input = app.textarea.lines().join("\n");
            if app.cursor_position > 0 {
                app.cursor_position -= 1;
            }

            terminal.draw(|f| ui(f, &mut app))?;
        }
        _ => {}
    }
    Ok(())
}

/// Handle Right arrow key for cursor movement
fn handle_right_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Chat | AppState::ApiKeyInput => {
            // Move cursor right using proper tui-textarea input
            app.textarea.input(Input {
                key: Key::Right,
                ctrl: false,
                alt: false,
                shift: false,
            });

            // Update legacy cursor position for compatibility
            app.input = app.textarea.lines().join("\n");
            if app.cursor_position < app.input.len() {
                app.cursor_position += 1;
            }

            terminal.draw(|f| ui(f, &mut app))?;
        }
        _ => {}
    }
    Ok(())
}

/// Process keyboard events
fn process_key_event(
    mut app: &mut App,
    key: KeyCode,
    modifiers: KeyModifiers,
    tx: &mpsc::Sender<String>,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    // Handle paste shortcuts - relies on terminal emulator's built-in paste support
    // Most terminals automatically handle paste operations by sending the text as if typed
    // We don't need to explicitly implement clipboard access, as the terminal will send
    // each character of the pasted content through the normal input channel
    // Handle permission response first if permission is required
    if app.permission_required {
        match key {
            KeyCode::Char('y') | KeyCode::Char('Y') => {
                // Grant permission
                app.handle_permission_response(true);
                app.permission_required = false;
                terminal.draw(|f| ui(f, &mut app))?;
                return Ok(());
            }
            KeyCode::Char('n') | KeyCode::Char('N') => {
                // Deny permission
                app.handle_permission_response(false);
                app.permission_required = false;
                terminal.draw(|f| ui(f, &mut app))?;
                return Ok(());
            }
            KeyCode::Esc => {
                // Cancel permission dialog (treat as deny)
                app.handle_permission_response(false);
                app.permission_required = false;
                terminal.draw(|f| ui(f, &mut app))?;
                return Ok(());
            }
            _ => return Ok(()), // Ignore other keys while permission dialog is active
        }
    }

    // Normal key handling if no permission dialog
    match key {
        KeyCode::Esc => {
            if app.debug_messages {
                app.messages.push("DEBUG: Esc pressed, exiting".into());
            }
            app.state = AppState::Error("quit".into());
        }
        KeyCode::Enter => {
            // Enhanced handling of newlines and Enter key
            if app.state == AppState::Chat {
                if modifiers.contains(KeyModifiers::SHIFT) || modifiers.contains(KeyModifiers::ALT)
                {
                    // Shift+Enter or Alt+Enter directly inserts a newline
                    // Using input method to ensure proper handling by tui-textarea
                    app.textarea.input(Input {
                        key: Key::Enter,
                        ctrl: false,
                        alt: modifiers.contains(KeyModifiers::ALT),
                        shift: modifiers.contains(KeyModifiers::SHIFT),
                    });

                    // Update the legacy input for compatibility
                    app.input = app.textarea.lines().join("\n");

                    // Force immediate redraw to update input box size and cursor position
                    terminal.draw(|f| ui(f, &mut app))?;
                } else {
                    // Regular Enter handling
                    handle_enter_key(app, tx, terminal)?;
                }
            } else {
                // Regular Enter handling for non-Chat states
                handle_enter_key(app, tx, terminal)?;
            }
        }
        KeyCode::Down => {
            if modifiers.contains(KeyModifiers::SHIFT) {
                // Shift+Down scrolls task list down
                handle_task_scroll_down(app, terminal)?
            } else {
                handle_down_key(app, terminal)?
            }
        }
        KeyCode::Tab => handle_tab_key(app, terminal)?,
        KeyCode::Up => {
            if modifiers.contains(KeyModifiers::SHIFT) {
                // Shift+Up scrolls task list up
                handle_task_scroll_up(app, terminal)?
            } else {
                handle_up_key(app, terminal)?
            }
        }
        KeyCode::Left => handle_left_key(app, terminal)?,
        KeyCode::Right => handle_right_key(app, terminal)?,
        KeyCode::BackTab => handle_backtab_key(app, terminal)?,
        KeyCode::Char(c) => handle_char_key(app, c, modifiers, terminal)?,
        KeyCode::Backspace => handle_backspace_key(app, terminal)?,
        KeyCode::PageUp => handle_page_up_key(app, terminal)?,
        KeyCode::PageDown => handle_page_down_key(app, terminal)?,
        KeyCode::Home => handle_home_key(app, terminal)?,
        KeyCode::End => handle_end_key(app, terminal)?,
        _ => {}
    }

    Ok(())
}

/// Handle Enter key in different application states
fn handle_enter_key(
    mut app: &mut App,
    tx: &mpsc::Sender<String>,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    if app.debug_messages {
        app.messages.push("DEBUG: Enter key pressed".into());
    }

    match app.state {
        AppState::Setup => {
            app.messages.push("DEBUG: Starting model setup...".into());
            terminal.draw(|f| ui(f, &mut app))?;

            if let Err(e) = app.setup_models(tx.clone()) {
                app.messages.push(format!("ERROR: Setup failed: {}", e));
            }
            terminal.draw(|f| ui(f, &mut app))?;
        }
        AppState::ApiKeyInput => {
            // Get input from the textarea
            let api_key = app.textarea.lines().join("\n");
            // Clear the textarea
            app.textarea.delete_line_by_end();

            if !api_key.is_empty() {
                app.messages
                    .push("DEBUG: API key entered, continuing setup...".into());

                // Set the API key and return to setup state
                app.api_key = Some(api_key);
                app.state = AppState::Setup;

                // When returning to regular input, unmask characters (use space as "no mask")
                app.textarea.set_mask_char(' ');

                // Continue with model setup using the provided API key
                if let Err(e) = app.setup_models(tx.clone()) {
                    app.messages.push(format!("ERROR: Setup failed: {}", e));
                }
                terminal.draw(|f| ui(f, &mut app))?;
            } else {
                app.messages
                    .push("API key cannot be empty. Please enter your Anthropic API key...".into());
            }
        }
        AppState::Chat => {
            // First check if we're in command mode
            if app.command_mode {
                // Try to execute the command
                let cmd_executed = app.execute_command();

                // Clear the textarea after executing the command
                app.textarea.delete_line_by_end();
                app.textarea.delete_line_by_head();
                app.input.clear(); // Clear legacy input for compatibility
                app.command_mode = false;
                app.show_command_menu = false;

                // Skip model querying if we executed a command
                if cmd_executed {
                    // Need to redraw to clear command menu
                    terminal.draw(|f| ui(f, &mut app))?;
                    return Ok(());
                }
            }

            // Get the input from the textarea
            let input = app.textarea.lines().join("\n");

            // Clear the textarea after submitting
            while !app.textarea.is_empty() {
                app.textarea.delete_line_by_end();
                app.textarea.delete_line_by_head();
                if !app.textarea.is_empty() {
                    // Move to next line if there are more lines
                    app.textarea.input(Input {
                        key: Key::Down,
                        ctrl: false,
                        alt: false,
                        shift: false,
                    });
                }
            }

            // Update legacy input field for compatibility
            app.input.clear();

            if !input.is_empty() {
                // No debug output needed here

                // Create a new task for this query
                let _task_id = app.create_task(&input);

                // Estimate input tokens - a basic approximation is 4 characters per token
                let estimated_input_tokens = (input.len() / 4) as u32;
                app.add_input_tokens(estimated_input_tokens);

                // Add user message with preserved newlines
                app.messages.push(format!("> {}", input));

                // No thinking message needed - async tasks will show their own progress
                // Force immediate redraw to show the input has been received
                app.auto_scroll_to_bottom();
                terminal.draw(|f| ui(f, &mut app))?;

                // Update the last query time
                app.last_query_time = std::time::Instant::now();

                // CRITICAL FIX: We need to process tool messages BEFORE showing the final answer
                // The key issue is that we need to continue processing agent messages while
                // the query is being executed, but before we get the final result.

                // Force UI refresh for better UX
                app.auto_scroll_to_bottom();
                terminal.draw(|f| ui(f, &mut app))?;

                // Start the model query - this initiates tool execution, but doesn't
                // return until all tool execution is complete
                app.tool_execution_in_progress = true; // Set this manually to ensure proper animation

                // Process a batch of agent messages before starting the query
                // to make sure the UI is set up properly
                process_agent_messages(app, terminal)?;
                terminal.draw(|f| ui(f, &mut app))?;

                // Process agent messages in a special loop to ensure they're displayed
                // BEFORE we get the final result
                let start_time = std::time::Instant::now();
                let timeout = Duration::from_secs(2); // Short timeout to ensure tools start processing

                // First phase - wait for the first tool message to appear
                // This ensures we see "tool executing" before we see results
                while start_time.elapsed() < timeout {
                    // Check for and process agent messages
                    process_agent_messages(app, terminal)?;
                    process_auto_scroll(app, terminal)?;

                    // Redraw the UI to show any updates
                    terminal.draw(|f| ui(f, &mut app))?;

                    // If we've processed any tool messages, we can start the query
                    if app.tool_execution_in_progress {
                        // Give tools a chance to execute and display
                        std::thread::sleep(Duration::from_millis(200));
                        break;
                    }

                    // Brief pause to avoid busy-waiting
                    std::thread::sleep(Duration::from_millis(50));
                }

                // Now execute the actual query and get the final result
                // This ensures all tool messages are displayed BEFORE we get the final result
                let result = app.query_model(&input);

                // Final phase - make sure we've displayed all tool messages
                let final_timeout = Duration::from_millis(500);
                let final_start = std::time::Instant::now();

                while final_start.elapsed() < final_timeout {
                    // Process any remaining agent messages
                    process_agent_messages(app, terminal)?;
                    process_auto_scroll(app, terminal)?;

                    // Redraw to ensure tools are displayed
                    terminal.draw(|f| ui(f, &mut app))?;

                    // Brief pause
                    std::thread::sleep(Duration::from_millis(50));
                }

                // Process the final result
                match result {
                    Ok(response_string) => {
                        // Remove any thinking messages
                        if let Some(last) = app.messages.last() {
                            if last == "Thinking..."
                                || last.starts_with("[thinking]")
                                || last.starts_with("⚪ Processing")
                            {
                                app.messages.pop();
                            }
                        }

                        // Process and format the response for better display
                        format_and_display_response(app, &response_string);

                        // Complete the task with estimated output tokens
                        let estimated_output_tokens = (response_string.len() / 4) as u32;
                        app.complete_current_task(estimated_output_tokens);

                        // Force scrolling to the bottom to show the new response
                        app.auto_scroll_to_bottom();
                    }
                    Err(e) => {
                        // Remove any thinking messages
                        if let Some(last) = app.messages.last() {
                            if last == "Thinking..."
                                || last.starts_with("[thinking]")
                                || last.starts_with("⚪ Processing")
                            {
                                app.messages.pop();
                            }
                        }

                        // Mark the task as failed
                        app.fail_current_task(&e.to_string());

                        app.messages.push(format!("Error: {}", e));
                        app.auto_scroll_to_bottom();
                    }
                }

                // Final redraw to ensure everything is displayed
                terminal.draw(|f| ui(f, &mut app))?;

                // Make sure to redraw after getting a response
                terminal.draw(|f| ui(f, &mut app))?;
            }
        }
        AppState::Error(_) => {
            app.state = AppState::Setup;
            app.error_message = None;
        }
    }
    terminal.draw(|f| ui(f, &mut app))?;

    Ok(())
}

/// Format and display a model response
fn format_and_display_response(app: &mut App, response: &str) {
    // Split long responses into multiple messages if needed
    let max_line_length = 80; // Reasonable line length for TUI display

    if response.contains('\n') {
        // For multi-line responses (code or structured content)
        // Add an empty line before the response for readability
        app.messages.push("".to_string());

        // Split by line to preserve formatting
        for line in response.lines() {
            // For very long lines, add wrapping
            if line.len() > max_line_length {
                // Simple wrapping at character boundaries
                // Use integer division that rounds up (equivalent to ceiling division)
                // Skip clippy suggestion as div_ceil might not be available in all Rust versions
                #[allow(clippy::manual_div_ceil)]
                let chunk_count = (line.len() + max_line_length - 1) / max_line_length;
                for i in 0..chunk_count {
                    let start = i * max_line_length;
                    let end = std::cmp::min(start + max_line_length, line.len());
                    if start < line.len() {
                        app.messages.push(line[start..end].to_string());
                    }
                }
            } else {
                app.messages.push(line.to_string());
            }
        }

        // Add another empty line after for readability
        app.messages.push("".to_string());
    } else {
        // For single-line responses, add directly
        app.messages.push(response.to_string());
    }
}

/// Handle Down key in different application states
fn handle_down_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Setup => {
            app.select_next_model();
            app.messages.push("DEBUG: Selected next model".into());
            terminal.draw(|f| ui(f, &mut app))?;
        }
        AppState::Chat => {
            // Navigate commands in command mode
            if app.show_command_menu {
                app.select_next_command();
                terminal.draw(|f| ui(f, &mut app))?;
            }
            // When not in command mode, handle multiline navigation with TextArea
            else if !app.textarea.is_empty() {
                // Move down using tui-textarea method
                app.textarea.input(Input {
                    key: Key::Down,
                    ctrl: false,
                    alt: false,
                    shift: false,
                });

                // Update legacy input and cursor for compatibility
                app.input = app.textarea.lines().join("\n");

                // Force redraw to update cursor position
                terminal.draw(|f| ui(f, &mut app))?;
            }
        }
        _ => {}
    }
    Ok(())
}

/// Handle Tab key in different application states
fn handle_tab_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Setup => {
            app.select_next_model();
            app.messages.push("DEBUG: Selected next model".into());
            terminal.draw(|f| ui(f, &mut app))?;
        }
        AppState::Chat => {
            // Auto-complete command if in command mode
            if app.show_command_menu {
                let filtered = app.filtered_commands();
                if !filtered.is_empty() && app.selected_command < filtered.len() {
                    // Auto-complete with selected command
                    app.input = filtered[app.selected_command].name.clone();
                    app.show_command_menu = true;
                    app.command_mode = true;
                }
                terminal.draw(|f| ui(f, &mut app))?;
            }
        }
        _ => {}
    }
    Ok(())
}

/// Handle Up key in different application states
fn handle_up_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Setup => {
            app.select_prev_model();
            app.messages.push("DEBUG: Selected previous model".into());
            terminal.draw(|f| ui(f, &mut app))?;
        }
        AppState::Chat => {
            // Navigate commands in command mode
            if app.show_command_menu {
                app.select_prev_command();
                terminal.draw(|f| ui(f, &mut app))?;
            }
            // When not in command mode, handle multiline navigation with TextArea
            else if !app.textarea.is_empty() {
                // Move up using tui-textarea method
                app.textarea.input(Input {
                    key: Key::Up,
                    ctrl: false,
                    alt: false,
                    shift: false,
                });

                // Update legacy input and cursor for compatibility
                app.input = app.textarea.lines().join("\n");

                // Force redraw to update cursor position
                terminal.draw(|f| ui(f, &mut app))?;
            }
        }
        _ => {}
    }
    Ok(())
}

/// Handle BackTab key in different application states
fn handle_backtab_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    if let AppState::Setup = app.state {
        app.select_prev_model();
        app.messages.push("DEBUG: Selected previous model".into());
        terminal.draw(|f| ui(f, &mut app))?;
    }
    Ok(())
}

/// Handle character key in different application states
fn handle_char_key(
    mut app: &mut App,
    c: char,
    modifiers: KeyModifiers,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Chat | AppState::ApiKeyInput => {
            // Special handling for '?' to toggle shortcut display
            if app.state == AppState::Chat && c == '?' && app.textarea.is_empty() {
                // Toggle detailed shortcuts display and don't add the character
                app.show_detailed_shortcuts = !app.show_detailed_shortcuts;
                terminal.draw(|f| ui(f, &mut app))?;
                return Ok(());
            }

            // Special handling for 'j' key with Ctrl modifier to add a newline (common shortcut in many editors)
            if app.state == AppState::Chat
                && (c == 'j' || c == 'J')
                && modifiers.contains(KeyModifiers::CONTROL)
            {
                // Insert a newline character
                app.textarea.insert_char('\n');

                // Update legacy input for compatibility
                app.input = app.textarea.lines().join("\n");

                // Update cursor position for compatibility
                let (x, y) = app.textarea.cursor();
                app.cursor_position = app
                    .textarea
                    .lines()
                    .iter()
                    .take(y)
                    .map(|line| line.len() + 1) // +1 for newline
                    .sum::<usize>()
                    + x;

                // Force immediate redraw to update input box size and cursor position
                terminal.draw(|f| ui(f, &mut app))?;
                return Ok(());
            }

            // Insert the character using proper input method
            app.textarea.input(Input {
                key: Key::Char(c),
                ctrl: modifiers.contains(KeyModifiers::CONTROL),
                alt: modifiers.contains(KeyModifiers::ALT),
                shift: modifiers.contains(KeyModifiers::SHIFT),
            });

            // Update legacy input field for compatibility
            app.input = app.textarea.lines().join("\n");

            // Update cursor position for compatibility
            let (x, y) = app.textarea.cursor();
            // Calculate cursor position based on line length up to the current line plus current position
            app.cursor_position = app
                .textarea
                .lines()
                .iter()
                .take(y)
                .map(|line| line.len() + 1) // +1 for newline
                .sum::<usize>()
                + x;

            // Check if we're entering command mode with the / character
            if app.state == AppState::Chat
                && c == '/'
                && app.textarea.lines().len() == 1
                && app.textarea.lines()[0] == "/"
            {
                app.command_mode = true;
                app.show_command_menu = true;
                app.selected_command = 0;
                // Hide detailed shortcuts when typing /
                app.show_detailed_shortcuts = false;
            } else if app.command_mode {
                // Update command mode state
                app.check_command_mode();
            } else {
                // Hide detailed shortcuts when typing anything else
                app.show_detailed_shortcuts = false;
            }

            terminal.draw(|f| ui(f, &mut app))?;
        }
        _ => {}
    }
    Ok(())
}

/// Handle backspace key in different application states
fn handle_backspace_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Chat | AppState::ApiKeyInput => {
            // Delete the character before the cursor using proper input method
            app.textarea.input(Input {
                key: Key::Backspace,
                ctrl: false,
                alt: false,
                shift: false,
            });

            // Update legacy input field for compatibility
            app.input = app.textarea.lines().join("\n");

            // Update legacy cursor position for compatibility
            if app.cursor_position > 0 {
                app.cursor_position -= 1;
            }

            // Check if we've exited command mode
            if app.state == AppState::Chat {
                app.check_command_mode();
            }

            terminal.draw(|f| ui(f, &mut app))?;
        }
        _ => {}
    }
    Ok(())
}

/// Handle PageUp key for scrolling
fn handle_page_up_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    if let AppState::Chat = app.state {
        // Turn off auto-follow when manually scrolling up
        app.message_scroll.follow_bottom = false;

        // Use page_up method for better scrolling behavior based on viewport size
        app.message_scroll.page_up();

        // Update legacy scroll position to match ScrollState
        app.scroll_position = app.message_scroll.position;

        // Immediately redraw to show new scroll position
        terminal.draw(|f| ui(f, &mut app))?;
    }
    Ok(())
}

/// Handle PageDown key for scrolling
fn handle_page_down_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    if let AppState::Chat = app.state {
        // Use page_down method for better scrolling behavior based on viewport size
        app.message_scroll.page_down();

        // Update legacy scroll position to match ScrollState
        app.scroll_position = app.message_scroll.position;

        // Enable auto-follow if we've reached the bottom
        if app.message_scroll.position >= app.message_scroll.max_scroll() {
            app.message_scroll.follow_bottom = true;
        }

        // Immediately redraw to show new scroll position
        terminal.draw(|f| ui(f, &mut app))?;
    }
    Ok(())
}

/// Handle task list scrolling with Shift+Up
fn handle_task_scroll_up(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    if let AppState::Chat = app.state {
        // Use the task scroll state
        app.task_scroll.scroll_up(1);

        // Update legacy scroll position to match ScrollState
        app.task_scroll_position = app.task_scroll.position;

        terminal.draw(|f| ui(f, &mut app))?;
    }
    Ok(())
}

/// Handle task list scrolling with Shift+Down
fn handle_task_scroll_down(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    if let AppState::Chat = app.state {
        // Use the task scroll state
        app.task_scroll.scroll_down(1);

        // Update legacy scroll position to match ScrollState
        app.task_scroll_position = app.task_scroll.position;

        terminal.draw(|f| ui(f, &mut app))?;
    }
    Ok(())
}

/// Handle Home key for scrolling to top and moving cursor to start of input
fn handle_home_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Chat => {
            if app.textarea.is_empty() {
                // If no input, use Home to scroll the message window to top
                // Turn off auto-follow when manually scrolling to top
                app.message_scroll.follow_bottom = false;
                app.message_scroll.scroll_to_top();

                // Update legacy scroll position to match ScrollState
                app.scroll_position = app.message_scroll.position;

                // Immediately redraw to show scroll position change
                terminal.draw(|f| ui(f, &mut app))?;
            } else {
                // Move to start of line
                app.textarea.input(Input {
                    key: Key::Home,
                    ctrl: false,
                    alt: false,
                    shift: false,
                });

                // Update legacy cursor position for compatibility
                app.input = app.textarea.lines().join("\n");
                let (x, _y) = app.textarea.cursor();
                app.cursor_position = x;

                // Redraw to update cursor position
                terminal.draw(|f| ui(f, &mut app))?;
            }
        }
        AppState::ApiKeyInput => {
            // Move cursor to start of input
            app.textarea.input(Input {
                key: Key::Home,
                ctrl: false,
                alt: false,
                shift: false,
            });
            app.cursor_position = 0; // Update legacy cursor position
            terminal.draw(|f| ui(f, &mut app))?;
        }
        _ => {}
    }
    Ok(())
}

/// Handle End key for scrolling to bottom and moving cursor to end of input
fn handle_end_key(
    mut app: &mut App,
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
    match app.state {
        AppState::Chat => {
            if app.textarea.is_empty() {
                // If no input, use End to scroll the message window to bottom
                // Enable auto-follow when scrolling to bottom
                app.message_scroll.follow_bottom = true;
                app.message_scroll.scroll_to_bottom();

                // Update legacy scroll position to match ScrollState
                app.scroll_position = app.message_scroll.position;

                // Immediately redraw to show scroll position change
                terminal.draw(|f| ui(f, &mut app))?;
            } else {
                // Move to end of line
                app.textarea.input(Input {
                    key: Key::End,
                    ctrl: false,
                    alt: false,
                    shift: false,
                });

                // Update legacy cursor position for compatibility
                app.input = app.textarea.lines().join("\n");
                app.cursor_position = app.input.len();

                // Redraw to update cursor position
                terminal.draw(|f| ui(f, &mut app))?;
            }
        }
        AppState::ApiKeyInput => {
            // Move cursor to end of input
            app.textarea.input(Input {
                key: Key::End,
                ctrl: false,
                alt: false,
                shift: false,
            });

            // Update legacy cursor position
            app.input = app.textarea.lines().join("\n");
            app.cursor_position = app.input.len();

            terminal.draw(|f| ui(f, &mut app))?;
        }
        _ => {}
    }
    Ok(())
}