1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::{collections::HashMap, time::Instant};
use clashctl_interactive::{Noop, RuleSort};
use smart_default::SmartDefault;
use crate::{
clashctl::model::{ConnectionWithSpeed, Log, Rule, Traffic, Version},
components::{MovableListManage, MovableListManager, MovableListState, ProxyTree},
Action, Event, InputEvent, Result, UpdateEvent,
};
pub(crate) type LogListState<'a> = MovableListState<'a, Log, Noop>;
pub(crate) type ConListState<'a> = MovableListState<'a, ConnectionWithSpeed, Noop>;
pub(crate) type RuleListState<'a> = MovableListState<'a, Rule, RuleSort>;
pub(crate) type DebugListState<'a> = MovableListState<'a, Event, Noop>;
#[derive(Debug, Clone, SmartDefault)]
pub struct TuiStates<'a> {
pub should_quit: bool,
#[default(_code = "Instant::now()")]
pub start_time: Instant,
pub version: Option<Version>,
pub traffics: Vec<Traffic>,
pub max_traffic: Traffic,
pub all_events_recv: usize,
pub page_index: u8,
pub show_debug: bool,
pub proxy_tree: ProxyTree<'a>,
pub rule_freq: HashMap<String, usize>,
pub con_size: (u64, u64),
#[default(_code = "{
let mut ret = MovableListState::default();
ret.with_index().dsc_index();
ret
}")]
pub log_state: LogListState<'a>,
pub con_state: ConListState<'a>,
pub rule_state: RuleListState<'a>,
pub debug_state: DebugListState<'a>,
}
impl<'a> TuiStates<'a> {
pub const TITLES: &'static [&'static str] = &[
"Status", "Proxies", "Rules", "Conns", "Logs", "Configs", "Debug",
];
pub fn handle(&mut self, event: Event) -> Result<Option<Action>> {
self.all_events_recv += 1;
if self.debug_state.len() >= 300 {
let _ = self.drop_events(100);
}
self.debug_state.push(event.to_owned());
match event {
Event::Quit => {
self.should_quit = true;
Ok(None)
}
Event::Input(event) => self.handle_input(event),
Event::Update(update) => self.handle_update(update),
_ => Ok(None),
}
}
#[inline]
pub fn page_len(&mut self) -> usize {
if self.show_debug {
Self::TITLES.len()
} else {
Self::TITLES.len() - 1
}
}
#[inline]
pub fn title(&self) -> &str {
Self::TITLES[self.page_index as usize]
}
fn active_list<'own>(&'own mut self) -> Option<MovableListManager<'a, 'own>> {
match self.title() {
"Rules" => Some(MovableListManager::Rule(&mut self.rule_state)),
"Debug" => Some(MovableListManager::Event(&mut self.debug_state)),
"Logs" => Some(MovableListManager::Log(&mut self.log_state)),
"Conns" => Some(MovableListManager::Connection(&mut self.con_state)),
"Proxies" => Some(MovableListManager::Proxy(&mut self.proxy_tree)),
_ => None,
}
}
fn handle_update(&mut self, update: UpdateEvent) -> Result<Option<Action>> {
match update {
UpdateEvent::Connection(connection) => {
self.con_size = (connection.upload_total, connection.download_total);
self.con_state.sorted_merge(connection.connections);
self.con_state.with_index();
}
UpdateEvent::Version(version) => self.version = Some(version),
UpdateEvent::Traffic(traffic) => {
let Traffic { up, down } = traffic;
self.max_traffic.up = self.max_traffic.up.max(up);
self.max_traffic.down = self.max_traffic.down.max(down);
self.traffics.push(traffic)
}
UpdateEvent::Proxies(proxies) => {
let mut new_tree = Into::<ProxyTree>::into(proxies);
new_tree.sort_groups_with_frequency(&self.rule_freq);
self.proxy_tree.replace_with(new_tree);
}
UpdateEvent::Log(log) => self.log_state.push(log),
UpdateEvent::Rules(rules) => {
self.rule_freq = rules.owned_frequency();
self.rule_state.sorted_merge(rules.rules);
self.rule_state.with_index();
}
UpdateEvent::ProxyTestLatencyDone => {
self.proxy_tree.end_testing();
}
}
Ok(None)
}
fn handle_input(&mut self, event: InputEvent) -> Result<Option<Action>> {
match event {
InputEvent::TabGoto(index) => {
if index >= 1 && index <= self.page_len() as u8 {
self.page_index = index - 1
}
}
InputEvent::ToggleDebug => {
self.show_debug = !self.show_debug;
if self.page_index == Self::TITLES.len() as u8 - 1 {
self.page_index -= 1;
} else if self.show_debug {
self.page_index = self.debug_page_index()
}
}
InputEvent::Esc => {
if let Some(mut list) = self.active_list() {
list.end();
}
}
InputEvent::ToggleHold => {
if let Some(mut list) = self.active_list() {
list.toggle();
}
}
InputEvent::List(list_event) => {
if let Some(mut list) = self.active_list() {
return Ok(list.handle(list_event));
}
}
InputEvent::TestLatency => {
if self.title() == "Proxies" && !self.proxy_tree.is_testing() {
self.proxy_tree.start_testing();
let group = self.proxy_tree.current_group();
let proxies = group
.members()
.iter()
.filter(|x| x.proxy_type().is_normal())
.map(|x| x.name().into())
.collect();
return Ok(Some(Action::TestLatency { proxies }));
}
}
InputEvent::NextSort => {
if let Some(mut list) = self.active_list() {
list.next_sort();
}
}
InputEvent::PrevSort => {
if let Some(mut list) = self.active_list() {
list.prev_sort();
}
}
InputEvent::Other(_) => {}
}
Ok(None)
}
pub const fn debug_page_index(&self) -> u8 {
Self::TITLES.len() as u8 - 1
}
fn drop_events(&mut self, num: usize) -> impl Iterator<Item = Event> + '_ {
self.debug_state.drain(..num)
}
}