TUI #1
11
src/tui.rs
11
src/tui.rs
@ -33,10 +33,13 @@ impl Tui {
|
|||||||
|
|
||||||
let mut dir = env::temp_dir();
|
let mut dir = env::temp_dir();
|
||||||
dir.push("clide.log");
|
dir.push("clide.log");
|
||||||
let file_options = TuiLoggerFile::new(dir.to_str().unwrap())
|
let file_options = TuiLoggerFile::new(
|
||||||
.output_level(Some(TuiLoggerLevelOutput::Abbreviated))
|
dir.to_str()
|
||||||
.output_file(false)
|
.context("Failed to set temp directory for file logging")?,
|
||||||
.output_separator(':');
|
)
|
||||||
|
.output_level(Some(TuiLoggerLevelOutput::Abbreviated))
|
||||||
|
.output_file(false)
|
||||||
|
.output_separator(':');
|
||||||
set_log_file(file_options);
|
set_log_file(file_options);
|
||||||
debug!(target:"Tui", "Logging to file: {dir:?}");
|
debug!(target:"Tui", "Logging to file: {dir:?}");
|
||||||
|
|
||||||
|
|||||||
242
src/tui/app.rs
242
src/tui/app.rs
@ -1,15 +1,15 @@
|
|||||||
|
use crate::tui::app::AppComponent::{AppEditor, AppExplorer, AppLogger};
|
||||||
use crate::tui::component::{Action, Component, Focus, FocusState};
|
use crate::tui::component::{Action, Component, Focus, FocusState};
|
||||||
use crate::tui::editor::Editor;
|
use crate::tui::editor::Editor;
|
||||||
use crate::tui::explorer::Explorer;
|
use crate::tui::explorer::Explorer;
|
||||||
use crate::tui::logger::Logger;
|
use crate::tui::logger::Logger;
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result};
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event;
|
use ratatui::crossterm::event;
|
||||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||||
use ratatui::prelude::{Color, Style, Widget};
|
use ratatui::prelude::{Color, Style, Widget};
|
||||||
use ratatui::text::Text;
|
|
||||||
use ratatui::widgets::{Block, Borders, Padding, Paragraph, Tabs, Wrap};
|
use ratatui::widgets::{Block, Borders, Padding, Paragraph, Tabs, Wrap};
|
||||||
use ratatui::{DefaultTerminal, symbols};
|
use ratatui::{DefaultTerminal, symbols};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@ -17,61 +17,50 @@ use std::time::Duration;
|
|||||||
|
|
||||||
// TODO: Need a way to dynamically run Widget::render on all widgets.
|
// TODO: Need a way to dynamically run Widget::render on all widgets.
|
||||||
// TODO: + Need a way to map Rect to Component::id() to position each widget?
|
// TODO: + Need a way to map Rect to Component::id() to position each widget?
|
||||||
// TODO: Need a way to dynamically run Component methods on all widgets.
|
// TODO: Need a good way to dynamically run Component methods on all widgets.
|
||||||
pub enum AppComponents<'a> {
|
#[derive(PartialEq)]
|
||||||
AppEditor(Editor),
|
pub enum AppComponent {
|
||||||
AppExplorer(Explorer<'a>),
|
AppEditor,
|
||||||
AppLogger(Logger),
|
AppExplorer,
|
||||||
#[allow(dead_code)]
|
AppLogger,
|
||||||
AppComponent(Box<dyn Component>),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Usage: get_component_mut::<Editor>() OR get_component::<Editor>()
|
|
||||||
///
|
|
||||||
/// Implementing this trait for each AppComponent allows for easy lookup in the vector.
|
|
||||||
pub(crate) trait ComponentOf<T> {
|
|
||||||
fn as_ref(&self) -> Option<&T>;
|
|
||||||
fn as_mut(&mut self) -> Option<&mut T>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App<'a> {
|
pub struct App<'a> {
|
||||||
components: Vec<AppComponents<'a>>,
|
editor: Editor,
|
||||||
|
explorer: Explorer<'a>,
|
||||||
|
logger: Logger,
|
||||||
|
last_active: AppComponent,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> App<'a> {
|
impl<'a> App<'a> {
|
||||||
|
pub fn id() -> &'static str {
|
||||||
|
"App"
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(root_path: PathBuf) -> Result<Self> {
|
pub fn new(root_path: PathBuf) -> Result<Self> {
|
||||||
let mut app = Self {
|
let app = Self {
|
||||||
components: vec![
|
editor: Editor::new(),
|
||||||
AppComponents::AppExplorer(Explorer::new(&root_path)?),
|
explorer: Explorer::new(&root_path)?,
|
||||||
AppComponents::AppEditor(Editor::new()),
|
logger: Logger::new(),
|
||||||
AppComponents::AppLogger(Logger::new()),
|
last_active: AppEditor,
|
||||||
],
|
|
||||||
};
|
};
|
||||||
let editor = app.get_component_mut::<Editor>().unwrap();
|
Ok(app)
|
||||||
editor
|
}
|
||||||
|
|
||||||
|
/// Logic that should be executed once on application startup.
|
||||||
|
pub fn start(&mut self) -> Result<()> {
|
||||||
|
let root_path = self.explorer.root_path.clone();
|
||||||
|
self.editor
|
||||||
.set_contents(&root_path.join("src/tui/app.rs"))
|
.set_contents(&root_path.join("src/tui/app.rs"))
|
||||||
.context(format!(
|
.context(format!(
|
||||||
"Failed to initialize editor contents to path: {root_path:?}"
|
"Failed to initialize editor contents to path: {root_path:?}"
|
||||||
))?;
|
))?;
|
||||||
editor.component_state.set_focus(Focus::Active);
|
self.editor.component_state.set_focus(Focus::Active);
|
||||||
Ok(app)
|
Ok(())
|
||||||
}
|
|
||||||
|
|
||||||
fn get_component<T>(&self) -> Option<&T>
|
|
||||||
where
|
|
||||||
AppComponents<'a>: ComponentOf<T>,
|
|
||||||
{
|
|
||||||
self.components.iter().find_map(|c| c.as_ref())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_component_mut<T>(&mut self) -> Option<&mut T>
|
|
||||||
where
|
|
||||||
AppComponents<'a>: ComponentOf<T>,
|
|
||||||
{
|
|
||||||
self.components.iter_mut().find_map(|c| c.as_mut())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||||
|
self.start()?;
|
||||||
loop {
|
loop {
|
||||||
self.refresh_editor_contents()
|
self.refresh_editor_contents()
|
||||||
.context("Failed to refresh editor contents.")?;
|
.context("Failed to refresh editor contents.")?;
|
||||||
@ -80,7 +69,6 @@ impl<'a> App<'a> {
|
|||||||
f.render_widget(&mut self, f.area());
|
f.render_widget(&mut self, f.area());
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// TODO: Handle events based on which component is active.
|
|
||||||
if event::poll(Duration::from_millis(250)).context("event poll failed")? {
|
if event::poll(Duration::from_millis(250)).context("event poll failed")? {
|
||||||
match self.handle_event(event::read()?)? {
|
match self.handle_event(event::read()?)? {
|
||||||
Action::Quit => break,
|
Action::Quit => break,
|
||||||
@ -103,32 +91,29 @@ impl<'a> App<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn draw_bottom_status(&self, area: Rect, buf: &mut Buffer) {
|
fn draw_bottom_status(&self, area: Rect, buf: &mut Buffer) {
|
||||||
// TODO: Set help text based on most recent component enabled.
|
// Determine help text from the most recently focused component.
|
||||||
Paragraph::new(
|
let help = match self.last_active {
|
||||||
self.get_component::<Logger>()
|
AppEditor => self.editor.component_state.help_text.clone(),
|
||||||
.unwrap()
|
AppExplorer => self.explorer.component_state.help_text.clone(),
|
||||||
.component_state
|
AppLogger => self.logger.component_state.help_text.clone(),
|
||||||
.help_text
|
};
|
||||||
.clone(),
|
Paragraph::new(help)
|
||||||
)
|
.style(Color::Gray)
|
||||||
.style(Color::Gray)
|
.wrap(Wrap { trim: false })
|
||||||
.wrap(Wrap { trim: false })
|
.centered()
|
||||||
.centered()
|
.render(area, buf);
|
||||||
.render(area, buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_tabs(&self, area: Rect, buf: &mut Buffer) {
|
fn draw_tabs(&self, area: Rect, buf: &mut Buffer) {
|
||||||
// Determine the tab title from the current file (or use a fallback).
|
// Determine the tab title from the current file (or use a fallback).
|
||||||
let mut title: Option<&str> = None;
|
if let Some(title) = self.editor.file_path.clone() {
|
||||||
if let Some(editor) = self.get_component::<Editor>() {
|
Tabs::new(vec![
|
||||||
title = editor
|
title
|
||||||
.file_path
|
.file_name()
|
||||||
.as_ref()
|
.map(|f| f.to_str())
|
||||||
.and_then(|p| p.file_name())
|
.unwrap_or(Some("Unknown"))
|
||||||
.and_then(|s| s.to_str())
|
.unwrap(),
|
||||||
}
|
])
|
||||||
|
|
||||||
Tabs::new(vec![title.unwrap_or("Unknown")])
|
|
||||||
.divider(symbols::DOT)
|
.divider(symbols::DOT)
|
||||||
.block(
|
.block(
|
||||||
Block::default()
|
Block::default()
|
||||||
@ -137,30 +122,44 @@ impl<'a> App<'a> {
|
|||||||
)
|
)
|
||||||
.highlight_style(Style::default().fg(Color::LightRed))
|
.highlight_style(Style::default().fg(Color::LightRed))
|
||||||
.render(area, buf);
|
.render(area, buf);
|
||||||
|
} else {
|
||||||
|
error!(target:Self::id(), "Failed to get Editor file_path while drawing Tabs widget.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn change_focus(&mut self, focus: AppComponent) {
|
||||||
|
if self.last_active == AppEditor {
|
||||||
|
self.editor.state.cursor.row = 0;
|
||||||
|
self.editor.state.cursor.col = 0;
|
||||||
|
}
|
||||||
|
match focus {
|
||||||
|
AppEditor => self.editor.component_state.set_focus(Focus::Active),
|
||||||
|
AppExplorer => self.explorer.component_state.set_focus(Focus::Active),
|
||||||
|
AppLogger => self.logger.component_state.set_focus(Focus::Active),
|
||||||
|
}
|
||||||
|
self.last_active = focus;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Refresh the contents of the editor to match the selected TreeItem in the file Explorer.
|
/// Refresh the contents of the editor to match the selected TreeItem in the file Explorer.
|
||||||
/// If the selected item is not a file, this does nothing.
|
/// If the selected item is not a file, this does nothing.
|
||||||
fn refresh_editor_contents(&mut self) -> Result<()> {
|
fn refresh_editor_contents(&mut self) -> Result<()> {
|
||||||
// Use the currently selected TreeItem or get an absolute path to this source file.
|
// Use the currently selected TreeItem or get an absolute path to this source file.
|
||||||
let selected_pathbuf = match self.get_component::<Explorer>().unwrap().selected() {
|
let selected_pathbuf = match self.explorer.selected() {
|
||||||
Ok(path) => PathBuf::from(path),
|
Ok(path) => PathBuf::from(path),
|
||||||
Err(_) => PathBuf::from(std::path::absolute(file!())?.to_string_lossy().to_string()),
|
Err(_) => PathBuf::from(std::path::absolute(file!())?.to_string_lossy().to_string()),
|
||||||
};
|
};
|
||||||
let editor = self
|
let current_file_path = self
|
||||||
.get_component_mut::<Editor>()
|
.editor
|
||||||
.context("Failed to get active editor while refreshing contents.")?;
|
.file_path
|
||||||
if let Some(current_file_path) = editor.file_path.clone() {
|
.clone()
|
||||||
if selected_pathbuf == current_file_path || !selected_pathbuf.is_file() {
|
.context("Failed to get Editor current file_path")?;
|
||||||
return Ok(());
|
if selected_pathbuf == current_file_path || !selected_pathbuf.is_file() {
|
||||||
}
|
return Ok(());
|
||||||
return editor.set_contents(&selected_pathbuf);
|
|
||||||
}
|
}
|
||||||
bail!("Failed to refresh editor contents")
|
self.editor.set_contents(&selected_pathbuf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Separate complex components into their own widgets.
|
|
||||||
impl<'a> Widget for &mut App<'a> {
|
impl<'a> Widget for &mut App<'a> {
|
||||||
fn render(self, area: Rect, buf: &mut Buffer)
|
fn render(self, area: Rect, buf: &mut Buffer)
|
||||||
where
|
where
|
||||||
@ -195,35 +194,17 @@ impl<'a> Widget for &mut App<'a> {
|
|||||||
self.draw_top_status(vertical[0], buf);
|
self.draw_top_status(vertical[0], buf);
|
||||||
self.draw_bottom_status(vertical[3], buf);
|
self.draw_bottom_status(vertical[3], buf);
|
||||||
self.draw_tabs(editor_layout[0], buf);
|
self.draw_tabs(editor_layout[0], buf);
|
||||||
let id = self.id().to_string();
|
let id = App::id().to_string();
|
||||||
for component in &mut self.components {
|
self.editor.render(editor_layout[1], buf);
|
||||||
match component {
|
self.explorer
|
||||||
AppComponents::AppEditor(editor) => editor.render(editor_layout[1], buf),
|
.render(horizontal[0], buf)
|
||||||
AppComponents::AppExplorer(explorer) => {
|
.context("Failed to render Explorer")
|
||||||
explorer
|
.unwrap_or_else(|e| error!(target:id.as_str(), "{}", e));
|
||||||
.render(horizontal[0], buf)
|
self.logger.render(vertical[2], buf);
|
||||||
.context("Failed to render Explorer")
|
|
||||||
.unwrap_or_else(|e| error!(target:id.as_str(), "{}", e));
|
|
||||||
}
|
|
||||||
AppComponents::AppLogger(logger) => logger.render(vertical[2], buf),
|
|
||||||
AppComponents::AppComponent(_) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Component for App<'a> {
|
impl<'a> Component for App<'a> {
|
||||||
fn id(&self) -> &str {
|
|
||||||
"App"
|
|
||||||
}
|
|
||||||
|
|
||||||
/// TODO: Get active widget with some Component trait function helper?
|
|
||||||
/// trait Component { fn get_state() -> ComponentState; }
|
|
||||||
/// if component.get_state() = ComponentState::Active { component.handle_event(); }
|
|
||||||
///
|
|
||||||
/// App could then provide helpers for altering Component state based on TUI grouping..
|
|
||||||
/// (such as editor tabs, file explorer, status bars, etc..)
|
|
||||||
///
|
|
||||||
/// Handles events for the App and delegates to attached Components.
|
/// Handles events for the App and delegates to attached Components.
|
||||||
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
||||||
// Handle events in the primary application.
|
// Handle events in the primary application.
|
||||||
@ -238,25 +219,21 @@ impl<'a> Component for App<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle events for all components.
|
// Handle events for all components.
|
||||||
for component in &mut self.components {
|
let action = match self.last_active {
|
||||||
let c = match component {
|
AppEditor => self.editor.handle_event(event)?,
|
||||||
AppComponents::AppEditor(e) => e as &mut dyn Component,
|
AppExplorer => self.explorer.handle_event(event)?,
|
||||||
AppComponents::AppExplorer(e) => e as &mut dyn Component,
|
AppLogger => self.logger.handle_event(event)?,
|
||||||
AppComponents::AppLogger(e) => e as &mut dyn Component,
|
};
|
||||||
AppComponents::AppComponent(e) => e.as_mut(),
|
// if !c.is_active() {
|
||||||
};
|
// if let Some(mouse) = event.as_mouse_event() {
|
||||||
if !c.is_active() {
|
// // Always handle mouse events for click interaction.
|
||||||
if let Some(mouse) = event.as_mouse_event() {
|
// c.handle_mouse_events(mouse)?;
|
||||||
// Always handle mouse events for click interaction.
|
// }
|
||||||
c.handle_mouse_events(mouse)?;
|
// continue;
|
||||||
}
|
// }
|
||||||
continue;
|
match action {
|
||||||
}
|
Action::Quit | Action::Handled => return Ok(action),
|
||||||
let action = c.handle_event(event.clone())?;
|
_ => {}
|
||||||
match action {
|
|
||||||
Action::Quit | Action::Handled => return Ok(action),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(Action::Noop)
|
Ok(Action::Noop)
|
||||||
}
|
}
|
||||||
@ -270,10 +247,7 @@ impl<'a> Component for App<'a> {
|
|||||||
kind: KeyEventKind::Press,
|
kind: KeyEventKind::Press,
|
||||||
state: _state,
|
state: _state,
|
||||||
} => {
|
} => {
|
||||||
self.get_component_mut::<Explorer>()
|
self.change_focus(AppExplorer);
|
||||||
.unwrap()
|
|
||||||
.component_state
|
|
||||||
.toggle_focus();
|
|
||||||
Ok(Action::Handled)
|
Ok(Action::Handled)
|
||||||
}
|
}
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
@ -282,10 +256,7 @@ impl<'a> Component for App<'a> {
|
|||||||
kind: KeyEventKind::Press,
|
kind: KeyEventKind::Press,
|
||||||
state: _state,
|
state: _state,
|
||||||
} => {
|
} => {
|
||||||
self.get_component_mut::<Editor>()
|
self.change_focus(AppEditor);
|
||||||
.unwrap()
|
|
||||||
.component_state
|
|
||||||
.toggle_focus();
|
|
||||||
Ok(Action::Handled)
|
Ok(Action::Handled)
|
||||||
}
|
}
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
@ -294,10 +265,7 @@ impl<'a> Component for App<'a> {
|
|||||||
kind: KeyEventKind::Press,
|
kind: KeyEventKind::Press,
|
||||||
state: _state,
|
state: _state,
|
||||||
} => {
|
} => {
|
||||||
self.get_component_mut::<Logger>()
|
self.change_focus(AppLogger);
|
||||||
.unwrap()
|
|
||||||
.component_state
|
|
||||||
.toggle_focus();
|
|
||||||
Ok(Action::Handled)
|
Ok(Action::Handled)
|
||||||
}
|
}
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
@ -306,11 +274,11 @@ impl<'a> Component for App<'a> {
|
|||||||
kind: KeyEventKind::Press,
|
kind: KeyEventKind::Press,
|
||||||
state: _state,
|
state: _state,
|
||||||
} => {
|
} => {
|
||||||
error!(target:self.id(), "an error");
|
error!(target:App::id(), "an error");
|
||||||
warn!(target:self.id(), "a warning");
|
warn!(target:App::id(), "a warning");
|
||||||
info!(target:self.id(), "a two line info\nsecond line");
|
info!(target:App::id(), "a two line info\nsecond line");
|
||||||
debug!(target:self.id(), "a debug");
|
debug!(target:App::id(), "a debug");
|
||||||
trace!(target:self.id(), "a trace");
|
trace!(target:App::id(), "a trace");
|
||||||
Ok(Action::Handled)
|
Ok(Action::Handled)
|
||||||
}
|
}
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
|
|||||||
@ -22,10 +22,6 @@ pub enum Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Component {
|
pub trait Component {
|
||||||
/// Returns a unique identifier for the component.
|
|
||||||
/// This is used for lookup in a container of Components.
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
|
|
||||||
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
||||||
match event {
|
match event {
|
||||||
Event::Key(key_event) => self.handle_key_events(key_event),
|
Event::Key(key_event) => self.handle_key_events(key_event),
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
use crate::tui::component::{Action, Component, ComponentState, Focus};
|
use crate::tui::component::{Action, Component, ComponentState, Focus};
|
||||||
|
|
||||||
use crate::tui::app::{AppComponents, ComponentOf};
|
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use edtui::{
|
use edtui::{
|
||||||
EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, Lines, SyntaxHighlighter,
|
EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, Lines, SyntaxHighlighter,
|
||||||
@ -24,22 +22,11 @@ pub struct Editor {
|
|||||||
pub(crate) component_state: ComponentState,
|
pub(crate) component_state: ComponentState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ComponentOf<Editor> for AppComponents<'a> {
|
|
||||||
fn as_ref(&self) -> Option<&Editor> {
|
|
||||||
if let AppComponents::AppEditor(ref e) = *self {
|
|
||||||
return Some(e);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
fn as_mut(&mut self) -> Option<&mut Editor> {
|
|
||||||
if let AppComponents::AppEditor(ref mut e) = *self {
|
|
||||||
return Some(e);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Editor {
|
impl Editor {
|
||||||
|
pub fn id() -> &'static str {
|
||||||
|
"Editor"
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Editor {
|
Editor {
|
||||||
state: EditorState::default(),
|
state: EditorState::default(),
|
||||||
@ -104,14 +91,6 @@ impl Widget for &mut Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Component for Editor {
|
impl Component for Editor {
|
||||||
fn id(&self) -> &str {
|
|
||||||
"Editor"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_active(&self) -> bool {
|
|
||||||
self.component_state.focus == Focus::Active
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
||||||
if let Some(key_event) = event.as_key_event() {
|
if let Some(key_event) = event.as_key_event() {
|
||||||
// Handle events here that should not be passed on to the vim emulation handler.
|
// Handle events here that should not be passed on to the vim emulation handler.
|
||||||
@ -140,4 +119,8 @@ impl Component for Editor {
|
|||||||
_ => Ok(Action::Noop),
|
_ => Ok(Action::Noop),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_active(&self) -> bool {
|
||||||
|
self.component_state.focus == Focus::Active
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
use crate::tui::app::{AppComponents, ComponentOf};
|
|
||||||
use crate::tui::component::{Action, Component, ComponentState, Focus};
|
use crate::tui::component::{Action, Component, ComponentState, Focus};
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
@ -12,28 +11,17 @@ use tui_tree_widget::{Tree, TreeItem, TreeState};
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Explorer<'a> {
|
pub struct Explorer<'a> {
|
||||||
root_path: std::path::PathBuf,
|
pub(crate) root_path: std::path::PathBuf,
|
||||||
tree_items: TreeItem<'a, String>,
|
tree_items: TreeItem<'a, String>,
|
||||||
tree_state: TreeState<String>,
|
tree_state: TreeState<String>,
|
||||||
pub(crate) component_state: ComponentState,
|
pub(crate) component_state: ComponentState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ComponentOf<Explorer<'a>> for AppComponents<'a> {
|
|
||||||
fn as_ref(&self) -> Option<&Explorer<'a>> {
|
|
||||||
if let AppComponents::AppExplorer(ref e) = *self {
|
|
||||||
return Some(e);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
fn as_mut(&mut self) -> Option<&mut Explorer<'a>> {
|
|
||||||
if let AppComponents::AppExplorer(ref mut e) = *self {
|
|
||||||
return Some(e);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Explorer<'a> {
|
impl<'a> Explorer<'a> {
|
||||||
|
pub fn id() -> &'static str {
|
||||||
|
"Explorer"
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(path: &std::path::PathBuf) -> Result<Self> {
|
pub fn new(path: &std::path::PathBuf) -> Result<Self> {
|
||||||
let explorer = Explorer {
|
let explorer = Explorer {
|
||||||
root_path: path.to_owned(),
|
root_path: path.to_owned(),
|
||||||
@ -123,21 +111,16 @@ impl<'a> Explorer<'a> {
|
|||||||
|
|
||||||
pub fn selected(&self) -> Result<String> {
|
pub fn selected(&self) -> Result<String> {
|
||||||
if let Some(path) = self.tree_state.selected().last() {
|
if let Some(path) = self.tree_state.selected().last() {
|
||||||
return Ok(std::path::absolute(path)?.to_str().unwrap().to_string());
|
return Ok(std::path::absolute(path)?
|
||||||
|
.to_str()
|
||||||
|
.context("Failed to get absolute path to selected TreeItem")?
|
||||||
|
.to_string());
|
||||||
}
|
}
|
||||||
bail!("Failed to get selected TreeItem")
|
bail!("Failed to get selected TreeItem")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Component for Explorer<'a> {
|
impl<'a> Component for Explorer<'a> {
|
||||||
fn id(&self) -> &str {
|
|
||||||
"Explorer"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_active(&self) -> bool {
|
|
||||||
self.component_state.focus == Focus::Active
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
||||||
if let Some(key_event) = event.as_key_event() {
|
if let Some(key_event) = event.as_key_event() {
|
||||||
// Handle events here that should not be passed on to the vim emulation handler.
|
// Handle events here that should not be passed on to the vim emulation handler.
|
||||||
@ -188,4 +171,8 @@ impl<'a> Component for Explorer<'a> {
|
|||||||
}
|
}
|
||||||
Ok(Action::Noop)
|
Ok(Action::Noop)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_active(&self) -> bool {
|
||||||
|
self.component_state.focus == Focus::Active
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
use crate::tui::app::{AppComponents, ComponentOf};
|
|
||||||
use crate::tui::component::{Action, Component, ComponentState, Focus};
|
use crate::tui::component::{Action, Component, ComponentState, Focus};
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
|
||||||
@ -14,27 +13,16 @@ pub struct Logger {
|
|||||||
pub(crate) component_state: ComponentState,
|
pub(crate) component_state: ComponentState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ComponentOf<Logger> for AppComponents<'a> {
|
|
||||||
fn as_ref(&self) -> Option<&Logger> {
|
|
||||||
if let AppComponents::AppLogger(ref e) = *self {
|
|
||||||
return Some(e);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
fn as_mut(&mut self) -> Option<&mut Logger> {
|
|
||||||
if let AppComponents::AppLogger(ref mut e) = *self {
|
|
||||||
return Some(e);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Logger {
|
impl Logger {
|
||||||
|
pub fn id() -> &'static str {
|
||||||
|
"Logger"
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: TuiWidgetState::new(),
|
state: TuiWidgetState::new(),
|
||||||
component_state: ComponentState::default().with_help_text(concat!(
|
component_state: ComponentState::default().with_help_text(concat!(
|
||||||
"Q: Quit | Tab: Switch state | ↑/↓: Select target | f: Focus target",
|
"Q: Quit | ↑/↓: Select target | f: Focus target",
|
||||||
" | ←/→: Display level | +/-: Filter level | Space: Toggle hidden targets",
|
" | ←/→: Display level | +/-: Filter level | Space: Toggle hidden targets",
|
||||||
" | h: Hide target selector | PageUp/Down: Scroll | Esc: Cancel scroll"
|
" | h: Hide target selector | PageUp/Down: Scroll | Esc: Cancel scroll"
|
||||||
)),
|
)),
|
||||||
@ -47,7 +35,6 @@ impl Widget for &Logger {
|
|||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
// TODO: Use output_file?
|
|
||||||
TuiLoggerSmartWidget::default()
|
TuiLoggerSmartWidget::default()
|
||||||
.style_error(Style::default().fg(Color::Red))
|
.style_error(Style::default().fg(Color::Red))
|
||||||
.style_debug(Style::default().fg(Color::Green))
|
.style_debug(Style::default().fg(Color::Green))
|
||||||
@ -66,14 +53,6 @@ impl Widget for &Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Component for Logger {
|
impl Component for Logger {
|
||||||
fn id(&self) -> &str {
|
|
||||||
"Logger"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_active(&self) -> bool {
|
|
||||||
self.component_state.focus == Focus::Active
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_event(&mut self, event: Event) -> anyhow::Result<Action> {
|
fn handle_event(&mut self, event: Event) -> anyhow::Result<Action> {
|
||||||
if let Some(key_event) = event.as_key_event() {
|
if let Some(key_event) = event.as_key_event() {
|
||||||
return self.handle_key_events(key_event);
|
return self.handle_key_events(key_event);
|
||||||
@ -99,4 +78,8 @@ impl Component for Logger {
|
|||||||
}
|
}
|
||||||
Ok(Action::Pass)
|
Ok(Action::Pass)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_active(&self) -> bool {
|
||||||
|
self.component_state.focus == Focus::Active
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user