aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-10-03 14:24:51 +0200
committermetamuffin <metamuffin@disroot.org>2023-10-03 14:24:51 +0200
commitbd8b233316820cd35178085ef132f82279be0504 (patch)
tree0589e6a03c7407c106345f532732a97ae15b489a
parent5528c6a2ee1b80770b046855fdd6be937bfafc28 (diff)
downloadjshelper-bd8b233316820cd35178085ef132f82279be0504.tar
jshelper-bd8b233316820cd35178085ef132f82279be0504.tar.bz2
jshelper-bd8b233316820cd35178085ef132f82279be0504.tar.zst
logger
-rw-r--r--src/log.ts49
-rw-r--r--src/show.ts5
2 files changed, 54 insertions, 0 deletions
diff --git a/src/log.ts b/src/log.ts
new file mode 100644
index 0000000..e99004e
--- /dev/null
+++ b/src/log.ts
@@ -0,0 +1,49 @@
+/*
+ This file is part of jshelper (https://codeberg.org/metamuffin/jshelper)
+ which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
+ Copyright (C) 2023 metamuffin <metamuffin.org>
+*/
+import { e } from "./element.ts"
+
+export class Logger<T = HTMLElement> {
+ public element: HTMLElement = e("div", { class: "jsh-log" })
+ constructor(
+ private map_fn: (value: T) => HTMLElement = e => {
+ if (e instanceof HTMLElement) return e;
+ else throw new Error("logger: map_fn required if T is not HTMLElement");
+ },
+ private reverse = false,
+ private timeout = 3000,
+ private animation_dduration = 1000,
+ ) {
+
+ }
+
+ private add(v: T) {
+ const el = this.map_fn(v)
+ if (this.reverse) this.element.prepend(el)
+ else this.element.append(el)
+ return el
+ }
+
+ log(value: T) {
+ const el = this.add(value)
+ el.classList.add("jsh-log-line", "jsh-log-line-timeout")
+ setTimeout(() => {
+ el.classList.add("jsh-log-line-disappear")
+ setTimeout(() => {
+ this.element.removeChild(el)
+ }, this.animation_dduration)
+ }, this.timeout + this.animation_dduration)
+ }
+ log_persistent(value: T): () => void {
+ const el = this.add(value)
+ el.classList.add("jsh-log-line", "jsh-log-line-persistent")
+ return () => {
+ el.classList.add("jsh-log-line-disappear")
+ setTimeout(() => {
+ this.element.removeChild(el)
+ }, this.animation_dduration)
+ }
+ }
+}
diff --git a/src/show.ts b/src/show.ts
index b75ed7b..08a4778 100644
--- a/src/show.ts
+++ b/src/show.ts
@@ -1,3 +1,8 @@
+/*
+ This file is part of jshelper (https://codeberg.org/metamuffin/jshelper)
+ which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
+ Copyright (C) 2023 metamuffin <metamuffin.org>
+*/
export function metric(x: number, unit = ""): string {
if (x > 1000000000) return (x / 1000000000).toFixed(1) + "G" + unit