|
| 1 | +use super::app::*; |
| 2 | +use super::render::ui; |
| 3 | +use super::state::subscribe_state; |
| 4 | +use crate::spider; |
| 5 | +use crate::state::IFEXIT; |
| 6 | +use std::io; |
| 7 | +use tui::widgets::ListState; |
| 8 | +use tui::{backend::Backend, Terminal}; |
| 9 | +/// App holds the state of the application |
| 10 | +pub struct AppSub { |
| 11 | + /// Current value of the input box |
| 12 | + pub input: String, |
| 13 | + pub settings_input: Vec<String>, |
| 14 | + /// Current input mode |
| 15 | + pub input_mode: InputMode, |
| 16 | + /// History of recorded messages |
| 17 | + pub messages: Vec<String>, |
| 18 | + pub state: ListState, |
| 19 | + pub index_subscription: ListState, |
| 20 | + pub index_settings: usize, |
| 21 | + pub stateoflist: bool, |
| 22 | + pub show_popup: bool, |
| 23 | + pub informations: Vec<spider::Information>, |
| 24 | + pub subscription: Vec<String>, |
| 25 | +} |
| 26 | +impl AppSub { |
| 27 | + pub fn next(&mut self) { |
| 28 | + let i = match self.state.selected() { |
| 29 | + Some(i) => { |
| 30 | + if i >= self.messages.len() - 1 { |
| 31 | + 0 |
| 32 | + } else { |
| 33 | + i + 1 |
| 34 | + } |
| 35 | + } |
| 36 | + None => 0, |
| 37 | + }; |
| 38 | + self.state.select(Some(i)); |
| 39 | + } |
| 40 | + |
| 41 | + pub fn previous(&mut self) { |
| 42 | + let i = match self.state.selected() { |
| 43 | + Some(i) => { |
| 44 | + if i == 0 { |
| 45 | + self.messages.len() - 1 |
| 46 | + } else { |
| 47 | + i - 1 |
| 48 | + } |
| 49 | + } |
| 50 | + None => 0, |
| 51 | + }; |
| 52 | + self.state.select(Some(i)); |
| 53 | + } |
| 54 | + |
| 55 | + pub fn unselect(&mut self) { |
| 56 | + self.state.select(None); |
| 57 | + } |
| 58 | + pub fn next_sub(&mut self) { |
| 59 | + let i = match self.index_subscription.selected() { |
| 60 | + Some(i) => { |
| 61 | + if i >= self.subscription.len() - 1 { |
| 62 | + 0 |
| 63 | + } else { |
| 64 | + i + 1 |
| 65 | + } |
| 66 | + } |
| 67 | + None => 0, |
| 68 | + }; |
| 69 | + self.index_subscription.select(Some(i)); |
| 70 | + //self.index = Some(i); |
| 71 | + } |
| 72 | + |
| 73 | + pub fn previous_sub(&mut self) { |
| 74 | + let i = match self.index_subscription.selected() { |
| 75 | + Some(i) => { |
| 76 | + if i == 0 { |
| 77 | + self.subscription.len() - 1 |
| 78 | + } else { |
| 79 | + i - 1 |
| 80 | + } |
| 81 | + } |
| 82 | + None => 0, |
| 83 | + }; |
| 84 | + self.index_subscription.select(Some(i)); |
| 85 | + //self.index = Some(i); |
| 86 | + } |
| 87 | + |
| 88 | + pub fn unselect_sub(&mut self) { |
| 89 | + self.index_subscription.select(None); |
| 90 | + } |
| 91 | +} |
| 92 | +impl App for AppSub { |
| 93 | + fn run_app_local<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<IFEXIT> { |
| 94 | + terminal.draw(|f| ui(f, self))?; |
| 95 | + subscribe_state(self) |
| 96 | + } |
| 97 | +} |
| 98 | +impl Default for AppSub { |
| 99 | + fn default() -> AppSub { |
| 100 | + AppSub { |
| 101 | + input: String::new(), |
| 102 | + settings_input: vec![String::new(), String::new()], |
| 103 | + input_mode: InputMode::Normal, |
| 104 | + messages: Vec::new(), |
| 105 | + state: ListState::default(), |
| 106 | + index_subscription: ListState::default(), |
| 107 | + index_settings: 0, |
| 108 | + stateoflist: false, |
| 109 | + show_popup: false, |
| 110 | + informations: Vec::new(), |
| 111 | + subscription: Vec::new(), |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments