-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
936 additions
and
717 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use derive_new::new; | ||
use ratatui::widgets::ListState; | ||
|
||
use crate::app::Action; | ||
|
||
pub struct ActionableListItem { | ||
pub label: String, | ||
pub value: Option<String>, | ||
pub action: Option<Action>, | ||
} | ||
|
||
impl ActionableListItem { | ||
pub fn with_label(label: &str) -> Self { | ||
Self { | ||
label: label.to_string(), | ||
value: None, | ||
action: None, | ||
} | ||
} | ||
pub fn with_labelled_value(label: &str, value: &str) -> Self { | ||
Self { | ||
label: label.to_string(), | ||
value: Some(value.to_string()), | ||
action: None, | ||
} | ||
} | ||
pub fn with_action(label: &str, value: &str, action: Action) -> Self { | ||
Self { | ||
label: label.to_string(), | ||
value: Some(value.to_string()), | ||
action: Some(action), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, new)] | ||
pub struct ActionableList { | ||
pub items: Vec<ActionableListItem>, | ||
pub state: ListState, | ||
} | ||
|
||
impl ActionableList { | ||
pub fn reset(&mut self) { | ||
self.state.select(Some(0)); | ||
} | ||
|
||
pub fn next(&mut self) { | ||
let i = match self.state.selected() { | ||
Some(i) => { | ||
if i >= self.items.len().saturating_sub(1) { | ||
i | ||
} else { | ||
i + 1 | ||
} | ||
} | ||
None => 0, | ||
}; | ||
self.state.select(Some(i)); | ||
} | ||
|
||
pub fn previous(&mut self) { | ||
let i = match self.state.selected() { | ||
Some(i) => { | ||
if i == 0 { | ||
i | ||
} else { | ||
i - 1 | ||
} | ||
} | ||
None => 0, | ||
}; | ||
self.state.select(Some(i)); | ||
} | ||
|
||
pub fn select(&mut self) -> Option<Action> { | ||
match self.state.selected() { | ||
Some(i) => { | ||
if let Some(item) = self.items.get(i) { | ||
item.action.clone() | ||
} else { | ||
None | ||
} | ||
} | ||
None => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.