diff --git a/CHANGELOG.md b/CHANGELOG.md index 77fe635..d515b12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.0.4] - 2026-10-11 + +### Changed + +- Improved status bar alignment by using figure spaces to maintain a consistent width for single- and double-digit visitor counts. +- Replaced https.get() with the native fetch() API for simpler and more reliable HTTP requests. + ## [1.0.3] - 2026-06-07 ### Fixed @@ -22,4 +29,4 @@ ### Added -- Initial release. \ No newline at end of file +- Initial release. diff --git a/src/http.ts b/src/http.ts index f872d2d..67af9f9 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1,37 +1,21 @@ -import * as https from 'https'; +const URL = 'https://www.gymcontrol.se/global/checkedin/checkedin.php?uid=9094'; -export function getVisitorCount(): Promise { - return new Promise((resolve, reject) => { - const req = https.get('https://www.gymcontrol.se/global/checkedin/checkedin.php?uid=9094', (res) => { - if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) { - res.resume(); - reject(new Error(`HTTP ${res.statusCode ?? '?'}`)); - return; - } - - res.setEncoding('utf8'); - - let raw = ''; - res.on('data', (chunk) => { - raw += chunk; - }); - - res.on('end', () => { - const text = raw.trim(); - const n = Number.parseInt(text, 10); - - if (Number.isFinite(n)) { - resolve(n); - } else { - reject(new Error(`Invalid response: "${text}"`)); - } - }); - }); - - req.on('error', reject); - - req.setTimeout(5000, () => { - req.destroy(new Error('Timeout after 5000 ms')); - }); +export async function getVisitorCount(): Promise { + const response = await fetch(URL, { + signal: AbortSignal.timeout(5000) }); + + const raw = await response.text(); + + if (!response.ok) { + throw new Error(`HTTP ${response.status}`); + } + + const text = raw.trim(); + + if (!/^\d+$/.test(text)) { + throw new Error(`Invalid response: ${JSON.stringify(text)}`); + } + + return Number(text); } diff --git a/src/statusBar.ts b/src/statusBar.ts index f124bb9..1b5ae45 100644 --- a/src/statusBar.ts +++ b/src/statusBar.ts @@ -1,6 +1,8 @@ import * as vscode from 'vscode'; const TOOLTIP_TEXT = 'Current Gym Visitors'; +const FIGURE_SPACE = '\u2007'; +const VISITOR_COUNT_WIDTH = 2; let item: vscode.StatusBarItem; let getCount: (() => Promise) | undefined; @@ -10,7 +12,7 @@ export function initStatusBar(getVisitorCount: () => Promise, commandId: item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 1000); item.command = commandId; - item.text = '$(adda-fitness) 0'; + item.text = `$(adda-fitness) ${formatCount(0)}`; item.tooltip = TOOLTIP_TEXT; item.show(); @@ -23,20 +25,26 @@ export async function refresh(): Promise { } item.text = '$(adda-fitness) $(sync~spin)'; + item.tooltip = 'Updating visitor count...'; + item.show(); try { const [count] = await Promise.all([getCount(), sleep(1500)]); - item.text = `$(adda-fitness) ${count}`; + item.text = `$(adda-fitness) ${formatCount(count)}`; item.tooltip = TOOLTIP_TEXT; - } catch (e: any) { + } catch (e: unknown) { item.text = '$(warning) Adda'; - item.tooltip = e?.message ?? 'Unknown error'; + item.tooltip = e instanceof Error ? e.message : 'Unknown error'; } finally { item.show(); } } +function formatCount(count: number): string { + return count.toString().padStart(VISITOR_COUNT_WIDTH, FIGURE_SPACE); +} + function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); }