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
+3
-1
@@ -7,6 +7,7 @@ import kleur from 'kleur';
|
||||
import { type CookieSource, resolveCredentials } from '../lib/cookies.js';
|
||||
import { extractTweetId } from '../lib/extract-tweet-id.js';
|
||||
import {
|
||||
hyperlink,
|
||||
labelPrefix,
|
||||
type OutputConfig,
|
||||
resolveOutputConfigFromArgv,
|
||||
@@ -323,7 +324,8 @@ export function createCliContext(normalizedArgs: string[], env: NodeJS.ProcessEn
|
||||
if (tweet.createdAt) {
|
||||
console.log(`${l('date')}${tweet.createdAt}`);
|
||||
}
|
||||
console.log(`${l('url')}https://x.com/${tweet.author.username}/status/${tweet.id}`);
|
||||
const tweetUrl = `https://x.com/${tweet.author.username}/status/${tweet.id}`;
|
||||
console.log(`${l('url')}${hyperlink(tweetUrl, tweetUrl, output)}`);
|
||||
if (opts.showSeparator ?? true) {
|
||||
console.log('─'.repeat(50));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import type { Command } from 'commander';
|
||||
import type { CliContext } from '../cli/shared.js';
|
||||
import { extractListId } from '../lib/extract-list-id.js';
|
||||
import { hyperlink } from '../lib/output.js';
|
||||
import type { TwitterList } from '../lib/twitter-client.js';
|
||||
import { TwitterClient } from '../lib/twitter-client.js';
|
||||
|
||||
@@ -23,7 +24,8 @@ function printLists(lists: TwitterList[], ctx: CliContext): void {
|
||||
if (list.owner) {
|
||||
console.log(` ${ctx.colors.muted(`Owner: @${list.owner.username}`)}`);
|
||||
}
|
||||
console.log(` ${ctx.colors.accent(`https://x.com/i/lists/${list.id}`)}`);
|
||||
const listUrl = `https://x.com/i/lists/${list.id}`;
|
||||
console.log(` ${ctx.colors.accent(hyperlink(listUrl, listUrl, ctx.getOutput()))}`);
|
||||
console.log('──────────────────────────────────────────────────');
|
||||
}
|
||||
}
|
||||
|
||||
+21
-3
@@ -2,6 +2,7 @@ export type OutputConfig = {
|
||||
plain: boolean;
|
||||
emoji: boolean;
|
||||
color: boolean;
|
||||
hyperlinks: boolean;
|
||||
};
|
||||
|
||||
export type StatusKind = 'ok' | 'warn' | 'err' | 'info' | 'hint';
|
||||
@@ -33,8 +34,9 @@ export function resolveOutputConfigFromArgv(argv: string[], env: NodeJS.ProcessE
|
||||
const plain = argv.includes('--plain');
|
||||
const emoji = !plain && !argv.includes('--no-emoji');
|
||||
const color = !plain && !argv.includes('--no-color') && defaultColor;
|
||||
const hyperlinks = !plain && isTty;
|
||||
|
||||
return { plain, emoji, color };
|
||||
return { plain, emoji, color, hyperlinks };
|
||||
}
|
||||
|
||||
export function resolveOutputConfigFromCommander(
|
||||
@@ -48,8 +50,9 @@ export function resolveOutputConfigFromCommander(
|
||||
const plain = Boolean(opts.plain);
|
||||
const emoji = !plain && (opts.emoji ?? true);
|
||||
const color = !plain && (opts.color ?? true) && defaultColor;
|
||||
const hyperlinks = !plain && isTty;
|
||||
|
||||
return { plain, emoji, color };
|
||||
return { plain, emoji, color, hyperlinks };
|
||||
}
|
||||
|
||||
export function statusPrefix(kind: StatusKind, cfg: OutputConfig): string {
|
||||
@@ -93,6 +96,21 @@ export function formatTweetUrl(tweetId: string): string {
|
||||
return `https://x.com/i/status/${tweetId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a URL in OSC 8 escape sequences to make it clickable in supported terminals.
|
||||
* Falls back to plain text when not in a TTY or when hyperlinks are disabled.
|
||||
*/
|
||||
export function hyperlink(url: string, text?: string, cfg?: OutputConfig): string {
|
||||
const displayText = text ?? url;
|
||||
// Only use hyperlinks when explicitly enabled (requires TTY and not plain mode)
|
||||
if (!cfg?.hyperlinks) {
|
||||
return displayText;
|
||||
}
|
||||
// OSC 8 hyperlink: \x1b]8;;URL\x07TEXT\x1b]8;;\x07
|
||||
return `\x1b]8;;${url}\x07${displayText}\x1b]8;;\x07`;
|
||||
}
|
||||
|
||||
export function formatTweetUrlLine(tweetId: string, cfg: OutputConfig): string {
|
||||
return `${labelPrefix('url', cfg)}${formatTweetUrl(tweetId)}`;
|
||||
const url = formatTweetUrl(tweetId);
|
||||
return `${labelPrefix('url', cfg)}${hyperlink(url, url, cfg)}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user