Skip to content

Commit

Permalink
Merge branch 'main' into feat/welcome-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
mfauzaan committed Nov 20, 2023
2 parents 7494125 + 628c5a7 commit e53017f
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 136 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde_json = "1.0"
once_cell = "1.18.0"
crossterm = "0.27"
ratatui = { version = "0.23.0", features = ["all-widgets"] }
textwrap = "0.16.0"

# logging
log = { version = "0.4.19", features = ["std"] }
Expand Down
6 changes: 5 additions & 1 deletion cli/src/commands/tui/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ pub enum Action {
FilterLogs,
TimeFilterLogs,
ExpandToFullScreen,
LineWrapLogs,
Quit,
}

impl Action {
// iterator for enum https://stackoverflow.com/a/21376984
pub fn iterator() -> Iter<'static, Action> {
static ACTIONS: [Action; 9] = [
static ACTIONS: [Action; 10] = [
Action::OpenNamespaceSelection,
Action::OpenVersionSelection,
Action::ToggleLogsTailing,
Expand All @@ -27,6 +28,7 @@ impl Action {
Action::FilterLogs,
Action::TimeFilterLogs,
Action::ExpandToFullScreen,
Action::LineWrapLogs,
Action::Quit,
];
ACTIONS.iter()
Expand All @@ -41,6 +43,7 @@ impl Action {
Action::SearchLogs => &[Key::Ctrl('s')],
Action::FilterLogs => &[Key::Ctrl('f')],
Action::ExpandToFullScreen => &[Key::Ctrl('w')],
Action::LineWrapLogs => &[Key::Ctrl('l')],
Action::Quit => &[Key::Char('q')],
Action::TimeFilterLogs => &[Key::Ctrl('r')],
}
Expand All @@ -63,6 +66,7 @@ impl Display for Action {
Action::Quit => write!(f, "Quit"),
Action::SearchLogs => write!(f, "Search logs"),
Action::FilterLogs => write!(f, "Filter logs"),
Action::LineWrapLogs => write!(f, "Line wrap logs"),
Action::TimeFilterLogs => write!(f, "Filter logs by time"),
Action::ExpandToFullScreen => write!(f, "Expand to full screen"),
}
Expand Down
28 changes: 17 additions & 11 deletions cli/src/commands/tui/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{collections::HashMap, fmt::Display, time::Instant};

use ratatui::{
prelude::Rect,
widgets::{ScrollbarState, TableState},
};
use ratatui::{prelude::Rect, widgets::ScrollbarState};
use tokio::sync::mpsc::Sender;
use wukong_sdk::services::gcloud::google::logging::{r#type::LogSeverity, v2::LogEntry};

Expand Down Expand Up @@ -77,7 +74,7 @@ impl Display for TimeFilter {
}
}

pub const MAX_LOG_ENTRIES_LENGTH: usize = 1_000;
pub const MAX_LOG_ENTRIES_LENGTH: usize = 2_000;

pub struct State {
pub current_application: String,
Expand Down Expand Up @@ -115,9 +112,12 @@ pub struct State {
pub logs_vertical_scroll: usize,
pub logs_horizontal_scroll: usize,
pub logs_enable_auto_scroll_to_bottom: bool,
pub logs_table_state: TableState,
pub logs_table_current_index: usize,
pub logs_table_start_position: usize,
pub logs_table_current_start_index: usize,
// last index of the table that is visible
pub logs_table_current_last_index: usize,
// whether last index of the table that is visible is fully rendered
// useful to know if we need to scroll during textwrap
pub logs_table_current_last_fully_rendered: bool,
pub expanded_block: Option<Block>,

// For log entries polling
Expand All @@ -133,6 +133,9 @@ pub struct State {
pub search_bar_input: Input,
pub filter_bar_include_input: Input,
pub filter_bar_exclude_input: Input,
pub logs_textwrap: bool,

pub logs_size: (u16, u16),
pub welcome_screen_timer: Option<Instant>,
}

Expand Down Expand Up @@ -225,9 +228,9 @@ impl App {
logs_vertical_scroll: 0,
logs_horizontal_scroll: 0,
instant_since_last_log_entries_poll: Instant::now(),
logs_table_state: TableState::default(),
logs_table_current_index: 0,
logs_table_start_position: 0,
logs_table_current_start_index: 0,
logs_table_current_last_index: 0,
logs_table_current_last_fully_rendered: true,
expanded_block: None,

logs_widget_width: 0,
Expand All @@ -239,6 +242,8 @@ impl App {
search_bar_input: Input::default(),
filter_bar_include_input: Input::default(),
filter_bar_exclude_input: Input::default(),
logs_textwrap: false,
logs_size: (0, 0),
welcome_screen_timer: None,
},
navigation_stack: vec![DEFAULT_ROUTE],
Expand All @@ -255,6 +260,7 @@ impl App {
Action::SearchLogs,
Action::FilterLogs,
Action::ExpandToFullScreen,
Action::LineWrapLogs,
Action::TimeFilterLogs,
],
network_event_sender: sender,
Expand Down
51 changes: 47 additions & 4 deletions cli/src/commands/tui/handlers/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,49 @@ pub async fn handler(key: Key, app: &mut App) -> AppReturn {
}
}
key if common_key_events::up_event(key) => {
app.state.logs_table_start_position =
app.state.logs_table_start_position.saturating_sub(5);
// FIXME: currently we are scrolling up based on the current rendered count.
// if 1 log rendered on the screen currently, then we will scroll up 1 log when up key
// is pressed,
// if 5 logs rendered on the screen currently, then we will scroll up 5 logs when up
// key is pressed.
// While this is working, but the UX is not great.
// I haven't come up a better solution yet. We need a better calculation for this
if app.state.logs_textwrap {
let count = app.state.logs_table_current_last_index
- app.state.logs_table_current_start_index
+ 1;

app.state.logs_table_current_start_index = app
.state
.logs_table_current_start_index
.saturating_sub(count);
} else {
app.state.logs_table_current_start_index = app
.state
.logs_table_current_start_index
.saturating_sub(app.state.logs_size.1 as usize);
}
}
key if common_key_events::down_event(key) => {
app.state.logs_table_start_position =
app.state.logs_table_start_position.saturating_add(5);
let next_start_index = if app.state.logs_textwrap {
if app.state.logs_table_current_last_fully_rendered {
app.state.logs_table_current_last_index.saturating_add(1)
} else {
app.state.logs_table_current_last_index
}
} else {
app.state
.logs_table_current_start_index
.saturating_add(app.state.logs_size.1 as usize)
};

// prevent going out of bounds
if next_start_index >= app.state.log_entries.len() {
app.state.logs_table_current_start_index =
app.state.log_entries.len().saturating_sub(1);
} else {
app.state.logs_table_current_start_index = next_start_index;
}
}
key if common_key_events::left_event(key) => {
let new_scroll_position = app.state.logs_horizontal_scroll.saturating_sub(5);
Expand Down Expand Up @@ -67,6 +104,12 @@ pub async fn handler(key: Key, app: &mut App) -> AppReturn {
key if Action::from_key(key) == Some(Action::ExpandToFullScreen) => {
app.state.expanded_block = Some(Block::Log);
}
key if Action::from_key(key) == Some(Action::LineWrapLogs) => {
app.state.logs_textwrap = !app.state.logs_textwrap;

// reset horizontal scroll position
handle_horizontal_scroll(app, 0);
}
key if Action::from_key(key) == Some(Action::TimeFilterLogs) => {
app.set_current_route_state(Some(Block::Dialog(DialogContext::LogTimeFilter)), None);
}
Expand Down
Loading

0 comments on commit e53017f

Please sign in to comment.