Skip to content

Commit 59c4c04

Browse files
committed
sh: fix todo
1 parent 24c0d86 commit 59c4c04

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

sh/src/shell/history.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,11 @@ impl History {
9898
match end_point {
9999
EndPoint::CommandNumber(n) => {
100100
if let Some(first) = self.entries.front() {
101-
if n <= first.command_number {
102-
// we wrapped around, look for the command from the back
103-
// TODO: I think this can be done in constant time
104-
Ok(self
105-
.entries
106-
.iter()
107-
.rposition(|e| e.command_number == n)
108-
.unwrap_or(self.entries.len() - 1))
101+
if n < first.command_number {
102+
// safe since there is at least one element
103+
let last_command_number = self.entries.back().unwrap().command_number;
104+
let commands_in_between = (last_command_number - n) as usize;
105+
Ok(self.entries.len() - commands_in_between - 1)
109106
} else {
110107
Ok((n - first.command_number) as usize)
111108
}

0 commit comments

Comments
 (0)