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
//! Facilities for tracking and persisting the shell's command history.
use std::collections::BTreeSet;
use std::io::{BufRead, Read, Write};
use std::path::Path;
use chrono::Utc;
use crate::error;
/// Represents a unique identifier for a history item.
type ItemId = i64;
/// Interface for querying and manipulating the shell's recorded history of
/// commands.
// TODO(history): support maximum item count
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct History {
/// The items, oldest first. Their IDs only increase, so they're in ID order
/// too, which finds an item by ID (by binary search) without a separate
/// map from IDs to items.
items: rpds::VectorSync<Item>,
next_id: ItemId,
}
impl History {
/// Constructs a new `History` instance, with its contents initialized from
/// the given readable stream. If errors are encountered reading lines
/// from the stream, unreadable lines will be skipped but the call will
/// still return successfully, with a warning logged. An error
/// result will be returned only if an internal error occurs updating the
/// history. Like bash, an entry the stream gives no timestamp gets the
/// time it's read.
///
/// # Arguments
///
/// * `reader` - The readable stream to import history from.
///
/// # Errors
///
/// Returns [`error::ErrorKind::IoError`] if reading from `reader` fails
/// with an error other than invalid data, which is skipped.
pub fn import(reader: impl Read) -> Result<Self, error::Error> {
let mut history = Self::default();
let read_time = Utc::now();
let buf_reader = std::io::BufReader::new(reader);
let mut next_timestamp = None;
for line_result in buf_reader.lines() {
let line = match line_result {
Ok(line) => line,
// If we couldn't decode the line due to invalid data (perhaps it wasn't
// valid UTF8?), skip it and make a best-effort attempt to proceed on.
// We'll later warn the user.
Err(err) if err.kind() == std::io::ErrorKind::InvalidData => {
tracing::warn!("unreadable history line; {err}");
continue;
}
// In the event of other kinds of errors, return an error result. We don't
// want to get stuck in a failing I/O loop.
Err(err) => {
return Err(err.into());
}
};
// Look for timestamp comments; ignore other comment lines.
if let Some(comment) = line.strip_prefix("#") {
if let Ok(seconds_since_epoch) = comment.trim().parse() {
next_timestamp = ItemTimestamp::from_timestamp(seconds_since_epoch, 0);
} else {
next_timestamp = None;
}
continue;
}
let item = Item {
id: history.next_id,
command_line: line,
timestamp: Some(next_timestamp.take().unwrap_or(read_time)),
dirty: false,
};
history.add(item)?;
}
Ok(history)
}
/// Tries to retrieve a history item by its unique identifier. Returns
/// `None` if no item is found.
///
/// # Arguments
///
/// * `id` - The unique identifier of the history item to retrieve.
///
/// # Errors
///
/// Currently never fails; the result type leaves room for fallible storage.
pub fn get_by_id(&self, id: ItemId) -> Result<Option<&Item>, error::Error> {
Ok(self.position_of(id).and_then(|index| self.items.get(index)))
}
/// Replaces the history item with the given ID with a new item. Returns an
/// error if the item cannot be updated.
///
/// # Arguments
///
/// * `id` - The unique identifier of the history item to update.
/// * `item` - The new history item to replace the old one.
///
/// # Errors
///
/// Returns [`error::ErrorKind::HistoryItemNotFound`] if no item has the
/// given ID.
pub fn update_by_id(&mut self, id: ItemId, item: Item) -> Result<(), error::Error> {
let index = self
.position_of(id)
.ok_or(error::ErrorKind::HistoryItemNotFound)?;
// The item keeps its ID, so the items stay in ID order.
self.items.set_mut(index, Item { id, ..item });
Ok(())
}
/// Removes the nth item from the history (counting from 0, the oldest).
/// Returns whether there was such an item to remove.
pub fn remove_nth_item(&mut self, n: usize) -> bool {
let len = self.items.len();
if n >= len {
return false;
}
// Take off the items from the nth on, and put back those after it: so
// removing a recent item (as `fc -s` does its own) touches only
// the few after it.
let after: Vec<Item> = (n + 1..len)
.filter_map(|i| self.items.get(i).cloned())
.collect();
for _ in n..len {
self.items.drop_last_mut();
}
for item in after {
self.items.push_back_mut(item);
}
true
}
/// Keeps only the items that `keep` returns true for, in order. Their IDs
/// don't change.
pub fn retain(&mut self, mut keep: impl FnMut(&Item) -> bool) {
let Some(first_removed) = self.items.iter().position(|item| !keep(item)) else {
return;
};
// As in `remove_nth_item`, rebuild only from the first item removed on.
let kept: Vec<Item> = self
.items
.iter()
.skip(first_removed + 1)
.filter(|item| keep(item))
.cloned()
.collect();
for _ in first_removed..self.items.len() {
self.items.drop_last_mut();
}
for item in kept {
self.items.push_back_mut(item);
}
}
/// Adds a new history item. Returns the unique identifier of the newly
/// added item.
///
/// # Arguments
///
/// * `item` - The history item to add.
///
/// # Errors
///
/// Currently never fails; the result type leaves room for fallible storage.
pub fn add(&mut self, mut item: Item) -> Result<ItemId, error::Error> {
let id = self.next_id;
item.id = id;
self.next_id += 1;
self.items.push_back_mut(item);
Ok(id)
}
/// Deletes a history item by its unique identifier. Returns an error if the
/// item cannot be deleted.
///
/// # Arguments
///
/// * `id` - The unique identifier of the history item to delete.
///
/// # Errors
///
/// Currently never fails. An ID with no matching item is ignored.
pub fn delete_item_by_id(&mut self, id: ItemId) -> Result<(), error::Error> {
if let Some(index) = self.position_of(id) {
self.remove_nth_item(index);
}
Ok(())
}
/// Clears all history items.
///
/// # Errors
///
/// Currently never fails; the result type leaves room for fallible storage.
pub fn clear(&mut self) -> Result<(), error::Error> {
self.items = rpds::VectorSync::new_sync();
Ok(())
}
/// Flushes the history to backing storage (if relevant).
///
/// # Arguments
///
/// * `history_file_path` - The path to the history file.
/// * `append` - Whether to append to the file or overwrite it.
/// * `unsaved_items_only` - Whether to only write unsaved items; if true,
/// any items will be marked as "saved" once saved.
/// * `write_timestamps` - Whether to write timestamps for each command
/// line.
///
/// # Errors
///
/// Returns [`error::ErrorKind::IoError`] if the history file cannot be
/// opened or written. Items stay marked unsaved when writing fails.
pub fn flush(
&mut self,
history_file_path: impl AsRef<Path>,
append: bool,
unsaved_items_only: bool,
write_timestamps: bool,
) -> Result<(), error::Error> {
// Open the file
let mut file_options = std::fs::File::options();
if append {
file_options.append(true);
} else {
file_options.write(true).truncate(true);
}
let mut file =
std::io::BufWriter::new(file_options.create(true).open(history_file_path.as_ref())?);
let mut written = Vec::new();
for (index, item) in self.items.iter().enumerate() {
if unsaved_items_only && !item.dirty {
continue;
}
if write_timestamps && let Some(timestamp) = item.timestamp {
writeln!(file, "#{}", timestamp.timestamp())?;
}
writeln!(file, "{}", item.command_line)?;
if unsaved_items_only {
written.push(index);
}
}
// Only once they're all written out, mark the items saved: if writing
// fails, they stay unsaved, so a later flush retries them.
file.into_inner()
.map_err(std::io::IntoInnerError::into_error)?;
for index in written {
if let Some(item) = self.items.get_mut(index) {
item.dirty = false;
}
}
Ok(())
}
/// Searches through history using the given query.
///
/// # Arguments
///
/// * `query` - The query to use.
///
/// # Errors
///
/// Currently never fails; the result type leaves room for fallible storage.
pub fn search(&self, query: Query) -> Result<impl Iterator<Item = &self::Item>, error::Error> {
Ok(Search::new(self, query))
}
/// Returns whether the history contains no items.
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// Returns an iterator over the history items.
pub fn iter(&self) -> impl Iterator<Item = &self::Item> {
self.items.iter()
}
/// Retrieves the nth history item, if it exists. Returns `None` if no such
/// item exists. Indexing is zero-based, with an index of 0 referencing
/// the oldest item in the history.
///
/// # Arguments
///
/// * `index` - The index of the history item to retrieve.
pub fn get(&self, index: usize) -> Option<&Item> {
self.items.get(index)
}
/// Returns the newest item, if there is one.
pub fn last(&self) -> Option<&Item> {
self.items.last()
}
/// Returns the number of items in the history.
pub fn count(&self) -> usize {
self.items.len()
}
/// Returns how many items have IDs less than `id`: the position of the item
/// with that ID, if there is one.
fn position_before(&self, id: ItemId) -> usize {
// The items are in ID order, so binary search. (By hand:
// `slice::partition_point` needs a slice, which rpds's vector,
// a tree, isn't.)
let (mut low, mut high) = (0, self.items.len());
while low < high {
let mid = low + (high - low) / 2;
if self.items.get(mid).is_some_and(|item| item.id < id) {
low = mid + 1;
} else {
high = mid;
}
}
low
}
/// Returns the position of the item with the given ID, if there is one.
fn position_of(&self, id: ItemId) -> Option<usize> {
let index = self.position_before(id);
self.items
.get(index)
.is_some_and(|item| item.id == id)
.then_some(index)
}
}
/// A value of `HISTCONTROL`, which controls what an interactive shell saves to
/// history.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, strum_macros::EnumString)]
#[strum(serialize_all = "lowercase")]
pub(crate) enum HistoryControl {
/// Don't save a command that starts with a space.
IgnoreSpace,
/// Don't save a command that's the same as the last one saved.
IgnoreDups,
/// Both `ignorespace` and `ignoredups`.
IgnoreBoth,
/// Before saving a command, remove any earlier ones the same as it.
EraseDups,
}
impl HistoryControl {
/// Parses `HISTCONTROL`'s colon-separated values into the set of them, with
/// `ignoreboth` as the two it stands for. Like bash, values it doesn't
/// know are ignored.
pub(crate) fn parse_list(value: &str) -> BTreeSet<Self> {
value
.split(':')
.filter_map(|value| value.parse().ok())
.flat_map(|control| match control {
Self::IgnoreBoth => [Self::IgnoreSpace, Self::IgnoreDups].as_slice(),
Self::IgnoreSpace => &[Self::IgnoreSpace],
Self::IgnoreDups => &[Self::IgnoreDups],
Self::EraseDups => &[Self::EraseDups],
})
.copied()
.collect()
}
}
/// Represents a timestamp for a history item.
pub type ItemTimestamp = chrono::DateTime<Utc>;
/// Represents an item in the history.
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Item {
/// The unique identifier of the history item.
pub id: ItemId,
/// The actual command line.
pub command_line: String,
/// The timestamp when the command was started.
pub timestamp: Option<ItemTimestamp>,
/// Whether or not the item is dirty, i.e., has not yet been written to
/// backing storage.
pub dirty: bool,
}
impl Item {
/// Constructs a new `Item` with the given command line.
///
/// # Arguments
///
/// * `command_line` - The command line of the item.
pub fn new(command_line: impl Into<String>) -> Self {
Self {
id: 0, // NOTE: ID will be assigned when added to the history.
command_line: command_line.into(),
timestamp: Some(chrono::Utc::now()),
dirty: true,
}
}
}
/// Encapsulates query parameters for searching through history.
#[derive(Default)]
pub struct Query {
/// Whether to search forward or backward
pub direction: Direction,
/// Optionally, clamp results to items with a timestamp strictly after this.
pub not_at_or_before_time: Option<ItemTimestamp>,
/// Optionally, clamp results to items with a timestamp strictly before
/// this.
pub not_at_or_after_time: Option<ItemTimestamp>,
/// Optionally, clamp results to items with an ID equal strictly after this.
pub not_at_or_before_id: Option<ItemId>,
/// Optionally, clamp results to items with an ID equal strictly before
/// this.
pub not_at_or_after_id: Option<ItemId>,
/// Optionally, maximum number of items to retrieve
pub max_items: Option<i64>,
/// Optionally, a string-based filter on command line.
pub command_line_filter: Option<CommandLineFilter>,
}
impl Query {
/// Checks if the query includes the given item.
///
/// # Arguments
///
/// * `item` - The item to check.
pub fn includes(&self, item: &Item) -> bool {
// Filter based on not_at_or_before_time.
if let Some(not_at_or_before_time) = &self.not_at_or_before_time {
if item
.timestamp
.is_some_and(|ts| ts <= *not_at_or_before_time)
{
return false;
}
}
// Filter based on not_at_or_after_time
if let Some(not_at_or_after_time) = &self.not_at_or_after_time {
if item.timestamp.is_some_and(|ts| ts >= *not_at_or_after_time) {
return false;
}
}
// Filter based on not_at_or_before_id
if self
.not_at_or_before_id
.is_some_and(|query_id| item.id <= query_id)
{
return false;
}
// Filter based on not_at_or_after_id
if self
.not_at_or_after_id
.is_some_and(|query_id| item.id >= query_id)
{
return false;
}
// Filter based on command_line_filter
if let Some(command_line_filter) = &self.command_line_filter {
match command_line_filter {
CommandLineFilter::Prefix(prefix) => {
if !item.command_line.starts_with(prefix) {
return false;
}
}
CommandLineFilter::Suffix(suffix) => {
if !item.command_line.ends_with(suffix) {
return false;
}
}
CommandLineFilter::Contains(contains) => {
if !item.command_line.contains(contains) {
return false;
}
}
CommandLineFilter::Exact(exact) => {
if item.command_line != *exact {
return false;
}
}
}
}
true
}
}
/// Represents the direction of a search operation.
#[derive(Default)]
pub enum Direction {
/// Search forward from the oldest part of history.
#[default]
Forward,
/// Search backward from the youngest part of history.
Backward,
}
/// Filter criteria for command lines.
pub enum CommandLineFilter {
/// The command line must start with this string.
Prefix(String),
/// The command line must end with this string.
Suffix(String),
/// The command line must contain this string.
Contains(String),
/// The command line must match this string exactly.
Exact(String),
}
/// Represents a search operation.
pub struct Search<'a> {
/// The history to search through.
history: &'a History,
/// The positions of the items left to search: those within the query's ID
/// bounds.
positions: std::ops::Range<usize>,
/// The query to apply.
query: Query,
/// Count of items returned so far.
count: usize,
}
impl<'a> Search<'a> {
/// Constructs a new search against the provided history, querying *all*
/// items.
///
/// # Arguments
///
/// * `history` - The history to search through.
pub fn all(history: &'a History) -> Self {
Self::new(history, Query::default())
}
/// Constructs a new search against the provided history, using the given
/// query.
///
/// # Arguments
///
/// * `history` - The history to search through.
/// * `query` - The query to use.
pub fn new(history: &'a History, query: Query) -> Self {
// The items within the query's ID bounds are a run of them, since
// they're in ID order: search just those positions.
let start = query.not_at_or_before_id.map_or(0, |id| {
id.checked_add(1)
.map_or(history.items.len(), |id| history.position_before(id))
});
let end = query
.not_at_or_after_id
.map_or(history.items.len(), |id| history.position_before(id));
Self {
history,
positions: start..end.max(start),
query,
count: 0,
}
}
}
impl<'a> Iterator for Search<'a> {
type Item = &'a Item;
fn next(&mut self) -> Option<Self::Item> {
// Once we hit the limit, we stop searching.
#[expect(clippy::cast_possible_truncation)]
#[expect(clippy::cast_sign_loss)]
if self
.query
.max_items
.is_some_and(|max_items| self.count >= max_items as usize)
{
return None;
}
loop {
let index = match self.query.direction {
Direction::Forward => self.positions.next(),
Direction::Backward => self.positions.next_back(),
}?;
let item = self.history.items.get(index)?;
if self.query.includes(item) {
self.count += 1;
return Some(item);
}
}
}
}
#[cfg(test)]
#[allow(clippy::panic_in_result_fn, reason = "assertions in a fallible test")]
mod tests {
use super::*;
fn history(lines: &[&str]) -> Result<History, error::Error> {
let mut history = History::default();
for line in lines {
history.add(Item::new(*line))?;
}
Ok(history)
}
fn command_lines<'a>(items: impl Iterator<Item = &'a Item>) -> Vec<&'a str> {
items.map(|item| item.command_line.as_str()).collect()
}
/// Removing an item shifts the positions of those after it (as `history -d`
/// shows), but no item's ID changes: an ID names its item for good.
#[test]
fn ids_are_stable_across_removal() -> Result<(), error::Error> {
let mut history = history(&["a", "b", "c", "d"])?;
assert!(history.remove_nth_item(1));
assert_eq!(
history.get(1).map(|item| item.command_line.as_str()),
Some("c")
);
assert_eq!(
history.get_by_id(2)?.map(|item| item.command_line.as_str()),
Some("c")
);
history.delete_item_by_id(0)?;
assert_eq!(command_lines(history.iter()), ["c", "d"]);
assert!(history.get_by_id(0)?.is_none());
assert!(history.get_by_id(1)?.is_none());
assert_eq!(history.get_by_id(3)?.map(|item| item.id), Some(3));
// New items still get new IDs.
assert_eq!(history.add(Item::new("e"))?, 4);
assert_eq!(
history.get_by_id(4)?.map(|item| item.command_line.as_str()),
Some("e")
);
// Even after clearing history: an old ID never comes to name a new
// item.
history.clear()?;
assert_eq!(history.add(Item::new("f"))?, 5);
assert!(history.get_by_id(0)?.is_none());
Ok(())
}
/// Updating an item keeps its ID, whatever ID the new item carries.
#[test]
fn update_by_id_keeps_the_id() -> Result<(), error::Error> {
let mut history = history(&["a", "b"])?;
history.update_by_id(
1,
Item {
id: 99,
..Item::new("x")
},
)?;
let updated = history
.get_by_id(1)?
.map(|item| (item.id, item.command_line.as_str()));
assert_eq!(updated, Some((1, "x")));
assert!(history.get_by_id(99)?.is_none());
assert!(history.update_by_id(7, Item::new("y")).is_err());
Ok(())
}
#[test]
fn search_by_direction_bounds_and_prefix() -> Result<(), error::Error> {
let history = history(&["echo hi there", "ls", "echo hello world", "cd /tmp"])?;
let search = |query| history.search(query).map(command_lines);
let prefix = |text: &str| Some(CommandLineFilter::Prefix(text.to_owned()));
// The newest match, as a hint asks for...
let newest = Query {
direction: Direction::Backward,
max_items: Some(1),
command_line_filter: prefix("echo h"),
..Query::default()
};
assert_eq!(search(newest)?, ["echo hello world"]);
// ...and older than a given ID, as history navigation asks for.
let older = Query {
direction: Direction::Backward,
not_at_or_after_id: Some(2),
command_line_filter: prefix("echo h"),
..Query::default()
};
assert_eq!(search(older)?, ["echo hi there"]);
let newer = Query {
not_at_or_before_id: Some(0),
not_at_or_after_id: Some(3),
..Query::default()
};
assert_eq!(search(newer)?, ["ls", "echo hello world"]);
// Newer than a given ID, with no upper bound, as moving down through
// history asks.
let down = Query {
not_at_or_before_id: Some(1),
..Query::default()
};
assert_eq!(search(down)?, ["echo hello world", "cd /tmp"]);
// By substring (as Ctrl-R searches) or exact match, or with no room for
// any.
let substring = Query {
direction: Direction::Backward,
command_line_filter: Some(CommandLineFilter::Contains("hel".to_owned())),
..Query::default()
};
assert_eq!(search(substring)?, ["echo hello world"]);
let exact = Query {
command_line_filter: Some(CommandLineFilter::Exact("ls".to_owned())),
..Query::default()
};
assert_eq!(search(exact)?, ["ls"]);
let no_room = Query {
max_items: Some(0),
..Query::default()
};
assert_eq!(search(no_room)?, Vec::<&str>::new());
let none = Query {
direction: Direction::Backward,
command_line_filter: prefix("echo hix"),
..Query::default()
};
assert_eq!(search(none)?, Vec::<&str>::new());
Ok(())
}
/// A search finds just what checking each item would, in order, whatever
/// its direction, ID bounds, limit, and filter -- including bounds at
/// deleted IDs and at the extremes. And an item is found by its ID just
/// as a scan would find it.
#[test]
fn search_and_get_by_id_match_a_scan() -> Result<(), error::Error> {
for len in 0..6_i64 {
for deleted in 0..(1 << len) {
let mut history = History::default();
for id in 0..len {
history.add(Item::new(if id % 2 == 0 { "even" } else { "odd" }))?;
}
for id in (0..len).filter(|id| deleted & (1 << id) != 0) {
history.delete_item_by_id(id)?;
}
let scan = || (0..history.count()).filter_map(|index| history.get(index));
let bounds = [None, Some(ItemId::MIN), Some(-1), Some(0), Some(2)]
.into_iter()
.chain([Some(len - 1), Some(len), Some(ItemId::MAX)]);
for lower in bounds.clone() {
for upper in bounds.clone() {
for backward in [false, true] {
for max_items in [None, Some(0), Some(1), Some(2), Some(-1)] {
for filter in [None, Some("odd")] {
let query = || Query {
direction: if backward {
Direction::Backward
} else {
Direction::Forward
},
not_at_or_before_id: lower,
not_at_or_after_id: upper,
max_items,
command_line_filter: filter
.map(|f| CommandLineFilter::Prefix(f.to_owned())),
..Query::default()
};
let found: Vec<ItemId> =
history.search(query())?.map(|item| item.id).collect();
let mut expected: Vec<ItemId> = scan()
.filter(|item| query().includes(item))
.map(|item| item.id)
.collect();
if backward {
expected.reverse();
}
// A negative limit is no limit.
if let Some(max) =
max_items.and_then(|m| usize::try_from(m).ok())
{
expected.truncate(max);
}
assert_eq!(
found, expected,
"len {len}, deleted {deleted:b}, bounds {lower:?}..{upper:?}, backward {backward}, max {max_items:?}, filter {filter:?}"
);
}
}
}
}
}
for id in -1..=len {
let by_scan = scan().find(|item| item.id == id).map(|item| item.id);
assert_eq!(history.get_by_id(id)?.map(|item| item.id), by_scan);
}
}
}
Ok(())
}
/// Appending unsaved items writes just those, once; writing everything
/// writes it all.
#[test]
fn flush_writes_unsaved_items_once() -> Result<(), error::Error> {
let dir = tempfile::tempdir()?;
let path = dir.path().join("history");
let read = || std::fs::read_to_string(&path);
// Imported items are already saved; added ones aren't.
let mut history = History::import(&b"old\n"[..])?;
history.add(Item::new("new1"))?;
history.add(Item::new("new2"))?;
history.flush(&path, true, true, false)?;
assert_eq!(read()?, "new1\nnew2\n");
history.flush(&path, true, true, false)?;
assert_eq!(read()?, "new1\nnew2\n");
history.flush(&path, false, false, false)?;
assert_eq!(read()?, "old\nnew1\nnew2\n");
Ok(())
}
/// Each of a query's filters, on its own. ID and time bounds are exclusive,
/// and an item with no timestamp isn't excluded by time bounds.
#[test]
fn query_includes_by_each_filter() {
let at = |seconds| ItemTimestamp::from_timestamp(seconds, 0);
let item = |id, command_line: &str, timestamp| Item {
id,
command_line: command_line.to_owned(),
timestamp,
dirty: false,
};
assert!(Query::default().includes(&item(5, "echo hi", None)));
let after_id = Query {
not_at_or_before_id: Some(2),
..Query::default()
};
assert!(!after_id.includes(&item(2, "x", None)));
assert!(after_id.includes(&item(3, "x", None)));
let before_id = Query {
not_at_or_after_id: Some(2),
..Query::default()
};
assert!(!before_id.includes(&item(2, "x", None)));
assert!(before_id.includes(&item(1, "x", None)));
let after_time = Query {
not_at_or_before_time: at(100),
..Query::default()
};
assert!(!after_time.includes(&item(0, "x", at(100))));
assert!(after_time.includes(&item(0, "x", at(101))));
assert!(after_time.includes(&item(0, "x", None)));
let before_time = Query {
not_at_or_after_time: at(100),
..Query::default()
};
assert!(!before_time.includes(&item(0, "x", at(100))));
assert!(before_time.includes(&item(0, "x", at(99))));
assert!(before_time.includes(&item(0, "x", None)));
for (filter, matching, other) in [
(CommandLineFilter::Prefix("ec".to_owned()), "echo", "sec"),
(CommandLineFilter::Suffix("ho".to_owned()), "echo", "hot"),
(CommandLineFilter::Contains("ch".to_owned()), "echo", "cd"),
(CommandLineFilter::Exact("echo".to_owned()), "echo", "echo "),
] {
let query = Query {
command_line_filter: Some(filter),
..Query::default()
};
assert!(query.includes(&item(0, matching, None)), "{matching:?}");
assert!(!query.includes(&item(0, other, None)), "{other:?}");
}
}
/// An empty history, positions past the end, and IDs no item has.
#[test]
fn empty_history_and_missing_items() -> Result<(), error::Error> {
let mut empty = History::default();
assert!(empty.is_empty());
assert_eq!(empty.count(), 0);
assert!(empty.get(0).is_none());
assert!(empty.get_by_id(0)?.is_none());
assert!(!empty.remove_nth_item(0));
empty.delete_item_by_id(0)?;
assert!(empty.update_by_id(0, Item::new("x")).is_err());
assert_eq!(empty.iter().count(), 0);
assert_eq!(empty.search(Query::default())?.count(), 0);
let mut history = history(&["a", "b", "c"])?;
assert!(!history.is_empty());
assert_eq!(history.count(), 3);
assert!(history.get(3).is_none());
assert!(!history.remove_nth_item(3));
history.delete_item_by_id(7)?;
assert_eq!(command_lines(history.iter()), ["a", "b", "c"]);
// Updating an item changes just it, where it is.
history.update_by_id(1, Item::new("B"))?;
assert_eq!(command_lines(history.iter()), ["a", "B", "c"]);
assert_eq!(command_lines(Search::all(&history)), ["a", "B", "c"]);
Ok(())
}
/// A new item is unsaved and stamped now; adding it gives it the next ID.
#[test]
fn new_items_are_unsaved_and_stamped() -> Result<(), error::Error> {
let before = Utc::now();
let item = Item::new("ls");
assert!(item.dirty);
assert!(item.timestamp.is_some_and(|ts| ts >= before));
let mut history = history(&["a"])?;
assert_eq!(history.add(item)?, 1);
assert_eq!(history.get(1).map(|item| item.id), Some(1));
Ok(())
}
/// Importing reads command lines and the timestamp comments before them
/// (the last of several; a comment that isn't one clears it), skips
/// unreadable lines, and leaves the items saved and numbered from 0.
#[test]
fn import_reads_history_files() -> Result<(), error::Error> {
let before = Utc::now();
let file = b"#1700000000\na\n#1700000005\n#not a time\nb\n#1700000001\n#1700000002\nc\n\xff\xfe\nd\n";
let mut history = History::import(&file[..])?;
assert_eq!(command_lines(history.iter()), ["a", "b", "c", "d"]);
let timestamps: Vec<_> = history.iter().map(|item| item.timestamp).collect();
assert_eq!(
timestamps[0],
ItemTimestamp::from_timestamp(1_700_000_000, 0)
);
assert!(timestamps[1].is_some_and(|ts| ts >= before));
assert_eq!(
timestamps[2],
ItemTimestamp::from_timestamp(1_700_000_002, 0)
);
assert!(timestamps[3].is_some_and(|ts| ts >= before));
assert!(history.iter().all(|item| !item.dirty));
assert_eq!(
history.iter().map(|item| item.id).collect::<Vec<_>>(),
[0, 1, 2, 3]
);
assert_eq!(history.add(Item::new("e"))?, 4);
assert!(History::import(&b""[..])?.is_empty());
Ok(())
}
/// Flushing appends to what's already in the file, with the timestamps of
/// items that have them if asked; importing that reads them back.
#[test]
fn flush_and_import_round_trip_with_timestamps() -> Result<(), error::Error> {
let dir = tempfile::tempdir()?;
let path = dir.path().join("history");
std::fs::write(&path, "existing\n")?;
let mut history = History::default();
history.add(Item {
timestamp: ItemTimestamp::from_timestamp(1_700_000_000, 0),
..Item::new("a")
})?;
history.add(Item {
timestamp: None,
..Item::new("b")
})?;
history.flush(&path, true, true, true)?;
assert_eq!(
std::fs::read_to_string(&path)?,
"existing\n#1700000000\na\nb\n"
);
let reread = History::import(std::fs::File::open(&path)?)?;
assert_eq!(command_lines(reread.iter()), ["existing", "a", "b"]);
assert_eq!(
reread.get(1).and_then(|item| item.timestamp),
ItemTimestamp::from_timestamp(1_700_000_000, 0)
);
Ok(())
}
#[test]
fn last_and_retain() -> Result<(), error::Error> {
assert!(History::default().last().is_none());
let mut history = history(&["a", "x", "b", "x", "c", "x"])?;
assert_eq!(
history.last().map(|item| item.command_line.as_str()),
Some("x")
);
// Keeping everything changes nothing.
history.retain(|_| true);
assert_eq!(
command_lines(history.iter()),
["a", "x", "b", "x", "c", "x"]
);
// Removing some keeps the rest in order, with their IDs.
history.retain(|item| item.command_line != "x");
assert_eq!(command_lines(history.iter()), ["a", "b", "c"]);
assert_eq!(
history.iter().map(|item| item.id).collect::<Vec<_>>(),
[0, 2, 4]
);
assert_eq!(
history.get_by_id(4)?.map(|item| item.command_line.as_str()),
Some("c")
);
history.retain(|item| item.command_line != "a");
assert_eq!(command_lines(history.iter()), ["b", "c"]);
history.retain(|_| false);
assert!(history.is_empty());
Ok(())
}
/// `HISTCONTROL` parses as bash reads it: `ignoreboth` is two values, and
/// unknown values, empty ones, and other cases are ignored.
#[test]
fn history_control_parses_like_bash() {
use HistoryControl::{EraseDups, IgnoreDups, IgnoreSpace};
let parse = |value| {
HistoryControl::parse_list(value)
.into_iter()
.collect::<Vec<_>>()
};
assert_eq!(parse("ignoredups"), [IgnoreDups]);
assert_eq!(parse("ignorespace"), [IgnoreSpace]);
assert_eq!(parse("erasedups"), [EraseDups]);
assert_eq!(parse("ignoreboth"), [IgnoreSpace, IgnoreDups]);
assert_eq!(parse("erasedups:ignorespace"), [IgnoreSpace, EraseDups]);
assert_eq!(parse("bogus::ignoredups:"), [IgnoreDups]);
assert_eq!(parse("IGNOREDUPS"), []);
assert_eq!(parse(""), []);
}
/// Searches and iterators over history can be held across an await in a
/// spawned task.
#[test]
fn searches_are_send_and_sync() -> Result<(), error::Error> {
const fn assert_send_sync<T: Send + Sync>(_: &T) {}
let history = history(&["a"])?;
assert_send_sync(&history.iter());
assert_send_sync(&history.search(Query::default())?);
Ok(())
}
/// Like bash, an entry the file gives no timestamp gets the time it's read.
#[test]
fn import_stamps_entries_without_timestamps() -> Result<(), error::Error> {
let before = Utc::now();
let history = History::import(&b"#1700000000\nls\necho hi\n"[..])?;
let timestamps: Vec<_> = history.iter().map(|item| item.timestamp).collect();
assert_eq!(
timestamps[0],
ItemTimestamp::from_timestamp(1_700_000_000, 0)
);
assert!(timestamps[1].is_some_and(|ts| ts >= before));
Ok(())
}
}