Reorganize main function
This commit is contained in:
parent
5c6b50fea2
commit
ed5a26d88f
245
src/main.rs
245
src/main.rs
@ -64,119 +64,9 @@ impl Default for App {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
enable_raw_mode()?;
|
||||
|
||||
let mut stdout = stdout();
|
||||
execute!(stdout, EnterAlternateScreen, DisableMouseCapture)?;
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut terminal = Terminal::new(backend)?;
|
||||
|
||||
let events = Events::new(250);
|
||||
let mut app = App::default();
|
||||
|
||||
'outer: loop {
|
||||
terminal.draw(|f| {
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.margin(2)
|
||||
.constraints(
|
||||
[
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(3),
|
||||
]
|
||||
.as_ref(),
|
||||
)
|
||||
.split(f.size());
|
||||
|
||||
let msg = match (&app.error_msg, &app.state) {
|
||||
(Some(e), _) => vec![
|
||||
Span::raw("Error: "),
|
||||
Span::styled(e, Style::default().add_modifier(Modifier::RAPID_BLINK)),
|
||||
],
|
||||
(None, AppState::Calculator) => {
|
||||
vec![
|
||||
Span::raw("Press "),
|
||||
Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
|
||||
Span::raw(" to exit, "),
|
||||
Span::styled("h", Style::default().add_modifier(Modifier::BOLD)),
|
||||
Span::raw(format!(
|
||||
" for help - [{}] [{}] [{}]",
|
||||
app.calculator.get_display_mode(),
|
||||
app.calculator.get_angle_mode(),
|
||||
if app.calculator.get_save_on_close() {
|
||||
"W"
|
||||
} else {
|
||||
"w"
|
||||
}
|
||||
)),
|
||||
]
|
||||
}
|
||||
(None, _) => vec![
|
||||
Span::raw("Press "),
|
||||
Span::styled("<esc>", Style::default().add_modifier(Modifier::BOLD)),
|
||||
Span::raw(" to exit"),
|
||||
],
|
||||
};
|
||||
|
||||
let text = Text::from(Spans::from(msg));
|
||||
let help_message = Paragraph::new(text);
|
||||
f.render_widget(help_message, chunks[0]);
|
||||
|
||||
let mut stack: Vec<ListItem> = app
|
||||
.calculator
|
||||
.get_stack()
|
||||
.iter()
|
||||
.take(chunks[1].height as usize - 2)
|
||||
.enumerate()
|
||||
.rev()
|
||||
.map(|(i, m)| {
|
||||
let content = vec![Spans::from(Span::raw(
|
||||
match app.calculator.get_display_mode() {
|
||||
CalculatorDisplayMode::Default => format!("{:>2}: {}", i, *m),
|
||||
CalculatorDisplayMode::Separated { separator } => {
|
||||
format!("{:>2}: {}", i, format::separated(*m, *separator))
|
||||
}
|
||||
CalculatorDisplayMode::Scientific { precision } => {
|
||||
format!("{:>2}: {}", i, format::scientific(*m, *precision))
|
||||
}
|
||||
CalculatorDisplayMode::Engineering { precision } => {
|
||||
format!("{:>2}: {}", i, format::engineering(*m, *precision))
|
||||
}
|
||||
CalculatorDisplayMode::Fixed { precision } => {
|
||||
format!("{:>2}: {:.precision$}", i, m, precision = precision)
|
||||
}
|
||||
},
|
||||
))];
|
||||
ListItem::new(content)
|
||||
})
|
||||
.collect();
|
||||
|
||||
for _ in 0..(chunks[1]
|
||||
.height
|
||||
.saturating_sub(stack.len() as u16)
|
||||
.saturating_sub(2))
|
||||
{
|
||||
stack.insert(0, ListItem::new(Span::raw("~")));
|
||||
}
|
||||
|
||||
let stack =
|
||||
List::new(stack).block(Block::default().borders(Borders::ALL).title("Stack"));
|
||||
f.render_widget(stack, chunks[1]);
|
||||
|
||||
let input = Paragraph::new(app.calculator.get_l().as_ref())
|
||||
.style(Style::default())
|
||||
.block(Block::default().borders(Borders::ALL).title("Input"));
|
||||
f.render_widget(input, chunks[2]);
|
||||
|
||||
f.set_cursor(
|
||||
chunks[2].x + app.calculator.get_l().len() as u16 + 1,
|
||||
chunks[2].y + 1,
|
||||
);
|
||||
|
||||
match (&app.state, app.calculator.get_state()) {
|
||||
impl App {
|
||||
fn draw_clippy_dialogs<T: std::io::Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>) {
|
||||
match (&self.state, self.calculator.get_state()) {
|
||||
(AppState::Help, _) => {
|
||||
draw_clippy_rect(
|
||||
ClippyRectangle {
|
||||
@ -208,7 +98,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
draw_clippy_rect(
|
||||
ClippyRectangle {
|
||||
title: "Constants",
|
||||
msg: app
|
||||
msg: self
|
||||
.calculator
|
||||
.get_constants_iter()
|
||||
.map(|(key, constant)| {
|
||||
@ -228,7 +118,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
draw_clippy_rect(
|
||||
ClippyRectangle {
|
||||
title,
|
||||
msg: app
|
||||
msg: self
|
||||
.calculator
|
||||
.get_registers_iter()
|
||||
.map(|(key, value)| format!("{}: {}", key, value))
|
||||
@ -242,7 +132,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
draw_clippy_rect(
|
||||
ClippyRectangle {
|
||||
title: "Macros",
|
||||
msg: app
|
||||
msg: self
|
||||
.calculator
|
||||
.get_macros_iter()
|
||||
.map(|(key, mac)| format!("{}: {}", key, mac.help))
|
||||
@ -255,7 +145,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
(AppState::Calculator, CalculatorState::WaitingForSetting) => {
|
||||
draw_clippy_rect(
|
||||
ClippyRectangle {
|
||||
title: "Help",
|
||||
title: "Settings",
|
||||
msg: "\
|
||||
d => Degrees\n\
|
||||
r => Radians\n\
|
||||
@ -277,7 +167,126 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
})?;
|
||||
}
|
||||
|
||||
pub fn terminal_draw_func<T: std::io::Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>) {
|
||||
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) {
|
||||
(Some(e), _) => vec![
|
||||
Span::raw("Error: "),
|
||||
Span::styled(e, Style::default().add_modifier(Modifier::RAPID_BLINK)),
|
||||
],
|
||||
(None, AppState::Calculator) => {
|
||||
vec![
|
||||
Span::raw("Press "),
|
||||
Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
|
||||
Span::raw(" to exit, "),
|
||||
Span::styled("h", Style::default().add_modifier(Modifier::BOLD)),
|
||||
Span::raw(format!(
|
||||
" for help - [{}] [{}] [{}]",
|
||||
self.calculator.get_display_mode(),
|
||||
self.calculator.get_angle_mode(),
|
||||
if self.calculator.get_save_on_close() {
|
||||
"W"
|
||||
} else {
|
||||
"w"
|
||||
}
|
||||
)),
|
||||
]
|
||||
}
|
||||
(None, _) => vec![
|
||||
Span::raw("Press "),
|
||||
Span::styled("<esc>", Style::default().add_modifier(Modifier::BOLD)),
|
||||
Span::raw(" to exit"),
|
||||
],
|
||||
};
|
||||
let text = Text::from(Spans::from(msg));
|
||||
let help_message = Paragraph::new(text);
|
||||
f.render_widget(help_message, chunks[0]);
|
||||
|
||||
// Stack
|
||||
let mut stack: Vec<ListItem> = self
|
||||
.calculator
|
||||
.get_stack()
|
||||
.iter()
|
||||
.take(chunks[1].height as usize - 2)
|
||||
.enumerate()
|
||||
.rev()
|
||||
.map(|(i, m)| {
|
||||
let content = vec![Spans::from(Span::raw(
|
||||
match self.calculator.get_display_mode() {
|
||||
CalculatorDisplayMode::Default => format!("{:>2}: {}", i, *m),
|
||||
CalculatorDisplayMode::Separated { separator } => {
|
||||
format!("{:>2}: {}", i, format::separated(*m, *separator))
|
||||
}
|
||||
CalculatorDisplayMode::Scientific { precision } => {
|
||||
format!("{:>2}: {}", i, format::scientific(*m, *precision))
|
||||
}
|
||||
CalculatorDisplayMode::Engineering { precision } => {
|
||||
format!("{:>2}: {}", i, format::engineering(*m, *precision))
|
||||
}
|
||||
CalculatorDisplayMode::Fixed { precision } => {
|
||||
format!("{:>2}: {:.precision$}", i, m, precision = precision)
|
||||
}
|
||||
},
|
||||
))];
|
||||
ListItem::new(content)
|
||||
})
|
||||
.collect();
|
||||
|
||||
for _ in 0..(chunks[1]
|
||||
.height
|
||||
.saturating_sub(stack.len() as u16)
|
||||
.saturating_sub(2))
|
||||
{
|
||||
stack.insert(0, ListItem::new(Span::raw("~")));
|
||||
}
|
||||
|
||||
let stack = List::new(stack).block(Block::default().borders(Borders::ALL).title("Stack"));
|
||||
f.render_widget(stack, chunks[1]);
|
||||
|
||||
// Input box
|
||||
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, chunks[2]);
|
||||
|
||||
f.set_cursor(
|
||||
chunks[2].x + self.calculator.get_l().len() as u16 + 1,
|
||||
chunks[2].y + 1,
|
||||
);
|
||||
|
||||
// Help text
|
||||
self.draw_clippy_dialogs(f);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
enable_raw_mode()?;
|
||||
|
||||
let mut stdout = stdout();
|
||||
execute!(stdout, EnterAlternateScreen, DisableMouseCapture)?;
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut terminal = Terminal::new(backend)?;
|
||||
|
||||
let events = Events::new(250);
|
||||
let mut app = App::default();
|
||||
|
||||
'outer: loop {
|
||||
terminal.draw(|f| app.terminal_draw_func(f))?;
|
||||
|
||||
// Slurp events without a redraw
|
||||
for e in events.blocking_iter()? {
|
||||
|
Loading…
Reference in New Issue
Block a user