Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
bloznelis committed Nov 5, 2023
1 parent 2381333 commit d122fbf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 90 deletions.
72 changes: 20 additions & 52 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use ratatui::{
layout::Rect,
Terminal,
};
use view::show_to_small_dialog;
use view::draw_too_small;

use clap::Parser;

Expand Down Expand Up @@ -62,18 +62,22 @@ fn run() -> Result<(), KbtError> {
let (sender, receiver): (Sender<AppEvent>, Receiver<AppEvent>) = channel();
let (_up_guard, _down_guard) = GenericKeyBackend::subscribe(&sender);
let handle = thread::spawn(move || listen_for_control(sender));
let layout = match &selection {
KeyboardSize::Keyboard60 => {
prepare_layout(keyboard60::ROWS.map(|rows| rows.to_vec()).to_vec())
}
KeyboardSize::Keyboard80 => {
prepare_layout(keyboard80::ROWS.map(|rows| rows.to_vec()).to_vec())
}
KeyboardSize::Keyboard100 => {
prepare_layout(keyboard100::ROWS.map(|rows| rows.to_vec()).to_vec())
}
};

let initial_app = App {
key_states: HashMap::new(),
event_receiver: receiver,
keyboard_size: selection,
layouts: KeyboardLayouts {
layout_60: prepare_layout(keyboard60::ROWS.map(|rows| rows.to_vec()).to_vec()),
layout_80: prepare_layout(keyboard80::ROWS.map(|rows| rows.to_vec()).to_vec()),
layout_100: prepare_layout(
keyboard100::ROWS.map(|rows| rows.to_vec()).to_vec(),
),
},
layout,
};

let res = run_keyboard(&mut terminal, initial_app);
Expand Down Expand Up @@ -157,14 +161,11 @@ fn run_keyboard<B: Backend>(terminal: &mut Terminal<B>, mut state: App) -> Resul
.recv_timeout(Duration::from_millis(100));

loop {
let does_fit = check_if_fits(terminal.size()?, &state);

match does_fit {
SizeCheckResult::Fits => {
terminal.draw(|f| view::draw(f, &state).expect("Failed to draw miserably"))
}
SizeCheckResult::TooSmall => terminal.draw(show_to_small_dialog),
}?;
if layout_fits(terminal.size()?, &state) {
terminal.draw(|f| view::draw(f, &state).expect("Failed to draw"))?
} else {
terminal.draw(draw_too_small)?
};

let app_event = state.event_receiver.recv()?;
match app_event {
Expand All @@ -190,39 +191,6 @@ fn run_keyboard<B: Backend>(terminal: &mut Terminal<B>, mut state: App) -> Resul
}
}

enum SizeCheckResult {
Fits,
TooSmall,
}

fn check_if_fits(terminal_size: Rect, state: &App) -> SizeCheckResult {
match state.keyboard_size {
KeyboardSize::Keyboard60 => {
if terminal_size.width > state.layouts.layout_60.width
&& terminal_size.height > state.layouts.layout_60.height
{
SizeCheckResult::Fits
} else {
SizeCheckResult::TooSmall
}
}
KeyboardSize::Keyboard80 => {
if terminal_size.width > state.layouts.layout_80.width
&& terminal_size.height > state.layouts.layout_80.height
{
SizeCheckResult::Fits
} else {
SizeCheckResult::TooSmall
}
}
KeyboardSize::Keyboard100 => {
if terminal_size.width > state.layouts.layout_100.width
&& terminal_size.height > state.layouts.layout_100.height
{
SizeCheckResult::Fits
} else {
SizeCheckResult::TooSmall
}
}
}
fn layout_fits(terminal_size: Rect, state: &App) -> bool {
terminal_size.width > state.layout.width && terminal_size.height > state.layout.height
}
9 changes: 1 addition & 8 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ use crate::key::Key;
pub struct App {
pub key_states: HashMap<Key, KeyState>,
pub event_receiver: Receiver<AppEvent>,
pub keyboard_size: KeyboardSize,
pub layouts: KeyboardLayouts,
}

pub struct KeyboardLayouts {
pub layout_60: KeyboardLayout,
pub layout_80: KeyboardLayout,
pub layout_100: KeyboardLayout,
pub layout: KeyboardLayout,
}

pub struct KeyboardLayout {
Expand Down
53 changes: 23 additions & 30 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,11 @@ use ratatui::widgets::{Block, BorderType, Borders, Paragraph};
use ratatui::Frame;

use crate::key::Key;
use crate::model::{KbtError, KeyState, KeyUI, KeyboardLayout, KeyboardSize, VerticalKeyPart};
use crate::model::{KbtError, KeyState, KeyUI, VerticalKeyPart};
use crate::{App, KEY_HEIGHT};

pub fn show_to_small_dialog(frame: &mut Frame) {
let terminal_size = frame.size();

let message = "window is too small :(";

let message_height: u16 = 1;
let message_width: u16 = 25;
let left_padding: u16 = (terminal_size.width / 2) - (message_width / 2);
let top_padding: u16 = (terminal_size.height / 2) - (message_height / 2);

let rect = Rect::new(left_padding, top_padding, message_width, message_height);

let title = Paragraph::new(message).style(
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::ITALIC),
);

frame.render_widget(title, rect);
}

pub fn draw(frame: &mut Frame, state: &App) -> Result<(), KbtError> {
match state.keyboard_size {
KeyboardSize::Keyboard60 => draw_layout(frame, state, &state.layouts.layout_60),
KeyboardSize::Keyboard80 => draw_layout(frame, state, &state.layouts.layout_80),
KeyboardSize::Keyboard100 => draw_layout(frame, state, &state.layouts.layout_100),
}
}

fn draw_layout(frame: &mut Frame, state: &App, layout: &KeyboardLayout) -> Result<(), KbtError> {
let layout = &state.layout;
let terminal_size: Rect = frame.size();

let left_padding: u16 = (terminal_size.width / 2) - (layout.width / 2);
Expand Down Expand Up @@ -144,3 +116,24 @@ fn draw_help(y_offset: u16, frame: &mut Frame) {
frame.render_widget(help, rect);
}
}

pub fn draw_too_small(frame: &mut Frame) {
let terminal_size = frame.size();

let message = "window is too small :(";

let message_height: u16 = 1;
let message_width: u16 = 25;
let left_padding: u16 = (terminal_size.width / 2) - (message_width / 2);
let top_padding: u16 = (terminal_size.height / 2) - (message_height / 2);

let rect = Rect::new(left_padding, top_padding, message_width, message_height);

let title = Paragraph::new(message).style(
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::ITALIC),
);

frame.render_widget(title, rect);
}

0 comments on commit d122fbf

Please sign in to comment.