Skip to content
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

refactor(tui): replace custom logger with tui-logger #314

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/kftui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ kftray-commons = { path = "../kftray-commons" }
kftray-portforward = { path = "../kftray-portforward" }
ratatui = { version = "0.28.1", features = ["unstable-widget-ref"] }
crossterm = { version = "0.28.1", optional = false }
tui-logger = "0.13.1"

# https://github.com/tatounee/ratatui-explorer/pull/2/files
ratatui-explorer = { git = "https://github.com/hcavarsan/ratatui-explorer", branch = "master" }
Expand All @@ -66,4 +67,4 @@ built = "0.7.4"


[build-dependencies]
built = "0.7"
built = "0.7"
69 changes: 0 additions & 69 deletions crates/kftui/src/core/logging.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/kftui/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod logging;
pub mod port_forward;

pub mod built_info {
Expand Down
7 changes: 3 additions & 4 deletions crates/kftui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
mod core;
mod tui;
mod utils;

use core::logging::init_logger;

use tui::run_tui;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
init_logger()?;
tui_logger::init_logger(log::LevelFilter::Debug).unwrap();
tui_logger::set_default_level(log::LevelFilter::Debug);

hcavarsan marked this conversation as resolved.
Show resolved Hide resolved
run_tui().await
}
23 changes: 0 additions & 23 deletions crates/kftui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ async fn run_app<B: ratatui::backend::Backend>(
let mut config_states = read_config_states().await.unwrap_or_default();

app.update_configs(&configs, &config_states);
fetch_new_logs(app).await;

terminal.draw(|f| {
draw_ui(f, app, &config_states);
Expand All @@ -81,25 +80,3 @@ async fn run_app<B: ratatui::backend::Backend>(
}
Ok(())
}

async fn fetch_new_logs(app: &mut App) {
let new_logs = {
let mut buffer = app.stdout_output.lock().unwrap();
let new_logs = buffer.clone();
buffer.clear();
new_logs
};

let mut log_content = app.log_content.lock().unwrap();
let was_at_bottom = app.log_scroll_offset == app.log_scroll_max_offset;

log_content.push_str(&new_logs);

let log_lines: Vec<&str> = log_content.lines().collect();
let log_height = app.visible_rows;
app.log_scroll_max_offset = log_lines.len().saturating_sub(log_height);

if was_at_bottom {
app.log_scroll_offset = app.log_scroll_max_offset;
}
}
63 changes: 16 additions & 47 deletions crates/kftui/src/tui/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ mod popup;

use std::collections::HashSet;
use std::io;
use std::sync::{
Arc,
Mutex,
};

use crossterm::event::{
self,
Expand All @@ -21,15 +17,17 @@ use kftray_commons::models::{
config_model::Config,
config_state_model::ConfigState,
};
use log::LevelFilter;
pub use popup::*;
use ratatui::widgets::ListState;
use ratatui::widgets::TableState;
use ratatui_explorer::{
FileExplorer,
Theme,
};
use tui_logger::TuiWidgetEvent;
use tui_logger::TuiWidgetState;

use crate::core::logging::LOGGER;
use crate::core::port_forward::stop_all_port_forward_and_exit;
use crate::tui::input::navigation::handle_auto_add_configs;
use crate::tui::input::navigation::handle_context_selection;
Expand Down Expand Up @@ -86,29 +84,26 @@ pub struct App {
pub file_content: Option<String>,
pub stopped_configs: Vec<Config>,
pub running_configs: Vec<Config>,
pub stdout_output: Arc<Mutex<String>>,
pub error_message: Option<String>,
pub log_scroll_offset: usize,
pub log_scroll_max_offset: usize,
pub active_component: ActiveComponent,
pub selected_menu_item: usize,
pub delete_confirmation_message: Option<String>,
pub selected_delete_button: DeleteButton,
pub visible_rows: usize,
pub table_state_stopped: TableState,
pub table_state_running: TableState,
pub log_content: Arc<Mutex<String>>,
pub contexts: Vec<String>,
pub selected_context_index: usize,
pub context_list_state: ListState,
pub logger_state: TuiWidgetState,
hcavarsan marked this conversation as resolved.
Show resolved Hide resolved
}

impl App {
pub fn new() -> Self {
let theme = Theme::default().add_default_title();
let import_file_explorer = FileExplorer::with_theme(theme.clone()).unwrap();
let export_file_explorer = FileExplorer::with_theme(theme).unwrap();
let stdout_output = LOGGER.buffer.clone();
let logger_state = TuiWidgetState::new().set_default_display_level(LevelFilter::Warn);
hcavarsan marked this conversation as resolved.
Show resolved Hide resolved

let mut app = Self {
details_scroll_offset: 0,
Expand All @@ -127,21 +122,18 @@ impl App {
file_content: None,
stopped_configs: Vec::new(),
running_configs: Vec::new(),
stdout_output,
error_message: None,
log_scroll_offset: 0,
log_scroll_max_offset: 0,
active_component: ActiveComponent::StoppedTable,
selected_menu_item: 0,
delete_confirmation_message: None,
selected_delete_button: DeleteButton::Confirm,
visible_rows: 0,
table_state_stopped: TableState::default(),
table_state_running: TableState::default(),
log_content: Arc::new(Mutex::new(String::new())),
contexts: Vec::new(),
selected_context_index: 0,
context_list_state: ListState::default(),
logger_state,
};

if let Ok((_, height)) = size() {
Expand Down Expand Up @@ -371,13 +363,6 @@ fn scroll_page_up(app: &mut App) {
app.table_state_running
.select(Some(app.selected_row_running));
}
ActiveComponent::Logs => {
if app.log_scroll_offset >= app.visible_rows {
app.log_scroll_offset -= app.visible_rows;
} else {
app.log_scroll_offset = 0;
}
}
ActiveComponent::Details => {
if app.details_scroll_offset >= app.visible_rows {
app.details_scroll_offset -= app.visible_rows;
Expand Down Expand Up @@ -411,13 +396,6 @@ fn scroll_page_down(app: &mut App) {
app.table_state_running
.select(Some(app.selected_row_running));
}
ActiveComponent::Logs => {
if app.log_scroll_offset + app.visible_rows < app.log_scroll_max_offset {
app.log_scroll_offset += app.visible_rows;
} else {
app.log_scroll_offset = app.log_scroll_max_offset;
}
}
ActiveComponent::Details => {
if app.details_scroll_offset + app.visible_rows < app.details_scroll_max_offset {
app.details_scroll_offset += app.visible_rows;
Expand Down Expand Up @@ -604,10 +582,6 @@ async fn handle_details_input(app: &mut App, key: KeyCode) -> io::Result<()> {
}

async fn handle_logs_input(app: &mut App, key: KeyCode) -> io::Result<()> {
if handle_common_hotkeys(app, key).await? {
return Ok(());
}

match key {
KeyCode::Left => app.active_component = ActiveComponent::Details,
KeyCode::Up => {
Expand All @@ -616,23 +590,23 @@ async fn handle_logs_input(app: &mut App, key: KeyCode) -> io::Result<()> {
clear_selection(app);
select_first_row(app);
}
KeyCode::PageUp => {
scroll_page_up(app);
}
KeyCode::PageDown => {
scroll_page_down(app);
}
// Pass key events to the logger state
KeyCode::PageUp => app.logger_state.transition(TuiWidgetEvent::PrevPageKey),
KeyCode::PageDown => app.logger_state.transition(TuiWidgetEvent::NextPageKey),
KeyCode::Down => app.logger_state.transition(TuiWidgetEvent::DownKey),
KeyCode::Char('+') => app.logger_state.transition(TuiWidgetEvent::PlusKey),
KeyCode::Char('-') => app.logger_state.transition(TuiWidgetEvent::MinusKey),
KeyCode::Char(' ') => app.logger_state.transition(TuiWidgetEvent::SpaceKey),
KeyCode::Esc => app.logger_state.transition(TuiWidgetEvent::EscapeKey),
KeyCode::Char('h') => app.logger_state.transition(TuiWidgetEvent::HideKey),
KeyCode::Char('f') => app.logger_state.transition(TuiWidgetEvent::FocusKey),
hcavarsan marked this conversation as resolved.
Show resolved Hide resolved
_ => {}
}
Ok(())
}

async fn handle_common_hotkeys(app: &mut App, key: KeyCode) -> io::Result<bool> {
match key {
KeyCode::Char('c') => {
clear_stdout_output(app);
Ok(true)
}
KeyCode::Char('q') => {
app.state = AppState::ShowAbout;
Ok(true)
Expand Down Expand Up @@ -672,11 +646,6 @@ fn toggle_row_selection(app: &mut App) {
}
}

fn clear_stdout_output(app: &mut App) {
let mut stdout_output = app.stdout_output.lock().unwrap();
stdout_output.clear();
}

async fn handle_port_forwarding(app: &mut App) -> io::Result<()> {
let (selected_rows, configs, selected_row) = match app.active_table {
ActiveTable::Stopped => (
Expand Down
Loading
Loading