Separate logic where required
This commit is contained in:
parent
416cc27006
commit
19632963bb
87
src/main.rs
87
src/main.rs
@ -1,3 +1,4 @@
|
|||||||
|
// I am not yet worthy for this yet: clippy::restriction
|
||||||
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
||||||
// This is intended behavior
|
// This is intended behavior
|
||||||
#![allow(clippy::cast_possible_truncation)]
|
#![allow(clippy::cast_possible_truncation)]
|
||||||
@ -8,6 +9,8 @@ mod calc;
|
|||||||
mod event;
|
mod event;
|
||||||
mod format;
|
mod format;
|
||||||
|
|
||||||
|
const BORDER_SIZE: u16 = 2;
|
||||||
|
|
||||||
use calc::{
|
use calc::{
|
||||||
errors::CalculatorResult,
|
errors::CalculatorResult,
|
||||||
types::{CalculatorAlignment, CalculatorDisplayMode, CalculatorState, RegisterState},
|
types::{CalculatorAlignment, CalculatorDisplayMode, CalculatorState, RegisterState},
|
||||||
@ -24,7 +27,7 @@ use event::{Event, Events};
|
|||||||
use std::{cmp, convert::TryFrom, error::Error, io, io::Write};
|
use std::{cmp, convert::TryFrom, error::Error, io, io::Write};
|
||||||
use tui::{
|
use tui::{
|
||||||
backend::CrosstermBackend,
|
backend::CrosstermBackend,
|
||||||
layout::{Constraint, Direction, Layout},
|
layout::{Constraint, Direction, Layout, Rect},
|
||||||
style::{Modifier, Style},
|
style::{Modifier, Style},
|
||||||
terminal::Frame,
|
terminal::Frame,
|
||||||
text::{Span, Spans, Text},
|
text::{Span, Spans, Text},
|
||||||
@ -178,21 +181,7 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn terminal_draw_func<T: Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>) {
|
fn draw_status_line<T: Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>, chunk: Rect) {
|
||||||
let chunks = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.margin(2)
|
|
||||||
.constraints(
|
|
||||||
[
|
|
||||||
Constraint::Length(1),
|
|
||||||
Constraint::Min(1),
|
|
||||||
Constraint::Length(3),
|
|
||||||
]
|
|
||||||
.as_ref(),
|
|
||||||
)
|
|
||||||
.split(f.size());
|
|
||||||
|
|
||||||
// Help message
|
|
||||||
let msg = match (&self.error_msg, &self.state) {
|
let msg = match (&self.error_msg, &self.state) {
|
||||||
(Some(e), _) => vec![
|
(Some(e), _) => vec![
|
||||||
Span::raw("Error: "),
|
Span::raw("Error: "),
|
||||||
@ -215,14 +204,27 @@ impl App {
|
|||||||
};
|
};
|
||||||
let text = Text::from(Spans::from(msg));
|
let text = Text::from(Spans::from(msg));
|
||||||
let help_message = Paragraph::new(text);
|
let help_message = Paragraph::new(text);
|
||||||
f.render_widget(help_message, chunks[0]);
|
f.render_widget(help_message, chunk);
|
||||||
|
}
|
||||||
|
|
||||||
// Stack
|
fn draw_input_box<T: Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>, chunk: Rect) {
|
||||||
|
let input = Paragraph::new(self.calculator.get_l().as_ref())
|
||||||
|
.style(Style::default())
|
||||||
|
.block(Block::default().borders(Borders::ALL).title("Input"));
|
||||||
|
f.render_widget(input, chunk);
|
||||||
|
|
||||||
|
f.set_cursor(
|
||||||
|
chunk.x + self.calculator.get_l().len() as u16 + 1,
|
||||||
|
chunk.y + 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_stack<T: Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>, chunk: Rect) {
|
||||||
let mut stack: Vec<ListItem> = self
|
let mut stack: Vec<ListItem> = self
|
||||||
.calculator
|
.calculator
|
||||||
.stack
|
.stack
|
||||||
.iter()
|
.iter()
|
||||||
.take((chunks[1].height as usize).saturating_sub(2))
|
.take(chunk.height.saturating_sub(BORDER_SIZE) as usize)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.rev()
|
.rev()
|
||||||
.map(|(i, m)| {
|
.map(|(i, m)| {
|
||||||
@ -245,11 +247,11 @@ impl App {
|
|||||||
CalculatorAlignment::Left => format!("{:>2}: {}", i, number),
|
CalculatorAlignment::Left => format!("{:>2}: {}", i, number),
|
||||||
CalculatorAlignment::Right => {
|
CalculatorAlignment::Right => {
|
||||||
let ret = format!("{} :{:>2}", number, i);
|
let ret = format!("{} :{:>2}", number, i);
|
||||||
if ret.len() < (chunks[1].width as usize).saturating_sub(2) {
|
if ret.len() < chunk.width.saturating_sub(BORDER_SIZE) as usize {
|
||||||
format!(
|
format!(
|
||||||
"{: >width$}",
|
"{: >width$}",
|
||||||
ret,
|
ret,
|
||||||
width = (chunks[1].width as usize).saturating_sub(2)
|
width = chunk.width.saturating_sub(BORDER_SIZE) as usize
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
ret
|
ret
|
||||||
@ -260,10 +262,10 @@ impl App {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for _ in 0..(chunks[1]
|
for _ in 0..(chunk
|
||||||
.height
|
.height
|
||||||
.saturating_sub(stack.len() as u16)
|
.saturating_sub(stack.len() as u16)
|
||||||
.saturating_sub(2))
|
.saturating_sub(BORDER_SIZE))
|
||||||
{
|
{
|
||||||
stack.insert(
|
stack.insert(
|
||||||
0,
|
0,
|
||||||
@ -272,27 +274,35 @@ impl App {
|
|||||||
CalculatorAlignment::Right => format!(
|
CalculatorAlignment::Right => format!(
|
||||||
"{: >width$}",
|
"{: >width$}",
|
||||||
"~",
|
"~",
|
||||||
width = (chunks[1].width as usize).saturating_sub(2)
|
width = chunk.width.saturating_sub(BORDER_SIZE) as usize
|
||||||
),
|
),
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let stack = List::new(stack).block(Block::default().borders(Borders::ALL).title("Stack"));
|
let stack = List::new(stack).block(Block::default().borders(Borders::ALL).title("Stack"));
|
||||||
f.render_widget(stack, chunks[1]);
|
f.render_widget(stack, chunk);
|
||||||
|
}
|
||||||
|
|
||||||
// Input box
|
pub fn terminal_draw_func<T: Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>) {
|
||||||
let input = Paragraph::new(self.calculator.get_l().as_ref())
|
let chunks = Layout::default()
|
||||||
.style(Style::default())
|
.direction(Direction::Vertical)
|
||||||
.block(Block::default().borders(Borders::ALL).title("Input"));
|
.margin(BORDER_SIZE)
|
||||||
f.render_widget(input, chunks[2]);
|
.constraints(
|
||||||
|
[
|
||||||
|
Constraint::Length(1),
|
||||||
|
Constraint::Min(1),
|
||||||
|
Constraint::Length(3),
|
||||||
|
]
|
||||||
|
.as_ref(),
|
||||||
|
)
|
||||||
|
.split(f.size());
|
||||||
|
|
||||||
f.set_cursor(
|
self.draw_stack(f, chunks[1]);
|
||||||
chunks[2].x + self.calculator.get_l().len() as u16 + 1,
|
self.draw_input_box(f, chunks[2]);
|
||||||
chunks[2].y + 1,
|
// Draw the status line last in case something above has an error
|
||||||
);
|
self.draw_status_line(f, chunks[0]);
|
||||||
|
|
||||||
// Help text
|
|
||||||
self.draw_clippy_dialogs(f);
|
self.draw_clippy_dialogs(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -473,14 +483,14 @@ impl ClippyRectangle<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn draw_clippy_rect<T: Write>(c: &ClippyRectangle, f: &mut Frame<CrosstermBackend<T>>) {
|
fn draw_clippy_rect<T: Write>(c: &ClippyRectangle, f: &mut Frame<CrosstermBackend<T>>) {
|
||||||
// let block = Block::default().title(c.title).borders(Borders::ALL);
|
|
||||||
let dimensions = c.size();
|
let dimensions = c.size();
|
||||||
let popup_layout = Layout::default()
|
let popup_layout = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
.constraints(
|
.constraints(
|
||||||
[
|
[
|
||||||
|
// Padding
|
||||||
Constraint::Min(1),
|
Constraint::Min(1),
|
||||||
Constraint::Length(dimensions.height + 2),
|
Constraint::Length(dimensions.height + BORDER_SIZE),
|
||||||
]
|
]
|
||||||
.as_ref(),
|
.as_ref(),
|
||||||
)
|
)
|
||||||
@ -489,8 +499,9 @@ fn draw_clippy_rect<T: Write>(c: &ClippyRectangle, f: &mut Frame<CrosstermBacken
|
|||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.constraints(
|
.constraints(
|
||||||
[
|
[
|
||||||
|
// Padding
|
||||||
Constraint::Min(1),
|
Constraint::Min(1),
|
||||||
Constraint::Length(cmp::max(dimensions.width, c.title.len() as u16) + 2),
|
Constraint::Length(cmp::max(dimensions.width, c.title.len() as u16) + BORDER_SIZE),
|
||||||
]
|
]
|
||||||
.as_ref(),
|
.as_ref(),
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user