Merge branch 'worktree-agent-a9a25296deadb0eaa'
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+1472
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,104 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:xterm/src/utils/unicode_tables.dart';
|
||||
|
||||
/// Cell widths and the grapheme properties the buffer needs, from the
|
||||
/// generated [unicodeTablesVersion] tables (tool/gen_unicode_tables.py).
|
||||
///
|
||||
/// Widths follow glibc's `wcwidth`, which tmux, ncurses, readline and most
|
||||
/// programs use to lay out their output, so the emulator's columns agree with
|
||||
/// theirs: combining marks and format characters 0, East Asian Wide and
|
||||
/// Fullwidth (every default-emoji character included) 2, the rest 1.
|
||||
abstract final class UnicodeWidth {
|
||||
static const _zero = 1;
|
||||
static const _wide = 2;
|
||||
static const _pictographic = 4;
|
||||
static const _textDefault = 8;
|
||||
static const _modifierBase = 16;
|
||||
|
||||
static final Uint8List _bmp = _buildBmp();
|
||||
|
||||
/// Columns [codePoint] takes alone: 0, 1 or 2. Controls are 0.
|
||||
static int width(int codePoint) {
|
||||
if (codePoint < 0x20) return 0;
|
||||
if (codePoint < 0x7F) return 1;
|
||||
if (codePoint < 0xA0) return 0;
|
||||
final props = _props(codePoint);
|
||||
if (props & _zero != 0) return 0;
|
||||
if (props & _wide != 0) return 2;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// Whether [codePoint] is a C0 or C1 control or DEL, which never prints.
|
||||
static bool isControl(int codePoint) =>
|
||||
codePoint < 0x20 || (codePoint >= 0x7F && codePoint < 0xA0);
|
||||
|
||||
/// Extended_Pictographic: what a zero width joiner joins into one emoji.
|
||||
static bool isPictographic(int codePoint) =>
|
||||
_props(codePoint) & _pictographic != 0;
|
||||
|
||||
/// An emoji shown as one-column text by default, which U+FE0F turns into
|
||||
/// its two-column emoji form.
|
||||
static bool isEmojiTextDefault(int codePoint) =>
|
||||
_props(codePoint) & _textDefault != 0;
|
||||
|
||||
/// Emoji_Modifier_Base: a skin tone modifier after it joins it.
|
||||
static bool isEmojiModifierBase(int codePoint) =>
|
||||
_props(codePoint) & _modifierBase != 0;
|
||||
|
||||
/// A skin tone modifier (U+1F3FB-1F3FF).
|
||||
static bool isEmojiModifier(int codePoint) =>
|
||||
codePoint >= 0x1F3FB && codePoint <= 0x1F3FF;
|
||||
|
||||
/// A regional indicator letter; two of them make a flag.
|
||||
static bool isRegionalIndicator(int codePoint) =>
|
||||
codePoint >= 0x1F1E6 && codePoint <= 0x1F1FF;
|
||||
|
||||
static int _props(int codePoint) {
|
||||
if (codePoint < 0x10000) return _bmp[codePoint];
|
||||
var props = 0;
|
||||
if (_within(zeroWidthRanges, codePoint)) props |= _zero;
|
||||
if (_within(wideRanges, codePoint)) props |= _wide;
|
||||
if (_within(extendedPictographicRanges, codePoint)) props |= _pictographic;
|
||||
if (_within(emojiTextDefaultRanges, codePoint)) props |= _textDefault;
|
||||
if (_within(emojiModifierBaseRanges, codePoint)) props |= _modifierBase;
|
||||
return props;
|
||||
}
|
||||
|
||||
static Uint8List _buildBmp() {
|
||||
final table = Uint8List(0x10000);
|
||||
void mark(List<int> ranges, int bit) {
|
||||
for (var i = 0; i < ranges.length; i += 2) {
|
||||
if (ranges[i] >= 0x10000) break;
|
||||
final last = ranges[i + 1] < 0x10000 ? ranges[i + 1] : 0xFFFF;
|
||||
for (var cp = ranges[i]; cp <= last; cp++) {
|
||||
table[cp] |= bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mark(zeroWidthRanges, _zero);
|
||||
mark(wideRanges, _wide);
|
||||
mark(extendedPictographicRanges, _pictographic);
|
||||
mark(emojiTextDefaultRanges, _textDefault);
|
||||
mark(emojiModifierBaseRanges, _modifierBase);
|
||||
return table;
|
||||
}
|
||||
|
||||
/// Binary search of inclusive [first, last] pairs.
|
||||
static bool _within(List<int> ranges, int codePoint) {
|
||||
var low = 0;
|
||||
var high = ranges.length ~/ 2 - 1;
|
||||
while (low <= high) {
|
||||
final mid = (low + high) >> 1;
|
||||
if (codePoint < ranges[mid * 2]) {
|
||||
high = mid - 1;
|
||||
} else if (codePoint > ranges[mid * 2 + 1]) {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user