Reorganize main function
This commit is contained in:
parent
5c6b50fea2
commit
ed5a26d88f
371
src/main.rs
371
src/main.rs
@ -64,124 +64,14 @@ impl Default for App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl App {
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn draw_clippy_dialogs<T: std::io::Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>) {
|
||||||
enable_raw_mode()?;
|
match (&self.state, self.calculator.get_state()) {
|
||||||
|
(AppState::Help, _) => {
|
||||||
let mut stdout = stdout();
|
draw_clippy_rect(
|
||||||
execute!(stdout, EnterAlternateScreen, DisableMouseCapture)?;
|
ClippyRectangle {
|
||||||
let backend = CrosstermBackend::new(stdout);
|
title: "Help",
|
||||||
let mut terminal = Terminal::new(backend)?;
|
msg: "\
|
||||||
|
|
||||||
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: "\
|
|
||||||
+ => Add s => Sin\n\
|
+ => Add s => Sin\n\
|
||||||
- => Subtract c => Cos\n\
|
- => Subtract c => Cos\n\
|
||||||
* => Multiply t => Tan\n\
|
* => Multiply t => Tan\n\
|
||||||
@ -200,63 +90,63 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
^s => Save Config ^l => Load Config\n\
|
^s => Save Config ^l => Load Config\n\
|
||||||
@ => Settings\
|
@ => Settings\
|
||||||
",
|
",
|
||||||
},
|
},
|
||||||
f,
|
f,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
(AppState::Calculator, CalculatorState::WaitingForConstant) => {
|
(AppState::Calculator, CalculatorState::WaitingForConstant) => {
|
||||||
draw_clippy_rect(
|
draw_clippy_rect(
|
||||||
ClippyRectangle {
|
ClippyRectangle {
|
||||||
title: "Constants",
|
title: "Constants",
|
||||||
msg: app
|
msg: self
|
||||||
.calculator
|
.calculator
|
||||||
.get_constants_iter()
|
.get_constants_iter()
|
||||||
.map(|(key, constant)| {
|
.map(|(key, constant)| {
|
||||||
format!("{}: {} ({})", key, constant.help, constant.value)
|
format!("{}: {} ({})", key, constant.help, constant.value)
|
||||||
})
|
})
|
||||||
.fold(String::new(), |acc, s| acc + &s + "\n")
|
.fold(String::new(), |acc, s| acc + &s + "\n")
|
||||||
.trim_end(),
|
.trim_end(),
|
||||||
},
|
},
|
||||||
f,
|
f,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
(AppState::Calculator, CalculatorState::WaitingForRegister(register_state)) => {
|
(AppState::Calculator, CalculatorState::WaitingForRegister(register_state)) => {
|
||||||
let title = match register_state {
|
let title = match register_state {
|
||||||
RegisterState::Save => "Registers (press char to save)",
|
RegisterState::Save => "Registers (press char to save)",
|
||||||
RegisterState::Load => "Registers",
|
RegisterState::Load => "Registers",
|
||||||
};
|
};
|
||||||
draw_clippy_rect(
|
draw_clippy_rect(
|
||||||
ClippyRectangle {
|
ClippyRectangle {
|
||||||
title,
|
title,
|
||||||
msg: app
|
msg: self
|
||||||
.calculator
|
.calculator
|
||||||
.get_registers_iter()
|
.get_registers_iter()
|
||||||
.map(|(key, value)| format!("{}: {}", key, value))
|
.map(|(key, value)| format!("{}: {}", key, value))
|
||||||
.fold(String::new(), |acc, s| acc + &s + "\n")
|
.fold(String::new(), |acc, s| acc + &s + "\n")
|
||||||
.trim_end(),
|
.trim_end(),
|
||||||
},
|
},
|
||||||
f,
|
f,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
(AppState::Calculator, CalculatorState::WaitingForMacro) => {
|
(AppState::Calculator, CalculatorState::WaitingForMacro) => {
|
||||||
draw_clippy_rect(
|
draw_clippy_rect(
|
||||||
ClippyRectangle {
|
ClippyRectangle {
|
||||||
title: "Macros",
|
title: "Macros",
|
||||||
msg: app
|
msg: self
|
||||||
.calculator
|
.calculator
|
||||||
.get_macros_iter()
|
.get_macros_iter()
|
||||||
.map(|(key, mac)| format!("{}: {}", key, mac.help))
|
.map(|(key, mac)| format!("{}: {}", key, mac.help))
|
||||||
.fold(String::new(), |acc, s| acc + &s + "\n")
|
.fold(String::new(), |acc, s| acc + &s + "\n")
|
||||||
.trim_end(),
|
.trim_end(),
|
||||||
},
|
},
|
||||||
f,
|
f,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
(AppState::Calculator, CalculatorState::WaitingForSetting) => {
|
(AppState::Calculator, CalculatorState::WaitingForSetting) => {
|
||||||
draw_clippy_rect(
|
draw_clippy_rect(
|
||||||
ClippyRectangle {
|
ClippyRectangle {
|
||||||
title: "Help",
|
title: "Settings",
|
||||||
msg: "\
|
msg: "\
|
||||||
d => Degrees\n\
|
d => Degrees\n\
|
||||||
r => Radians\n\
|
r => Radians\n\
|
||||||
_ => Default\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 => Do not write settings and stack on quit (default)\n\
|
||||||
W => Write stack and settings on quit\
|
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
|
// Slurp events without a redraw
|
||||||
for e in events.blocking_iter()? {
|
for e in events.blocking_iter()? {
|
||||||
|
Loading…
Reference in New Issue
Block a user