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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
/*
* _ _ _
* | | __ _ ______ __| | | | __ _ _ __ ___ __ _
* | | / _` ||_ /\ \/ /| | | |/ _` | '_ ` _ \ / _` |
* | |__| (_| | / / \ / | |___ | | (_| | | | | | | (_| |
* |_____\__,_|/___| /_/ |_____||_|\__,_|_| |_| |_|\__,_|
*
* Copyright (C) 2026 Raimo Geisel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//! Application logic and state management.
//!
//! This module contains the [`App`] structure that manages the entire state
//! of the Terminal UI as well as communication with Ollama AI models.
//!
//! The core functionality includes:
//! - Model discovery and management
//! - Per-model buffer isolation (input, history, scroll position)
//! - Streaming response handling
//! - State persistence across model switches
use anyhow::Result;
use ollama_rs::{generation::completion::request::GenerationRequest, Ollama};
use ratatui::{backend::CrosstermBackend, widgets::ListState, Terminal};
use std::collections::HashMap;
use std::env;
use std::io;
use std::time::Instant;
use tokio_stream::StreamExt;
/// Main application state container for LazyLlama.
///
/// This structure holds all the necessary state for the Terminal UI including
/// model information, user input, conversation history, and UI state like
/// scrolling and loading indicators. Each AI model maintains separate buffers
/// for input text, conversation history, and scroll position to provide
/// seamless switching between different models.
pub struct App {
/// List of locally available Ollama models discovered from the API.
pub models: Vec<String>,
/// State of the model selection list widget (currently selected index).
pub list_state: ListState,
/// Current text in the input field for the active model.
pub input: String,
/// Complete conversation history as a string for the active model.
pub history: String,
/// Separate input buffers maintained for each LLM model.
pub model_inputs: HashMap<String, String>,
/// Separate cursor positions maintained for each LLM model.
pub model_cursors: HashMap<String, usize>,
/// Separate text selections maintained for each LLM model.
pub model_selections: HashMap<String, Option<usize>>,
/// Separate conversation histories maintained for each LLM model.
pub model_histories: HashMap<String, String>,
/// Separate scroll positions maintained for each LLM model.
pub model_scrolls: HashMap<String, u16>,
/// Current vertical scroll position in the conversation history.
pub scroll: u16,
/// Current cursor position in the input field (character index).
pub cursor_pos: usize,
/// Starting position of text selection (None if no selection).
pub selection_start: Option<usize>,
/// Flag indicating whether the view should automatically scroll to the bottom.
pub autoscroll: bool,
/// Indicates whether an AI query is currently being processed.
pub is_loading: bool,
/// Instance of the Ollama client for API communication.
pub ollama: Ollama,
/// Timestamp of application start (used for UI animations like spinner).
pub start_time: Instant,
/// Timestamp of last cursor blink toggle.
pub last_cursor_blink: Instant,
/// Whether the input cursor should be visible this frame.
pub cursor_visible: bool,
/// Enables on-screen debug info when true.
pub debug_keys: bool,
/// Last key event debug string (when enabled).
pub debug_last_key: Option<String>,
/// Frame counter for render debugging.
pub render_count: u64,
}
impl App {
/// Creates a new instance of the application and initializes the model list.
///
/// This constructor performs the following initialization steps:
/// 1. Creates a default Ollama client instance
/// 2. Initializes all application state with default values
/// 3. Sets up empty HashMaps for per-model buffer management
/// 4. Automatically discovers and caches available models
/// 5. Selects the first model if any are available
///
/// # Returns
///
/// A new `App` instance with populated model list and initialized state.
///
/// # Example
///
/// ```no_run
/// use lazyllama::app::App;
///
/// #[tokio::main]
/// async fn main() {
/// let app = App::new().await;
/// println!("Found {} models", app.models.len());
/// }
/// ```
pub async fn new() -> Self {
let ollama = Ollama::default();
let debug_keys = env::var("LAZYLLAMA_DEBUG_KEYS")
.map(|v| v != "0" && v.to_lowercase() != "false")
.unwrap_or(false);
let mut app = App {
models: Vec::new(),
list_state: ListState::default(),
input: String::new(),
cursor_pos: 0,
selection_start: None,
history: String::new(),
model_inputs: HashMap::new(),
model_cursors: HashMap::new(),
model_selections: HashMap::new(),
model_histories: HashMap::new(),
model_scrolls: HashMap::new(),
scroll: 0,
autoscroll: true,
is_loading: false,
ollama,
start_time: Instant::now(),
last_cursor_blink: Instant::now(),
cursor_visible: true,
debug_keys,
debug_last_key: None,
render_count: 0,
};
app.refresh_models().await;
app
}
/// Refreshes the list of locally available AI models from Ollama.
///
/// This method queries the Ollama API to discover all locally installed models
/// and updates the internal model list. For any new models discovered, it
/// initializes empty buffer entries (input text, conversation history, and
/// scroll position). If models are available and none is currently selected,
/// it automatically selects the first model and loads its buffers.
///
/// # Behavior
///
/// - Queries Ollama's `/api/tags` endpoint for local models
/// - Preserves existing buffer data for known models
/// - Initializes empty buffers for newly discovered models
/// - Auto-selects first model if no selection exists
/// - Loads buffers for the currently selected model
///
/// # Error Handling
///
/// Silently handles Ollama API errors by leaving the model list unchanged.
/// This prevents the application from crashing if Ollama is temporarily
/// unavailable or returns an error.
pub async fn refresh_models(&mut self) {
if let Ok(models) = self.ollama.list_local_models().await {
self.models = models.into_iter().map(|m| m.name).collect::<Vec<String>>();
// Initialisiere Buffer für neue Modelle
for model in &self.models {
self.model_inputs.entry(model.clone()).or_insert_with(String::new);
self.model_cursors.entry(model.clone()).or_insert(0);
self.model_selections.entry(model.clone()).or_insert(None);
self.model_histories.entry(model.clone()).or_insert_with(String::new);
self.model_scrolls.entry(model.clone()).or_insert(0);
}
if !self.models.is_empty() {
self.list_state.select(Some(0));
self.load_current_model_buffers();
}
}
}
/// Saves the current UI state to the per-model buffer storage.
///
/// This method preserves the current application state (input text, conversation
/// history, and scroll position) by storing it in the model-specific HashMaps.
/// This allows seamless switching between models without losing context.
///
/// # Behavior
///
/// - Retrieves the currently selected model from `list_state`
/// - Stores current `input` text in `model_inputs` HashMap
/// - Stores current `history` string in `model_histories` HashMap
/// - Stores current `scroll` position in `model_scrolls` HashMap
/// - Does nothing if no model is currently selected
///
/// # Usage
///
/// Should be called before:
/// - Switching to a different model
/// - Modifying input text or scroll position
/// - Application shutdown to preserve final state
pub fn save_current_model_buffers(&mut self) {
if let Some(index) = self.list_state.selected() {
if let Some(model) = self.models.get(index) {
self.model_inputs.insert(model.clone(), self.input.clone());
self.model_cursors.insert(model.clone(), self.cursor_pos);
self.model_selections.insert(model.clone(), self.selection_start);
self.model_histories.insert(model.clone(), self.history.clone());
self.model_scrolls.insert(model.clone(), self.scroll);
}
}
}
/// Loads the stored state for the currently selected model.
///
/// This method restores the application state (input text, conversation history,
/// and scroll position) from the model-specific buffer storage. This enables
/// each AI model to maintain its own independent conversation context.
///
/// # Behavior
///
/// - Retrieves the currently selected model from `list_state`
/// - Loads stored `input` text from `model_inputs` HashMap (empty if not found)
/// - Loads stored `history` from `model_histories` HashMap (empty if not found)
/// - Loads stored `scroll` position from `model_scrolls` HashMap (0 if not found)
/// - Updates current application state with the loaded values
/// - Does nothing if no model is currently selected
///
/// # Usage
///
/// Should be called after:
/// - Switching to a different model
/// - Initial model selection during startup
/// - Model list refresh if a model was previously selected
pub fn load_current_model_buffers(&mut self) {
if let Some(index) = self.list_state.selected() {
if let Some(model) = self.models.get(index) {
self.input = self.model_inputs.get(model).cloned().unwrap_or_default();
self.cursor_pos = *self.model_cursors.get(model).unwrap_or(&0);
self.selection_start = *self.model_selections.get(model).unwrap_or(&None);
self.history = self.model_histories.get(model).cloned().unwrap_or_default();
self.scroll = *self.model_scrolls.get(model).unwrap_or(&0);
self.clamp_cursor();
}
}
}
/// Inserts a character at the current cursor position.
///
/// This method performs a character-aware insertion (not byte-based),
/// advances the cursor by one character, and resets the blink timer
/// so the caret remains visible after input. If there is an active
/// selection, it deletes the selection first.
pub fn insert_char(&mut self, c: char) {
// Delete selection first if it exists
if self.selection_start.is_some() {
self.delete_selection();
}
let byte_idx = self.char_index_to_byte_index(self.cursor_pos);
self.input.insert(byte_idx, c);
self.cursor_pos = self.cursor_pos.saturating_add(1);
self.reset_cursor_blink();
}
/// Deletes the character immediately before the cursor.
///
/// This is the standard Backspace behavior: it removes one character to
/// the left of the caret, shifts the cursor left by one, and resets the
/// blink timer.
pub fn backspace(&mut self) {
if self.cursor_pos == 0 {
return;
}
let remove_idx = self.cursor_pos - 1;
let byte_idx = self.char_index_to_byte_index(remove_idx);
self.input.remove(byte_idx);
self.cursor_pos = self.cursor_pos.saturating_sub(1);
self.reset_cursor_blink();
}
/// Deletes the word immediately before the cursor.
///
/// Word boundaries use `is_word_char` rules, treating non-alphanumeric
/// characters (except underscore) as separators. Leading separators to
/// the left of the caret are removed along with the word.
pub fn delete_word_left(&mut self) {
if self.cursor_pos == 0 {
return;
}
let chars: Vec<char> = self.input.chars().collect();
let mut i = self.cursor_pos.min(chars.len());
while i > 0 && !Self::is_word_char(chars[i - 1]) {
i -= 1;
}
while i > 0 && Self::is_word_char(chars[i - 1]) {
i -= 1;
}
if i != self.cursor_pos {
let start = self.char_index_to_byte_index(i);
let end = self.char_index_to_byte_index(self.cursor_pos);
self.input.replace_range(start..end, "");
self.cursor_pos = i;
self.reset_cursor_blink();
}
}
/// Deletes the character at the cursor position.
///
/// This is the standard Delete behavior: it removes the character under
/// the caret (to the right), leaving the cursor position unchanged.
pub fn delete_forward(&mut self) {
let len = self.input.chars().count();
if self.cursor_pos >= len {
return;
}
let byte_idx = self.char_index_to_byte_index(self.cursor_pos);
self.input.remove(byte_idx);
self.reset_cursor_blink();
}
/// Deletes the word immediately after the cursor.
///
/// Word boundaries use `is_word_char` rules, treating non-alphanumeric
/// characters (except underscore) as separators. Leading separators to
/// the right of the caret are removed along with the word.
pub fn delete_word_right(&mut self) {
let chars: Vec<char> = self.input.chars().collect();
let len = chars.len();
if self.cursor_pos >= len {
return;
}
let mut i = self.cursor_pos.min(len);
while i < len && !Self::is_word_char(chars[i]) {
i += 1;
}
while i < len && Self::is_word_char(chars[i]) {
i += 1;
}
if i != self.cursor_pos {
let start = self.char_index_to_byte_index(self.cursor_pos);
let end = self.char_index_to_byte_index(i);
self.input.replace_range(start..end, "");
self.reset_cursor_blink();
}
}
/// Moves the cursor one character to the left.
///
/// No-op if already at the beginning of the input. Resets the blink
/// timer to keep the caret visible after navigation. Clears any active selection.
pub fn move_cursor_left(&mut self) {
self.clear_selection();
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
self.reset_cursor_blink();
}
}
/// Moves the cursor one character to the right.
///
/// No-op if already at the end of the input. Resets the blink timer to
/// keep the caret visible after navigation. Clears any active selection.
pub fn move_cursor_right(&mut self) {
self.clear_selection();
let len = self.input.chars().count();
if self.cursor_pos < len {
self.cursor_pos += 1;
self.reset_cursor_blink();
}
}
/// Moves the cursor to the start of the input.
///
/// This is the Home key behavior. Resets the blink timer if the cursor
/// position changes. Clears any active selection.
pub fn move_cursor_home(&mut self) {
self.clear_selection();
if self.cursor_pos != 0 {
self.cursor_pos = 0;
self.reset_cursor_blink();
}
}
/// Moves the cursor to the end of the input.
///
/// This is the End key behavior. Resets the blink timer if the cursor
/// position changes. Clears any active selection.
pub fn move_cursor_end(&mut self) {
self.clear_selection();
let len = self.input.chars().count();
if self.cursor_pos != len {
self.cursor_pos = len;
self.reset_cursor_blink();
}
}
/// Moves the cursor one word to the left.
///
/// Word boundaries use `is_word_char` rules, treating non-alphanumeric
/// characters (except underscore) as separators. Leading separators to
/// the left are skipped before landing on the previous word boundary.
/// Clears any active selection.
pub fn move_cursor_word_left(&mut self) {
self.clear_selection();
if self.cursor_pos == 0 {
return;
}
let chars: Vec<char> = self.input.chars().collect();
let mut i = self.cursor_pos.min(chars.len());
while i > 0 && !Self::is_word_char(chars[i - 1]) {
i -= 1;
}
while i > 0 && Self::is_word_char(chars[i - 1]) {
i -= 1;
}
if i != self.cursor_pos {
self.cursor_pos = i;
self.reset_cursor_blink();
}
}
/// Moves the cursor one word to the right.
///
/// Word boundaries use `is_word_char` rules, treating non-alphanumeric
/// characters (except underscore) as separators. Leading separators to
/// the right are skipped before landing on the next word boundary.
/// Clears any active selection.
pub fn move_cursor_word_right(&mut self) {
self.clear_selection();
let chars: Vec<char> = self.input.chars().collect();
let len = chars.len();
let mut i = self.cursor_pos.min(len);
while i < len && !Self::is_word_char(chars[i]) {
i += 1;
}
while i < len && Self::is_word_char(chars[i]) {
i += 1;
}
if i != self.cursor_pos {
self.cursor_pos = i;
self.reset_cursor_blink();
}
}
/// Toggles cursor blink state when enough time has elapsed.
///
/// Returns `true` when a toggle occurs so the caller can trigger a
/// redraw; otherwise returns `false` to avoid unnecessary updates.
pub fn update_cursor_blink(&mut self) -> bool {
if self.last_cursor_blink.elapsed().as_millis() >= 500 {
self.cursor_visible = !self.cursor_visible;
self.last_cursor_blink = Instant::now();
return true;
}
false
}
pub fn reset_cursor_blink(&mut self) {
self.cursor_visible = true;
self.last_cursor_blink = Instant::now();
}
pub fn clamp_cursor(&mut self) {
let len = self.input.chars().count();
if self.cursor_pos > len {
self.cursor_pos = len;
}
}
pub fn char_index_to_byte_index(&self, char_index: usize) -> usize {
self.input
.char_indices()
.nth(char_index)
.map(|(idx, _)| idx)
.unwrap_or_else(|| self.input.len())
}
pub fn is_word_char(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
/// Clears the current text selection.
///
/// This method removes any active selection by setting `selection_start` to `None`.
/// After calling this method, no text will be highlighted in the input field.
///
/// # Behavior
///
/// - Sets `selection_start` to `None`
/// - Does nothing if no selection is active
/// - Does not affect cursor position or input content
///
/// # Usage
///
/// This is typically called automatically when:
/// - Moving the cursor without Shift modifier
/// - Inserting new text
/// - Deleting text
pub fn clear_selection(&mut self) {
self.selection_start = None;
}
/// Starts a text selection at the current cursor position.
///
/// This internal method initializes a new text selection by recording the
/// current cursor position as the selection anchor. If a selection is already
/// active, this method does nothing, preserving the original anchor point.
///
/// # Behavior
///
/// - Records current `cursor_pos` as `selection_start` if no selection exists
/// - Preserves existing `selection_start` if selection is already active
/// - Does not affect cursor position or input content
///
/// # Usage
///
/// Called automatically by cursor movement methods when Shift is pressed:
/// - `move_cursor_left_with_selection`
/// - `move_cursor_right_with_selection`
/// - Word-wise and line-wise selection methods
fn start_selection(&mut self) {
if self.selection_start.is_none() {
self.selection_start = Some(self.cursor_pos);
}
}
/// Returns the selection range as (start, end) with start <= end.
///
/// This method calculates the normalized selection range where the start
/// position is always less than or equal to the end position, regardless
/// of the selection direction (forward or backward).
///
/// # Returns
///
/// - `Some((start, end))`: A tuple with start <= end if selection is active
/// - `None`: If no selection is active
///
/// # Behavior
///
/// The range is always normalized:
/// - If `selection_start <= cursor_pos`: Returns `(selection_start, cursor_pos)`
/// - If `selection_start > cursor_pos`: Returns `(cursor_pos, selection_start)`
///
/// # Character Indexing
///
/// The returned indices are character positions (not byte positions),
/// ensuring proper handling of multi-byte Unicode characters.
///
/// # Usage
///
/// Used internally by:
/// - `get_selected_text()`: To extract the text substring
/// - `delete_selection()`: To determine which text to delete
/// - UI rendering: To highlight the selected portion
pub fn get_selection_range(&self) -> Option<(usize, usize)> {
self.selection_start.map(|start| {
if start <= self.cursor_pos {
(start, self.cursor_pos)
} else {
(self.cursor_pos, start)
}
})
}
/// Returns the selected text, or None if no selection exists.
///
/// Extracts the substring between the selection start and end positions,
/// properly handling Unicode characters by operating on character indices
/// rather than byte positions.
///
/// # Returns
///
/// - `Some(String)`: The selected text if an active selection exists
/// - `None`: If no selection is active or selection is empty
///
/// # Character Safety
///
/// This method operates on Unicode characters, not bytes:
/// - Multi-byte characters (e.g., emojis, accented letters) are handled correctly
/// - Selection boundaries respect character boundaries
/// - No risk of splitting multi-byte characters
///
/// # Usage
///
/// Used by:
/// - `copy_selection()`: To get the text to copy to clipboard
/// - UI rendering: To display selection information
/// - Testing: To verify selection behavior
pub fn get_selected_text(&self) -> Option<String> {
self.get_selection_range().map(|(start, end)| {
let chars: Vec<char> = self.input.chars().collect();
chars[start..end].iter().collect()
})
}
/// Deletes the currently selected text and clears the selection.
///
/// This internal method removes the text within the active selection range,
/// repositions the cursor to the start of the removed text, and clears the
/// selection state. The operation is performed safely with respect to
/// character boundaries.
///
/// # Behavior
///
/// 1. Calculates normalized selection range (start <= end)
/// 2. Converts character indices to byte indices
/// 3. Removes the substring using `replace_range`
/// 4. Positions cursor at the start of the deleted range
/// 5. Clears the selection state
/// 6. Resets cursor blink timer
///
/// # Character Safety
///
/// - Uses character-based indices internally
/// - Converts to byte indices only for the actual deletion
/// - Preserves multi-byte character integrity
///
/// # Side Effects
///
/// - Modifies `self.input` by removing text
/// - Updates `self.cursor_pos`
/// - Clears `self.selection_start`
/// - Resets cursor blink state
///
/// # Usage
///
/// Called automatically by:
/// - `insert_char()`: Before inserting new text
/// - `insert_text_at_cursor()`: Before pasting
fn delete_selection(&mut self) {
if let Some((start, end)) = self.get_selection_range() {
let start_byte = self.char_index_to_byte_index(start);
let end_byte = self.char_index_to_byte_index(end);
self.input.replace_range(start_byte..end_byte, "");
self.cursor_pos = start;
self.clear_selection();
self.reset_cursor_blink();
}
}
/// Inserts text at the cursor position, replacing any selected text.
///
/// This method provides smart text insertion behavior: if a selection is
/// active, it first deletes the selected text, then inserts the new text
/// at the cursor position. This matches standard text editor behavior.
///
/// # Arguments
///
/// * `text` - The string to insert at the cursor position
///
/// # Behavior
///
/// 1. If selection exists: Deletes selected text first
/// 2. Inserts the provided text at the current cursor position
/// 3. Advances cursor by the number of characters inserted
/// 4. Resets cursor blink timer to keep cursor visible
///
/// # Character Handling
///
/// - Properly counts Unicode characters (not bytes)
/// - Cursor advances by character count, not byte length
/// - Supports multi-byte characters and emojis
///
/// # Use Cases
///
/// - Pasting text from clipboard (`paste_from_clipboard`)
/// - Inserting multi-character strings
/// - Replacing selected text with new content
///
/// # Example
///
/// ```ignore
/// // Replace selected "world" with "Rust"
/// app.insert_text_at_cursor("Rust"); // "hello Rust" instead of "hello world"
/// ```
pub fn insert_text_at_cursor(&mut self, text: &str) {
// Delete selection first if it exists
if self.selection_start.is_some() {
self.delete_selection();
}
// Insert text at cursor
let byte_idx = self.char_index_to_byte_index(self.cursor_pos);
self.input.insert_str(byte_idx, text);
self.cursor_pos += text.chars().count();
self.reset_cursor_blink();
}
/// Moves the cursor one character to the left while extending the selection.
///
/// This method implements the Shift+Left keyboard shortcut behavior,
/// allowing users to select text character by character in the backward
/// direction. If no selection exists, it creates one starting from the
/// current cursor position.
///
/// # Behavior
///
/// 1. Starts a new selection if none exists (anchors at current position)
/// 2. Moves cursor one character to the left
/// 3. Extends or contracts the selection based on direction
/// 4. Resets cursor blink timer for visibility
///
/// # Selection Dynamics
///
/// - **No selection**: Creates selection from cursor position
/// - **Expanding left**: Increases selection size
/// - **Collapsing right**: Decreases selection size
/// - **At start**: No-op if cursor is at position 0
///
/// # Keyboard Mapping
///
/// - Primary: `Shift + Left Arrow`
pub fn move_cursor_left_with_selection(&mut self) {
self.start_selection();
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
self.reset_cursor_blink();
}
}
/// Moves the cursor one character to the right while extending the selection.
///
/// This method implements the Shift+Right keyboard shortcut behavior,
/// allowing users to select text character by character in the forward
/// direction. If no selection exists, it creates one starting from the
/// current cursor position.
///
/// # Behavior
///
/// 1. Starts a new selection if none exists (anchors at current position)
/// 2. Moves cursor one character to the right
/// 3. Extends or contracts the selection based on direction
/// 4. Resets cursor blink timer for visibility
///
/// # Selection Dynamics
///
/// - **No selection**: Creates selection from cursor position
/// - **Expanding right**: Increases selection size
/// - **Collapsing left**: Decreases selection size
/// - **At end**: No-op if cursor is at last position
///
/// # Keyboard Mapping
///
/// - Primary: `Shift + Right Arrow`
pub fn move_cursor_right_with_selection(&mut self) {
self.start_selection();
let len = self.input.chars().count();
if self.cursor_pos < len {
self.cursor_pos += 1;
self.reset_cursor_blink();
}
}
/// Moves the cursor one word to the left while extending the selection.
///
/// This method implements the Ctrl+Shift+Left keyboard shortcut, enabling
/// users to select text word by word in the backward direction. It uses
/// the same word boundary detection as `move_cursor_word_left`, treating
/// alphanumeric characters and underscores as word constituents.
///
/// # Behavior
///
/// 1. Starts a new selection if none exists
/// 2. Skips over non-word characters (spaces, punctuation)
/// 3. Jumps to the beginning of the previous word
/// 4. Extends or contracts selection accordingly
///
/// # Word Boundaries
///
/// Uses `is_word_char` for detection:
/// - **Word characters**: Letters, digits, underscore
/// - **Separators**: Spaces, punctuation, special characters
///
/// # Edge Cases
///
/// - At start of input: No-op
/// - Multiple separators: Skips all before landing on word
///
/// # Keyboard Mapping
///
/// - Primary: `Ctrl + Shift + Left Arrow`
pub fn move_cursor_word_left_with_selection(&mut self) {
self.start_selection();
if self.cursor_pos == 0 {
return;
}
let chars: Vec<char> = self.input.chars().collect();
let mut i = self.cursor_pos.min(chars.len());
while i > 0 && !Self::is_word_char(chars[i - 1]) {
i -= 1;
}
while i > 0 && Self::is_word_char(chars[i - 1]) {
i -= 1;
}
if i != self.cursor_pos {
self.cursor_pos = i;
self.reset_cursor_blink();
}
}
/// Moves the cursor one word to the right while extending the selection.
///
/// This method implements the Ctrl+Shift+Right keyboard shortcut, enabling
/// users to select text word by word in the forward direction. It uses
/// the same word boundary detection as `move_cursor_word_right`, treating
/// alphanumeric characters and underscores as word constituents.
///
/// # Behavior
///
/// 1. Starts a new selection if none exists
/// 2. Skips over non-word characters (spaces, punctuation)
/// 3. Jumps to the end of the next word
/// 4. Extends or contracts selection accordingly
///
/// # Word Boundaries
///
/// Uses `is_word_char` for detection:
/// - **Word characters**: Letters, digits, underscore
/// - **Separators**: Spaces, punctuation, special characters
///
/// # Edge Cases
///
/// - At end of input: No-op
/// - Multiple separators: Skips all before landing on word
///
/// # Keyboard Mapping
///
/// - Primary: `Ctrl + Shift + Right Arrow`
pub fn move_cursor_word_right_with_selection(&mut self) {
self.start_selection();
let chars: Vec<char> = self.input.chars().collect();
let len = chars.len();
let mut i = self.cursor_pos.min(len);
while i < len && !Self::is_word_char(chars[i]) {
i += 1;
}
while i < len && Self::is_word_char(chars[i]) {
i += 1;
}
if i != self.cursor_pos {
self.cursor_pos = i;
self.reset_cursor_blink();
}
}
/// Moves the cursor to the start of the input while extending the selection.
///
/// This method implements the Shift+Home keyboard shortcut, allowing users
/// to select all text from the current cursor position to the beginning of
/// the input field in a single action.
///
/// # Behavior
///
/// 1. Starts a new selection if none exists
/// 2. Moves cursor to position 0 (start of input)
/// 3. Selection spans from original position to start
/// 4. Resets cursor blink timer if position changes
///
/// # Use Cases
///
/// - Quick selection of text from cursor to start
/// - Useful for replacing beginning of input
/// - Efficient bulk text selection
///
/// # Keyboard Mapping
///
/// - Primary: `Shift + Home`
/// - Alternative: `Shift + Ctrl + Home` (also supported)
pub fn move_cursor_home_with_selection(&mut self) {
self.start_selection();
if self.cursor_pos != 0 {
self.cursor_pos = 0;
self.reset_cursor_blink();
}
}
/// Moves the cursor to the end of the input while extending the selection.
///
/// This method implements the Shift+End keyboard shortcut, allowing users
/// to select all text from the current cursor position to the end of the
/// input field in a single action.
///
/// # Behavior
///
/// 1. Starts a new selection if none exists
/// 2. Moves cursor to end of input (last character position)
/// 3. Selection spans from original position to end
/// 4. Resets cursor blink timer if position changes
///
/// # Use Cases
///
/// - Quick selection of text from cursor to end
/// - Useful for replacing end of input
/// - Efficient bulk text selection
///
/// # Keyboard Mapping
///
/// - Primary: `Shift + End`
/// - Alternative: `Shift + Ctrl + End` (also supported)
pub fn move_cursor_end_with_selection(&mut self) {
self.start_selection();
let len = self.input.chars().count();
if self.cursor_pos != len {
self.cursor_pos = len;
self.reset_cursor_blink();
}
}
/// Copies the currently selected text to the system clipboard.
///
/// This method implements the copy-to-clipboard functionality, allowing
/// users to copy selected text for use in other applications or for pasting
/// later within LazyLlama. It uses the `arboard` crate for cross-platform
/// clipboard access.
///
/// # Returns
///
/// - `Ok(())`: Text successfully copied to clipboard
/// - `Err`: If no text is selected or clipboard access fails
///
/// # Error Cases
///
/// - **No selection**: Returns error "No text selected"
/// - **Clipboard unavailable**: System clipboard access denied
/// - **Platform issues**: OS-specific clipboard errors
///
/// # Behavior
///
/// 1. Checks if selection exists via `get_selected_text()`
/// 2. Creates new clipboard instance
/// 3. Writes selected text to clipboard
/// 4. Maintains selection state (does not clear)
///
/// # Platform Support
///
/// - **Windows**: Uses Win32 clipboard API
/// - **Linux**: Uses X11/Wayland clipboard
/// - **macOS**: Uses NSPasteboard
///
/// # Keyboard Mapping
///
/// - Primary: `Ctrl + Shift + C`
///
/// # Usage
///
/// ```ignore
/// if let Err(e) = app.copy_selection() {
/// // Handle error (e.g., no selection)
/// }
/// ```
pub fn copy_selection(&self) -> Result<()> {
if let Some(text) = self.get_selected_text() {
let mut clipboard = arboard::Clipboard::new()?;
clipboard.set_text(text)?;
Ok(())
} else {
Err(anyhow::anyhow!("No text selected"))
}
}
/// Pastes text from the system clipboard at the cursor position.
///
/// This method implements paste-from-clipboard functionality, retrieving
/// text from the system clipboard and inserting it at the current cursor
/// position. If a selection is active, the selected text is replaced by
/// the pasted content, matching standard text editor behavior.
///
/// # Returns
///
/// - `Ok(())`: Text successfully pasted from clipboard
/// - `Err`: If clipboard access fails or clipboard is empty
///
/// # Error Cases
///
/// - **Empty clipboard**: No text content available
/// - **Clipboard unavailable**: System clipboard access denied
/// - **Platform issues**: OS-specific clipboard errors
///
/// # Behavior
///
/// 1. Creates new clipboard instance
/// 2. Retrieves text from clipboard
/// 3. Calls `insert_text_at_cursor()` to insert text
/// 4. Automatically replaces selection if active
/// 5. Advances cursor to end of pasted text
///
/// # Selection Handling
///
/// - **With selection**: Replaces selected text with clipboard content
/// - **Without selection**: Inserts at cursor position
///
/// # Platform Support
///
/// - **Windows**: Uses Win32 clipboard API
/// - **Linux**: Uses X11/Wayland clipboard
/// - **macOS**: Uses NSPasteboard
///
/// # Keyboard Mapping
///
/// - Primary: `Ctrl + Shift + V`
///
/// # Usage
///
/// ```ignore
/// if let Err(e) = app.paste_from_clipboard() {
/// // Handle error (e.g., clipboard empty)
/// }
/// ```
pub fn paste_from_clipboard(&mut self) -> Result<()> {
let mut clipboard = arboard::Clipboard::new()?;
let text = clipboard.get_text()?;
self.insert_text_at_cursor(&text);
Ok(())
}
/// Switches to the next model in the list (Down arrow key behavior).
///
/// This method implements circular navigation through the model list,
/// wrapping from the last model back to the first. It preserves the
/// current model's state before switching and loads the target model's
/// stored state.
///
/// # Behavior
///
/// 1. Saves current model's state via `save_current_model_buffers()`
/// 2. Calculates next index with wraparound (last → first)
/// 3. Updates `list_state` selection to new index
/// 4. Loads the new model's state via `load_current_model_buffers()`
///
/// # Model Selection Logic
///
/// - If at last model: wraps to index 0 (first model)
/// - Otherwise: increments to next index
/// - If no model selected: selects index 0
/// - Handles empty model list gracefully
pub fn select_next_model(&mut self) {
if self.models.is_empty() {
return;
}
self.save_current_model_buffers();
let i = match self.list_state.selected() {
Some(i) => {
if i >= self.models.len() - 1 {
0
} else {
i + 1
}
}
None => 0,
};
self.list_state.select(Some(i));
self.load_current_model_buffers();
}
/// Switches to the previous model in the list (Up arrow key behavior).
///
/// This method implements circular navigation through the model list,
/// wrapping from the first model back to the last. It preserves the
/// current model's state before switching and loads the target model's
/// stored state.
///
/// # Behavior
///
/// 1. Saves current model's state via `save_current_model_buffers()`
/// 2. Calculates previous index with wraparound (first → last)
/// 3. Updates `list_state` selection to new index
/// 4. Loads the new model's state via `load_current_model_buffers()`
///
/// # Model Selection Logic
///
/// - If at first model (index 0): wraps to last model
/// - Otherwise: decrements to previous index
/// - If no model selected: selects index 0
/// - Handles empty model list gracefully
pub fn select_previous_model(&mut self) {
if self.models.is_empty() {
return;
}
self.save_current_model_buffers();
let i = match self.list_state.selected() {
Some(i) => {
if i == 0 {
self.models.len() - 1
} else {
i - 1
}
}
None => 0,
};
self.list_state.select(Some(i));
self.load_current_model_buffers();
}
/// Sends the current input to the selected model and streams the response.
///
/// This method handles the complete query lifecycle including prompt formatting,
/// API communication, real-time response streaming, and UI updates. The response
/// is written directly to `self.history` as tokens are received, providing
/// immediate visual feedback to the user.
///
/// # Arguments
///
/// * `terminal` - Mutable reference to the terminal backend for real-time UI updates
///
/// # Returns
///
/// Returns `Ok(())` on successful completion or an `anyhow::Error` if the API
/// request fails, streaming encounters an error, or terminal drawing fails.
///
/// # Behavior
///
/// 1. **Validation**: Ensures a model is selected before proceeding
/// 2. **Formatting**: Adds user prompt to conversation history with "YOU:" label
/// 3. **State Management**: Clears input field and saves current buffers
/// 4. **UI Updates**: Sets loading state and enables autoscroll
/// 5. **Streaming**: Sends request to Ollama and processes response tokens
/// 6. **Real-time Display**: Updates terminal display for each received token
/// 7. **Completion**: Adds separator and saves final state
///
/// # Error Handling
///
/// - Gracefully handles API connection errors
/// - Continues processing partial responses if streaming is interrupted
/// - Ensures loading state is cleared even on errors
/// - Preserves conversation history even if request fails
///
/// # Side Effects
///
/// - Modifies `self.history` with formatted conversation
/// - Clears `self.input` field
/// - Updates `self.is_loading` state
/// - Triggers terminal redraws for real-time display
/// - Saves state to model-specific buffers
pub async fn send_query(
&mut self,
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<()> {
if let Some(i) = self.list_state.selected() {
let model = self.models[i].clone();
let prompt = self.input.clone();
self.history.push_str(&format!("\nYOU: {}\n\nAI: ", prompt));
self.input.clear();
self.cursor_pos = 0;
// Speichere die aktualisierten Buffer für das aktuelle Modell
self.save_current_model_buffers();
self.is_loading = true;
self.autoscroll = true;
let request = GenerationRequest::new(model.clone(), prompt);
let mut stream = self.ollama.generate_stream(request).await?;
while let Some(res) = stream.next().await {
if let Ok(responses) = res {
for resp in responses {
self.history.push_str(&resp.response);
}
terminal.draw(|f| crate::ui::ui(f, self))?;
}
}
self.history.push_str("\n---\n");
self.is_loading = false;
// Speichere die finale History für dieses Modell
self.save_current_model_buffers();
}
Ok(())
}
}