Add unit choice
diff --git a/Makefile b/Makefile
index 37b594f..af47130 100644
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,7 @@
webview-frontend: webview-frontend.cpp units.o getopt.o getopt1.o parse.tab.o strfunc.o | bundle-webview-js.h
g++ $^ -o webview-frontend $(WEBCFLAGS) -I json/include
+ strip $@
units.wasm: units.c
emcc -o units.lib.js *.c $(CFLAGS) --preload-file definitions.units --preload-file elements.units --preload-file currency.units --preload-file cpi.units
@@ -25,10 +26,10 @@
xxd -i $^ > $@
bundle-webview.js: webview-frontend.js frontend-impl.js styles.css
- cp styles.css styles.css.txt
+ yarn esbuild styles.css --outfile=styles.css.txt --minify
yarn esbuild webview-frontend.js $(ESFLAGS) --outfile=bundle-webview.js
-watch:
+watch: units.wasm
yarn esbuild frontend.js $(ESFLAGS) --watch --outfile=bundle.js
.PHONY: watch webview-frontend
diff --git a/frontend-impl.js b/frontend-impl.js
index 95ca17f..9c7b07c 100644
--- a/frontend-impl.js
+++ b/frontend-impl.js
@@ -1,20 +1,27 @@
const React = require("preact");
-const {useState} = require('preact/hooks');
+const { useState } = require('preact/hooks');
export const UnitsApp = () => {
const [hist, setHist] = useState([]);
const [query, setQuery] = useState("");
const [to, setTo] = useState("");
+ const [unitSystem, setUnitSystem] = useState('si');
function doConversion(e) {
e.preventDefault();
console.log("conversion");
+
+ if (query == '')
+ return;
+
+ console.log("units", unitSystem);
+
+ convert(query, to, unitSystem).then(res => {
+ setHist([...hist, [query, to, res]]);
+ });
+
setQuery('');
setTo('');
-
- convert(query, to).then(res => {
- setHist([...hist, [query, to, res]]);
- })
}
return (
@@ -34,21 +41,37 @@
placeholder="To"
onChange={(val) => setTo(val.target.value)}
></input>
-
+
<input value="Convert" type="submit"></input>
</form>
- <div class="table-container">
- <table>
- <thead>
- <tr><td style="width: 30%">From</td><td style="width: 15%">To</td><td style="width: 55%">Result</td></tr>
- </thead>
- {hist.map((entry) => (
- <tr>{entry.map(t => <td>{t}</td>)}</tr>
- ))}
- <tr><td></td></tr>
- </table>
- </div>
+ <div class="table-container">
+ <table>
+ <thead>
+ <tr><td style="width: 30%">From</td><td style="width: 15%">To</td><td style="width: 55%">Result</td></tr>
+ </thead>
+ {hist.map((entry) => (
+ <tr>{entry.map(t => <td>{t}</td>)}</tr>
+ ))}
+ <tr><td></td></tr>
+ </table>
+ </div>
+
+ <div class="bottom-menu">
+ <span>
+ Units:
+ <select onChange={val => setUnitSystem(val.target.value)}>
+ <option value="si">SI</option>
+ <option value="gauss">CGS Gaussian</option>
+ <option value="esu">CGS ESU</option>
+ <option value="emu">CGS EMU</option>
+ <option value="lhu">CGS LHU</option>
+ </select>
+ </span>
+
+ <button onClick={() => setHist([])}>Clear</button>
+ </div>
+
</>
);
};
diff --git a/styles.css b/styles.css
index 87743a3..73d891b 100644
--- a/styles.css
+++ b/styles.css
@@ -23,7 +23,7 @@
#app {
display: grid;
- grid-template-rows: min-content 1fr;
+ grid-template-rows: min-content 1fr min-content;
width: 100%;
height: 100%;
}
@@ -34,13 +34,20 @@
/* border: 1px solid gray;*/
}
+.bottom-menu {
+ font-size: 0.85rem!important;
+ display: grid;
+ grid-template-columns: 1fr auto;
+ margin-top: 8px;
+}
+
table {
width: 100%;
border-collapse: collapse;
margin: 0;
background: white;
- font-size: 0.85rem!important;
table-layout: fixed;
+ font-size: 0.85rem!important;
}
td {
padding: 2px 4px;
diff --git a/units-impl-wasm.js b/units-impl-wasm.js
index b8f6191..bf562e1 100644
--- a/units-impl-wasm.js
+++ b/units-impl-wasm.js
@@ -4,13 +4,15 @@
let _do_a_conversion = Module.cwrap("do_a_conversion", "number", [
"number",
"number",
+ "number",
]);
- async function convert(from, to = "") {
+ async function convert(from, to = "", system="si") {
let from_c = Module.stringToNewUTF8(from);
let to_c = Module.stringToNewUTF8(to);
+ let sys_c = Module.stringToNewUTF8(system);
let lenBefore = globalThis.printBuffer.length;
- _do_a_conversion(from_c, to === "" ? 0 : to_c);
+ _do_a_conversion(from_c, to === "" ? 0 : to_c, system);
return globalThis.printBuffer.slice(lenBefore).trim();
}
diff --git a/units.c b/units.c
index 54be48b..9bc6c39 100644
--- a/units.c
+++ b/units.c
@@ -6340,8 +6340,6 @@
if (setup)
return;
- setup = 1;
-
flags.quiet = 1; /* Do not supress prompting */
flags.unitcheck = 0; /* Unit checking is off */
flags.verbose = 2; /* Medium verbosity */
@@ -6400,7 +6398,7 @@
void
-do_a_conversion(char *input, char *output)
+do_a_conversion(char *input, char *output, char *system)
{
char *inp, *out;
@@ -6421,6 +6419,9 @@
else
setnumformat();
+ setenv("UNITS_SYSTEM", system, 1);
+ printf("units system %s\n", getenv("UNITS_SYSTEM"));
+
conversion_worker(input, output);
}
@@ -6428,9 +6429,9 @@
int test_main()
{
char a[] = "pi", b[] = "";
- do_a_conversion(a, b);
- do_a_conversion(a, b);
- do_a_conversion(a, b);
+ do_a_conversion(a, b, "si");
+ do_a_conversion(a, b, "si");
+ do_a_conversion(a, b, "si");
}
diff --git a/webview-frontend.cpp b/webview-frontend.cpp
index e3a2ed6..ac2fc90 100644
--- a/webview-frontend.cpp
+++ b/webview-frontend.cpp
@@ -7,7 +7,7 @@
using json = nlohmann::json;
extern "C" {
- extern void do_a_conversion(char *, char *);
+ extern void do_a_conversion(char *, char *, char *);
}
extern char log_buffer[4096];
@@ -19,11 +19,12 @@
json j = json::parse(arg);
auto from = j[0].template get<std::string>(),
- to = j[1].template get<std::string>();
+ to = j[1].template get<std::string>(),
+ system = j[2].template get<std::string>();
std::cout << from << ' ' << to << std::endl;
- do_a_conversion((char *)from.data(), to.size() > 0 ? (char *)to.data() : nullptr);
+ do_a_conversion((char *)from.data(), to.size() > 0 ? (char *)to.data() : nullptr, (char *)system.data());
std::string res = json(std::string(log_buffer, log_buffer_ptr - log_buffer)).dump();
log_buffer_ptr = log_buffer;
std::cout << res << std::endl;