Reorganize main function

This commit is contained in:
Austen Adler 2021-05-14 20:59:35 -04:00
parent 5c6b50fea2
commit ed5a26d88f

View File

@ -64,124 +64,14 @@ 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()) {
(AppState::Help, _) => {
draw_clippy_rect(
ClippyRectangle {
title: "Help",
msg: "\
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 {
title: "Help",
msg: "\
+ => Add s => Sin\n\
- => Subtract c => Cos\n\
* => Multiply t => Tan\n\
@ -200,63 +90,63 @@ fn main() -> Result<(), Box<dyn Error>> {
^s => Save Config ^l => Load Config\n\
@ => Settings\
",
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForConstant) => {
draw_clippy_rect(
ClippyRectangle {
title: "Constants",
msg: app
.calculator
.get_constants_iter()
.map(|(key, constant)| {
format!("{}: {} ({})", key, constant.help, constant.value)
})
.fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(),
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForRegister(register_state)) => {
let title = match register_state {
RegisterState::Save => "Registers (press char to save)",
RegisterState::Load => "Registers",
};
draw_clippy_rect(
ClippyRectangle {
title,
msg: app
.calculator
.get_registers_iter()
.map(|(key, value)| format!("{}: {}", key, value))
.fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(),
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForMacro) => {
draw_clippy_rect(
ClippyRectangle {
title: "Macros",
msg: app
.calculator
.get_macros_iter()
.map(|(key, mac)| format!("{}: {}", key, mac.help))
.fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(),
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForSetting) => {
draw_clippy_rect(
ClippyRectangle {
title: "Help",
msg: "\
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForConstant) => {
draw_clippy_rect(
ClippyRectangle {
title: "Constants",
msg: self
.calculator
.get_constants_iter()
.map(|(key, constant)| {
format!("{}: {} ({})", key, constant.help, constant.value)
})
.fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(),
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForRegister(register_state)) => {
let title = match register_state {
RegisterState::Save => "Registers (press char to save)",
RegisterState::Load => "Registers",
};
draw_clippy_rect(
ClippyRectangle {
title,
msg: self
.calculator
.get_registers_iter()
.map(|(key, value)| format!("{}: {}", key, value))
.fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(),
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForMacro) => {
draw_clippy_rect(
ClippyRectangle {
title: "Macros",
msg: self
.calculator
.get_macros_iter()
.map(|(key, mac)| format!("{}: {}", key, mac.help))
.fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(),
},
f,
);
}
(AppState::Calculator, CalculatorState::WaitingForSetting) => {
draw_clippy_rect(
ClippyRectangle {
title: "Settings",
msg: "\
d => Degrees\n\
r => Radians\n\
_ => Default\n\
@ -271,13 +161,132 @@ fn main() -> Result<(), Box<dyn Error>> {
w => Do not write settings and stack on quit (default)\n\
W => Write stack and settings on quit\
",
},
f,
);
}
_ => {}
},
f,
);
}
})?;
_ => {}
}
}
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()? {