@edgarlopezcalomarde/stopwatch 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -0
  3. package/index.ts +171 -0
  4. package/package.json +33 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Edgar Lopez Calomarde
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # stopwatch
2
+
3
+ Cronómetro para la terminal con dígitos grandes, construido con [Bun](https://bun.sh) y [OpenTUI](https://opentui.dev).
4
+
5
+ ## Instalación
6
+
7
+ ```bash
8
+ bun i -g @edgarlopezcalomarde/stopwatch
9
+ ```
10
+
11
+ Requiere tener [Bun](https://bun.sh) instalado.
12
+
13
+ ## Uso
14
+
15
+ ```bash
16
+ stopwatch # abre el cronómetro en 00:00:00 (detenido)
17
+ stopwatch --start # abre el cronómetro y empieza a contar solo
18
+ ```
19
+
20
+ ### Teclas
21
+
22
+ | Tecla | Acción |
23
+ | ------- | ------------------------------------------ |
24
+ | `Enter` | Iniciar / parar |
25
+ | `r` | Reiniciar a 00:00:00 |
26
+ | `c` | Color aleatorio (se guarda entre sesiones) |
27
+ | `q` | Salir (también Esc/Ctrl+C) |
28
+
29
+ El color elegido se guarda en `~/.config/stopwatch/config.json`.
30
+
31
+ ## Desarrollo
32
+
33
+ ```bash
34
+ git clone https://github.com/edgarlopezcalomarde/stopwatch.git
35
+ cd stopwatch
36
+ bun install
37
+ bun run index.ts
38
+ ```
39
+
40
+ Para registrar el comando `stopwatch` global desde el código local:
41
+
42
+ ```bash
43
+ bun link
44
+ ```
45
+
46
+ ## Licencia
47
+
48
+ MIT © Edgar Lopez Calomarde
package/index.ts ADDED
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env bun
2
+ import {
3
+ createCliRenderer,
4
+ ASCIIFontRenderable,
5
+ TextRenderable,
6
+ BoxRenderable,
7
+ type KeyEvent,
8
+ } from "@opentui/core"
9
+ import { homedir } from "node:os"
10
+ import { join } from "node:path"
11
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs"
12
+
13
+ const DEFAULT_COLOR = "#00FF88"
14
+ const CONFIG_DIR = join(homedir(), ".config", "stopwatch")
15
+ const CONFIG_PATH = join(CONFIG_DIR, "config.json")
16
+
17
+ const autoStart = process.argv.includes("--start")
18
+
19
+ function loadColor(): string {
20
+ try {
21
+ const config = JSON.parse(readFileSync(CONFIG_PATH, "utf8"))
22
+ if (typeof config.color === "string" && /^#[0-9a-fA-F]{6}$/.test(config.color)) {
23
+ return config.color
24
+ }
25
+ } catch {}
26
+ return DEFAULT_COLOR
27
+ }
28
+
29
+ function saveColor(color: string) {
30
+ try {
31
+ mkdirSync(CONFIG_DIR, { recursive: true })
32
+ writeFileSync(CONFIG_PATH, JSON.stringify({ color }, null, 2))
33
+ } catch {}
34
+ }
35
+
36
+ function randomColor(): string {
37
+ // Random hue with fixed high saturation/lightness so digits stay readable
38
+ const h = Math.random()
39
+ const s = 0.85
40
+ const l = 0.6
41
+ const q = l + s * Math.min(l, 1 - l)
42
+ const p = 2 * l - q
43
+ const channel = (t: number) => {
44
+ if (t < 0) t += 1
45
+ if (t > 1) t -= 1
46
+ if (t < 1 / 6) return p + (q - p) * 6 * t
47
+ if (t < 1 / 2) return q
48
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
49
+ return p
50
+ }
51
+ const toHex = (v: number) =>
52
+ Math.round(v * 255)
53
+ .toString(16)
54
+ .padStart(2, "0")
55
+ return `#${toHex(channel(h + 1 / 3))}${toHex(channel(h))}${toHex(channel(h - 1 / 3))}`
56
+ }
57
+
58
+ function dim(color: string): string {
59
+ const scale = (hex: string) =>
60
+ Math.round(parseInt(hex, 16) * 0.45)
61
+ .toString(16)
62
+ .padStart(2, "0")
63
+ return `#${scale(color.slice(1, 3))}${scale(color.slice(3, 5))}${scale(color.slice(5, 7))}`
64
+ }
65
+
66
+ let clockColor = loadColor()
67
+
68
+ function formatTime(elapsedMs: number): string {
69
+ const totalSeconds = Math.floor(elapsedMs / 1000)
70
+ const hours = Math.floor(totalSeconds / 3600)
71
+ const minutes = Math.floor((totalSeconds % 3600) / 60)
72
+ const seconds = totalSeconds % 60
73
+ const pad = (n: number) => String(n).padStart(2, "0")
74
+ return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`
75
+ }
76
+
77
+ const renderer = await createCliRenderer({
78
+ exitOnCtrlC: true,
79
+ useMouse: false,
80
+ })
81
+
82
+ const container = new BoxRenderable(renderer, {
83
+ id: "container",
84
+ width: "100%",
85
+ height: "100%",
86
+ flexDirection: "column",
87
+ alignItems: "center",
88
+ justifyContent: "center",
89
+ gap: 1,
90
+ })
91
+
92
+ const clock = new ASCIIFontRenderable(renderer, {
93
+ id: "clock",
94
+ text: "00:00:00",
95
+ font: "block",
96
+ color: autoStart ? clockColor : dim(clockColor),
97
+ })
98
+
99
+ const status = new TextRenderable(renderer, {
100
+ id: "status",
101
+ content: autoStart ? "corriendo" : "detenido",
102
+ fg: "#888888",
103
+ })
104
+
105
+ const help = new TextRenderable(renderer, {
106
+ id: "help",
107
+ content: "enter: iniciar/parar r: reiniciar c: color q: salir",
108
+ fg: "#444444",
109
+ })
110
+
111
+ container.add(clock)
112
+ container.add(status)
113
+ container.add(help)
114
+ renderer.root.add(container)
115
+
116
+ let running = false
117
+ let elapsedMs = 0
118
+ let startedAt = 0
119
+ let interval: ReturnType<typeof setInterval> | null = null
120
+
121
+ function currentElapsed(): number {
122
+ return running ? elapsedMs + (Date.now() - startedAt) : elapsedMs
123
+ }
124
+
125
+ function refreshDisplay() {
126
+ clock.text = formatTime(currentElapsed())
127
+ clock.color = running ? clockColor : dim(clockColor)
128
+ status.content = running ? "corriendo" : "detenido"
129
+ }
130
+
131
+ function start() {
132
+ if (running) return
133
+ running = true
134
+ startedAt = Date.now()
135
+ interval = setInterval(refreshDisplay, 100)
136
+ refreshDisplay()
137
+ }
138
+
139
+ function stop() {
140
+ if (!running) return
141
+ elapsedMs += Date.now() - startedAt
142
+ running = false
143
+ if (interval) {
144
+ clearInterval(interval)
145
+ interval = null
146
+ }
147
+ refreshDisplay()
148
+ }
149
+
150
+ function reset() {
151
+ elapsedMs = 0
152
+ startedAt = Date.now()
153
+ refreshDisplay()
154
+ }
155
+
156
+ renderer.keyInput.on("keypress", (key: KeyEvent) => {
157
+ if (key.name === "return") {
158
+ running ? stop() : start()
159
+ } else if (key.name === "r") {
160
+ reset()
161
+ } else if (key.name === "c" && !key.ctrl) {
162
+ clockColor = randomColor()
163
+ saveColor(clockColor)
164
+ refreshDisplay()
165
+ } else if (key.name === "q" || key.name === "escape") {
166
+ renderer.destroy()
167
+ process.exit(0)
168
+ }
169
+ })
170
+
171
+ if (autoStart) start()
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@edgarlopezcalomarde/stopwatch",
3
+ "version": "1.0.0",
4
+ "description": "Cronómetro para la terminal con dígitos grandes, construido con Bun y OpenTUI",
5
+ "author": "Edgar Lopez Calomarde <edgarlopezcalomarde@gmail.com>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/edgarlopezcalomarde/stopwatch.git"
10
+ },
11
+ "keywords": ["stopwatch", "timer", "cli", "terminal", "tui", "bun", "opentui"],
12
+ "module": "index.ts",
13
+ "type": "module",
14
+ "bin": {
15
+ "stopwatch": "./index.ts"
16
+ },
17
+ "files": ["index.ts", "README.md", "LICENSE"],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "engines": {
22
+ "bun": ">=1.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/bun": "latest"
26
+ },
27
+ "peerDependencies": {
28
+ "typescript": "^5"
29
+ },
30
+ "dependencies": {
31
+ "@opentui/core": "^0.4.2"
32
+ }
33
+ }