Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyde46 committed May 20, 2024
1 parent c289a2d commit 9a57099
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
35 changes: 14 additions & 21 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
mod event;
mod search;

use eyre::Result;
use ratatui::{prelude::*, widgets::*};
use std::io::stdout;
use std::{
error::Error,
io,
time::{Duration, Instant},
};
use std::io::stdout;
use eyre::Result;
use ratatui::{prelude::*, widgets::*};
use termion::event::Key;
use termion::raw::IntoRawMode;
use termion::screen::IntoAlternateScreen;
use termion::event::Key;

use crate::config::HoardConfig;
use crate::core::trove::Trove;
use crate::core::HoardCmd;
use crate::ui::event::{Config, Event, Events};
use crate::ui::search::render::draw_search_screen;
use crate::ui::search::controls::draw_search_key_handler;
use crate::ui::search::controls::draw_search_key_handler;
use crate::ui::search::render::draw_search_screen;

const DEFAULT_COLLECTIONS: [&str; 2] = ["All", "Local"];

Expand All @@ -38,13 +38,13 @@ pub struct App {
// ratatui list of collections to display
pub collections: ListState,
// current screen to draw
pub screen: DrawState,
pub screen: DrawState,
// search string to filter commands displayed at the bottom
pub search_string: String,
pub collection: String,

pub trove: Trove,
}
}

impl Default for App {
fn default() -> Self {
Expand Down Expand Up @@ -72,7 +72,6 @@ impl App {
}
}


/// The main entry point for the UI
/// Handles setting up the UI, running the main loop
/// and switching between different screens based on events it recieves
Expand Down Expand Up @@ -104,7 +103,7 @@ fn run_app<B: Backend>(
terminal: &mut Terminal<B>,
mut app: App,
tick_rate: Duration,
) -> Result<Option<HoardCmd>> {
) -> Result<Option<HoardCmd>> {
let mut last_tick = Instant::now();
let events = Events::with_config(Config {
tick_rate: Duration::from_millis(tick_rate.as_millis() as u64),
Expand All @@ -115,19 +114,13 @@ fn run_app<B: Backend>(
// then check for any events that might have happened and handle them
loop {
let screen = match app.screen {
DrawState::Search => {
draw_search_screen
}
DrawState::Explore => {
not_implemented_ui
}
DrawState::About => {
not_implemented_ui
}
DrawState::Search => draw_search_screen,
DrawState::Explore => not_implemented_ui,
DrawState::About => not_implemented_ui,
};

terminal.draw(|f| screen(f, &mut app))?;

if let Event::Input(input) = events.next()? {
match app.screen {
DrawState::Search => {
Expand Down Expand Up @@ -159,7 +152,7 @@ fn run_app<B: Backend>(
}
}

pub fn not_implemented_key_handler(input: Key, app: &mut App) -> Option<HoardCmd> {
pub fn not_implemented_key_handler(input: Key, app: &mut App) -> Option<HoardCmd> {
match input {
Key::Esc | Key::Ctrl('c' | 'd' | 'g') => {
app.should_exit = true;
Expand Down
43 changes: 23 additions & 20 deletions src/ui/search/render.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::config::HoardConfig; use crate::ui::App; use ratatui::{prelude::*, widgets::*};
use crate::config::HoardConfig;
use crate::ui::App;
use ratatui::{prelude::*, widgets::*};

const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Draw the search screen
Expand All @@ -11,7 +14,6 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
/// The main screen will display the list of commands and details of the selected command
/// The footer will display the search string and the current collection
pub fn draw_search_screen(frame: &mut Frame, app: &mut App) {

let main_layout = Layout::new(
Direction::Vertical,
[
Expand All @@ -30,32 +32,26 @@ pub fn draw_search_screen(frame: &mut Frame, app: &mut App) {
}

/// Draw the version header
///
///
/// # Arguments
/// * `frame` - The frame to draw the UI components
/// * `rect` - The area to draw the version header
fn render_version_header(frame: &mut Frame, rect: Rect) {
let version = format!("Hoard v{}", VERSION);

frame.render_widget(
Paragraph::new(version),
rect,
);
frame.render_widget(Paragraph::new(version), rect);
}

/// Draw the search field
///
///
/// # Arguments
/// * `frame` - The frame to draw the UI components
/// * `rect` - The area to draw the search field
/// * `app` - The application state
fn render_search_field(frame: &mut Frame, rect: Rect, app: &mut App) {
let search_string = format!("[ {} ] > {}", app.collection, app.search_string);

frame.render_widget(
Paragraph::new(search_string),
rect,
);
frame.render_widget(Paragraph::new(search_string), rect);
}

/// Draw the main screen
Expand All @@ -71,12 +67,16 @@ fn render_main_screen(frame: &mut Frame, rect: Rect, app: &mut App) {
Constraint::Percentage(30), // Scrollable tray for a list of available commands
Constraint::Percentage(70), // Detail view for the "hovered" command by the selector
],
).split(rect);
)
.split(rect);

let vertical_scroll = 0; // from app state

let items = vec![
Line::from(vec![Span::raw("★ connect_"), Span::styled("psql", Style::new().fg(Color::Red))]),
Line::from(vec![
Span::raw("★ connect_"),
Span::styled("psql", Style::new().fg(Color::Red)),
]),
Line::from("★ deploy_heroku"),
Line::from(" hoard_trove_server_psql"),
Line::from(" connect_mcme_local_db"),
Expand All @@ -94,13 +94,13 @@ fn render_main_screen(frame: &mut Frame, rect: Rect, app: &mut App) {
let paragraph = Paragraph::new(items.clone())
.scroll((vertical_scroll as u16, 0))
.block(Block::new().borders(Borders::ALL)); // to show a background for the scrollbar

let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("↑"))
.end_symbol(Some("↓"));

let mut scrollbar_state = ScrollbarState::new(items.len()).position(vertical_scroll);

let area = main_screen_layout[0];

frame.render_widget(paragraph, area);
Expand All @@ -122,10 +122,14 @@ fn render_main_screen(frame: &mut Frame, rect: Rect, app: &mut App) {
Constraint::Percentage(30),
Constraint::Percentage(50),
],
).split(main_screen_layout[1]);
)
.split(main_screen_layout[1]);

frame.render_widget(
Paragraph::new("cd /home/monarch/code").block(Block::default().borders(Borders::ALL).title(" Command ")).alignment(Alignment::Center).wrap(Wrap { trim: false }),
Paragraph::new("cd /home/monarch/code")
.block(Block::default().borders(Borders::ALL).title(" Command "))
.alignment(Alignment::Center)
.wrap(Wrap { trim: false }),
detail_layout[0],
);

Expand All @@ -141,4 +145,3 @@ fn render_main_screen(frame: &mut Frame, rect: Rect, app: &mut App) {
detail_layout[2],
);
}

0 comments on commit 9a57099

Please sign in to comment.