-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rewrite of calc.py/filepicker in rust
- Loading branch information
Showing
6 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Calc | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
paths: | ||
- calc/** | ||
pull_request: | ||
branches: [ main ] | ||
paths: | ||
- calc/** | ||
workflow_dispatch: | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build: | ||
runs-on: windows-2019 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Build | ||
run: cargo build --verbose --release --manifest-path ./calc/Cargo.toml | ||
- name: Upload a Build Artifact | ||
uses: actions/upload-artifact@v2.3.1 | ||
with: | ||
# Artifact name | ||
# optional, default is artifact | ||
# A file, directory or wildcard pattern that describes what to upload | ||
path: D:\a\Atlas-Utilities\Atlas-Utilities\calc\target\release\calc.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "calc" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
csv = "1.1.6" | ||
|
||
[profile.release] | ||
opt-level = 3 | ||
lto = true | ||
codegen-units = 1 | ||
panic = "abort" | ||
debug = 0 | ||
|
||
[profile.dev] | ||
debug = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
fn main() { | ||
let args: Vec<String> = std::env::args().collect(); | ||
if args.len() > 1 { | ||
match args[1].as_ref() { | ||
"parse" => parse(args[2].as_ref()), | ||
"add" => add(args[2].as_ref(), args[3].as_ref()), | ||
"rnd" => println!("{}", args[2].parse::<f64>().unwrap().round()), | ||
"div" => div(args[2].as_ref(), args[3].as_ref()), | ||
"divint" => divint(args[2].as_ref(), args[3].as_ref()), | ||
"help" => help(), | ||
_ => help(), | ||
} | ||
} else { | ||
help(); | ||
} | ||
} | ||
|
||
fn parse(path: &str) { | ||
// OCAT CSVs change amount of headers after the first line, so make reader flexible | ||
let mut reader = csv::ReaderBuilder::new() | ||
.has_headers(true) | ||
.flexible(true) | ||
.from_path(path) | ||
.unwrap(); | ||
let mut sum: f64 = 0.0; | ||
let mut sorted_values: Vec<f64> = Vec::new(); | ||
for result in reader.records() { | ||
let value = result.unwrap()[12].parse::<f64>().unwrap(); | ||
// push into vector for sorting later | ||
sorted_values.push(value); | ||
// for counting benchmark time | ||
sum+=value; | ||
} | ||
// sort values in descending order | ||
sorted_values.sort_by(|a, b| b.partial_cmp(a).unwrap()); | ||
let mut current_total: f64 = 0.0; | ||
// collect lows | ||
for present in sorted_values { | ||
current_total += present; | ||
if current_total >= (0.01 / 100.0 * sum) { | ||
println!("{}", 1000.0/present as f32); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
fn add(val1: &str, val2: &str) { | ||
let val1 = val1.parse::<f64>().unwrap(); | ||
let val2 = val2.parse::<f64>().unwrap(); | ||
println!("{}", val1 + val2); | ||
} | ||
|
||
fn div(val1: &str, val2: &str) { | ||
let val1 = val1.parse::<f64>().unwrap(); | ||
let val2 = val2.parse::<f64>().unwrap(); | ||
println!("{}", val1 / val2); | ||
} | ||
|
||
fn divint(val1: &str, val2: &str) { | ||
let val1 = val1.parse::<i32>().unwrap(); | ||
let val2 = val2.parse::<i32>().unwrap(); | ||
println!("{}", val1 / val2); | ||
} | ||
|
||
fn help() { | ||
println!("{}", " | ||
Usage: | ||
calc parse <path> - calculate %0.01 lows of an OCAT CSV | ||
calc add <val1> <val2> - adds two values | ||
calc rnd <val> - rounds <val> to the nearest integer | ||
calc div <val1> <val2> - integer division | ||
calc divint <val1> <val2> - float division | ||
calc help - prints this message"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "filepicker" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
rfd = "0.6.2" | ||
|
||
[profile.release] | ||
opt-level = 3 | ||
lto = true | ||
codegen-units = 1 | ||
panic = "abort" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
fn main() { | ||
// get first argument as string | ||
// example "exe" | ||
let args: Vec<String> = std::env::args().collect(); | ||
// check if argument is empty | ||
if args.len() == 2 { | ||
// launch prompt | ||
let res = rfd::FileDialog::new() | ||
// add file extension filter | ||
.add_filter(&args[1], &[&args[1]]) | ||
.pick_file(); | ||
// check if file prompt was cancelled | ||
if res == None { | ||
println!("cancelled by user"); | ||
} else{ | ||
println!("{}", res.unwrap().to_str().unwrap().replace("\\", "/")); | ||
} | ||
} else { | ||
let res = rfd::FileDialog::new() | ||
.add_filter("All Files", &["*.*"]) | ||
.pick_file(); | ||
if res == None { | ||
println!("cancelled by user"); | ||
} else { | ||
println!("{}", res.unwrap().to_str().unwrap().replace("\\", "/")); | ||
} | ||
} | ||
} |