aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-07-22 21:30:53 -0230
committerSam Anthony <sam@samanthony.xyz>2023-07-22 21:30:53 -0230
commit4e17a27eb8d36868bcf7e1edf32affd84412a1f4 (patch)
tree2411be41f24f4ba697577147df14efebf2c11f9b
parenteb3dd36c4229241aad610b8ed812cc12d8f87c2b (diff)
downloadpfc-4e17a27eb8d36868bcf7e1edf32affd84412a1f4.zip
init go module
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock281
-rw-r--r--Cargo.toml8
-rw-r--r--Makefile22
-rw-r--r--README6
-rw-r--r--cmd/pfc.go9
-rw-r--r--go.mod3
-rw-r--r--src/function.rs61
-rw-r--r--src/input.rs94
-rw-r--r--src/lib.rs122
-rw-r--r--src/main.rs24
-rw-r--r--src/ui.rs106
12 files changed, 20 insertions, 717 deletions
diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index ea8c4bf..0000000
--- a/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/target
diff --git a/Cargo.lock b/Cargo.lock
deleted file mode 100644
index 08d4da2..0000000
--- a/Cargo.lock
+++ /dev/null
@@ -1,281 +0,0 @@
-# This file is automatically @generated by Cargo.
-# It is not intended for manual editing.
-version = 3
-
-[[package]]
-name = "autocfg"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
-
-[[package]]
-name = "bitflags"
-version = "1.3.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
-
-[[package]]
-name = "cassowary"
-version = "0.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
-
-[[package]]
-name = "cfg-if"
-version = "1.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
-
-[[package]]
-name = "crossterm"
-version = "0.25.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67"
-dependencies = [
- "bitflags",
- "crossterm_winapi",
- "libc",
- "mio",
- "parking_lot",
- "signal-hook",
- "signal-hook-mio",
- "winapi",
-]
-
-[[package]]
-name = "crossterm_winapi"
-version = "0.9.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c"
-dependencies = [
- "winapi",
-]
-
-[[package]]
-name = "libc"
-version = "0.2.139"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
-
-[[package]]
-name = "lock_api"
-version = "0.4.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df"
-dependencies = [
- "autocfg",
- "scopeguard",
-]
-
-[[package]]
-name = "log"
-version = "0.4.17"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
-dependencies = [
- "cfg-if",
-]
-
-[[package]]
-name = "mio"
-version = "0.8.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de"
-dependencies = [
- "libc",
- "log",
- "wasi",
- "windows-sys",
-]
-
-[[package]]
-name = "parking_lot"
-version = "0.12.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
-dependencies = [
- "lock_api",
- "parking_lot_core",
-]
-
-[[package]]
-name = "parking_lot_core"
-version = "0.9.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf"
-dependencies = [
- "cfg-if",
- "libc",
- "redox_syscall",
- "smallvec",
- "windows-sys",
-]
-
-[[package]]
-name = "pfc"
-version = "0.8.0"
-dependencies = [
- "crossterm",
- "tui",
-]
-
-[[package]]
-name = "redox_syscall"
-version = "0.2.16"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
-dependencies = [
- "bitflags",
-]
-
-[[package]]
-name = "scopeguard"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
-
-[[package]]
-name = "signal-hook"
-version = "0.3.14"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d"
-dependencies = [
- "libc",
- "signal-hook-registry",
-]
-
-[[package]]
-name = "signal-hook-mio"
-version = "0.2.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
-dependencies = [
- "libc",
- "mio",
- "signal-hook",
-]
-
-[[package]]
-name = "signal-hook-registry"
-version = "1.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
-dependencies = [
- "libc",
-]
-
-[[package]]
-name = "smallvec"
-version = "1.10.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
-
-[[package]]
-name = "tui"
-version = "0.19.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ccdd26cbd674007e649a272da4475fb666d3aa0ad0531da7136db6fab0e5bad1"
-dependencies = [
- "bitflags",
- "cassowary",
- "crossterm",
- "unicode-segmentation",
- "unicode-width",
-]
-
-[[package]]
-name = "unicode-segmentation"
-version = "1.10.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
-
-[[package]]
-name = "unicode-width"
-version = "0.1.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
-
-[[package]]
-name = "wasi"
-version = "0.11.0+wasi-snapshot-preview1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
-
-[[package]]
-name = "winapi"
-version = "0.3.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
-dependencies = [
- "winapi-i686-pc-windows-gnu",
- "winapi-x86_64-pc-windows-gnu",
-]
-
-[[package]]
-name = "winapi-i686-pc-windows-gnu"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
-
-[[package]]
-name = "winapi-x86_64-pc-windows-gnu"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
-
-[[package]]
-name = "windows-sys"
-version = "0.42.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
-dependencies = [
- "windows_aarch64_gnullvm",
- "windows_aarch64_msvc",
- "windows_i686_gnu",
- "windows_i686_msvc",
- "windows_x86_64_gnu",
- "windows_x86_64_gnullvm",
- "windows_x86_64_msvc",
-]
-
-[[package]]
-name = "windows_aarch64_gnullvm"
-version = "0.42.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
-
-[[package]]
-name = "windows_aarch64_msvc"
-version = "0.42.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
-
-[[package]]
-name = "windows_i686_gnu"
-version = "0.42.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
-
-[[package]]
-name = "windows_i686_msvc"
-version = "0.42.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
-
-[[package]]
-name = "windows_x86_64_gnu"
-version = "0.42.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
-
-[[package]]
-name = "windows_x86_64_gnullvm"
-version = "0.42.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
-
-[[package]]
-name = "windows_x86_64_msvc"
-version = "0.42.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
diff --git a/Cargo.toml b/Cargo.toml
deleted file mode 100644
index 8e064b7..0000000
--- a/Cargo.toml
+++ /dev/null
@@ -1,8 +0,0 @@
-[package]
-name = "pfc"
-version = "0.8.0"
-edition = "2021"
-
-[dependencies]
-tui = "0.19"
-crossterm = "0.25"
diff --git a/Makefile b/Makefile
index c6695e2..dd45c4c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,29 +1,21 @@
include config.mk
build:
- cargo fmt --all
- cargo test
- cargo build
-
-run: build
- target/debug/pfc
-
-doc:
- cargo doc --open
+ go mod tidy
+ gofmt -l -s -w ./**/*.go
clean:
- rm -r target
+ rm -f pfc
-install:
- cargo build --release
- cp target/release/pfc ${DESTDIR}${PREFIX}/bin
+install: build
+ cp pfc ${DESTDIR}${PREFIX}/bin
chmod 755 ${DESTDIR}${PREFIX}/bin/pfc
mkdir -p ${DESTDIR}${MANPREFIX}/man1
sed "s/VERSION/${VERSION}/g" < pfc.1 > ${DESTDIR}${MANPREFIX}/man1/pfc.1
chmod 644 ${DESTDIR}${MANPREFIX}/man1/pfc.1
uninstall:
- rm -f ${DESTDIR}${PREFIX}/bin/pfc\
+ rm -f ${DESTDIR}${PREFIX}/bin/pfc \
${DESTDIR}${MANPREFIX}/man1/pfc.1
-.PHONY: build run doc clean install uninstall
+.PHONY: build install uninstall
diff --git a/README b/README
index b9f5060..4acd7ad 100644
--- a/README
+++ b/README
@@ -6,11 +6,7 @@ terminal.
Requirements
------------
-Cargo, the Rust package manager, is required to build pfc.
-It can be installed from most Linux package managers or like so:
-
- curl https://sh.rustup.rs -sSf | sh
-
+Go is required to build pfc.
Installation
------------
diff --git a/cmd/pfc.go b/cmd/pfc.go
new file mode 100644
index 0000000..67821f5
--- /dev/null
+++ b/cmd/pfc.go
@@ -0,0 +1,9 @@
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ fmt.Println("Hello, world")
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..84505d8
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module git.samanthony.xyz/pfc
+
+go 1.20
diff --git a/src/function.rs b/src/function.rs
deleted file mode 100644
index bb6462d..0000000
--- a/src/function.rs
+++ /dev/null
@@ -1,61 +0,0 @@
-use crate::AngleMode;
-
-pub enum Function {
- Sin, // Sine
- Cos, // Cosine
- Tan, // Tangent
- Asin, // Inverse sine
- Acos, // Inverse cosine
- Atan, // Inverse tangent
- Deg, // Convert from radians to degrees
- Rad, // Convert from degrees to radians
-}
-
-impl Function {
- pub fn parse(s: &str) -> Result<Self, ParseFunctionError> {
- match s {
- "sin" => Ok(Self::Sin),
- "cos" => Ok(Self::Cos),
- "tan" => Ok(Self::Tan),
- "asin" => Ok(Self::Asin),
- "acos" => Ok(Self::Acos),
- "atan" => Ok(Self::Atan),
- "deg" => Ok(Self::Deg),
- "rad" => Ok(Self::Rad),
- _ => Err(ParseFunctionError(s.to_string())),
- }
- }
-
- pub fn call(&self, val: f64, angle_mode: AngleMode) -> f64 {
- match self {
- Self::Sin => match angle_mode {
- AngleMode::Degrees => val.to_radians().sin(),
- AngleMode::Radians => val.sin(),
- },
- Self::Cos => match angle_mode {
- AngleMode::Degrees => val.to_radians().cos(),
- AngleMode::Radians => val.cos(),
- },
- Self::Tan => match angle_mode {
- AngleMode::Degrees => val.to_radians().tan(),
- AngleMode::Radians => val.tan(),
- },
- Self::Asin => match angle_mode {
- AngleMode::Degrees => val.asin().to_degrees(),
- AngleMode::Radians => val.asin(),
- },
- Self::Acos => match angle_mode {
- AngleMode::Degrees => val.acos().to_degrees(),
- AngleMode::Radians => val.acos(),
- },
- Self::Atan => match angle_mode {
- AngleMode::Degrees => val.atan().to_degrees(),
- AngleMode::Radians => val.atan(),
- },
- Self::Deg => val.to_degrees(),
- Self::Rad => val.to_radians(),
- }
- }
-}
-
-pub struct ParseFunctionError(String);
diff --git a/src/input.rs b/src/input.rs
deleted file mode 100644
index 2d982e5..0000000
--- a/src/input.rs
+++ /dev/null
@@ -1,94 +0,0 @@
-use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
-
-use crate::{Calculator, Constant, Function, Operator, Signal};
-
-impl Calculator {
- pub fn handle_input(&mut self, key: KeyEvent) -> Signal {
- match key.modifiers {
- KeyModifiers::CONTROL => match key.code {
- KeyCode::Char('c') => {
- return Signal::Exit;
- }
- _ => {}
- },
- KeyModifiers::SHIFT => match key.code {
- KeyCode::Char('Q') => {
- return Signal::Exit;
- }
- KeyCode::Char('J' | 'K') => self.swap(),
- KeyCode::Char('D') => {
- self.input_buffer = String::new();
- }
- KeyCode::Char('C') => self.clear(),
- KeyCode::Char('A') => {
- self.angle_mode = self.angle_mode.toggle();
- }
- KeyCode::Char('N') => {
- if self.input_buffer.len() > 0 {
- if let Ok(f) = self.input_buffer.parse::<f64>() {
- self.input_buffer = format!("{}", -f);
- }
- } else if let Some(v) = self.stack.pop() {
- self.stack.push(-v);
- }
- }
- _ => {}
- },
- KeyModifiers::NONE => match key.code {
- KeyCode::Char(c) => self.push_to_buffer(c),
- KeyCode::Backspace => {
- self.input_buffer.pop();
- }
- KeyCode::Enter => {
- if let Ok(func) = Function::parse(&self.input_buffer) {
- if let Some(val) = self.stack.pop() {
- self.stack.push(func.call(val, self.angle_mode));
- }
- } else if let Ok(c) = Constant::parse(&self.input_buffer) {
- self.stack.push(c.value());
- } else if let Ok(bu) = self.input_buffer.parse::<f64>() {
- self.stack.push(bu);
- }
- self.input_buffer = String::new();
- }
- _ => {}
- },
- _ => {}
- }
- return Signal::None;
- }
-
- fn push_to_buffer(&mut self, c: char) {
- if c == '.' && !self.input_buffer.contains('.') {
- if self.input_buffer.len() == 0 {
- self.input_buffer.push('0');
- }
- self.input_buffer.push(c);
- } else if let Ok(op) = Operator::parse(c) {
- if let Ok(c) = Constant::parse(&self.input_buffer) {
- self.stack.push(c.value());
- } else if let Ok(f) = self.input_buffer.parse::<f64>() {
- self.stack.push(f);
- }
- self.input_buffer = String::new();
- self.perform_operation(op);
- } else {
- self.input_buffer.push(c);
- }
- }
-
- fn swap(&mut self) {
- if let Some(st) = self.stack.pop() {
- if let Ok(bu) = self.input_buffer.parse::<f64>() {
- self.stack.push(bu);
- }
- self.input_buffer = format!("{}", st);
- }
- }
-
- // Clear stack and input buffer.
- fn clear(&mut self) {
- self.input_buffer = String::new();
- self.stack = Vec::new();
- }
-}
diff --git a/src/lib.rs b/src/lib.rs
deleted file mode 100644
index db316f0..0000000
--- a/src/lib.rs
+++ /dev/null
@@ -1,122 +0,0 @@
-use std::{
- f64::consts::{E, PI},
- fmt::{self, Display, Formatter},
-};
-
-mod function;
-mod input;
-pub mod ui;
-
-pub use function::Function;
-
-#[derive(Default)]
-pub struct Calculator {
- stack: Vec<f64>,
- input_buffer: String,
- angle_mode: AngleMode,
-}
-
-impl Calculator {
- fn perform_operation(&mut self, op: Operator) {
- let rhs = match self.stack.pop() {
- Some(f) => f,
- None => {
- return;
- }
- };
- let lhs = match self.stack.pop() {
- Some(f) => f,
- None => {
- return;
- }
- };
- self.stack.push(match op {
- Operator::Add => lhs + rhs,
- Operator::Sub => lhs - rhs,
- Operator::Mul => lhs * rhs,
- Operator::Div => lhs / rhs,
- Operator::Exp => lhs.powf(rhs),
- });
- }
-}
-
-#[derive(Default, Copy, Clone, PartialEq)]
-pub enum AngleMode {
- #[default]
- Degrees,
- Radians,
-}
-
-impl AngleMode {
- fn toggle(&self) -> Self {
- match self {
- Self::Degrees => Self::Radians,
- Self::Radians => Self::Degrees,
- }
- }
-}
-
-impl Display for AngleMode {
- fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
- write!(
- f,
- "{}",
- match self {
- Self::Degrees => "deg",
- Self::Radians => "rad",
- }
- )
- }
-}
-
-enum Operator {
- Add,
- Sub,
- Mul,
- Div,
- Exp,
-}
-
-impl Operator {
- fn parse(c: char) -> Result<Self, ParseOperatorError> {
- match c {
- '+' => Ok(Self::Add),
- '-' => Ok(Self::Sub),
- '*' => Ok(Self::Mul),
- '/' => Ok(Self::Div),
- '^' => Ok(Self::Exp),
- _ => Err(ParseOperatorError(c)),
- }
- }
-}
-
-struct ParseOperatorError(char);
-
-enum Constant {
- Pi, // Archimedes’ constant (π)
- E, // Euler's number (e)
-}
-
-impl Constant {
- fn parse(s: &str) -> Result<Self, ParseConstantError> {
- match s {
- "pi" => Ok(Self::Pi),
- "e" => Ok(Self::E),
- _ => Err(ParseConstantError(s.to_string())),
- }
- }
-
- fn value(&self) -> f64 {
- match self {
- Self::Pi => PI,
- Self::E => E,
- }
- }
-}
-
-struct ParseConstantError(String);
-
-pub enum Signal {
- None,
- Exit,
-}
diff --git a/src/main.rs b/src/main.rs
deleted file mode 100644
index 85e3888..0000000
--- a/src/main.rs
+++ /dev/null
@@ -1,24 +0,0 @@
-use crossterm::event::{self, Event};
-use std::io;
-
-use pfc::{ui, Calculator, Signal};
-
-fn main() -> io::Result<()> {
- let mut terminal = ui::init_terminal()?;
- let mut calculator = Calculator::default();
-
- let result = || -> io::Result<()> {
- loop {
- terminal.draw(|f| calculator.draw(f))?;
-
- if let Event::Key(key) = event::read()? {
- if let Signal::Exit = calculator.handle_input(key) {
- return Ok(());
- }
- }
- }
- }();
-
- ui::cleanup_terminal(terminal)?;
- result
-}
diff --git a/src/ui.rs b/src/ui.rs
deleted file mode 100644
index 76ecaf5..0000000
--- a/src/ui.rs
+++ /dev/null
@@ -1,106 +0,0 @@
-use crossterm::{
- execute,
- terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
-};
-use std::io;
-use tui::{
- backend::{Backend, CrosstermBackend},
- layout::{Alignment, Constraint, Direction, Layout, Rect},
- style::{Modifier, Style},
- text::{Span, Spans},
- widgets::{Block, Borders, List, ListItem, Paragraph, Widget},
- Frame, Terminal,
-};
-
-use crate::{AngleMode, Calculator, Constant, Function};
-
-const WIDTH: u16 = 32;
-
-impl Calculator {
- pub fn draw<B: Backend>(&self, f: &mut Frame<B>) {
- let chunks = layout(self.stack.len(), f.size());
- f.render_widget(version_number_widget(), chunks[0]);
- f.render_widget(angle_mode_widget(self.angle_mode), chunks[2]);
- f.render_widget(stack_widget(&self.stack), chunks[3]);
- f.render_widget(input_buffer_widget(&self.input_buffer), chunks[4]);
- }
-}
-
-pub fn init_terminal() -> Result<Terminal<CrosstermBackend<io::Stdout>>, io::Error> {
- enable_raw_mode()?;
- let mut stdout = io::stdout();
- execute!(stdout, EnterAlternateScreen)?;
- let backend = CrosstermBackend::new(stdout);
- Terminal::new(backend)
-}
-
-pub fn cleanup_terminal<B>(mut terminal: Terminal<B>) -> Result<(), io::Error>
-where
- B: Backend + io::Write,
-{
- disable_raw_mode()?;
- execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
- terminal.show_cursor()?;
- Ok(())
-}
-
-fn layout(stack_size: usize, frame_size: Rect) -> Vec<Rect> {
- let columns = Layout::default()
- .direction(Direction::Horizontal)
- .constraints([Constraint::Length(WIDTH), Constraint::Max(u16::MAX)].as_ref())
- .split(frame_size);
-
- Layout::default()
- .direction(Direction::Vertical)
- .constraints(
- [
- Constraint::Length(1), // Version number
- Constraint::Max(u16::MAX), // Fill
- Constraint::Length(1), // Angle mode
- Constraint::Length(stack_size as u16 + 2), // Stack
- Constraint::Length(3), // Input buffer
- ]
- .as_ref(),
- )
- .split(columns[0])
-}
-
-fn angle_mode_widget(angle_mode: AngleMode) -> impl Widget {
- Paragraph::new(format!("{}", angle_mode)).alignment(Alignment::Right)
-}
-
-fn stack_widget(stack: &Vec<f64>) -> impl Widget {
- List::new(
- stack
- .iter()
- .map(|f| ListItem::new(format!(" {}", f)))
- .collect::<Vec<ListItem>>(),
- )
- .block(Block::default().borders(Borders::ALL))
-}
-
-fn input_buffer_widget(input_buffer: &str) -> impl Widget {
- Paragraph::new(Spans::from(vec![
- Span::raw(">"),
- Span::styled(
- format!(" {}", input_buffer),
- input_buffer_style(input_buffer),
- ),
- ]))
- .block(Block::default().borders(Borders::ALL))
-}
-
-fn version_number_widget() -> impl Widget {
- Paragraph::new(format!("pfc-{}", option_env!("CARGO_PKG_VERSION").unwrap()))
- .alignment(Alignment::Center)
-}
-
-fn input_buffer_style(input_buffer: &str) -> Style {
- if let Ok(_) = Function::parse(&input_buffer) {
- Style::default().add_modifier(Modifier::BOLD)
- } else if let Ok(_) = Constant::parse(&input_buffer) {
- Style::default().add_modifier(Modifier::BOLD)
- } else {
- Style::default()
- }
-}