/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ // Text on a terminal: grapheme clusters and their widths in columns. using System.Globalization; using System.Text; namespace Bjoterm; /// /// Grapheme clusters and display widths. /// /// /// /// Clusters are .NET's extended grapheme clusters (UAX #29). The /// line editor moves and deletes by cluster. /// Widths follow wcwidth: 2 for East Asian wide and /// fullwidth characters and for emoji shown as emoji, 0 for combining /// marks and format characters, 1 otherwise. A terminal's own table can /// differ for new emoji. /// A C0 control or DEL is shown as ^X, 2 columns. A C1 /// control (U+0080 to U+009F) is shown as U+FFFD, 1 column. /// /// public static class Text { /// The grapheme clusters of . CR LF is one. public static string[] Clusters(string text) { var result = new List(); var e = StringInfo.GetTextElementEnumerator(text); while (e.MoveNext()) result.Add(e.GetTextElement()); return result.ToArray(); } /// The clusters of a prompt: as , except that an /// escape sequence (CSI, or OSC ended by BEL or ST) is one item, of /// width 0. public static string[] PromptClusters(string text) { var result = new List(); int i = 0, plain = 0; while (i < text.Length) { int seq = EscapeLength(text, i); if (seq == 0) { i++; continue; } if (i > plain) result.AddRange(Clusters(text[plain..i])); result.Add(text.Substring(i, seq)); i += seq; plain = i; } if (text.Length > plain) result.AddRange(Clusters(text[plain..])); return result.ToArray(); } /// The columns takes. 0 for an escape /// sequence from . public static int Width(string cluster) { if (cluster.Length == 0) return 0; if (cluster.Length > 1 && cluster[0] == '\x1b') return 0; int first = char.ConvertToUtf32(cluster, 0); if (first < 0x20 || first == 0x7f) return 2; if (first >= 0x80 && first < 0xa0) return 1; int width = RuneWidth(first); if (width == 2) return 2; // Emoji presentation, or a flag of two regional indicators. if (cluster.Contains('️')) return 2; if (first >= 0x1f1e6 && first <= 0x1f1ff && cluster.Length >= 4) return 2; if (width == 0) { // A cluster that starts with a mark: the widest of its parts. int widest = 0; foreach (var rune in cluster.EnumerateRunes()) widest = Math.Max(widest, RuneWidth(rune.Value)); return widest; } return width; } /// The columns of , escape sequences as 0. public static int TextWidth(string text) { int total = 0; foreach (var cluster in PromptClusters(text)) total += Width(cluster); return total; } /// What is written for : ^X for a C0 /// control or DEL, U+FFFD for a C1 control, else the cluster. public static string Shown(string cluster) { if (cluster.Length == 0) return cluster; if (cluster.Length > 1 && cluster[0] == '\x1b') return cluster; int first = cluster[0]; if (first < 0x20) return "^" + (char)(first + 0x40); if (first == 0x7f) return "^?"; if (first >= 0x80 && first < 0xa0) return "�"; return cluster; } /// Whether is part of a word: it starts with /// a letter, a digit or '_'. public static bool IsWord(string cluster) { if (cluster.Length == 0) return false; var rune = Rune.GetRuneAt(cluster, 0); return Rune.IsLetterOrDigit(rune) || rune.Value == '_'; } /// The cluster index of the first occurrence of /// in that starts and /// ends on cluster boundaries, or -1. Ordinal; case matters. public static int Find(string text, string query) { var clusters = Clusters(text); var starts = new int[clusters.Length + 1]; for (int i = 0; i < clusters.Length; i++) starts[i + 1] = starts[i] + clusters[i].Length; int from = 0; while (from <= text.Length) { int at = text.IndexOf(query, from, StringComparison.Ordinal); if (at < 0) return -1; int index = Array.BinarySearch(starts, at); if (index >= 0 && Array.BinarySearch(starts, at + query.Length) >= 0) return index; from = at + 1; } return -1; } /// The longest common prefix of , whole /// clusters only. Empty for no texts. public static string CommonPrefix(string[] texts) { if (texts.Length == 0) return ""; var first = Clusters(texts[0]); int length = first.Length; foreach (var text in texts.AsSpan(1)) { var other = Clusters(text); int same = 0; while (same < length && same < other.Length && first[same] == other[same]) same++; length = same; } return string.Concat(first.AsSpan(0, length)); } /// The width of one code point, as wcwidth. public static int RuneWidth(int c) { if (c == 0) return 0; if (c == 0x200b || (c >= 0x1160 && c <= 0x11ff)) return 0; if (!Rune.IsValid(c)) return 1; switch (Rune.GetUnicodeCategory(new Rune(c))) { case UnicodeCategory.NonSpacingMark: case UnicodeCategory.EnclosingMark: case UnicodeCategory.Format: return c == 0xad ? 1 : 0; } return IsWide(c) ? 2 : 1; } private static bool IsWide(int c) { if (c < 0x1100) return false; int lo = 0, hi = Wide.Length / 2 - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (c < Wide[mid * 2]) hi = mid - 1; else if (c > Wide[mid * 2 + 1]) lo = mid + 1; else return true; } return false; } // Ranges of East Asian Wide and Fullwidth code points, and of emoji // with emoji presentation by default (Unicode 15). Sorted, inclusive. private static readonly int[] Wide = [ 0x1100, 0x115f, 0x231a, 0x231b, 0x2329, 0x232a, 0x23e9, 0x23ec, 0x23f0, 0x23f0, 0x23f3, 0x23f3, 0x25fd, 0x25fe, 0x2614, 0x2615, 0x2648, 0x2653, 0x267f, 0x267f, 0x2693, 0x2693, 0x26a1, 0x26a1, 0x26aa, 0x26ab, 0x26bd, 0x26be, 0x26c4, 0x26c5, 0x26ce, 0x26ce, 0x26d4, 0x26d4, 0x26ea, 0x26ea, 0x26f2, 0x26f3, 0x26f5, 0x26f5, 0x26fa, 0x26fa, 0x26fd, 0x26fd, 0x2705, 0x2705, 0x270a, 0x270b, 0x2728, 0x2728, 0x274c, 0x274c, 0x274e, 0x274e, 0x2753, 0x2755, 0x2757, 0x2757, 0x2795, 0x2797, 0x27b0, 0x27b0, 0x27bf, 0x27bf, 0x2b1b, 0x2b1c, 0x2b50, 0x2b50, 0x2b55, 0x2b55, 0x2e80, 0x303e, 0x3041, 0x33ff, 0x3400, 0x4dbf, 0x4e00, 0x9fff, 0xa000, 0xa4cf, 0xa960, 0xa97f, 0xac00, 0xd7a3, 0xf900, 0xfaff, 0xfe10, 0xfe19, 0xfe30, 0xfe6f, 0xff00, 0xff60, 0xffe0, 0xffe6, 0x16fe0, 0x16fe4, 0x17000, 0x18aff, 0x1b000, 0x1b2ff, 0x1f004, 0x1f004, 0x1f0cf, 0x1f0cf, 0x1f18e, 0x1f18e, 0x1f191, 0x1f19a, 0x1f200, 0x1f202, 0x1f210, 0x1f23b, 0x1f240, 0x1f248, 0x1f250, 0x1f251, 0x1f260, 0x1f265, 0x1f300, 0x1f320, 0x1f32d, 0x1f335, 0x1f337, 0x1f37c, 0x1f37e, 0x1f393, 0x1f3a0, 0x1f3ca, 0x1f3cf, 0x1f3d3, 0x1f3e0, 0x1f3f0, 0x1f3f4, 0x1f3f4, 0x1f3f8, 0x1f43e, 0x1f440, 0x1f440, 0x1f442, 0x1f4fc, 0x1f4ff, 0x1f53d, 0x1f54b, 0x1f54e, 0x1f550, 0x1f567, 0x1f57a, 0x1f57a, 0x1f595, 0x1f596, 0x1f5a4, 0x1f5a4, 0x1f5fb, 0x1f64f, 0x1f680, 0x1f6c5, 0x1f6cc, 0x1f6cc, 0x1f6d0, 0x1f6d2, 0x1f6d5, 0x1f6d7, 0x1f6dc, 0x1f6df, 0x1f6eb, 0x1f6ec, 0x1f6f4, 0x1f6fc, 0x1f7e0, 0x1f7eb, 0x1f7f0, 0x1f7f0, 0x1f90c, 0x1f93a, 0x1f93c, 0x1f945, 0x1f947, 0x1f9ff, 0x1fa70, 0x1faff, 0x20000, 0x2fffd, 0x30000, 0x3fffd, ]; // The length of an escape sequence at `i`, or 0. private static int EscapeLength(string text, int i) { if (text[i] != '\x1b' || i + 1 >= text.Length) return 0; char kind = text[i + 1]; if (kind == '[') { int j = i + 2; while (j < text.Length && text[j] >= 0x20 && text[j] <= 0x3f) j++; return j < text.Length && text[j] >= 0x40 && text[j] <= 0x7e ? j + 1 - i : 0; } if (kind == ']') { for (int j = i + 2; j < text.Length; j++) { if (text[j] == '\a') return j + 1 - i; if (text[j] == '\x1b' && j + 1 < text.Length && text[j + 1] == '\\') return j + 2 - i; } return 0; } return 0; } }