emoji-space-shim
v0.1.3
Published
shim console.log to fix emoji space render issues across terminals
Maintainers
Readme
emoji-space-shim
shim console.log to fix emoji space render issues across terminals.
problem
some emojis render incorrectly across different terminals due to character width inconsistencies. for example, the beaver emoji (🦫) may appear to "eat" the space after it in VS Code's terminal, while it renders correctly in other terminals.
before (without shim)
// vscode terminal - space after emoji is "eaten"
console.log('🦫 beaver')
→ 🦫beaver ← space disappeared!
// other terminals - renders correctly
console.log('🦫 beaver')
→ 🦫 beaver ← space preservedafter (with shim)
// vscode terminal - shim adds extra space to compensate
console.log('🦫 beaver')
→ 🦫 beaver ← looks correct!
// other terminals - no adjustment needed
console.log('🦫 beaver')
→ 🦫 beaver ← still correctsolution
this package provides a shim that automatically adjusts space after emojis based on the detected terminal. this ensures consistent visual output.
install
npm install emoji-space-shimusage
basic usage
import { shimConsoleLog } from 'emoji-space-shim';
// apply the shim (auto-detects terminal)
const shim = shimConsoleLog();
// console.log now adjusts emoji space automatically
console.log('🦫 hello world');
// restore original behavior when done
shim.restore();explicit terminal
import { shimConsoleLog } from 'emoji-space-shim';
// specify terminal explicitly
const shim = shimConsoleLog({ terminal: 'vscode' });
console.log('🦫 hello world'); // outputs: "🦫 hello world" (extra space added)scoped usage with automatic cleanup
import { withEmojiSpaceShim } from 'emoji-space-shim';
// shim is applied for duration of logic, then automatically restored
const result = await withEmojiSpaceShim({
logic: async () => {
console.log('🦫 hello world'); // emoji space adjusted
return await doWork();
},
});
// console.log is restored to original behavior here
console.log('normal log'); // no emoji adjustmentthis is useful when you want to:
- ensure the shim is always cleaned up (even if errors occur)
- scope the shim to a specific block of code
- avoid manual restore() calls
terminal support
| terminal | detection |
| -------- | ----------------------------------- |
| vscode | TERM_PROGRAM === 'vscode' |
| xterm | TERM.includes('xterm') |
| gnome | TERM_PROGRAM === 'gnome-terminal' |
| default | fallback |
extend the registry
you can extend the emoji registry for custom emoji support:
import { EMOJI_SPACE_REGISTRY, shimConsoleLog } from 'emoji-space-shim';
// add custom emoji rules
EMOJI_SPACE_REGISTRY['🎉'] = { vscode: 1, default: 0 };
// now shim will handle this emoji too
const shim = shimConsoleLog();
console.log('🎉 celebration');exports
| export | description |
| ----------------------------- | ---------------------------------- |
| shimConsoleLog | main shim function |
| withEmojiSpaceShim | scoped wrapper with auto-cleanup |
| TerminalChoice | type for terminal variants |
| EMOJI_SPACE_REGISTRY | emoji space rules dictionary |
| detectTerminalChoice | terminal detection function |
| computeSpaceAdjustment | space calculation function |
| transformMessageForTerminal | message transform function |
how it works
detectTerminalChoice()determines the current terminal from environment variablescomputeSpaceAdjustment()looks up the required space for each emoji in the registrytransformMessageForTerminal()applies the space adjustments to stringsshimConsoleLog()wrapsconsole.logto automatically transform all string arguments
