Skip to content

Commit

Permalink
implement StatefulWidget for filebrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Sep 2, 2024
1 parent 06325fc commit 3425a87
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 239 deletions.
2 changes: 1 addition & 1 deletion lumni/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ tokio = { version = "1.12", default-features = false, features = ["rt-multi-thre
# tokio = { version = "1.12", default-features = false, features = ["full"], optional = true }
clap = { version = "4.2" , default-features = false, features = ["std", "help"], optional = true }
crossterm = { version = "0.27", optional = true }
ratatui = { version = ">=0.27.0, <1", default-features = false, features = ["crossterm"], optional = true }
ratatui = { version = ">=0.27.0, <1", default-features = false, features = ["crossterm", "unstable-widget-ref" ], optional = true }
arboard = { version = "3.2", default-features = false, optional = true }

# WEB
Expand Down
40 changes: 28 additions & 12 deletions lumni/src/apps/builtin/llm/prompt/src/tui/modals/filebrowser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use ratatui::Frame;

use super::{
ApplicationError, ConversationDbHandler, FileBrowserWidget, KeyTrack,
ModalAction, ModalWindowTrait, ModalWindowType, ThreadedChatSession,
WindowEvent,
ApplicationError, ConversationDbHandler, FileBrowserState,
FileBrowserWidget, KeyTrack, ModalAction, ModalWindowTrait,
ModalWindowType, ThreadedChatSession, WindowEvent,
};
pub use crate::external as lumni;

pub struct FileBrowserModal<'a> {
file_browser: FileBrowserWidget<'a>,
pub struct FileBrowserModal {
file_browser: FileBrowserWidget,
file_browser_state: FileBrowserState<'static>,
selected_file_content: Option<String>,
selected_file_details: Option<FileDetails>,
}
Expand All @@ -29,10 +30,13 @@ struct FileDetails {
is_dir: bool,
}

impl<'a> FileBrowserModal<'a> {
impl FileBrowserModal {
pub fn new(base_path: Option<PathBuf>) -> Self {
let (file_browser, file_browser_state) =
FileBrowserWidget::new(base_path);
Self {
file_browser: FileBrowserWidget::new(base_path),
file_browser,
file_browser_state,
selected_file_content: None,
selected_file_details: None,
}
Expand Down Expand Up @@ -83,7 +87,10 @@ impl<'a> FileBrowserModal<'a> {
async fn update_selected_file_info(
&mut self,
) -> Result<(), ApplicationError> {
if let Some(row) = &self.file_browser.get_selected_table_row() {
if let Some(row) = self
.file_browser
.get_selected_table_row(&self.file_browser_state)
{
let name = row
.get_value("name")
.and_then(|v| match v {
Expand Down Expand Up @@ -139,7 +146,7 @@ impl<'a> FileBrowserModal<'a> {
}

#[async_trait]
impl ModalWindowTrait for FileBrowserModal<'_> {
impl ModalWindowTrait for FileBrowserModal {
fn get_type(&self) -> ModalWindowType {
ModalWindowType::FileBrowser
}
Expand All @@ -162,7 +169,12 @@ impl ModalWindowTrait for FileBrowserModal<'_> {
])
.split(chunks[0]);

self.file_browser.render(frame, main_chunks[0]);
//self.file_browser.render(frame, main_chunks[0]);
frame.render_stateful_widget(
&self.file_browser,
main_chunks[0],
&mut self.file_browser_state,
);

let details_content_chunks = Layout::default()
.direction(Direction::Vertical)
Expand All @@ -183,7 +195,9 @@ impl ModalWindowTrait for FileBrowserModal<'_> {
_tab_chat: Option<&'b mut ThreadedChatSession>,
_handler: &mut ConversationDbHandler,
) -> Result<WindowEvent, ApplicationError> {
let modal_action = self.file_browser.handle_key_event(key_event)?;
let modal_action = self
.file_browser
.handle_key_event(key_event, &mut self.file_browser_state)?;

match key_event.current_key().code {
KeyCode::Enter | KeyCode::Up | KeyCode::Down => {
Expand All @@ -200,7 +214,9 @@ impl ModalWindowTrait for FileBrowserModal<'_> {
async fn poll_background_task(
&mut self,
) -> Result<WindowEvent, ApplicationError> {
self.file_browser.poll_background_task().await?;
self.file_browser
.poll_background_task(&mut self.file_browser_state)
.await?;
self.update_selected_file_info().await?;
Ok(WindowEvent::Modal(ModalAction::PollBackGroundTask))
}
Expand Down
2 changes: 1 addition & 1 deletion lumni/src/apps/builtin/llm/prompt/src/tui/modals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use filebrowser::FileBrowserModal;
use ratatui::layout::Rect;
use ratatui::Frame;
pub use settings::SettingsModal;
use widgets::FileBrowserWidget;
use widgets::{FileBrowserState, FileBrowserWidget};

pub use super::widgets;
use super::{
Expand Down
Loading

0 comments on commit 3425a87

Please sign in to comment.