Skip to content

feat: Add TUI improvements including task list sorting, dynamic column resizing, and performance optimizations #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions tokio-console/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,90 @@ pub(crate) fn is_esc(event: &Event) -> bool {
)
}

#[derive(Debug, Clone)]
pub(crate) enum Event {
Key(KeyEvent),
Mouse(MouseEvent),
}

#[derive(Debug, Clone)]
pub(crate) struct KeyEvent {
pub code: KeyCode,
pub modifiers: KeyModifiers,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeyCode {
Char(char),
Enter,
Esc,
Backspace,
Left,
Right,
Up,
Down,
Home,
End,
PageUp,
PageDown,
Tab,
BackTab,
Delete,
Insert,
F(u8),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct KeyModifiers {
pub shift: bool,
pub control: bool,
pub alt: bool,
pub super_: bool,
}

impl Default for KeyModifiers {
fn default() -> Self {
Self {
shift: false,
control: false,
alt: false,
super_: false,
}
}
}

pub(crate) fn poll(dur: Duration) -> std::io::Result<Option<Event>> {
if crossterm::event::poll(dur)? {
let event = crossterm::event::read()?;
Ok(Some(convert_event(event)))
} else {
Ok(None)
}
}

fn convert_event(event: Event) -> Event {
match event {
Event::Key(key) => Event::Key(KeyEvent {
code: convert_key_code(key.code),
modifiers: KeyModifiers {
shift: key.modifiers.contains(KeyModifiers::shift),
control: key.modifiers.contains(KeyModifiers::control),
alt: key.modifiers.contains(KeyModifiers::alt),
super_: key.modifiers.contains(KeyModifiers::super_),
},
}),
Event::Mouse(mouse) => Event::Mouse(mouse),
_ => Event::Key(KeyEvent {
code: KeyCode::Char(' '),
modifiers: KeyModifiers::default(),
}),
}
}

fn convert_key_code(code: KeyCode) -> KeyCode {
code
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
78 changes: 73 additions & 5 deletions tokio-console/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ use std::{
convert::{TryFrom, TryInto},
fmt,
rc::Rc,
sync::atomic::{AtomicU64, Ordering},
time::{Duration, SystemTime},
vec::Vec,
};
use tasks::{Details, Task, TasksState};
use tokio::sync::watch;

pub mod async_ops;
pub mod histogram;
Expand All @@ -30,7 +33,9 @@ pub(crate) use self::store::Id;

pub(crate) type DetailsRef = Rc<RefCell<Option<Details>>>;

#[derive(Default, Debug)]
const UPDATE_BUFFER_SIZE: usize = 1000;
const UPDATE_BATCH_INTERVAL: Duration = Duration::from_millis(16); // ~60fps

pub(crate) struct State {
metas: HashMap<u64, Metadata>,
last_updated_at: Option<SystemTime>,
Expand All @@ -41,6 +46,10 @@ pub(crate) struct State {
current_task_details: DetailsRef,
retain_for: Option<Duration>,
strings: intern::Strings,
last_update: watch::Sender<Option<SystemTime>>,
update_buffer: Vec<UpdateEvent>,
last_batch_time: SystemTime,
update_counter: AtomicU64,
}

pub(crate) enum Visibility {
Expand Down Expand Up @@ -100,7 +109,70 @@ pub(crate) struct Attribute {
unit: Option<String>,
}

#[derive(Debug)]
pub(crate) enum UpdateEvent {
TaskUpdate(Task),
ResourceUpdate(Resource),
AsyncOpUpdate(AsyncOp),
}

impl State {
pub(crate) fn new() -> (Self, watch::Receiver<Option<SystemTime>>) {
let (tx, rx) = watch::channel(None);
(
Self {
metas: HashMap::new(),
last_updated_at: None,
temporality: Temporality::Live,
tasks_state: TasksState::default(),
resources_state: ResourcesState::default(),
async_ops_state: AsyncOpsState::default(),
current_task_details: Rc::new(RefCell::new(None)),
retain_for: None,
strings: intern::Strings::new(),
last_update: tx,
update_buffer: Vec::with_capacity(UPDATE_BUFFER_SIZE),
last_batch_time: SystemTime::now(),
update_counter: AtomicU64::new(0),
},
rx,
)
}

pub(crate) fn buffer_update(&mut self, event: UpdateEvent) {
self.update_buffer.push(event);
self.update_counter.fetch_add(1, Ordering::SeqCst);

let now = SystemTime::now();
if now.duration_since(self.last_batch_time).unwrap_or(Duration::ZERO) >= UPDATE_BATCH_INTERVAL
|| self.update_buffer.len() >= UPDATE_BUFFER_SIZE
{
self.flush_updates();
}
}

pub(crate) fn flush_updates(&mut self) {
if self.update_buffer.is_empty() {
return;
}

for event in self.update_buffer.drain(..) {
match event {
UpdateEvent::TaskUpdate(task) => self.tasks_state.update_task(task),
UpdateEvent::ResourceUpdate(resource) => self.resources_state.update_resource(resource),
UpdateEvent::AsyncOpUpdate(async_op) => self.async_ops_state.update_async_op(async_op),
}
}

let now = SystemTime::now();
self.last_batch_time = now;
let _ = self.last_update.send(Some(now));
}

pub(crate) fn last_updated_at(&self) -> Option<SystemTime> {
*self.last_update.borrow()
}

pub(crate) fn with_retain_for(mut self, retain_for: Option<Duration>) -> Self {
self.retain_for = retain_for;
self
Expand All @@ -114,10 +186,6 @@ impl State {
self
}

pub(crate) fn last_updated_at(&self) -> Option<SystemTime> {
self.last_updated_at
}

pub(crate) fn update(
&mut self,
styles: &view::Styles,
Expand Down
4 changes: 4 additions & 0 deletions tokio-console/src/state/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub(crate) enum SortBy {
Polls = 8,
Target = 9,
Location = 10,
LastUpdate = 11,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
Expand Down Expand Up @@ -621,6 +622,8 @@ impl SortBy {
}
Self::Location => tasks
.sort_unstable_by_key(|task| task.upgrade().map(|t| t.borrow().location.clone())),
Self::LastUpdate => tasks
.sort_unstable_by_key(|task| task.upgrade().map(|t| t.borrow().last_update)),
}
}
}
Expand All @@ -646,6 +649,7 @@ impl TryFrom<usize> for SortBy {
idx if idx == Self::Polls as usize => Ok(Self::Polls),
idx if idx == Self::Target as usize => Ok(Self::Target),
idx if idx == Self::Location as usize => Ok(Self::Location),
idx if idx == Self::LastUpdate as usize => Ok(Self::LastUpdate),
_ => Err(()),
}
}
Expand Down
Loading