xenmotif/src/structs.rs

95 lines
2.3 KiB
Rust

use mpd::Client;
use tui::{
widgets::{ListState},
};
use paris::error;
use std::process::exit;
pub struct StatefulList<T> {
pub state: ListState,
pub items: Vec<T>,
}
impl<T> StatefulList<T> {
pub fn with_items(items: Vec<T>) -> StatefulList<T> {
StatefulList {
state: ListState::default(),
items,
}
}
pub fn next(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i >= self.items.len() - 1 {
0
} 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 {
self.items.len() - 1
} else {
i - 1
}
}
None => 0,
};
self.state.select(Some(i));
}
pub fn unselect(&mut self) {
self.state.select(None);
}
}
/// This struct holds the current state of the app. In particular, it has the `items` field which is a wrapper
/// around `ListState`. Keeping track of the items state let us render the associated widget with its state
/// and have access to features such as natural scrolling.
///
/// Check the event handling at the bottom to see how to change the state on incoming events.
/// Check the drawing logic for items on how to specify the highlighting style for selected items.
pub struct App<'a> {
pub items: StatefulList<(&'a str, usize)>,
pub mpd_client: Client,
pub input: String,
pub input_mode: InputMode,
pub messages: Vec<String>,
}
impl<'a> App<'a> {
pub fn new() -> App<'a> {
let client = match Client::connect("127.0.0.1:6600") {
Ok(conn) => {
conn
},
Err(e) => {
error!("Could not connect to MPD daemon: {}", e);
exit(1);
}
};
App {
items: StatefulList::with_items(vec![("Item0", 1), ("Item1", 2), ("Item2", 3)]),
mpd_client: client,
input: String::new(),
input_mode: InputMode::Normal,
messages: Vec::new(),
}
}
}
pub enum InputMode {
Normal,
Editing,
}