Skip to content

Commit bc60820

Browse files
committed
Simplify LastIndex, only requires checked fn for rest to work.
1 parent cc5d573 commit bc60820

6 files changed

Lines changed: 38 additions & 47 deletions

File tree

src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ impl App {
13211321

13221322
Some(PopupMenu::Macros) => {
13231323
if self.popup_table_state.selected()
1324-
>= Some(self.macros.visible_len().saturating_sub(1))
1324+
>= self.macros.last_index_checked()
13251325
{
13261326
self.popup_table_state.select(None);
13271327
self.popup_single_line_state.active = true;

src/keybinds.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ctrl-d = "toggle-indices"
5050
ctrl-f = "reload-colors"
5151
F20 = "esp-hard-reset"
5252
F21 = "esp-bootloader"
53+
shift-F21 = "esp-bootloader-unchecked"
5354
ctrl-z = "esp-bootloader-unchecked"
5455
5556
[macros]
@@ -66,8 +67,6 @@ F18 = "PIO Core v2"
6667

6768
use crate::macros::MacroNameTag;
6869

69-
// TODO use ; to chain macros
70-
7170
#[derive(Serialize, Deserialize)]
7271
pub struct Keybinds {
7372
pub keybindings: IndexMap<KeyCombination, String>,

src/macros/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use tracing::{error, info};
2424
use tui_input::Input;
2525

2626
use crate::{
27-
keybinds::Keybinds, traits::HasEscapedBytes, tui::single_line_selector::SingleLineSelectorState,
27+
keybinds::Keybinds,
28+
traits::{HasEscapedBytes, LastIndex},
29+
tui::single_line_selector::SingleLineSelectorState,
2830
};
2931

3032
mod macro_nametag;
@@ -343,6 +345,18 @@ impl Macros {
343345
Ok(())
344346
}
345347
}
348+
349+
impl LastIndex for Macros {
350+
fn last_index_checked(&self) -> Option<usize> {
351+
let visible = self.visible_len();
352+
if visible == 0 {
353+
None
354+
} else {
355+
Some(visible - 1)
356+
}
357+
}
358+
}
359+
346360
#[derive(Debug, serde::Deserialize)]
347361
struct MacroFile {
348362
#[serde(default)]

src/traits.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,41 @@ use crate::tui::buffer::LineEnding;
1313

1414
/// Trait that provides simple methods to get the last valid index of a collection or slice.
1515
pub trait LastIndex {
16+
/// Returns the index of the last element in the collection.
17+
///
18+
/// Returns `None` if the collection is empty.
19+
///
20+
/// Under most cirumstances, this should be the only trait function you need to define.
21+
fn last_index_checked(&self) -> Option<usize>;
1622
/// Returns `true` if the given index matches the index of the last element in the collection.
1723
///
1824
/// Returns `false` if the index doesn't match, or if the collection is empty.
19-
fn last_index_eq(&self, index: usize) -> bool;
25+
fn last_index_eq(&self, index: usize) -> bool {
26+
if let Some(last_index) = self.last_index_checked() {
27+
last_index == index
28+
} else {
29+
false
30+
}
31+
}
2032
/// Returns `true` if the given index matches or is greater than the index of the last element in the collection.
2133
///
2234
/// Returns `false` if the index doesn't fit either condition, or if the collection is empty.
23-
fn last_index_eq_or_greater(&self, index: usize) -> bool;
35+
fn last_index_eq_or_under(&self, index: usize) -> bool {
36+
if let Some(last_index) = self.last_index_checked() {
37+
index >= last_index
38+
} else {
39+
false
40+
}
41+
}
2442
/// Returns the index of the last element in the collection.
2543
///
2644
/// **Panics** if the collection is empty.
2745
fn last_index(&self) -> usize {
2846
self.last_index_checked()
2947
.expect("empty collection; no final element exists")
3048
}
31-
/// Returns the index of the last element in the collection.
32-
///
33-
/// Returns `None` if the collection is empty.
34-
fn last_index_checked(&self) -> Option<usize>;
3549
}
3650
impl<T> LastIndex for [T] {
37-
fn last_index_eq(&self, index: usize) -> bool {
38-
if self.is_empty() {
39-
false
40-
} else if index == self.len() - 1 {
41-
true
42-
} else {
43-
false
44-
}
45-
}
46-
fn last_index_eq_or_greater(&self, index: usize) -> bool {
47-
if self.is_empty() {
48-
false
49-
} else if index >= self.len() - 1 {
50-
true
51-
} else {
52-
false
53-
}
54-
}
5551
fn last_index_checked(&self) -> Option<usize> {
5652
if self.is_empty() {
5753
None

src/tui/esp.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,24 +496,6 @@ impl LastIndex for EspFlashState {
496496
Some((self.elfs.len() + self.bins.len()) - 1)
497497
}
498498
}
499-
fn last_index_eq(&self, index: usize) -> bool {
500-
if self.is_empty() {
501-
false
502-
} else if index == (self.elfs.len() + self.bins.len()) - 1 {
503-
true
504-
} else {
505-
false
506-
}
507-
}
508-
fn last_index_eq_or_greater(&self, index: usize) -> bool {
509-
if self.is_empty() {
510-
false
511-
} else if index >= (self.elfs.len() + self.bins.len()) - 1 {
512-
true
513-
} else {
514-
false
515-
}
516-
}
517499
}
518500

519501
pub const ESPFLASH_BUTTON_COUNT: usize = 4;

src/tui/single_line_selector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl StatefulWidget for &SingleLineSelector<'_> {
102102
type State = SingleLineSelectorState;
103103

104104
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
105-
if self.items.last_index_eq_or_greater(state.current_index) {
105+
if self.items.last_index_eq_or_under(state.current_index) {
106106
state.current_index = self.items.last_index();
107107
}
108108

0 commit comments

Comments
 (0)