make the experience 1000 times better (thank you kyza)

master
Tymon 1 year ago committed by Tymon
parent ee1584be50
commit 710c965216

8
Cargo.lock generated

@ -165,6 +165,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "levenshtein"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
[[package]]
name = "libc"
version = "0.2.143"
@ -195,6 +201,8 @@ version = "0.1.0"
dependencies = [
"chrono",
"chrono-tz",
"iana-time-zone",
"levenshtein",
]
[[package]]

@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
chrono = "0.4.24"
chrono-tz = "0.8.2"
iana-time-zone = "0.1.56"
levenshtein = "1.0.5"
[lib]
crate_type = ["cdylib"]

@ -1,10 +1,9 @@
use std::{
ffi::{c_char, c_double, CStr, CString},
str::FromStr,
};
use std::ffi::{c_char, c_double, CStr, CString};
use chrono::Local;
use chrono_tz::Tz;
use chrono_tz::{Tz, TZ_VARIANTS};
use levenshtein::levenshtein;
#[no_mangle]
pub static id: &str = "time";
@ -26,17 +25,33 @@ pub extern "C" fn get_priority(data: *const c_char) -> c_double {
}
}
#[no_mangle]
pub extern "C" fn get_timezone_name(data: *const c_char) -> *const c_char {
let data = ptr_to_string(data);
let local_time = iana_time_zone::get_timezone().unwrap_or_default();
if data == "" {
return str_to_ptr(&local_time);
}
if let Some(timezone) = get_closest_zone(data) {
return str_to_ptr(&timezone.to_string());
};
str_to_ptr("INVALID_TIME_ZONE")
}
#[no_mangle]
pub extern "C" fn get_time(data: *const c_char) -> *const c_char {
let data = ptr_to_string(data);
let Ok(timezone) = Tz::from_str(&data) else {
let Some(timezone) = get_closest_zone(data) else {
return str_to_ptr("INVALID_TIME_ZONE");
};
let current = Local::now();
let converted = current.with_timezone(&timezone);
let converted_string = converted.time().format("%H:%M:%S").to_string();
let converted_string = converted.time().format("%H:%M:%S").to_string();
str_to_ptr(&converted_string)
}
@ -44,11 +59,28 @@ pub extern "C" fn get_time(data: *const c_char) -> *const c_char {
#[no_mangle]
pub extern "C" fn get_local_time() -> *const c_char {
let current = Local::now();
let current_string = current.time().format("%H:%M:%S").to_string();
let current_string = current.time().format("%H:%M:%S").to_string();
str_to_ptr(&current_string)
}
fn get_closest_zone(zone: String) -> Option<Tz> {
let mut closest_distance = usize::MAX;
let mut closest_zone = None;
for variant in TZ_VARIANTS {
let variant_distance = levenshtein(
&zone.to_string().to_lowercase(),
&variant.to_string().to_lowercase(),
);
if variant_distance < closest_distance {
closest_distance = variant_distance;
closest_zone = Some(variant);
}
}
closest_zone
}
fn ptr_to_string(c_buf: *const c_char) -> String {
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };

@ -785,8 +785,8 @@ packages:
resolution: {integrity: sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA==}
dev: true
/is-what@4.1.8:
resolution: {integrity: sha512-yq8gMao5upkPoGEU9LsB2P+K3Kt8Q3fQFCGyNCWOAnJAMzEXVV9drYb0TXr42TTliLLhKIBvulgAXgtLLnwzGA==}
/is-what@4.1.9:
resolution: {integrity: sha512-I3FU0rkVvwhgLLEs6iITwZ/JaLXe7tQcHyzupXky8jigt1vu4KM0UOqDr963j36JRvJ835EATVIm6MnGz/i1/g==}
engines: {node: '>=12.13'}
dev: true
@ -812,11 +812,11 @@ packages:
yallist: 3.1.1
dev: true
/merge-anything@5.1.5:
resolution: {integrity: sha512-9lquMsJxgaef2BXYUy8VnqHmuLYaEiGd7SULqOTuDFA9Lw6g6Hmdsblc6+yqshdJOQKkn9I106+3D5mnQMstvg==}
/merge-anything@5.1.6:
resolution: {integrity: sha512-0SIP3417t0sOL6/crPb6oC+ZNSMrjJeWkydlddgZVzsjQA86l8v3+f3WwvKanbsHxVF80QouJIdSh+Q249bu0g==}
engines: {node: '>=12.13'}
dependencies:
is-what: 4.1.8
is-what: 4.1.9
dev: true
/ms@2.1.2:
@ -936,7 +936,7 @@ packages:
'@babel/preset-typescript': 7.21.5(@babel/core@7.21.8)
'@types/babel__core': 7.20.0
babel-preset-solid: 1.7.4(@babel/core@7.21.8)
merge-anything: 5.1.5
merge-anything: 5.1.6
solid-js: 1.7.5
solid-refresh: 0.5.2(solid-js@1.7.5)
vite: 4.3.5

@ -1,56 +1,79 @@
import { invoke } from "@tauri-apps/api";
import {
createSignal,
type Component,
onCleanup,
onMount,
Show,
Show,
createSignal,
onCleanup,
onMount,
type Component,
} from "solid-js";
const App: Component = () => {
let [timezone, setTimezone] = createSignal("");
let [time, setTime] = createSignal("");
async function submit(event: CustomEventInit) {
setTimezone(event.detail.substring(4).trim());
setTime(
await invoke("run_plugin_function", {
id: "time",
name: "get_time",
data: timezone().replace(" ", "_"),
})
);
}
onMount(async () => {
window.addEventListener("maccha-submit", submit);
setTime(
await invoke("run_plugin_function", {
id: "time",
name: "get_local_time",
data: "",
})
);
setTimezone("your local time zone");
});
onCleanup(() => {
window.removeEventListener("maccha-submit", submit);
});
return (
<div class="maccha-time">
<Show when={time() === "INVALID_TIME_ZONE"}>
Invalid time zone specified!
</Show>
<Show when={time() !== "INVALID_TIME_ZONE"}>
{`It's currently ${time()} in ${timezone()}`}
</Show>
</div>
);
let [timezone, setTimezone] = createSignal("");
let [time, setTime] = createSignal("");
async function query(event: CustomEventInit) {
setTimezone(
await invoke("run_plugin_function", {
id: "time",
name: "get_timezone_name",
data: event.detail.substring(4).trim(),
})
);
setTime(
await invoke("run_plugin_function", {
id: "time",
name: "get_time",
data: timezone().replace(" ", "_"),
})
);
}
const timeUpdater = setInterval(async () => {
setTime(
await invoke("run_plugin_function", {
id: "time",
name: "get_time",
data: timezone().replace(" ", "_"),
})
);
}, 1e2);
onMount(async () => {
window.addEventListener("maccha-query", query);
setTime(
await invoke("run_plugin_function", {
id: "time",
name: "get_local_time",
data: "",
})
);
setTimezone(
await invoke("run_plugin_function", {
id: "time",
name: "get_timezone_name",
data: "",
})
);
});
onCleanup(() => {
window.removeEventListener("maccha-query", query);
clearInterval(timeUpdater);
});
return (
<div class="maccha-time">
<Show when={time() === "INVALID_TIME_ZONE"}>
Invalid time zone specified!
</Show>
<Show when={time() !== "INVALID_TIME_ZONE"}>
{`It's currently ${time()} in ${timezone()}`}
</Show>
</div>
);
};
export default App;

Loading…
Cancel
Save