Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/mistress' into github-page
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-extracts committed Nov 19, 2023
2 parents 586ff03 + 6b5dc69 commit 19a9f0e
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mini-calc"
version = "2.9.0"
version = "2.9.1"
license = "GPL-3.0-or-later"
description = "A fully-featured minimalistic configurable rust calculator"
homepage = "https://calc.nwa2coco.fr"
Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ Plotting is powered by gnuplot, it will works on Unix-like (MacOS, Linux) but I
- [X] Plot runtime functions
- [X] Save plot to png/svg/pdf

- [ ] Add terminal ploting
- [ ] Calculate ploting height and width
- [ ] Computes individual points
- [ ] Generate each line
- [ ] Prints each line
- [X] Add terminal ploting
- [X] Calculate ploting height and width
- [X] Computes individual points
- [X] Generate each line
- [X] Prints each line

- [X] Auto level x axis in termplot
- [ ] Auto level y axis in termplot

#### Example of plotting

Expand All @@ -170,6 +173,17 @@ Defining f(x) = x² and plotting it with custom values (from -10 to 10, with a s

![](docs/assets/plot_f.png)

#### Example of terminal plotting

You can plot in the terminal, for example

![](docs/assets/plot_term_x_squared.png)

And it supports the labels too

![](docs/assets/plot_term_x_squared_labels.png)


### Exact math calculator

As this project provides a frontend with a parser we could plug an exact math engine in the backend to transform `calc` into a
Expand Down
Binary file added docs/assets/plot_term_x_squared.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/plot_term_x_squared_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/configuration/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn load_color(string: String) -> Color {

pub fn replace_variable(str: String) -> String {
str.replace("%author%", "Charlotte Thomas")
.replace("%version%", "v2.9.0")
.replace("%version%", "v2.9.1")
.to_string()
}

Expand Down
15 changes: 10 additions & 5 deletions src/interpreting/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::configuration::loader::{load, load_config, Config};
use crate::interpreting::interpreter::interpret;
use crate::parsing::ast::{Ast, Parameters};
use crate::utils::matrix_utils::{lup_decompose, lup_determinant, lup_invert, transpose};
use crate::utils::plot_utils::computes_lines;

use super::function::{add as other_add, mult};

Expand Down Expand Up @@ -41,7 +42,8 @@ pub fn exec(
"transpose" => transpose_matrices(&lst, &ram),
"det" => det_matrix(&lst, &ram),
"invert" => inverse_matrix(&lst, &ram),
"plot" => plot_fn(&lst, &ram, functions),
"plot" => plot_fn(&lst, &ram, functions, false),
"termplot" => plot_fn(&lst, &ram, functions, true),
s => {
let mut sram: HashMap<String, Parameters> = HashMap::new();
sram.insert("pi".to_string(), Parameters::Float(PI));
Expand Down Expand Up @@ -1036,6 +1038,7 @@ pub fn plot_fn(
p: &Vec<Parameters>,
ram: &Option<&mut HashMap<String, Parameters>>,
functions: Option<&mut HashMap<String, (Vec<Ast>, Ast)>>,
terminal: bool,
) -> Parameters {
let color = match load() {
Ok(cfg) => load_config(cfg).general_color,
Expand Down Expand Up @@ -1296,7 +1299,7 @@ pub fn plot_fn(
let mut sram: HashMap<String, Parameters> = HashMap::new();
sram.insert("pi".to_string(), Parameters::Float(PI));
sram.insert("e".to_string(), Parameters::Float(E));

let st = start;
while start <= end {
x.push(start);
if &fd == "" {
Expand Down Expand Up @@ -1359,8 +1362,10 @@ pub fn plot_fn(
.lines_points(&x, &y, &[]),
_ => f.axes2d().points(&x, &y, &[]),
};

f.show().unwrap();

if !terminal {
f.show().unwrap();
} else {
computes_lines(&x, &y, st, end, steps, title, xlabel, ylabel);
}
Parameters::Null
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ fn main() {
let style = &loaded.clone().prompt_style;
let mut text = &loaded.clone().prompt;
let mut verbose = false;
let version: String = "v2.9.0".to_string();
let version: String = "v2.9.1".to_string();
interface.set_completer(Arc::new(CalcCompleter));
interface
.set_prompt(&format!(
Expand Down Expand Up @@ -385,7 +385,7 @@ impl<Term: Terminal> Completer<Term> for CalcCompleter {
word: &str,
prompter: &linefeed::Prompter<Term>,
start: usize,
end: usize,
_end: usize,
) -> Option<Vec<linefeed::Completion>> {
let line = prompter.buffer();
let mut words = line[..start].split_whitespace();
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::slice::Iter;

use crate::lexing::token::Token::*;
use crate::lexing::token::{Precedence, Token, TokenType};
use crate::parsing::ast::{Ast, Parameters};
use crate::parsing::ast::Ast;
use crate::parsing::parselets::infix_parselet::{
AssignParselet, CallParselet, InfixParselet, NullParset, OperatorInfixParselet,
};
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod matrix_utils;
pub mod plot_utils;
123 changes: 123 additions & 0 deletions src/utils/plot_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
*
*
* **********
* **title **
* *| *
* *|+ *
* *|+ *
* *| *
* *--------*
*
*
*
*/
pub fn computes_lines(
x: &Vec<f64>,
y: &Vec<f64>,
start: f64,
end: f64,
steps: f64,
title: String,
xlabel: String,
ylabel: String,
) -> () {
let mut bitmap = vec![vec![' '; 100]; 30];
let z = x
.into_iter()
.zip(y)
.map(|(x, y)| (((*x - start) / steps) as usize, *y as usize));

z.for_each(|(x, y)| {
if x < 100 && y < 30 {
bitmap[y][x] = '+';
}
});

let first_line = vec!['*'; 103];
let last_line = vec!['*'; 103];

let x_line = vec!['-'; 100];
for char in first_line {
print!("{char}");
}

println!("");

if &title != "" {
let left_padding = (103 - title.len()) / 2;
let right_padding = (103 - title.len()) - left_padding;
for _ in 0..left_padding {
print!("*");
}
print!("{}", title);
for _ in 0..right_padding {
print!("*")
}

println!("");
}

let size = ylabel.len();
let i = ylabel.chars();
let mut label = Vec::new();
let f = 30 / 2 - size;
let e = 30 - f - size;
for _ in 0..f {
label.push('*')
}
i.for_each(|x| label.push(x));
for _ in 0..e {
label.push('*')
}
let mut iter_label = label.into_iter();

for x in (0..(bitmap.len())).rev() {
print!("{}", iter_label.next().unwrap());
print!("|");
let xs = &bitmap[x];
for y in 0..xs.len() {
print!("{}", xs[y]);
}
print!("*\n");
}

print!("*|");
for char in x_line {
print!("{char}");
}
println!("*");

print!("* ");
let string_start = format!("{start}").len();
let string_end = format!("{end}").len();
let middle = ((end + start) / 2.0) as i32;
let middle_string = format!("{middle}").len();
print!("{start}");
for _ in (string_start)..(100 / 2) {
print!(" ");
}
print!("{middle}");
for _ in (middle_string)..(100 / 2 - 1 - string_end) {
print!(" ");
}
println!("{end} *");

if &xlabel != "" {
let first = 103 / 2 - xlabel.len();
let last = 103 - first - xlabel.len();
for _ in 0..first {
print!("*")
}
print!("{xlabel}");
for _ in 0..last {
print!("*")
}
} else {
for char in last_line {
print!("{char}");
}
}

println!("");
}

0 comments on commit 19a9f0e

Please sign in to comment.