Skip to content

Commit 21ab549

Browse files
committed
refactor: use shared emoji_widths.py library in status_handler
Replaced hardcoded WIDTH_2_SINGLE/WIDTH_2_EMOJIS sets with shared emoji_widths.py library that reads from emoji-widths.json. Added missing emojis to emoji-widths.json: - 📁 (folder), 🌿 (herb), 📝 (memo), 🚀 (rocket) This centralizes emoji width definitions so all handlers use the same source of truth, making it easier to add new emojis and support different terminals.
1 parent 843c0cc commit 21ab549

2 files changed

Lines changed: 18 additions & 46 deletions

File tree

plugin/emoji-widths.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@
3434
"triangle_hollow": "",
3535
"diamond": "",
3636
"four_pointed_star": "",
37-
"warning": ""
37+
"warning": "",
38+
"folder": "📁",
39+
"herb": "🌿",
40+
"memo": "📝",
41+
"rocket": "🚀"
3842
},
3943
"terminals": {
4044
"Windows Terminal": {
4145
"☑️": 2, "🔄": 2, "🔳": 2, "🚫": 2, "🚧": 2,
4246
"📊": 2, "📦": 2, "🎯": 2, "📋": 2, "⚙️": 2, "🏆": 2,
4347
"🧠": 2, "🐱": 2, "🧹": 2, "🤝": 2, "✅": 2,
4448
"🔍": 2, "👀": 2, "🔭": 2, "⏳": 2, "⚡": 2, "🔒": 2, "✨": 2, "⚠️": 2,
45-
"✓": 1, "✗": 1, "→": 1, "•": 1, "▸": 1, "▹": 1, "◆": 1, "✦": 2, "⚠": 1
49+
"✓": 1, "✗": 1, "→": 1, "•": 1, "▸": 1, "▹": 1, "◆": 1, "✦": 2, "⚠": 1,
50+
"📁": 2, "🌿": 2, "📝": 2, "🚀": 2
4651
}
4752
}
4853
}

plugin/hooks/skill_handlers/status_handler.py

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import re
8+
import sys
89
from pathlib import Path
910

1011
from . import register_handler
@@ -13,55 +14,21 @@
1314
SCRIPT_DIR = Path(__file__).parent.parent
1415
PLUGIN_ROOT = SCRIPT_DIR.parent
1516

17+
# Add scripts/lib to path for emoji_widths import
18+
_LIB_PATH = PLUGIN_ROOT / "scripts" / "lib"
19+
if str(_LIB_PATH) not in sys.path:
20+
sys.path.insert(0, str(_LIB_PATH))
1621

17-
# Emojis that display as width 2 in most terminals
18-
WIDTH_2_EMOJIS = {
19-
'📊', '📦', '🎯', '📋', '⚙️', '🏆', '🧠', '🐱', '🧹', '🤝',
20-
'✅', '🔍', '👀', '🔭', '⏳', '⚡', '🔒', '✨', '⚠️', '✦',
21-
'☑️', '🔄', '🔳', '🚫', '🚧', '🚀'
22-
}
22+
# Import shared emoji width library
23+
from emoji_widths import EmojiWidths as _EmojiWidths
2324

24-
# Single-character emojis (without variation selector) that are width 2
25-
WIDTH_2_SINGLE = {
26-
'📊', '📦', '🎯', '📋', '🏆', '🧠', '🐱', '🧹', '🤝',
27-
'✅', '🔍', '👀', '🔭', '⏳', '⚡', '🔒', '✨', '✦',
28-
'🔄', '🔳', '🚫', '🚧', '🚀',
29-
# Added for cleanup_handler
30-
'📁', '🌿', '📝',
31-
}
25+
# Singleton instance for emoji width calculations
26+
_emoji_widths = _EmojiWidths(plugin_root=PLUGIN_ROOT)
3227

3328

3429
def display_width(text: str) -> int:
35-
"""Calculate terminal display width of a string."""
36-
width = 0
37-
i = 0
38-
while i < len(text):
39-
char = text[i]
40-
41-
# Check for two-character emoji sequences (char + variation selector)
42-
if i + 1 < len(text):
43-
two_char = text[i:i+2]
44-
if two_char in WIDTH_2_EMOJIS:
45-
width += 2
46-
i += 2
47-
continue
48-
49-
# Check for single-character width-2 emojis
50-
if char in WIDTH_2_SINGLE:
51-
width += 2
52-
i += 1
53-
continue
54-
55-
# Skip variation selectors (they don't add width)
56-
if char == '\ufe0f': # variation selector-16
57-
i += 1
58-
continue
59-
60-
# All other characters are width 1
61-
width += 1
62-
i += 1
63-
64-
return width
30+
"""Calculate terminal display width of a string using shared emoji_widths library."""
31+
return _emoji_widths.display_width(text)
6532

6633

6734
def build_line(content: str, max_width: int) -> str:

0 commit comments

Comments
 (0)