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
use crossterm::event::{Event, KeyEvent, KeyEventKind};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use strum::{EnumDiscriminants, EnumIter, EnumString, VariantArray};
/// Which mouse button was pressed.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MouseButton {
/// Left mouse button
#[default]
Left,
/// Right mouse button
Right,
/// Middle mouse button
Middle,
}
impl From<crossterm::event::MouseButton> for MouseButton {
fn from(button: crossterm::event::MouseButton) -> Self {
match button {
crossterm::event::MouseButton::Left => Self::Left,
crossterm::event::MouseButton::Right => Self::Right,
crossterm::event::MouseButton::Middle => Self::Middle,
}
}
}
/// Valid ways how `Reedline::read_line()` can return
#[non_exhaustive]
#[derive(Debug)]
pub enum Signal {
/// Entry succeeded with the provided content
Success(String),
/// Entry was aborted with `Ctrl+C`
CtrlC, // Interrupt current editing
/// Abort with `Ctrl+D` signalling `EOF` or abort of a whole interactive session
CtrlD, // End terminal session
/// An external signal requested that `read_line()` return.
/// Contains the current buffer contents at the time of interruption.
ExternalBreak(String),
}
/// Scope of text object operation ("i" inner or "a" around)
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum TextObjectScope {
/// Just the text object itself
Inner,
/// Expanded to include surrounding based on object type
Around,
}
/// Type of text object to operate on
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum TextObjectType {
/// word (delimited by non-alphanumeric characters)
Word,
/// WORD (delimited only by whitespace)
BigWord,
/// (, ), [, ], {, }
Brackets,
/// ", ', `
Quote,
}
/// Text objects that can be operated on with vim-style commands
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct TextObject {
/// Whether to include surrounding context
pub scope: TextObjectScope,
/// The type of text object
pub object_type: TextObjectType,
}
impl Default for TextObject {
fn default() -> Self {
Self {
scope: TextObjectScope::Inner,
object_type: TextObjectType::Word,
}
}
}
/// Editing actions which can be mapped to key bindings.
///
/// Executed by `Reedline::run_edit_commands()`
#[non_exhaustive]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, EnumDiscriminants, EnumIter)]
#[strum_discriminants(doc = "This is the auto generated discriminant type for [`EditCommand`]")]
#[strum_discriminants(derive(EnumString, VariantArray))]
#[strum_discriminants(strum(ascii_case_insensitive))]
pub enum EditCommand {
/// Move to the start of the buffer
MoveToStart {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move to the start of the current line
MoveToLineStart {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move to the start of the current line skipping any whitespace
MoveToLineNonBlankStart {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move to the end of the buffer
MoveToEnd {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move to the end of the current line
MoveToLineEnd {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one line up
MoveLineUp {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one line down
MoveLineDown {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one character to the left
MoveLeft {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one character to the right
MoveRight {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one word to the left
MoveWordLeft {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one WORD to the left
MoveBigWordLeft {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one word to the right
MoveWordRight {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one word to the right, stop at start of word
MoveWordRightStart {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one WORD to the right, stop at start of WORD
MoveBigWordRightStart {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one word to the right, stop at end of word
MoveWordRightEnd {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move one WORD to the right, stop at end of WORD
MoveBigWordRightEnd {
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move to position
MoveToPosition {
/// Position to move to
position: usize,
/// Select the text between the current cursor position and destination
select: bool,
},
/// Insert a character at the current insertion point
InsertChar(char),
/// Insert a string at the current insertion point
InsertString(String),
/// Inserts the system specific new line character
///
/// - On Unix systems LF (`"\n"`)
/// - On Windows CRLF (`"\r\n"`)
InsertNewline,
/// Replace a character
ReplaceChar(char),
/// Replace characters with string
ReplaceChars(usize, String),
/// Backspace delete from the current insertion point
Backspace,
/// Delete in-place from the current insertion point
Delete,
/// Cut the grapheme right from the current insertion point
CutChar,
/// Backspace delete a word from the current insertion point
BackspaceWord,
/// Delete in-place a word from the current insertion point
DeleteWord,
/// Clear the current buffer
Clear,
/// Clear to the end of the current line
ClearToLineEnd,
/// Insert completion: entire completion if there is only one possibility, or else up to shared prefix.
Complete,
/// Cut the current line
CutCurrentLine,
/// Cut from the start of the buffer to the insertion point
CutFromStart,
/// Cut from the start of the buffer to the line of insertion point
CutFromStartLinewise {
/// When true, an empty line will remain after the operation
leave_blank_line: bool,
},
/// Cut from the start of the current line to the insertion point
CutFromLineStart,
/// Cut from the first non whitespace character of the current line to the insertion point
CutFromLineNonBlankStart,
/// Cut from the insertion point to the end of the buffer
CutToEnd,
/// Cut from the line of insertion point to the end of the buffer
CutToEndLinewise {
/// When true, an empty line will remain after the operation
leave_blank_line: bool,
},
/// Cut from the insertion point to the end of the current line
CutToLineEnd,
/// Cut from the insertion point to the end of the current line
/// If the cursor is already at the end of the line, remove the newline character
KillLine,
/// Cut the word left of the insertion point
CutWordLeft,
/// Cut the WORD left of the insertion point
CutBigWordLeft,
/// Cut the word right of the insertion point
CutWordRight,
/// Cut the word right of the insertion point
CutBigWordRight,
/// Cut the word right of the insertion point and any following space
CutWordRightToNext,
/// Cut the WORD right of the insertion point and any following space
CutBigWordRightToNext,
/// Paste the cut buffer in front of the insertion point (Emacs, vi `P`)
PasteCutBufferBefore,
/// Paste the cut buffer in front of the insertion point (vi `p`)
PasteCutBufferAfter,
/// Upper case the current word
UppercaseWord,
/// Lower case the current word
LowercaseWord,
/// Capitalize the current character
CapitalizeChar,
/// Switch the case of the current character
SwitchcaseChar,
/// Swap the current word with the word to the right
SwapWords,
/// Swap the current grapheme/character with the one to the right
SwapGraphemes,
/// Undo the previous edit command
Undo,
/// Redo an edit command from the undo history
Redo,
/// CutUntil right until char
CutRightUntil(char),
/// CutUntil right before char
CutRightBefore(char),
/// CutUntil right until char
MoveRightUntil {
/// Char to move towards
c: char,
/// Select the text between the current cursor position and destination
select: bool,
},
/// CutUntil right before char
MoveRightBefore {
/// Char to move towards
c: char,
/// Select the text between the current cursor position and destination
select: bool,
},
/// CutUntil left until char
CutLeftUntil(char),
/// CutUntil left before char
CutLeftBefore(char),
/// Move left until char
MoveLeftUntil {
/// Char to move towards
c: char,
/// Select the text between the current cursor position and destination
select: bool,
},
/// Move left before char
MoveLeftBefore {
/// Char to move towards
c: char,
/// Select the text between the current cursor position and destination
select: bool,
},
/// Select whole input buffer
SelectAll,
/// Cut selection to local buffer
CutSelection,
/// Copy selection to local buffer
CopySelection,
/// Paste content from local buffer at the current cursor position
Paste,
/// Copy from the start of the buffer to the insertion point
CopyFromStart,
/// Copy from the start of the buffer to the line of insertion point
CopyFromStartLinewise,
/// Copy from the start of the current line to the insertion point
CopyFromLineStart,
/// Copy from the first non whitespace character of the current line to the insertion point
CopyFromLineNonBlankStart,
/// Copy from the insertion point to the end of the buffer
CopyToEnd,
/// Copy from the line of insertion point to the end of the buffer
CopyToEndLinewise,
/// Copy from the insertion point to the end of the current line
CopyToLineEnd,
/// Copy the current line
CopyCurrentLine,
/// Copy the word left of the insertion point
CopyWordLeft,
/// Copy the WORD left of the insertion point
CopyBigWordLeft,
/// Copy the word right of the insertion point
CopyWordRight,
/// Copy the WORD right of the insertion point
CopyBigWordRight,
/// Copy the word right of the insertion point and any following space
CopyWordRightToNext,
/// Copy the WORD right of the insertion point and any following space
CopyBigWordRightToNext,
/// Copy one character to the left
CopyLeft,
/// Copy one character to the right
CopyRight,
/// Copy until right until char
CopyRightUntil(char),
/// Copy right before char
CopyRightBefore(char),
/// Copy left until char
CopyLeftUntil(char),
/// Copy left before char
CopyLeftBefore(char),
/// Swap the positions of the cursor and anchor
SwapCursorAndAnchor,
/// Cut selection to system clipboard
#[cfg(feature = "system_clipboard")]
CutSelectionSystem,
/// Copy selection to system clipboard
#[cfg(feature = "system_clipboard")]
CopySelectionSystem,
/// Paste content from system clipboard at the current cursor position
#[cfg(feature = "system_clipboard")]
PasteSystem,
/// Delete text between matching characters atomically
CutInsidePair {
/// Left character of the pair
left: char,
/// Right character of the pair (usually matching bracket)
right: char,
},
/// Yank text between matching characters atomically
CopyInsidePair {
/// Left character of the pair
left: char,
/// Right character of the pair (usually matching bracket)
right: char,
},
/// Delete text around matching characters atomically (including the pair characters)
CutAroundPair {
/// Left character of the pair
left: char,
/// Right character of the pair (usually matching bracket)
right: char,
},
/// Yank text around matching characters atomically (including the pair characters)
CopyAroundPair {
/// Left character of the pair
left: char,
/// Right character of the pair (usually matching bracket)
right: char,
},
/// Cut the specified text object
CutTextObject {
/// The text object to operate on
text_object: TextObject,
},
/// Copy the specified text object
CopyTextObject {
/// The text object to operate on
text_object: TextObject,
},
}
impl Display for EditCommand {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
EditCommand::MoveToStart { .. } => write!(f, "MoveToStart Optional[select: <bool>]"),
EditCommand::MoveToLineStart { .. } => {
write!(f, "MoveToLineStart Optional[select: <bool>]")
}
EditCommand::MoveToLineNonBlankStart { .. } => {
write!(f, "MoveToLineNonBlankStart Optional[select: <bool>]")
}
EditCommand::MoveToEnd { .. } => write!(f, "MoveToEnd Optional[select: <bool>]"),
EditCommand::MoveToLineEnd { .. } => {
write!(f, "MoveToLineEnd Optional[select: <bool>]")
}
EditCommand::MoveLineUp { .. } => write!(f, "MoveLineUp Optional[select: <bool>]"),
EditCommand::MoveLineDown { .. } => write!(f, "MoveLineDown Optional[select: <bool>]"),
EditCommand::MoveLeft { .. } => write!(f, "MoveLeft Optional[select: <bool>]"),
EditCommand::MoveRight { .. } => write!(f, "MoveRight Optional[select: <bool>]"),
EditCommand::MoveWordLeft { .. } => write!(f, "MoveWordLeft Optional[select: <bool>]"),
EditCommand::MoveBigWordLeft { .. } => {
write!(f, "MoveBigWordLeft Optional[select: <bool>]")
}
EditCommand::MoveWordRight { .. } => {
write!(f, "MoveWordRight Optional[select: <bool>]")
}
EditCommand::MoveWordRightEnd { .. } => {
write!(f, "MoveWordRightEnd Optional[select: <bool>]")
}
EditCommand::MoveBigWordRightEnd { .. } => {
write!(f, "MoveBigWordRightEnd Optional[select: <bool>]")
}
EditCommand::MoveWordRightStart { .. } => {
write!(f, "MoveWordRightStart Optional[select: <bool>]")
}
EditCommand::MoveBigWordRightStart { .. } => {
write!(f, "MoveBigWordRightStart Optional[select: <bool>]")
}
EditCommand::MoveToPosition { .. } => {
write!(f, "MoveToPosition Value: <int>, Optional[select: <bool>]")
}
EditCommand::MoveLeftUntil { .. } => {
write!(f, "MoveLeftUntil Value: <char>, Optional[select: <bool>]")
}
EditCommand::MoveLeftBefore { .. } => {
write!(f, "MoveLeftBefore Value: <char>, Optional[select: <bool>]")
}
EditCommand::InsertChar(_) => write!(f, "InsertChar Value: <char>"),
EditCommand::InsertString(_) => write!(f, "InsertString Value: <string>"),
EditCommand::InsertNewline => write!(f, "InsertNewline"),
EditCommand::ReplaceChar(_) => write!(f, "ReplaceChar <char>"),
EditCommand::ReplaceChars(_, _) => write!(f, "ReplaceChars <int> <string>"),
EditCommand::Backspace => write!(f, "Backspace"),
EditCommand::Delete => write!(f, "Delete"),
EditCommand::CutChar => write!(f, "CutChar"),
EditCommand::BackspaceWord => write!(f, "BackspaceWord"),
EditCommand::DeleteWord => write!(f, "DeleteWord"),
EditCommand::Clear => write!(f, "Clear"),
EditCommand::ClearToLineEnd => write!(f, "ClearToLineEnd"),
EditCommand::Complete => write!(f, "Complete"),
EditCommand::CutCurrentLine => write!(f, "CutCurrentLine"),
EditCommand::CutFromStart => write!(f, "CutFromStart"),
EditCommand::CutFromStartLinewise { .. } => {
write!(f, "CutFromStartLinewise Value: <bool>")
}
EditCommand::CutFromLineStart => write!(f, "CutFromLineStart"),
EditCommand::CutFromLineNonBlankStart => write!(f, "CutFromLineNonBlankStart"),
EditCommand::CutToEnd => write!(f, "CutToEnd"),
EditCommand::CutToEndLinewise { .. } => {
write!(f, "CutToEndLinewise Value: <bool>")
}
EditCommand::CutToLineEnd => write!(f, "CutToLineEnd"),
EditCommand::KillLine => write!(f, "KillLine"),
EditCommand::CutWordLeft => write!(f, "CutWordLeft"),
EditCommand::CutBigWordLeft => write!(f, "CutBigWordLeft"),
EditCommand::CutWordRight => write!(f, "CutWordRight"),
EditCommand::CutBigWordRight => write!(f, "CutBigWordRight"),
EditCommand::CutWordRightToNext => write!(f, "CutWordRightToNext"),
EditCommand::CutBigWordRightToNext => write!(f, "CutBigWordRightToNext"),
EditCommand::PasteCutBufferBefore => write!(f, "PasteCutBufferBefore"),
EditCommand::PasteCutBufferAfter => write!(f, "PasteCutBufferAfter"),
EditCommand::UppercaseWord => write!(f, "UppercaseWord"),
EditCommand::LowercaseWord => write!(f, "LowercaseWord"),
EditCommand::SwitchcaseChar => write!(f, "SwitchcaseChar"),
EditCommand::CapitalizeChar => write!(f, "CapitalizeChar"),
EditCommand::SwapWords => write!(f, "SwapWords"),
EditCommand::SwapGraphemes => write!(f, "SwapGraphemes"),
EditCommand::Undo => write!(f, "Undo"),
EditCommand::Redo => write!(f, "Redo"),
EditCommand::CutRightUntil(_) => write!(f, "CutRightUntil Value: <char>"),
EditCommand::CutRightBefore(_) => write!(f, "CutRightBefore Value: <char>"),
EditCommand::MoveRightUntil { .. } => write!(f, "MoveRightUntil Value: <char>"),
EditCommand::MoveRightBefore { .. } => write!(f, "MoveRightBefore Value: <char>"),
EditCommand::CutLeftUntil(_) => write!(f, "CutLeftUntil Value: <char>"),
EditCommand::CutLeftBefore(_) => write!(f, "CutLeftBefore Value: <char>"),
EditCommand::SelectAll => write!(f, "SelectAll"),
EditCommand::CutSelection => write!(f, "CutSelection"),
EditCommand::CopySelection => write!(f, "CopySelection"),
EditCommand::Paste => write!(f, "Paste"),
EditCommand::CopyFromStart => write!(f, "CopyFromStart"),
EditCommand::CopyFromStartLinewise => write!(f, "CopyFromStartLinewise"),
EditCommand::CopyFromLineStart => write!(f, "CopyFromLineStart"),
EditCommand::CopyFromLineNonBlankStart => write!(f, "CopyFromLineNonBlankStart"),
EditCommand::CopyToEnd => write!(f, "CopyToEnd"),
EditCommand::CopyToEndLinewise => write!(f, "CopyToEndLinewise"),
EditCommand::CopyToLineEnd => write!(f, "CopyToLineEnd"),
EditCommand::CopyCurrentLine => write!(f, "CopyCurrentLine"),
EditCommand::CopyWordLeft => write!(f, "CopyWordLeft"),
EditCommand::CopyBigWordLeft => write!(f, "CopyBigWordLeft"),
EditCommand::CopyWordRight => write!(f, "CopyWordRight"),
EditCommand::CopyBigWordRight => write!(f, "CopyBigWordRight"),
EditCommand::CopyWordRightToNext => write!(f, "CopyWordRightToNext"),
EditCommand::CopyBigWordRightToNext => write!(f, "CopyBigWordRightToNext"),
EditCommand::CopyLeft => write!(f, "CopyLeft"),
EditCommand::CopyRight => write!(f, "CopyRight"),
EditCommand::CopyRightUntil(_) => write!(f, "CopyRightUntil Value: <char>"),
EditCommand::CopyRightBefore(_) => write!(f, "CopyRightBefore Value: <char>"),
EditCommand::CopyLeftUntil(_) => write!(f, "CopyLeftUntil Value: <char>"),
EditCommand::CopyLeftBefore(_) => write!(f, "CopyLeftBefore Value: <char>"),
EditCommand::SwapCursorAndAnchor => write!(f, "SwapCursorAndAnchor"),
#[cfg(feature = "system_clipboard")]
EditCommand::CutSelectionSystem => write!(f, "CutSelectionSystem"),
#[cfg(feature = "system_clipboard")]
EditCommand::CopySelectionSystem => write!(f, "CopySelectionSystem"),
#[cfg(feature = "system_clipboard")]
EditCommand::PasteSystem => write!(f, "PasteSystem"),
EditCommand::CutInsidePair { .. } => write!(f, "CutInsidePair Value: <char> <char>"),
EditCommand::CopyInsidePair { .. } => write!(f, "CopyInsidePair Value: <char> <char>"),
EditCommand::CutAroundPair { .. } => write!(f, "CutAroundPair Value: <char> <char>"),
EditCommand::CopyAroundPair { .. } => write!(f, "CopyAroundPair Value: <char> <char>"),
EditCommand::CutTextObject { .. } => write!(f, "CutTextObject Value: <TextObject>"),
EditCommand::CopyTextObject { .. } => write!(f, "CopyTextObject Value: <TextObject>"),
}
}
}
impl EditCommand {
/// Determine if a certain operation should be undoable
/// or if the operations should be coalesced for undoing
pub fn edit_type(&self) -> EditType {
match self {
// Cursor moves
EditCommand::MoveToStart { select, .. }
| EditCommand::MoveToEnd { select, .. }
| EditCommand::MoveToLineStart { select, .. }
| EditCommand::MoveToLineEnd { select, .. }
| EditCommand::MoveToLineNonBlankStart { select, .. }
| EditCommand::MoveToPosition { select, .. }
| EditCommand::MoveLineUp { select, .. }
| EditCommand::MoveLineDown { select, .. }
| EditCommand::MoveLeft { select, .. }
| EditCommand::MoveRight { select, .. }
| EditCommand::MoveWordLeft { select, .. }
| EditCommand::MoveBigWordLeft { select, .. }
| EditCommand::MoveWordRight { select, .. }
| EditCommand::MoveWordRightStart { select, .. }
| EditCommand::MoveBigWordRightStart { select, .. }
| EditCommand::MoveWordRightEnd { select, .. }
| EditCommand::MoveBigWordRightEnd { select, .. }
| EditCommand::MoveRightUntil { select, .. }
| EditCommand::MoveRightBefore { select, .. }
| EditCommand::MoveLeftUntil { select, .. }
| EditCommand::MoveLeftBefore { select, .. } => {
EditType::MoveCursor { select: *select }
}
EditCommand::SwapCursorAndAnchor => EditType::MoveCursor { select: true },
EditCommand::SelectAll => EditType::MoveCursor { select: true },
// Text edits
EditCommand::InsertChar(_)
| EditCommand::Backspace
| EditCommand::Delete
| EditCommand::CutChar
| EditCommand::InsertString(_)
| EditCommand::InsertNewline
| EditCommand::ReplaceChar(_)
| EditCommand::ReplaceChars(_, _)
| EditCommand::BackspaceWord
| EditCommand::DeleteWord
| EditCommand::Clear
| EditCommand::ClearToLineEnd
| EditCommand::Complete
| EditCommand::CutCurrentLine
| EditCommand::CutFromStart
| EditCommand::CutFromStartLinewise { .. }
| EditCommand::CutFromLineStart
| EditCommand::CutFromLineNonBlankStart
| EditCommand::CutToLineEnd
| EditCommand::KillLine
| EditCommand::CutToEnd
| EditCommand::CutToEndLinewise { .. }
| EditCommand::CutWordLeft
| EditCommand::CutBigWordLeft
| EditCommand::CutWordRight
| EditCommand::CutBigWordRight
| EditCommand::CutWordRightToNext
| EditCommand::CutBigWordRightToNext
| EditCommand::PasteCutBufferBefore
| EditCommand::PasteCutBufferAfter
| EditCommand::UppercaseWord
| EditCommand::LowercaseWord
| EditCommand::SwitchcaseChar
| EditCommand::CapitalizeChar
| EditCommand::SwapWords
| EditCommand::SwapGraphemes
| EditCommand::CutRightUntil(_)
| EditCommand::CutRightBefore(_)
| EditCommand::CutLeftUntil(_)
| EditCommand::CutLeftBefore(_)
| EditCommand::CutSelection
| EditCommand::Paste
| EditCommand::CutInsidePair { .. }
| EditCommand::CutAroundPair { .. }
| EditCommand::CutTextObject { .. } => EditType::EditText,
#[cfg(feature = "system_clipboard")] // Sadly cfg attributes in patterns don't work
EditCommand::CutSelectionSystem | EditCommand::PasteSystem => EditType::EditText,
EditCommand::Undo | EditCommand::Redo => EditType::UndoRedo,
EditCommand::CopySelection => EditType::NoOp,
#[cfg(feature = "system_clipboard")]
EditCommand::CopySelectionSystem => EditType::NoOp,
EditCommand::CopyFromStart
| EditCommand::CopyFromStartLinewise
| EditCommand::CopyFromLineStart
| EditCommand::CopyFromLineNonBlankStart
| EditCommand::CopyToEnd
| EditCommand::CopyToEndLinewise
| EditCommand::CopyToLineEnd
| EditCommand::CopyCurrentLine
| EditCommand::CopyWordLeft
| EditCommand::CopyBigWordLeft
| EditCommand::CopyWordRight
| EditCommand::CopyBigWordRight
| EditCommand::CopyWordRightToNext
| EditCommand::CopyBigWordRightToNext
| EditCommand::CopyLeft
| EditCommand::CopyRight
| EditCommand::CopyRightUntil(_)
| EditCommand::CopyRightBefore(_)
| EditCommand::CopyLeftUntil(_)
| EditCommand::CopyLeftBefore(_)
| EditCommand::CopyInsidePair { .. }
| EditCommand::CopyAroundPair { .. }
| EditCommand::CopyTextObject { .. } => EditType::NoOp,
}
}
}
/// Specifies the types of edit commands, used to simplify grouping edits
/// to mark undo behavior
#[derive(PartialEq, Eq)]
pub enum EditType {
/// Cursor movement commands
MoveCursor { select: bool },
/// Undo/Redo commands
UndoRedo,
/// Text editing commands
EditText,
/// No effect on line buffer
NoOp,
}
/// Every line change should come with an `UndoBehavior` tag, which can be used to
/// calculate how the change should be reflected on the undo stack
#[derive(Debug)]
pub enum UndoBehavior {
/// Character insertion, tracking the character inserted
InsertCharacter(char),
/// Backspace command, tracking the deleted character (left of cursor)
/// Warning: this does not track the whole grapheme, just the character
Backspace(Option<char>),
/// Delete command, tracking the deleted character (right of cursor)
/// Warning: this does not track the whole grapheme, just the character
Delete(Option<char>),
/// Move the cursor position
MoveCursor,
/// Navigated the history using up or down arrows
HistoryNavigation,
/// Catch-all for actions that should always form a unique undo point and never be
/// grouped with later edits
CreateUndoPoint,
/// For actions that shouldn't be reflected on the edit stack e.g. Undo/Redo
NoOp,
}
impl UndoBehavior {
/// Return if the current operation should start a new undo set, or be
/// combined with the previous operation
pub fn create_undo_point_after(&self, previous: &UndoBehavior) -> bool {
use UndoBehavior as UB;
match (previous, self) {
// Never start an undo set with cursor movement
(_, UB::MoveCursor) => false,
(UB::HistoryNavigation, UB::HistoryNavigation) => false,
// When inserting/deleting repeatedly, each undo set should encompass
// inserting/deleting a complete word and the associated whitespace
(UB::InsertCharacter(c_prev), UB::InsertCharacter(c_new)) => {
(*c_prev == '\n' || *c_prev == '\r')
|| (!c_prev.is_whitespace() && c_new.is_whitespace())
}
(UB::Backspace(Some(c_prev)), UB::Backspace(Some(c_new))) => {
(*c_new == '\n' || *c_new == '\r')
|| (c_prev.is_whitespace() && !c_new.is_whitespace())
}
(UB::Backspace(_), UB::Backspace(_)) => false,
(UB::Delete(Some(c_prev)), UB::Delete(Some(c_new))) => {
(*c_new == '\n' || *c_new == '\r')
|| (c_prev.is_whitespace() && !c_new.is_whitespace())
}
(UB::Delete(_), UB::Delete(_)) => false,
(_, _) => true,
}
}
}
/// Reedline supported actions.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug, EnumDiscriminants, EnumIter)]
#[strum_discriminants(doc = "This is the auto generated discriminant type for [`ReedlineEvent`]")]
#[strum_discriminants(derive(EnumString, VariantArray))]
#[strum_discriminants(strum(ascii_case_insensitive))]
pub enum ReedlineEvent {
/// No op event
None,
/// Complete history hint (default in full)
HistoryHintComplete,
/// Complete a single token/word of the history hint
HistoryHintWordComplete,
/// Handle EndOfLine event
///
/// Expected Behavior:
///
/// - On empty line breaks execution to exit with [`Signal::CtrlD`]
/// - Secondary behavior [`EditCommand::Delete`]
CtrlD,
/// Handle SIGTERM key input
///
/// Expected behavior:
///
/// Abort entry
/// Run [`EditCommand::Clear`]
/// Clear the current undo
/// Bubble up [`Signal::CtrlC`]
CtrlC,
/// Clears the screen and sets prompt to first line
ClearScreen,
/// Clears the screen and the scrollback buffer
///
/// Sets the prompt back to the first line
ClearScrollback,
/// Handle enter event
Enter,
/// Handle unconditional submit event
Submit,
/// Submit at the end of the *complete* text, otherwise newline
SubmitOrNewline,
/// Esc event
#[strum_discriminants(strum(serialize = "Esc", serialize = "Escape"))]
Esc,
/// Mouse click event with screen coordinates
Mouse {
/// Column (x) position, 0-indexed from left
column: u16,
/// Row (y) position, 0-indexed from top
row: u16,
/// Which mouse button was clicked
button: MouseButton,
},
/// trigger terminal resize
Resize(u16, u16),
/// Run these commands in the editor
Edit(Vec<EditCommand>),
/// Trigger full repaint
Repaint,
/// Navigate to the previous historic buffer
PreviousHistory,
/// Move up to the previous line, if multiline, or up into the historic buffers
Up,
/// Move down to the next line, if multiline, or down through the historic buffers
Down,
/// Move right to the next column, completion entry, or complete hint
Right,
/// Move left to the next column, or completion entry
Left,
/// Move to the start of the buffer
ToStart,
/// Move to the end of the buffer
ToEnd,
/// Navigate to the next historic buffer
NextHistory,
/// Search the history for a string
SearchHistory,
/// In vi mode multiple reedline events can be chained while parsing the
/// command or movement characters
Multiple(Vec<ReedlineEvent>),
/// Test
UntilFound(Vec<ReedlineEvent>),
/// Trigger a menu event. It activates a menu with the event name
Menu(String),
/// Next element in the menu
MenuNext,
/// Previous element in the menu
MenuPrevious,
/// Moves up in the menu
MenuUp,
/// Moves down in the menu
MenuDown,
/// Moves left in the menu
MenuLeft,
/// Moves right in the menu
MenuRight,
/// Move to the next history page
MenuPageNext,
/// Move to the previous history page
MenuPagePrevious,
/// Way to bind the execution of a whole command (directly returning from [`crate::Reedline::read_line()`]) to a keybinding
ExecuteHostCommand(String),
/// Open text editor
OpenEditor,
/// Change mode (vi mode only)
ViChangeMode(String),
}
impl Display for ReedlineEvent {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
ReedlineEvent::None => write!(f, "None"),
ReedlineEvent::HistoryHintComplete => write!(f, "HistoryHintComplete"),
ReedlineEvent::HistoryHintWordComplete => write!(f, "HistoryHintWordComplete"),
ReedlineEvent::CtrlD => write!(f, "CtrlD"),
ReedlineEvent::CtrlC => write!(f, "CtrlC"),
ReedlineEvent::ClearScreen => write!(f, "ClearScreen"),
ReedlineEvent::ClearScrollback => write!(f, "ClearScrollback"),
ReedlineEvent::Enter => write!(f, "Enter"),
ReedlineEvent::Submit => write!(f, "Submit"),
ReedlineEvent::SubmitOrNewline => write!(f, "SubmitOrNewline"),
ReedlineEvent::Esc => write!(f, "Esc"),
ReedlineEvent::Mouse {
column,
row,
button,
} => write!(f, "Mouse({}, {}, {:?})", column, row, button),
ReedlineEvent::Resize(_, _) => write!(f, "Resize <int> <int>"),
ReedlineEvent::Edit(_) => write!(
f,
"Edit: <EditCommand> or Edit: <EditCommand> value: <string>"
),
ReedlineEvent::Repaint => write!(f, "Repaint"),
ReedlineEvent::PreviousHistory => write!(f, "PreviousHistory"),
ReedlineEvent::Up => write!(f, "Up"),
ReedlineEvent::Down => write!(f, "Down"),
ReedlineEvent::ToStart => write!(f, "ToStart"),
ReedlineEvent::ToEnd => write!(f, "ToEnd"),
ReedlineEvent::Right => write!(f, "Right"),
ReedlineEvent::Left => write!(f, "Left"),
ReedlineEvent::NextHistory => write!(f, "NextHistory"),
ReedlineEvent::SearchHistory => write!(f, "SearchHistory"),
ReedlineEvent::Multiple(_) => write!(f, "Multiple[ {{ ReedLineEvents, }} ]"),
ReedlineEvent::UntilFound(_) => write!(f, "UntilFound [ {{ ReedLineEvents, }} ]"),
ReedlineEvent::Menu(_) => write!(f, "Menu Name: <string>"),
ReedlineEvent::MenuNext => write!(f, "MenuNext"),
ReedlineEvent::MenuPrevious => write!(f, "MenuPrevious"),
ReedlineEvent::MenuUp => write!(f, "MenuUp"),
ReedlineEvent::MenuDown => write!(f, "MenuDown"),
ReedlineEvent::MenuLeft => write!(f, "MenuLeft"),
ReedlineEvent::MenuRight => write!(f, "MenuRight"),
ReedlineEvent::MenuPageNext => write!(f, "MenuPageNext"),
ReedlineEvent::MenuPagePrevious => write!(f, "MenuPagePrevious"),
ReedlineEvent::ExecuteHostCommand(_) => write!(f, "ExecuteHostCommand"),
ReedlineEvent::OpenEditor => write!(f, "OpenEditor"),
ReedlineEvent::ViChangeMode(_) => write!(f, "ViChangeMode mode: <string>"),
}
}
}
pub enum EventStatus {
Handled,
Inapplicable,
Exits(Signal),
}
/// A wrapper for [crossterm::event::Event].
///
/// It ensures that the given event doesn't contain [KeyEventKind::Release]
/// (which is rejected) or [KeyEventKind::Repeat] (which is converted to
/// [KeyEventKind::Press]).
pub struct ReedlineRawEvent(Event);
impl TryFrom<Event> for ReedlineRawEvent {
type Error = ();
fn try_from(event: Event) -> Result<Self, Self::Error> {
match event {
Event::Key(KeyEvent {
kind: KeyEventKind::Release,
..
}) => Err(()),
Event::Key(KeyEvent {
code,
modifiers,
kind: KeyEventKind::Repeat,
state,
}) => Ok(Self(Event::Key(KeyEvent {
code,
modifiers,
kind: KeyEventKind::Press,
state,
}))),
other => Ok(Self(other)),
}
}
}
impl From<ReedlineRawEvent> for Event {
fn from(event: ReedlineRawEvent) -> Self {
event.0
}
}