view.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. debug!("NEXT STATE {:?}", i);
  43. }
  44. pub fn previous(&mut self) {
  45. let i = match self.state.selected() {
  46. Some(i) => {
  47. if i == 0 {
  48. self.node_id.len() - 1
  49. } else {
  50. i - 1
  51. }
  52. }
  53. None => 0,
  54. };
  55. self.state.select(Some(i));
  56. debug!("PREV STATE {:?}", i);
  57. }
  58. pub fn unselect(&mut self) {
  59. self.state.select(None);
  60. }
  61. }
  62. #[derive(Clone)]
  63. pub struct InfoListView {
  64. pub index: usize,
  65. pub infos: FxHashMap<String, NodeInfo>,
  66. }
  67. impl InfoListView {
  68. pub fn new(infos: FxHashMap<String, NodeInfo>) -> InfoListView {
  69. let index = 0;
  70. InfoListView { index, infos }
  71. }
  72. pub async fn next(&mut self) {
  73. self.index = (self.index + 1) % self.infos.len();
  74. }
  75. pub async fn previous(&mut self) {
  76. if self.index > 0 {
  77. self.index -= 1;
  78. } else {
  79. self.index = self.infos.len() - 1;
  80. }
  81. }
  82. }
  83. #[derive(Clone)]
  84. pub struct AddrListView {
  85. pub index: usize,
  86. pub infos: FxHashMap<String, NodeInfo>,
  87. }
  88. impl AddrListView {
  89. pub fn new(infos: FxHashMap<String, NodeInfo>) -> AddrListView {
  90. let index = 0;
  91. AddrListView { index, infos }
  92. }
  93. pub async fn next(&mut self) {
  94. self.index = (self.index + 1) % self.infos.len();
  95. }
  96. pub async fn previous(&mut self) {
  97. if self.index > 0 {
  98. self.index -= 1;
  99. } else {
  100. self.index = self.infos.len() - 1;
  101. }
  102. }
  103. }