feat: add clickable hyperlinks for URLs in terminal output
Use OSC 8 escape sequences to make tweet and list URLs clickable in supported terminals (iTerm2, Ghostty, Kitty, WezTerm, VS Code, etc.). - Add hyperlink() helper function in output.ts - Apply hyperlinks in printTweets() for tweet URLs - Apply hyperlinks in printLists() for list URLs - Add hyperlinks flag to OutputConfig (auto-disabled for non-TTY) - Automatically falls back to plain text in --plain mode or when piped 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
This commit is contained in:
committed by
Peter Steinberger
co-authored by
Claude Opus 4.5
parent
bbc90dbe08
commit
837b09fcad
+52
-13
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
formatStatsLine,
|
||||
formatTweetUrlLine,
|
||||
hyperlink,
|
||||
labelPrefix,
|
||||
resolveOutputConfigFromArgv,
|
||||
resolveOutputConfigFromCommander,
|
||||
@@ -9,14 +10,19 @@ import {
|
||||
} from '../src/lib/output.js';
|
||||
|
||||
describe('output', () => {
|
||||
it('defaults to emoji + color on TTY', () => {
|
||||
it('defaults to emoji + color + hyperlinks on TTY', () => {
|
||||
const cfg = resolveOutputConfigFromArgv([], {}, true);
|
||||
expect(cfg).toEqual({ plain: false, emoji: true, color: true });
|
||||
expect(cfg).toEqual({ plain: false, emoji: true, color: true, hyperlinks: true });
|
||||
});
|
||||
|
||||
it('plain disables emoji + color', () => {
|
||||
it('disables hyperlinks on non-TTY', () => {
|
||||
const cfg = resolveOutputConfigFromArgv([], {}, false);
|
||||
expect(cfg.hyperlinks).toBe(false);
|
||||
});
|
||||
|
||||
it('plain disables emoji + color + hyperlinks', () => {
|
||||
const cfg = resolveOutputConfigFromArgv(['--plain'], {}, true);
|
||||
expect(cfg).toEqual({ plain: true, emoji: false, color: false });
|
||||
expect(cfg).toEqual({ plain: true, emoji: false, color: false, hyperlinks: false });
|
||||
expect(statusPrefix('ok', cfg)).toBe('[ok] ');
|
||||
expect(labelPrefix('url', cfg)).toBe('url: ');
|
||||
});
|
||||
@@ -33,7 +39,7 @@ describe('output', () => {
|
||||
|
||||
it('--no-color disables colors', () => {
|
||||
const cfg = resolveOutputConfigFromArgv(['--no-color'], {}, true);
|
||||
expect(cfg).toEqual({ plain: false, emoji: true, color: false });
|
||||
expect(cfg).toEqual({ plain: false, emoji: true, color: false, hyperlinks: true });
|
||||
});
|
||||
|
||||
it('--no-emoji switches to text prefixes', () => {
|
||||
@@ -44,32 +50,65 @@ describe('output', () => {
|
||||
|
||||
it('commander opts override defaults', () => {
|
||||
const cfg = resolveOutputConfigFromCommander({ emoji: false, color: false }, {}, true);
|
||||
expect(cfg).toEqual({ plain: false, emoji: false, color: false });
|
||||
expect(cfg).toEqual({ plain: false, emoji: false, color: false, hyperlinks: true });
|
||||
expect(statusPrefix('info', cfg)).toBe('Info: ');
|
||||
expect(labelPrefix('date', cfg)).toBe('Date: ');
|
||||
});
|
||||
|
||||
it('commander plain wins over emoji/color', () => {
|
||||
const cfg = resolveOutputConfigFromCommander({ plain: true, emoji: true, color: true }, {}, true);
|
||||
expect(cfg).toEqual({ plain: true, emoji: false, color: false });
|
||||
expect(cfg).toEqual({ plain: true, emoji: false, color: false, hyperlinks: false });
|
||||
});
|
||||
|
||||
it('commander disables hyperlinks on non-TTY', () => {
|
||||
const cfg = resolveOutputConfigFromCommander({}, {}, false);
|
||||
expect(cfg.hyperlinks).toBe(false);
|
||||
});
|
||||
|
||||
it('formats stats line for all modes', () => {
|
||||
const stats = { likeCount: null, retweetCount: undefined, replyCount: 2 };
|
||||
|
||||
expect(formatStatsLine(stats, { plain: true, emoji: false, color: false })).toBe(
|
||||
expect(formatStatsLine(stats, { plain: true, emoji: false, color: false, hyperlinks: false })).toBe(
|
||||
'likes: 0 retweets: 0 replies: 2',
|
||||
);
|
||||
expect(formatStatsLine(stats, { plain: false, emoji: false, color: false })).toBe('Likes 0 Retweets 0 Replies 2');
|
||||
expect(formatStatsLine(stats, { plain: false, emoji: true, color: false })).toBe('❤️ 0 🔁 0 💬 2');
|
||||
expect(formatStatsLine(stats, { plain: false, emoji: false, color: false, hyperlinks: false })).toBe(
|
||||
'Likes 0 Retweets 0 Replies 2',
|
||||
);
|
||||
expect(formatStatsLine(stats, { plain: false, emoji: true, color: false, hyperlinks: false })).toBe(
|
||||
'❤️ 0 🔁 0 💬 2',
|
||||
);
|
||||
});
|
||||
|
||||
it('always includes tweet URL in all modes', () => {
|
||||
const id = '1234567890';
|
||||
const url = `https://x.com/i/status/${id}`;
|
||||
|
||||
expect(formatTweetUrlLine(id, { plain: true, emoji: false, color: false })).toContain(url);
|
||||
expect(formatTweetUrlLine(id, { plain: false, emoji: false, color: false })).toContain(url);
|
||||
expect(formatTweetUrlLine(id, { plain: false, emoji: true, color: false })).toContain(url);
|
||||
expect(formatTweetUrlLine(id, { plain: true, emoji: false, color: false, hyperlinks: false })).toContain(url);
|
||||
expect(formatTweetUrlLine(id, { plain: false, emoji: false, color: false, hyperlinks: false })).toContain(url);
|
||||
expect(formatTweetUrlLine(id, { plain: false, emoji: true, color: false, hyperlinks: true })).toContain(url);
|
||||
});
|
||||
|
||||
it('hyperlink returns plain text when hyperlinks disabled', () => {
|
||||
const cfg = { plain: true, emoji: false, color: false, hyperlinks: false };
|
||||
expect(hyperlink('https://x.com/test', undefined, cfg)).toBe('https://x.com/test');
|
||||
});
|
||||
|
||||
it('hyperlink returns plain text on non-TTY (hyperlinks: false)', () => {
|
||||
const cfg = { plain: false, emoji: true, color: false, hyperlinks: false };
|
||||
expect(hyperlink('https://x.com/test', undefined, cfg)).toBe('https://x.com/test');
|
||||
});
|
||||
|
||||
it('hyperlink wraps URL with OSC 8 escapes when hyperlinks enabled', () => {
|
||||
const cfg = { plain: false, emoji: true, color: true, hyperlinks: true };
|
||||
const result = hyperlink('https://x.com/test', undefined, cfg);
|
||||
expect(result).toContain('\x1b]8;;');
|
||||
expect(result).toContain('\x07');
|
||||
});
|
||||
|
||||
it('hyperlink uses custom display text', () => {
|
||||
const cfg = { plain: false, emoji: true, color: true, hyperlinks: true };
|
||||
const result = hyperlink('https://x.com/test', 'Click here', cfg);
|
||||
expect(result).toContain('Click here');
|
||||
expect(result).toContain('\x1b]8;;https://x.com/test\x07');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user