view.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. use fxhash::{FxHashMap, FxHashSet};
  2. //use log::debug;
  3. use tui::widgets::ListState;
  4. use crate::model::NodeInfo;
  5. #[derive(Clone)]
  6. pub struct View {
  7. pub id_list: IdListView,
  8. pub info_list: InfoListView,
  9. }
  10. impl View {
  11. pub fn new(id_list: IdListView, info_list: InfoListView) -> View {
  12. View { id_list, info_list }
  13. }
  14. pub fn update(&mut self, infos: FxHashMap<String, NodeInfo>) {
  15. for (id, info) in infos {
  16. self.id_list.node_id.insert(id.clone());
  17. self.info_list.infos.insert(id, info);
  18. }
  19. }
  20. }
  21. #[derive(Clone)]
  22. pub struct IdListView {
  23. pub state: ListState,
  24. pub node_id: FxHashSet<String>,
  25. }
  26. impl IdListView {
  27. pub fn new(node_id: FxHashSet<String>) -> IdListView {
  28. IdListView { state: ListState::default(), node_id }
  29. }
  30. pub fn next(&mut self) {
  31. let i = match self.state.selected() {
  32. Some(i) => {
  33. if i >= self.node_id.len() - 1 {
  34. 0
  35. } else {
  36. i + 1
  37. }
  38. }
  39. None => 0,
  40. };
  41. self.state.select(Some(i));
  42. }
  43. pub fn previous(&mut self) {
  44. let i = match self.state.selected() {
  45. Some(i) => {
  46. if i == 0 {
  47. self.node_id.len() - 1
  48. } else {
  49. i - 1
  50. }
  51. }
  52. None => 0,
  53. };
  54. self.state.select(Some(i));
  55. }
  56. pub fn unselect(&mut self) {
  57. self.state.select(None);
  58. }
  59. }
  60. #[derive(Clone)]
  61. pub struct InfoListView {
  62. pub index: usize,
  63. pub infos: FxHashMap<String, NodeInfo>,
  64. }
  65. impl InfoListView {
  66. pub fn new(infos: FxHashMap<String, NodeInfo>) -> InfoListView {
  67. let index = 0;
  68. InfoListView { index, infos }
  69. }
  70. pub async fn next(&mut self) {
  71. self.index = (self.index + 1) % self.infos.len();
  72. }
  73. pub async fn previous(&mut self) {
  74. if self.index > 0 {
  75. self.index -= 1;
  76. } else {
  77. self.index = self.infos.len() - 1;
  78. }
  79. }
  80. }