Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
Merge v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-dr authored Aug 17, 2023
2 parents 816e348 + da1507e commit a6c3ffa
Show file tree
Hide file tree
Showing 21 changed files with 401 additions and 221 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "daisycalc"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
build = "buildscript/main.rs"
license = "GPL-3.0-only"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A high-precision scientific calculator with support for units, derivatives, and
Many features are missing, this is still under development.

# 📦 Installation
- **Cargo:** `cargo install daisycalc`
- **Arch:** `yay -S daisy`
- **Debian:** coming soon

Expand All @@ -14,7 +15,7 @@ Binary will be in `target/release/daisy`

# 📹 Screenshot

![](https://betalupi.com/static/git/daisy.png)
![Screenshot](https://github.com/rm-dr/daisy/assets/96270320/cc71887a-0fde-46b2-a13b-96b05098b158)

# 🛠️ Features
- Open-source
Expand Down Expand Up @@ -65,4 +66,4 @@ Daisy instead provides four functions (`fromCelsius`, `toCelsius`, `fromFahrenhe

## Multiplication Order

Implicit multiplication has a higher priority than division. `pi/2 radians` will parse as `pi/(2 radians)`. Type `(pi/2) radians` or `pi/2 * radians` to get 90 degrees.
Implicit multiplication has a higher priority than division. `pi/2 radians` will parse as `pi/(2 radians)`. Type `(pi/2) radians` or `pi/2 * radians` to get 90 degrees.
8 changes: 2 additions & 6 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
- run cargo test
- commit
- git tag -a v1.0.0 -m "Version 1.0.0"
- push
- git push
- git push origin v1.0.0
- cargo publish
- Update packages

Expand All @@ -26,11 +27,6 @@
- Optional history file
- daisyrc file
- Compile to WASM, publish a webapp
- Options:
- disable replacement
- disable special characters
- 1/ as -1 power toggle
- powers as superscripts toggle
- evaluate straight from command line
- Auto-push to crates.io
- Package for debian
Expand Down
8 changes: 4 additions & 4 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn greeter() -> FormattedText {

#[inline(always)]
pub fn do_command(
context: &mut Context,
s: &String,
context: &mut Context
) -> FormattedText {
let args: Vec<&str> = s.split(" ").collect();
let first = args[0];
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn do_command(

t.push(&format!(
" {key}{padding} = [c]{v}[n]\n",
v = value.to_string(),
v = value.display(context),
));
}
}
Expand All @@ -184,7 +184,7 @@ pub fn do_command(

t.push(&format!(
" {s}{padding} = [c]{v}[n]\n",
v = exp.to_string(),
v = exp.display(context),
));
}
}
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn do_command(
}

let v = args[1].to_string();
let v = substitute(&v, context);
let v = substitute(context, &v);
let r = context.delete(&v);

return match r {
Expand Down
70 changes: 66 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,68 @@ use crate::quantity::freeunit_from_string;
use std::collections::HashMap;

#[derive(Debug)]
#[derive(Clone)]
pub struct Config {

// How to color terminal text.
// 0: No colors
// 1: ANSI-compatible, 8 colors
// 2: Full 256 color and special styles
pub term_color_type: u8,

// Should we accept input and print in unicode?
//pub enable_unicode: bool,

// Should we replace certain strings (like "pi")
// with prettier unicode alternatives?
//
// Automatically disabled if enable_unicode is off.
//pub enable_substituion: bool,

// Should we print simple powers
// as unicode superscript chars?
//
// Automatically disables if enable_unicode is off.
pub enable_super_powers: bool,

// Should we write "one-over" fractions
// as -1 powers?
//
// Automatically disabled if enable_super_powers is off.
pub enable_one_over_power: bool,
}

impl Config {
pub fn new() -> Config {
Config{
term_color_type: 2,
//enable_substituion: true,
//enable_unicode: true,
enable_super_powers: true,
enable_one_over_power: true
}
}

pub fn check(&mut self) {
//if !self.enable_unicode {
// self.enable_substituion = false;
// self.enable_super_powers = false;
//}

if !self.enable_super_powers {
self.enable_one_over_power = false
}
}
}




#[derive(Debug)]
#[derive(Clone)]
pub struct Context {
pub config: Config,

history: Vec<Expression>,
variables: HashMap<String, Expression>,
functions: HashMap<String, (Vec<String>, Expression)>,
Expand All @@ -16,6 +77,7 @@ pub struct Context {
impl Context {
pub fn new() -> Context {
Context{
config: Config::new(),
history: Vec::new(),
variables: HashMap::new(),
functions: HashMap::new(),
Expand Down Expand Up @@ -84,10 +146,10 @@ impl Context {
}

pub fn is_varible(&self, s: &str) -> bool {
return self.valid_varible(s) && (
self.variables.contains_key(s) ||
self.shadow.contains_key(s)
);
return {
self.valid_varible(s) &&
(self.variables.contains_key(s) || self.shadow.contains_key(s))
};
}

pub fn get_variables(&self) -> &HashMap<String, Expression> {
Expand Down
8 changes: 4 additions & 4 deletions src/entrypoint/unix/promptbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl PromptBuffer {
}

// Same as write_primpt, but pretends there is no cursor
pub fn write_prompt_nocursor(&mut self, stdout: &mut RawTerminal<std::io::Stdout>, context: &Context) -> Result<(), std::io::Error> {
pub fn write_prompt_nocursor(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
// Draw prettyprinted expression
let (_, s) = substitute_cursor(&self.get_contents(), self.buffer.chars().count(), context);
let (_, s) = substitute_cursor(context, &self.get_contents(), self.buffer.chars().count());

write!(
stdout, "\r{}{}==>{}{} {}",
Expand All @@ -64,12 +64,12 @@ impl PromptBuffer {
return Ok(());
}

pub fn write_prompt(&mut self, stdout: &mut RawTerminal<std::io::Stdout>, context: &Context) -> Result<(), std::io::Error> {
pub fn write_prompt(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
let l = self.buffer.chars().count();
let i = if l == 0 {0} else {l - self.cursor};

// Draw prettyprinted expression
let (display_cursor, s) = substitute_cursor(&self.get_contents(), i, context);
let (display_cursor, s) = substitute_cursor(context, &self.get_contents(), i);

write!(
stdout, "\r{}{}==>{}{} {}",
Expand Down
55 changes: 41 additions & 14 deletions src/entrypoint/unix/unix.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@
use std::io::Write;
use std::io::stdout;
use std::io::stdin;
use std::env;

use termion::{
event::Key,
input::TermRead,
raw::IntoRawMode
raw::IntoRawMode,
color::DetectColors
};

use super::promptbuffer::PromptBuffer;
use crate::command;
use crate::context::Context;

use crate::FormattedText;

#[inline(always)]
pub fn main() -> Result<(), std::io::Error> {
let mut stdout = stdout().into_raw_mode().unwrap();
let mut pb: PromptBuffer = PromptBuffer::new(64);
let mut context: Context = Context::new();
let mut context = Context::new();

// Set color compatibilty
let term_colors = stdout.available_colors().unwrap_or(0);
if term_colors >= 256 {
context.config.term_color_type = 2
} else if term_colors >= 8 {
context.config.term_color_type = 1
} else {
context.config.term_color_type = 0
}

context.config.check();



// Handle command-line arguments
let args: Vec<String> = env::args().collect();
if args.iter().any(|s| s == "--help") {
let t = command::do_command(&String::from("help"), &mut context);
t.write(&mut stdout)?;
let t = command::do_command(&mut context, &String::from("help"));
t.write(&context, &mut stdout)?;
return Ok(());
} else if args.iter().any(|s| s == "--version") {
write!(stdout, "Daisy v{}\r\n", env!("CARGO_PKG_VERSION"))?;
let t = FormattedText::new(format!(
"Daisy v{}\n", env!("CARGO_PKG_VERSION")
));
t.write(&context, &mut stdout)?;
return Ok(());
} else if args.iter().any(|s| s == "--debug") {
let t = FormattedText::new(format!(
concat!(
"Daisy v{}\n",
"Your terminal supports {} colors.\n"
),
env!("CARGO_PKG_VERSION"),
term_colors
));
t.write(&context, &mut stdout)?;
return Ok(());
}

'outer: loop {

pb.write_prompt(&mut stdout, &context)?;
pb.write_prompt(&mut context, &mut stdout)?;

let stdin = stdin();
for c in stdin.keys() {
Expand All @@ -43,19 +70,19 @@ pub fn main() -> Result<(), std::io::Error> {
'\n' => {
// Print again without cursor, in case we pressed enter
// while inside a substitution
pb.write_prompt_nocursor(&mut stdout, &context)?;
pb.write_prompt_nocursor(&mut context, &mut stdout)?;
let in_str = pb.enter();
write!(stdout, "\r\n")?;
FormattedText::newline(&mut stdout)?;
if in_str == "" { break; }

if in_str.trim() == "quit" {
break 'outer;
} else {
let r = crate::do_string(&in_str, &mut context);
let r = crate::do_string(&mut context, &in_str);

match r {
Ok(t) | Err(t) => {
t.write(&mut stdout).unwrap();
t.write(&context, &mut stdout).unwrap();
}
}
}
Expand All @@ -79,10 +106,10 @@ pub fn main() -> Result<(), std::io::Error> {
};
};

pb.write_prompt(&mut stdout, &context)?;
pb.write_prompt(&mut context, &mut stdout)?;
}
}

write!(stdout, "\r\n")?;
FormattedText::newline(&mut stdout)?;
return Ok(());
}
8 changes: 4 additions & 4 deletions src/evaluate/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use super::function::eval_function;


pub fn evaluate(
t: &Expression,
context: &mut Context
context: &mut Context,
t: &Expression
) -> Result<
Expression,
(LineLocation, DaisyError)
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn evaluate(
let new = match g {
Expression::Quantity(_, _) => None,
Expression::Tuple(_, _) => None,
Expression::Constant(_, c) => { Some(evaluate(&c.value(), context).unwrap()) },
Expression::Constant(_, c) => { Some(evaluate(context, &c.value()).unwrap()) },
Expression::Variable(l, s) => {
// Don't move up, re-evaluate
// This makes variables containing floating variables work properly
Expand All @@ -64,7 +64,7 @@ pub fn evaluate(
context.get_variable(&s)
},
Expression::Operator(_, Operator::Function(_), _) => { Some(eval_function(g)?) },
Expression::Operator(_, _, _) => { eval_operator(g, context)? },
Expression::Operator(_, _, _) => { eval_operator(context, g)? },
};

if let Some(mut new) = new {
Expand Down
Loading

0 comments on commit a6c3ffa

Please sign in to comment.