mirror of
https://github.com/Xithrius/twitch-tui.git
synced 2024-10-05 17:48:50 +03:00
Storage::get_last_n
. Storage:add
pushes back if item already exists
This commit is contained in:
parent
1a3cc339ef
commit
a0918ee8ca
@ -78,7 +78,10 @@ impl Storage {
|
||||
pub fn add(&mut self, key: &str, value: String) {
|
||||
if ITEM_KEYS.contains(&key) {
|
||||
if let Some(item) = self.items.get_mut(&key.to_string()) {
|
||||
if !item.content.contains(&value) && item.enabled {
|
||||
if item.enabled {
|
||||
if let Some(position) = item.content.iter().position(|x| x == &value) {
|
||||
item.content.remove(position);
|
||||
}
|
||||
item.content.push(value);
|
||||
}
|
||||
}
|
||||
@ -97,6 +100,22 @@ impl Storage {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_last_n(&self, key: &str, n: usize, reverse: bool) -> Vec<String> {
|
||||
let items = self.get(key);
|
||||
|
||||
let mut out = if items.len() <= n {
|
||||
items
|
||||
} else {
|
||||
items[items.len() - n..].to_vec()
|
||||
};
|
||||
|
||||
if reverse {
|
||||
out.reverse();
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub fn contains(&self, key: &str, value: &str) -> bool {
|
||||
self.get(key).contains(&value.to_string())
|
||||
}
|
||||
|
@ -238,19 +238,20 @@ pub async fn handle_stateful_user_input(
|
||||
return Some(TerminalAction::Quitting);
|
||||
}
|
||||
Key::Enter => {
|
||||
app.clear_messages();
|
||||
app.set_state(State::Normal);
|
||||
}
|
||||
Key::Char(c) => {
|
||||
app.clear_messages();
|
||||
|
||||
if let Some(selection) = c.to_digit(10) {
|
||||
let channels = app
|
||||
.storage
|
||||
.get("channels")
|
||||
.drain(0..5)
|
||||
.collect::<Vec<String>>();
|
||||
let channels = app.storage.get_last_n("channels", 5, true);
|
||||
|
||||
if let Some(channel) = channels.get(selection as usize) {
|
||||
app.set_state(State::Normal);
|
||||
tx.send(TwitchAction::Join(channel.to_string())).unwrap();
|
||||
config.twitch.channel = channel.to_string();
|
||||
app.storage.add("channels", channel.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ fn render_channel_selection_widget<T: Backend>(
|
||||
|
||||
frame.render_widget(channel_selection_title, *v_chunks.next().unwrap());
|
||||
|
||||
let items_binding = app.storage.get("channels");
|
||||
let items_binding = app.storage.get_last_n("channels", 5, true);
|
||||
|
||||
let items = items_binding
|
||||
.iter()
|
||||
@ -76,11 +76,22 @@ fn render_channel_selection_widget<T: Backend>(
|
||||
})
|
||||
.collect::<Vec<ListItem>>();
|
||||
|
||||
let list = List::new(items)
|
||||
let channel_options = List::new(items)
|
||||
.style(Style::default().fg(Color::White))
|
||||
.highlight_style(Style::default().add_modifier(Modifier::ITALIC));
|
||||
|
||||
frame.render_widget(list, *v_chunks.next().unwrap());
|
||||
frame.render_widget(channel_options, *v_chunks.next().unwrap());
|
||||
}
|
||||
|
||||
fn render_quit_selection_widget<T: Backend>(frame: &mut Frame<T>, v_chunks: &mut Iter<Rect>) {
|
||||
let quit_option = Paragraph::new(Spans::from(vec![
|
||||
Span::raw("["),
|
||||
Span::styled("q", Style::default().fg(Color::Red)),
|
||||
Span::raw("] "),
|
||||
Span::raw("Quit"),
|
||||
]));
|
||||
|
||||
frame.render_widget(quit_option, *v_chunks.next().unwrap());
|
||||
}
|
||||
|
||||
pub fn render_dashboard_ui<T: Backend>(
|
||||
@ -95,7 +106,8 @@ pub fn render_dashboard_ui<T: Backend>(
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(2),
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(5),
|
||||
Constraint::Min(6),
|
||||
Constraint::Min(1),
|
||||
])
|
||||
.margin(2)
|
||||
.split(frame.size());
|
||||
@ -105,4 +117,6 @@ pub fn render_dashboard_ui<T: Backend>(
|
||||
render_dashboard_title_widget(frame, &mut v_chunks);
|
||||
|
||||
render_channel_selection_widget(frame, &mut v_chunks, app, config.twitch.channel.clone());
|
||||
|
||||
render_quit_selection_widget(frame, &mut v_chunks);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user