Skip to content

Commit

Permalink
refactor: make TUI components pub(crate)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Feb 8, 2025
1 parent 2b40a9a commit 71a9c96
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 67 deletions.
14 changes: 2 additions & 12 deletions crates/tui/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use app::{configuration::GlobalConfig, search::ValidSearchQuery};
use app::search::ValidSearchQuery;
use std::collections::HashSet;

use lib::{kafka::SchemaId, search::OrderBy, KafkaRecord, TopicDetail};
Expand All @@ -10,7 +10,7 @@ use super::component::{ComponentName, Shortcut};
/// Actions that can be dispatched to the UI
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
pub enum Action {
pub(crate) enum Action {
Tick,
Render,
/// Notify the UI that the terminal has been resized
Expand All @@ -19,12 +19,8 @@ pub enum Action {
Quit,
/// Request the app to export the given record into the file
Export(KafkaRecord),
/// Dispatch statistics about the number of processed records
Count((usize, usize, usize)),
/// Dispatch the new shortcuts to the UI
Shortcuts(Vec<Shortcut>, bool),
/// Request the app to clear the current notification
ResetNotification(),
/// Request the UI to show a new notification
Notification(Notification),
/// Request the UI to start searching for kafka records
Expand All @@ -47,14 +43,10 @@ pub enum Action {
Schemas(Option<SchemaDetail>, Option<SchemaDetail>),
/// Notify the UI the list of topics
Topics(Vec<String>),
/// Notify the UI that a new record has been polled
NewRecord(KafkaRecord),
/// Request the list of kafka records to be sorted in a specific way
OrderBy(OrderBy),
/// List of topics to consume
SelectedTopics(Vec<String>),
/// Dispatch the new configuration to the UI
NewConfig(GlobalConfig),
/// Copy the given record to the clipboard
CopyToClipboard(String),
/// Notify the UI that a new component has been be displayed
Expand All @@ -63,8 +55,6 @@ pub enum Action {
ViewStack((ComponentName, Vec<ComponentName>)),
/// Request to open the web browser with the URL template (AKHQ, redpanda-console, etc.) pointing to the given record
Open(KafkaRecord),
/// Request the UI to close the specified component
Close(ComponentName),
/// Notify the UI some details (consumer groups, members...) of a given topic
TopicDetails(Vec<TopicDetail>),
/// Notify the UI that the user typed a new search query
Expand Down
4 changes: 0 additions & 4 deletions crates/tui/src/component/footer_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ impl Component for FooterComponent {
self.ticks = 0;
self.notification = Some(notification)
}
Action::ResetNotification() => {
self.notification = None;
self.ticks = 0;
}
Action::Tick => {
self.ticks += 1;
if self.ticks > 20 {
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/help_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const REPOSITORY_URL: &str = concat!(
);

#[derive(Default)]
pub struct HelpComponent {
pub(crate) struct HelpComponent {
scroll: ScrollState,
rendered: usize,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/issue_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::error::TuiError;
use super::{Component, ComponentName, State};

#[derive(Default)]
pub struct IssueComponent {}
pub(crate) struct IssueComponent {}

impl Component for IssueComponent {
fn id(&self) -> ComponentName {
Expand Down
6 changes: 3 additions & 3 deletions crates/tui/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ use ratatui::{
widgets::{Block, BorderType},
Frame,
};
pub use shortcut::Shortcut;
pub(crate) use root_component::RootComponent;
pub(crate) use shortcut::Shortcut;
use strum::Display;
use tokio::sync::mpsc::UnboundedSender;
pub use ui::Ui;

use std::sync::{Arc, LazyLock, Mutex};

pub use root_component::RootComponent;
pub use state::State;

use serde::Deserialize;

use crate::{records_buffer::RecordsBuffer, tui::Event, Action, TuiError};

pub type ConcurrentRecordsBuffer = LazyLock<Arc<Mutex<RecordsBuffer>>>;
pub(crate) type ConcurrentRecordsBuffer = LazyLock<Arc<Mutex<RecordsBuffer>>>;
static BUFFER: ConcurrentRecordsBuffer =
LazyLock::new(|| Arc::new(Mutex::new(RecordsBuffer::new())));

Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/progress_bar_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ratatui::{
};

#[derive(Clone, Default)]
pub struct ProgressBarComponent {
pub(crate) struct ProgressBarComponent {
length: u64,
progress: u64,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/record_details_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{error::TuiError, Action};
use super::{scroll_state::ScrollState, Component, ComponentName, Shortcut, State};

#[derive(Default)]
pub struct RecordDetailsComponent<'a> {
pub(crate) struct RecordDetailsComponent<'a> {
record: Option<KafkaRecord>,
lines: Vec<Line<'a>>,
search_query: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/records_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{action::Notification, error::TuiError, records_buffer::BufferAction,

use super::{Component, ComponentName, ConcurrentRecordsBuffer, Shortcut, State};

pub struct RecordsComponent<'a> {
pub(crate) struct RecordsComponent<'a> {
records: &'a ConcurrentRecordsBuffer,
state: TableState,
status: ThrobberState,
Expand Down
3 changes: 1 addition & 2 deletions crates/tui/src/component/root_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::{
Component, ComponentName, ConcurrentRecordsBuffer, State,
};

pub struct RootComponent {
pub(crate) struct RootComponent {
components: HashMap<ComponentName, Arc<Mutex<dyn Component>>>,
views: Vec<ComponentName>,
state: State,
Expand Down Expand Up @@ -322,7 +322,6 @@ impl Component for RootComponent {
Action::RecordsToRead(length) => {
self.progress_bar.set_length(length);
}
Action::Close(_) => self.close(),
Action::CopyToClipboard(ref content) => {
let mut ctx = ClipboardContext::new().unwrap();
self.action_tx
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/schemas_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
use super::{scroll_state::ScrollState, Component, ComponentName, Shortcut, State};

#[derive(Default)]
pub struct SchemasComponent<'a> {
pub(crate) struct SchemasComponent<'a> {
key: Option<SchemaDetail>,
value: Option<SchemaDetail>,
lines: Vec<Line<'a>>,
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/search_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
use super::{Component, ComponentName, Shortcut, State};

#[derive(Default)]
pub struct SearchComponent {
pub(crate) struct SearchComponent {
input: Input,
index_history: usize,
history: Vec<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::Deserialize;
/// will be visible as `[CTRL + P]: Show details` in the footer.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Shortcut {
pub(crate) struct Shortcut {
pub key: &'static str,
pub description: &'static str,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/topic_details_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{error::TuiError, Action, Notification};
use super::{Component, ComponentName, State, WithHeight};

#[derive(Default)]
pub struct TopicDetailsComponent {
pub(crate) struct TopicDetailsComponent {
details: Vec<TopicDetail>,
action_tx: Option<UnboundedSender<Action>>,
state: TableState,
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/topics_and_records_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::error::TuiError;

use super::{Component, ComponentName, State};

pub struct TopicsAndRecordsComponent {
pub(crate) struct TopicsAndRecordsComponent {
records: Arc<Mutex<dyn Component>>,
topics: Arc<Mutex<dyn Component>>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/topics_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{error::TuiError, Action};
use super::{Component, ComponentName, Shortcut, State};

#[derive(Default)]
pub struct TopicsComponent {
pub(crate) struct TopicsComponent {
topics: Vec<String>,
visible_topics: Vec<String>,
selected: HashSet<String>,
Expand Down
39 changes: 19 additions & 20 deletions crates/tui/src/component/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ use crate::tui;
use super::{ConcurrentRecordsBuffer, State, BUFFER};

pub struct Ui {
pub app: App,
pub should_quit: bool,

pub root: RootComponent,
pub worker: CancellationToken,
pub topics: Vec<String>,
pub last_tick_key_events: Vec<KeyEvent>,
pub last_time_consuming: Instant,
pub records_sender: Option<UnboundedSender<KafkaRecord>>,
pub records: &'static ConcurrentRecordsBuffer,
app: App,
should_quit: bool,
root: RootComponent,
worker: CancellationToken,
topics: Vec<String>,
last_tick_key_events: Vec<KeyEvent>,
records_sender: Option<UnboundedSender<KafkaRecord>>,
records: &'static ConcurrentRecordsBuffer,
}

impl Ui {
Expand All @@ -58,7 +56,6 @@ impl Ui {
root: RootComponent::new(query, selected_topics, &config.global, &BUFFER, state),
records_sender: None,
last_tick_key_events: Vec::new(),
last_time_consuming: Instant::now(),
})
}

Expand All @@ -74,7 +71,7 @@ impl Ui {
Ok(())
}

pub async fn create_consumer(
pub(crate) async fn create_consumer(
app: &App,
topics: Vec<String>,
tx: UnboundedSender<Action>,
Expand All @@ -92,7 +89,10 @@ impl Ui {
}
}

pub async fn consume_topics(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
pub(crate) async fn consume_topics(
&mut self,
tx: UnboundedSender<Action>,
) -> Result<(), TuiError> {
self.worker.cancel();
self.records.lock().unwrap().reset();
if self.topics.is_empty() {
Expand Down Expand Up @@ -213,7 +213,7 @@ impl Ui {
Ok(())
}

pub fn topics_details(
pub(crate) fn topics_details(
&mut self,
topics: HashSet<String>,
action_tx: UnboundedSender<Action>,
Expand All @@ -233,7 +233,7 @@ impl Ui {
Ok(())
}

pub fn export_record(
pub(crate) fn export_record(
&mut self,
record: &KafkaRecord,
action_tx: UnboundedSender<Action>,
Expand All @@ -246,7 +246,10 @@ impl Ui {
Ok(())
}

pub fn load_topics(&mut self, action_tx: UnboundedSender<Action>) -> Result<(), TuiError> {
pub(crate) fn load_topics(
&mut self,
action_tx: UnboundedSender<Action>,
) -> Result<(), TuiError> {
let app = self.app.clone();
tokio::spawn(async move {
info!("Loading topics");
Expand Down Expand Up @@ -301,10 +304,6 @@ impl Ui {
}
while let Ok(action) = action_rx.try_recv() {
match action {
Action::NewConfig(ref config) => {
self.app.config.global = config.clone();
self.save_config()?;
}
Action::NewSearchPrompt(ref prompt) => {
self.app.config.global.history.push(prompt.to_string());
self.app.config.global.history.dedup();
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/vertical_scrollable_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{action::Action, error::TuiError, tui::Event};
use super::{ComponentName, State};

#[derive(Default)]
pub struct VerticalScrollableBlock<C> {
pub(crate) struct VerticalScrollableBlock<C> {
scroll: u16,
scroll_length: u16,
scrollbar_state: ScrollbarState,
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod schema_detail;
pub mod theme;
mod tui;

pub use action::Action;
pub(crate) use action::Action;
pub use action::Notification;

pub use component::State;
Expand Down
10 changes: 1 addition & 9 deletions crates/tui/src/records_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const BUFFER_SIZE: usize = 500;
const BUFFER_SIZE: usize = 120;

/// Wrapper around [CircularBuffer]
pub struct RecordsBuffer {
pub(crate) struct RecordsBuffer {
buffer: CircularBuffer<BUFFER_SIZE, KafkaRecord>,
read: usize,
pub channels: (Sender<BufferAction>, Receiver<BufferAction>),
Expand Down Expand Up @@ -58,10 +58,6 @@ impl RecordsBuffer {
}
}

pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}

/// Empty the buffer and reset metrics
pub fn reset(&mut self) {
self.buffer.clear();
Expand All @@ -80,10 +76,6 @@ impl RecordsBuffer {
self.read += 1;
}

pub fn len(&self) -> usize {
self.buffer.len()
}

pub fn get(&self, index: usize) -> Option<&KafkaRecord> {
self.buffer.get(index)
}
Expand Down
4 changes: 0 additions & 4 deletions crates/tui/src/schema_detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ impl SchemaDetail {

Some(Self { response, url, id })
}

pub fn schema_to_string_pretty(&self) -> Option<String> {
self.response.as_ref().map(|r| r.schema_to_string_pretty())
}
}

#[derive(Clone, Debug, Serialize, Hash, PartialEq, Eq, Default)]
Expand Down

0 comments on commit 71a9c96

Please sign in to comment.