From 7a53a9f683ad6d681c57574df6228a9f3bb167ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:56:01 +0000 Subject: [PATCH] Revert back to old CELL_WIDTHS format. Ideally we'd use the parameter `key` of `bisect_right` with `itemgetter(0)` but that's +3.10 only. So, instead we build the tuple `(cp,)`. This means that the tuple `(2,)` will be placed to the right of the range `(1, 31, -1)`, and we can do the calculations as before. However, this will codepoints at the start of their ranges on the wrong side of the range. E.g., `(1,)` would be placed to the left of `(1, 31, -1)` instead of to the right. To fix this, we add a second element to the tuple that is larger than any second element in the ranges, which ensures that whenever the first element of the tuples (the codepoint and the range start) match, the tuple with the codepoint is always placed on the right. Relevant review comment: https://github.com/Textualize/rich/pull/3300#issuecomment-1987269715 --- rich/_cell_widths.py | 1808 ++++++++------------------------- rich/cells.py | 18 +- tools/make_terminal_widths.py | 39 +- 3 files changed, 473 insertions(+), 1392 deletions(-) diff --git a/rich/_cell_widths.py b/rich/_cell_widths.py index 10a71436..608ae3a7 100644 --- a/rich/_cell_widths.py +++ b/rich/_cell_widths.py @@ -1,1362 +1,454 @@ # Auto generated by make_terminal_widths.py -CELL_WIDTH_RANGE_STARTS = [ - 0, - 1, - 127, - 173, - 768, - 1155, - 1425, - 1471, - 1473, - 1476, - 1479, - 1536, - 1552, - 1564, - 1611, - 1648, - 1750, - 1759, - 1767, - 1770, - 1807, - 1809, - 1840, - 1958, - 2027, - 2045, - 2070, - 2075, - 2085, - 2089, - 2137, - 2192, - 2200, - 2250, - 2362, - 2366, - 2385, - 2402, - 2433, - 2492, - 2494, - 2503, - 2507, - 2519, - 2530, - 2558, - 2561, - 2620, - 2622, - 2631, - 2635, - 2641, - 2672, - 2677, - 2689, - 2748, - 2750, - 2759, - 2763, - 2786, - 2810, - 2817, - 2876, - 2878, - 2887, - 2891, - 2901, - 2914, - 2946, - 3006, - 3014, - 3018, - 3031, - 3072, - 3132, - 3134, - 3142, - 3146, - 3157, - 3170, - 3201, - 3260, - 3262, - 3270, - 3274, - 3285, - 3298, - 3315, - 3328, - 3387, - 3390, - 3398, - 3402, - 3415, - 3426, - 3457, - 3530, - 3535, - 3542, - 3544, - 3570, - 3633, - 3636, - 3655, - 3761, - 3764, - 3784, - 3864, - 3893, - 3895, - 3897, - 3902, - 3953, - 3974, - 3981, - 3993, - 4038, - 4139, - 4182, - 4190, - 4194, - 4199, - 4209, - 4226, - 4239, - 4250, - 4352, - 4448, - 4957, - 5906, - 5938, - 5970, - 6002, - 6068, - 6109, - 6155, - 6277, - 6313, - 6432, - 6448, - 6679, - 6741, - 6752, - 6783, - 6832, - 6912, - 6964, - 7019, - 7040, - 7073, - 7142, - 7204, - 7376, - 7380, - 7405, - 7412, - 7415, - 7616, - 8203, - 8232, - 8288, - 8294, - 8400, - 8986, - 9001, - 9193, - 9200, - 9203, - 9725, - 9748, - 9800, - 9855, - 9875, - 9889, - 9898, - 9917, - 9924, - 9934, - 9940, - 9962, - 9970, - 9973, - 9978, - 9981, - 9989, - 9994, - 10024, - 10060, - 10062, - 10067, - 10071, - 10133, - 10160, - 10175, - 11035, - 11088, - 11093, - 11503, - 11647, - 11744, - 11904, - 11931, - 12032, - 12272, - 12330, - 12336, - 12353, - 12441, - 12443, - 12549, - 12593, - 12688, - 12783, - 12832, - 12880, - 19968, - 42128, - 42607, - 42612, - 42654, - 42736, - 43010, - 43014, - 43019, - 43043, - 43052, - 43136, - 43188, - 43232, - 43263, - 43302, - 43335, - 43360, - 43392, - 43443, - 43493, - 43561, - 43587, - 43596, - 43643, - 43696, - 43698, - 43703, - 43710, - 43713, - 43755, - 43765, - 44003, - 44012, - 44032, - 55216, - 63744, - 64286, - 65024, - 65040, - 65056, - 65072, - 65108, - 65128, - 65279, - 65281, - 65504, - 65529, - 66045, - 66272, - 66422, - 68097, - 68101, - 68108, - 68152, - 68159, - 68325, - 68900, - 69291, - 69373, - 69446, - 69506, - 69632, - 69688, - 69744, - 69747, - 69759, - 69808, - 69821, - 69826, - 69837, - 69888, - 69927, - 69957, - 70003, - 70016, - 70067, - 70089, - 70094, - 70188, - 70206, - 70209, - 70367, - 70400, - 70459, - 70462, - 70471, - 70475, - 70487, - 70498, - 70502, - 70512, - 70709, - 70750, - 70832, - 71087, - 71096, - 71132, - 71216, - 71339, - 71453, - 71724, - 71984, - 71991, - 71995, - 72000, - 72002, - 72145, - 72154, - 72164, - 72193, - 72243, - 72251, - 72263, - 72273, - 72330, - 72751, - 72760, - 72850, - 72873, - 73009, - 73018, - 73020, - 73023, - 73031, - 73098, - 73104, - 73107, - 73459, - 73472, - 73475, - 73524, - 73534, - 78896, - 78919, - 92912, - 92976, - 94031, - 94033, - 94095, - 94176, - 94180, - 94192, - 94208, - 100352, - 101632, - 110576, - 110581, - 110589, - 110592, - 110898, - 110928, - 110933, - 110948, - 110960, - 113821, - 113824, - 118528, - 118576, - 119141, - 119149, - 119173, - 119210, - 119362, - 121344, - 121403, - 121461, - 121476, - 121499, - 121505, - 122880, - 122888, - 122907, - 122915, - 122918, - 123023, - 123184, - 123566, - 123628, - 124140, - 125136, - 125252, - 126980, - 127183, - 127374, - 127377, - 127488, - 127504, - 127552, - 127568, - 127584, - 127744, - 127789, - 127799, - 127870, - 127904, - 127951, - 127968, - 127988, - 127992, - 127995, - 128000, - 128064, - 128066, - 128255, - 128331, - 128336, - 128378, - 128405, - 128420, - 128507, - 128640, - 128716, - 128720, - 128725, - 128732, - 128747, - 128756, - 128992, - 129008, - 129292, - 129340, - 129351, - 129648, - 129664, - 129680, - 129727, - 129742, - 129760, - 129776, - 131072, - 196608, - 917505, - 917536, - 917760, -] - - -CELL_WIDTH_RANGE_ENDS = [ - 0, - 31, - 159, - 173, - 879, - 1161, - 1469, - 1471, - 1474, - 1477, - 1479, - 1541, - 1562, - 1564, - 1631, - 1648, - 1757, - 1764, - 1768, - 1773, - 1807, - 1809, - 1866, - 1968, - 2035, - 2045, - 2073, - 2083, - 2087, - 2093, - 2139, - 2193, - 2207, - 2307, - 2364, - 2383, - 2391, - 2403, - 2435, - 2492, - 2500, - 2504, - 2509, - 2519, - 2531, - 2558, - 2563, - 2620, - 2626, - 2632, - 2637, - 2641, - 2673, - 2677, - 2691, - 2748, - 2757, - 2761, - 2765, - 2787, - 2815, - 2819, - 2876, - 2884, - 2888, - 2893, - 2903, - 2915, - 2946, - 3010, - 3016, - 3021, - 3031, - 3076, - 3132, - 3140, - 3144, - 3149, - 3158, - 3171, - 3203, - 3260, - 3268, - 3272, - 3277, - 3286, - 3299, - 3315, - 3331, - 3388, - 3396, - 3400, - 3405, - 3415, - 3427, - 3459, - 3530, - 3540, - 3542, - 3551, - 3571, - 3633, - 3642, - 3662, - 3761, - 3772, - 3790, - 3865, - 3893, - 3895, - 3897, - 3903, - 3972, - 3975, - 3991, - 4028, - 4038, - 4158, - 4185, - 4192, - 4196, - 4205, - 4212, - 4237, - 4239, - 4253, - 4447, - 4607, - 4959, - 5909, - 5940, - 5971, - 6003, - 6099, - 6109, - 6159, - 6278, - 6313, - 6443, - 6459, - 6683, - 6750, - 6780, - 6783, - 6862, - 6916, - 6980, - 7027, - 7042, - 7085, - 7155, - 7223, - 7378, - 7400, - 7405, - 7412, - 7417, - 7679, - 8207, - 8238, - 8292, - 8303, - 8432, - 8987, - 9002, - 9196, - 9200, - 9203, - 9726, - 9749, - 9811, - 9855, - 9875, - 9889, - 9899, - 9918, - 9925, - 9934, - 9940, - 9962, - 9971, - 9973, - 9978, - 9981, - 9989, - 9995, - 10024, - 10060, - 10062, - 10069, - 10071, - 10135, - 10160, - 10175, - 11036, - 11088, - 11093, - 11505, - 11647, - 11775, - 11929, - 12019, - 12245, - 12329, - 12335, - 12350, - 12438, - 12442, - 12543, - 12591, - 12686, - 12771, - 12830, - 12871, - 19903, - 42124, - 42182, - 42610, - 42621, - 42655, - 42737, - 43010, - 43014, - 43019, - 43047, - 43052, - 43137, - 43205, - 43249, - 43263, - 43309, - 43347, - 43388, - 43395, - 43456, - 43493, - 43574, - 43587, - 43597, - 43645, - 43696, - 43700, - 43704, - 43711, - 43713, - 43759, - 43766, - 44010, - 44013, - 55203, - 55295, - 64255, - 64286, - 65039, - 65049, - 65071, - 65106, - 65126, - 65131, - 65279, - 65376, - 65510, - 65531, - 66045, - 66272, - 66426, - 68099, - 68102, - 68111, - 68154, - 68159, - 68326, - 68903, - 69292, - 69375, - 69456, - 69509, - 69634, - 69702, - 69744, - 69748, - 69762, - 69818, - 69821, - 69826, - 69837, - 69890, - 69940, - 69958, - 70003, - 70018, - 70080, - 70092, - 70095, - 70199, - 70206, - 70209, - 70378, - 70403, - 70460, - 70468, - 70472, - 70477, - 70487, - 70499, - 70508, - 70516, - 70726, - 70750, - 70851, - 71093, - 71104, - 71133, - 71232, - 71351, - 71467, - 71738, - 71989, - 71992, - 71998, - 72000, - 72003, - 72151, - 72160, - 72164, - 72202, - 72249, - 72254, - 72263, - 72283, - 72345, - 72758, - 72767, - 72871, - 72886, - 73014, - 73018, - 73021, - 73029, - 73031, - 73102, - 73105, - 73111, - 73462, - 73473, - 73475, - 73530, - 73538, - 78912, - 78933, - 92916, - 92982, - 94031, - 94087, - 94098, - 94179, - 94180, - 94193, - 100343, - 101589, - 101640, - 110579, - 110587, - 110590, - 110882, - 110898, - 110930, - 110933, - 110951, - 111355, - 113822, - 113827, - 118573, - 118598, - 119145, - 119170, - 119179, - 119213, - 119364, - 121398, - 121452, - 121461, - 121476, - 121503, - 121519, - 122886, - 122904, - 122913, - 122916, - 122922, - 123023, - 123190, - 123566, - 123631, - 124143, - 125142, - 125258, - 126980, - 127183, - 127374, - 127386, - 127490, - 127547, - 127560, - 127569, - 127589, - 127776, - 127797, - 127868, - 127891, - 127946, - 127955, - 127984, - 127988, - 127994, - 127999, - 128062, - 128064, - 128252, - 128317, - 128334, - 128359, - 128378, - 128406, - 128420, - 128591, - 128709, - 128716, - 128722, - 128727, - 128735, - 128748, - 128764, - 129003, - 129008, - 129338, - 129349, - 129535, - 129660, - 129672, - 129725, - 129733, - 129755, - 129768, - 129784, - 196605, - 262141, - 917505, - 917631, - 917999, -] - - CELL_WIDTHS = [ - 0, - -1, - -1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 0, - 0, - 0, - 2, - 2, - 2, - 2, - 0, - 2, - 2, - 0, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 2, - 0, - 2, - 0, - 0, - 2, - 0, - 2, - 2, - 2, - 0, - 2, - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 2, - 0, - 0, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 0, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 0, - 0, - 0, + (0, 0, 0), + (1, 31, -1), + (127, 159, -1), + (173, 173, 0), + (768, 879, 0), + (1155, 1161, 0), + (1425, 1469, 0), + (1471, 1471, 0), + (1473, 1474, 0), + (1476, 1477, 0), + (1479, 1479, 0), + (1536, 1541, 0), + (1552, 1562, 0), + (1564, 1564, 0), + (1611, 1631, 0), + (1648, 1648, 0), + (1750, 1757, 0), + (1759, 1764, 0), + (1767, 1768, 0), + (1770, 1773, 0), + (1807, 1807, 0), + (1809, 1809, 0), + (1840, 1866, 0), + (1958, 1968, 0), + (2027, 2035, 0), + (2045, 2045, 0), + (2070, 2073, 0), + (2075, 2083, 0), + (2085, 2087, 0), + (2089, 2093, 0), + (2137, 2139, 0), + (2192, 2193, 0), + (2200, 2207, 0), + (2250, 2307, 0), + (2362, 2364, 0), + (2366, 2383, 0), + (2385, 2391, 0), + (2402, 2403, 0), + (2433, 2435, 0), + (2492, 2492, 0), + (2494, 2500, 0), + (2503, 2504, 0), + (2507, 2509, 0), + (2519, 2519, 0), + (2530, 2531, 0), + (2558, 2558, 0), + (2561, 2563, 0), + (2620, 2620, 0), + (2622, 2626, 0), + (2631, 2632, 0), + (2635, 2637, 0), + (2641, 2641, 0), + (2672, 2673, 0), + (2677, 2677, 0), + (2689, 2691, 0), + (2748, 2748, 0), + (2750, 2757, 0), + (2759, 2761, 0), + (2763, 2765, 0), + (2786, 2787, 0), + (2810, 2815, 0), + (2817, 2819, 0), + (2876, 2876, 0), + (2878, 2884, 0), + (2887, 2888, 0), + (2891, 2893, 0), + (2901, 2903, 0), + (2914, 2915, 0), + (2946, 2946, 0), + (3006, 3010, 0), + (3014, 3016, 0), + (3018, 3021, 0), + (3031, 3031, 0), + (3072, 3076, 0), + (3132, 3132, 0), + (3134, 3140, 0), + (3142, 3144, 0), + (3146, 3149, 0), + (3157, 3158, 0), + (3170, 3171, 0), + (3201, 3203, 0), + (3260, 3260, 0), + (3262, 3268, 0), + (3270, 3272, 0), + (3274, 3277, 0), + (3285, 3286, 0), + (3298, 3299, 0), + (3315, 3315, 0), + (3328, 3331, 0), + (3387, 3388, 0), + (3390, 3396, 0), + (3398, 3400, 0), + (3402, 3405, 0), + (3415, 3415, 0), + (3426, 3427, 0), + (3457, 3459, 0), + (3530, 3530, 0), + (3535, 3540, 0), + (3542, 3542, 0), + (3544, 3551, 0), + (3570, 3571, 0), + (3633, 3633, 0), + (3636, 3642, 0), + (3655, 3662, 0), + (3761, 3761, 0), + (3764, 3772, 0), + (3784, 3790, 0), + (3864, 3865, 0), + (3893, 3893, 0), + (3895, 3895, 0), + (3897, 3897, 0), + (3902, 3903, 0), + (3953, 3972, 0), + (3974, 3975, 0), + (3981, 3991, 0), + (3993, 4028, 0), + (4038, 4038, 0), + (4139, 4158, 0), + (4182, 4185, 0), + (4190, 4192, 0), + (4194, 4196, 0), + (4199, 4205, 0), + (4209, 4212, 0), + (4226, 4237, 0), + (4239, 4239, 0), + (4250, 4253, 0), + (4352, 4447, 2), + (4448, 4607, 0), + (4957, 4959, 0), + (5906, 5909, 0), + (5938, 5940, 0), + (5970, 5971, 0), + (6002, 6003, 0), + (6068, 6099, 0), + (6109, 6109, 0), + (6155, 6159, 0), + (6277, 6278, 0), + (6313, 6313, 0), + (6432, 6443, 0), + (6448, 6459, 0), + (6679, 6683, 0), + (6741, 6750, 0), + (6752, 6780, 0), + (6783, 6783, 0), + (6832, 6862, 0), + (6912, 6916, 0), + (6964, 6980, 0), + (7019, 7027, 0), + (7040, 7042, 0), + (7073, 7085, 0), + (7142, 7155, 0), + (7204, 7223, 0), + (7376, 7378, 0), + (7380, 7400, 0), + (7405, 7405, 0), + (7412, 7412, 0), + (7415, 7417, 0), + (7616, 7679, 0), + (8203, 8207, 0), + (8232, 8238, 0), + (8288, 8292, 0), + (8294, 8303, 0), + (8400, 8432, 0), + (8986, 8987, 2), + (9001, 9002, 2), + (9193, 9196, 2), + (9200, 9200, 2), + (9203, 9203, 2), + (9725, 9726, 2), + (9748, 9749, 2), + (9800, 9811, 2), + (9855, 9855, 2), + (9875, 9875, 2), + (9889, 9889, 2), + (9898, 9899, 2), + (9917, 9918, 2), + (9924, 9925, 2), + (9934, 9934, 2), + (9940, 9940, 2), + (9962, 9962, 2), + (9970, 9971, 2), + (9973, 9973, 2), + (9978, 9978, 2), + (9981, 9981, 2), + (9989, 9989, 2), + (9994, 9995, 2), + (10024, 10024, 2), + (10060, 10060, 2), + (10062, 10062, 2), + (10067, 10069, 2), + (10071, 10071, 2), + (10133, 10135, 2), + (10160, 10160, 2), + (10175, 10175, 2), + (11035, 11036, 2), + (11088, 11088, 2), + (11093, 11093, 2), + (11503, 11505, 0), + (11647, 11647, 0), + (11744, 11775, 0), + (11904, 11929, 2), + (11931, 12019, 2), + (12032, 12245, 2), + (12272, 12329, 2), + (12330, 12335, 0), + (12336, 12350, 2), + (12353, 12438, 2), + (12441, 12442, 0), + (12443, 12543, 2), + (12549, 12591, 2), + (12593, 12686, 2), + (12688, 12771, 2), + (12783, 12830, 2), + (12832, 12871, 2), + (12880, 19903, 2), + (19968, 42124, 2), + (42128, 42182, 2), + (42607, 42610, 0), + (42612, 42621, 0), + (42654, 42655, 0), + (42736, 42737, 0), + (43010, 43010, 0), + (43014, 43014, 0), + (43019, 43019, 0), + (43043, 43047, 0), + (43052, 43052, 0), + (43136, 43137, 0), + (43188, 43205, 0), + (43232, 43249, 0), + (43263, 43263, 0), + (43302, 43309, 0), + (43335, 43347, 0), + (43360, 43388, 2), + (43392, 43395, 0), + (43443, 43456, 0), + (43493, 43493, 0), + (43561, 43574, 0), + (43587, 43587, 0), + (43596, 43597, 0), + (43643, 43645, 0), + (43696, 43696, 0), + (43698, 43700, 0), + (43703, 43704, 0), + (43710, 43711, 0), + (43713, 43713, 0), + (43755, 43759, 0), + (43765, 43766, 0), + (44003, 44010, 0), + (44012, 44013, 0), + (44032, 55203, 2), + (55216, 55295, 0), + (63744, 64255, 2), + (64286, 64286, 0), + (65024, 65039, 0), + (65040, 65049, 2), + (65056, 65071, 0), + (65072, 65106, 2), + (65108, 65126, 2), + (65128, 65131, 2), + (65279, 65279, 0), + (65281, 65376, 2), + (65504, 65510, 2), + (65529, 65531, 0), + (66045, 66045, 0), + (66272, 66272, 0), + (66422, 66426, 0), + (68097, 68099, 0), + (68101, 68102, 0), + (68108, 68111, 0), + (68152, 68154, 0), + (68159, 68159, 0), + (68325, 68326, 0), + (68900, 68903, 0), + (69291, 69292, 0), + (69373, 69375, 0), + (69446, 69456, 0), + (69506, 69509, 0), + (69632, 69634, 0), + (69688, 69702, 0), + (69744, 69744, 0), + (69747, 69748, 0), + (69759, 69762, 0), + (69808, 69818, 0), + (69821, 69821, 0), + (69826, 69826, 0), + (69837, 69837, 0), + (69888, 69890, 0), + (69927, 69940, 0), + (69957, 69958, 0), + (70003, 70003, 0), + (70016, 70018, 0), + (70067, 70080, 0), + (70089, 70092, 0), + (70094, 70095, 0), + (70188, 70199, 0), + (70206, 70206, 0), + (70209, 70209, 0), + (70367, 70378, 0), + (70400, 70403, 0), + (70459, 70460, 0), + (70462, 70468, 0), + (70471, 70472, 0), + (70475, 70477, 0), + (70487, 70487, 0), + (70498, 70499, 0), + (70502, 70508, 0), + (70512, 70516, 0), + (70709, 70726, 0), + (70750, 70750, 0), + (70832, 70851, 0), + (71087, 71093, 0), + (71096, 71104, 0), + (71132, 71133, 0), + (71216, 71232, 0), + (71339, 71351, 0), + (71453, 71467, 0), + (71724, 71738, 0), + (71984, 71989, 0), + (71991, 71992, 0), + (71995, 71998, 0), + (72000, 72000, 0), + (72002, 72003, 0), + (72145, 72151, 0), + (72154, 72160, 0), + (72164, 72164, 0), + (72193, 72202, 0), + (72243, 72249, 0), + (72251, 72254, 0), + (72263, 72263, 0), + (72273, 72283, 0), + (72330, 72345, 0), + (72751, 72758, 0), + (72760, 72767, 0), + (72850, 72871, 0), + (72873, 72886, 0), + (73009, 73014, 0), + (73018, 73018, 0), + (73020, 73021, 0), + (73023, 73029, 0), + (73031, 73031, 0), + (73098, 73102, 0), + (73104, 73105, 0), + (73107, 73111, 0), + (73459, 73462, 0), + (73472, 73473, 0), + (73475, 73475, 0), + (73524, 73530, 0), + (73534, 73538, 0), + (78896, 78912, 0), + (78919, 78933, 0), + (92912, 92916, 0), + (92976, 92982, 0), + (94031, 94031, 0), + (94033, 94087, 0), + (94095, 94098, 0), + (94176, 94179, 2), + (94180, 94180, 0), + (94192, 94193, 0), + (94208, 100343, 2), + (100352, 101589, 2), + (101632, 101640, 2), + (110576, 110579, 2), + (110581, 110587, 2), + (110589, 110590, 2), + (110592, 110882, 2), + (110898, 110898, 2), + (110928, 110930, 2), + (110933, 110933, 2), + (110948, 110951, 2), + (110960, 111355, 2), + (113821, 113822, 0), + (113824, 113827, 0), + (118528, 118573, 0), + (118576, 118598, 0), + (119141, 119145, 0), + (119149, 119170, 0), + (119173, 119179, 0), + (119210, 119213, 0), + (119362, 119364, 0), + (121344, 121398, 0), + (121403, 121452, 0), + (121461, 121461, 0), + (121476, 121476, 0), + (121499, 121503, 0), + (121505, 121519, 0), + (122880, 122886, 0), + (122888, 122904, 0), + (122907, 122913, 0), + (122915, 122916, 0), + (122918, 122922, 0), + (123023, 123023, 0), + (123184, 123190, 0), + (123566, 123566, 0), + (123628, 123631, 0), + (124140, 124143, 0), + (125136, 125142, 0), + (125252, 125258, 0), + (126980, 126980, 2), + (127183, 127183, 2), + (127374, 127374, 2), + (127377, 127386, 2), + (127488, 127490, 2), + (127504, 127547, 2), + (127552, 127560, 2), + (127568, 127569, 2), + (127584, 127589, 2), + (127744, 127776, 2), + (127789, 127797, 2), + (127799, 127868, 2), + (127870, 127891, 2), + (127904, 127946, 2), + (127951, 127955, 2), + (127968, 127984, 2), + (127988, 127988, 2), + (127992, 127994, 2), + (127995, 127999, 0), + (128000, 128062, 2), + (128064, 128064, 2), + (128066, 128252, 2), + (128255, 128317, 2), + (128331, 128334, 2), + (128336, 128359, 2), + (128378, 128378, 2), + (128405, 128406, 2), + (128420, 128420, 2), + (128507, 128591, 2), + (128640, 128709, 2), + (128716, 128716, 2), + (128720, 128722, 2), + (128725, 128727, 2), + (128732, 128735, 2), + (128747, 128748, 2), + (128756, 128764, 2), + (128992, 129003, 2), + (129008, 129008, 2), + (129292, 129338, 2), + (129340, 129349, 2), + (129351, 129535, 2), + (129648, 129660, 2), + (129664, 129672, 2), + (129680, 129725, 2), + (129727, 129733, 2), + (129742, 129755, 2), + (129760, 129768, 2), + (129776, 129784, 2), + (131072, 196605, 2), + (196608, 262141, 2), + (917505, 917505, 0), + (917536, 917631, 0), + (917760, 917999, 0), ] diff --git a/rich/cells.py b/rich/cells.py index ef1eb223..307b6812 100644 --- a/rich/cells.py +++ b/rich/cells.py @@ -2,15 +2,12 @@ from __future__ import annotations import bisect import re +import sys from functools import lru_cache from itertools import accumulate from typing import Callable -from ._cell_widths import ( - CELL_WIDTH_RANGE_ENDS, - CELL_WIDTH_RANGE_STARTS, - CELL_WIDTHS, -) +from ._cell_widths import CELL_WIDTHS # Regex to match sequence of the most common character ranges _is_single_cell_widths = re.compile("^[\u0020-\u006f\u00a0\u02ff\u0370-\u0482]*$").match @@ -73,10 +70,13 @@ def _get_codepoint_cell_size(codepoint: int) -> int: Returns: int: Number of cells (0, 1 or 2) occupied by that character. """ - - idx = bisect.bisect_right(CELL_WIDTH_RANGE_STARTS, codepoint) - if codepoint <= CELL_WIDTH_RANGE_ENDS[idx - 1]: - width = CELL_WIDTHS[idx - 1] + # We create the tuple as `(cp, sys.maxunicode + 2)` instead of just (cp,) + # because we want the index to always be on the right of the range that + # `cp` belongs to. E.g., `(1,)` won't be placed to the right of `(1, 31, -1)` + # but `(1, sys.maxunicode + 2)` will. + idx = bisect.bisect_right(CELL_WIDTHS, (codepoint, sys.maxunicode + 2)) + _start, end, width = CELL_WIDTHS[idx - 1] + if codepoint <= end: return 0 if width == -1 else width else: return 1 diff --git a/tools/make_terminal_widths.py b/tools/make_terminal_widths.py index dd262342..35c9fe10 100644 --- a/tools/make_terminal_widths.py +++ b/tools/make_terminal_widths.py @@ -13,10 +13,8 @@ from wcwidth import wcwidth progress = Progress() -def make_widths_lists() -> Tuple[List[int], List[int], List[int]]: - starts: List[int] = [] - ends: List[int] = [] - widths: List[int] = [] +def make_widths_table() -> List[Tuple[int, int, int]]: + widths: List[Tuple[int, int, int]] = [] make_table_task = progress.add_task("Calculating table...") @@ -32,16 +30,12 @@ def make_widths_lists() -> Tuple[List[int], List[int], List[int]]: progress.advance(make_table_task, len(cp_list)) if width == 1: continue - starts.append(cp_list[0][0]) - ends.append(cp_list[-1][0]) - widths.append(width) - return starts, ends, widths + widths.append((cp_list[0][0], cp_list[-1][0], width)) + return widths def get_cell_size( - starts: List[int], - ends: List[int], - widths: List[int], + widths: List[Tuple[int, int, int]], codepoint: int, ) -> int: """Get the cell size of a character. @@ -53,37 +47,32 @@ def get_cell_size( int: Number of cells (0, 1 or 2) occupied by that character. """ - idx = bisect.bisect_right(starts, codepoint) - if codepoint <= ends[idx - 1]: - return widths[idx - 1] + idx = bisect.bisect_right(widths, (codepoint, sys.maxunicode + 2)) + _start, end, width = widths[idx - 1] + if codepoint <= end: + return width else: return 1 -def test(starts: List[int], ends: List[int], widths: List[int]) -> None: +def test(widths: List[Tuple[int, int, int]]) -> None: for codepoint in progress.track( range(0, sys.maxunicode + 1), description="Testing..." ): character = chr(codepoint) - width1 = get_cell_size(starts, ends, widths, codepoint) + width1 = get_cell_size(widths, codepoint) width2 = wcwidth(character) if width1 != width2: - print(f"{width1} != {width2}") + print(f"{codepoint}: {width1} != {width2}") break def run() -> None: with progress: - starts, ends, widths = make_widths_lists() - test(starts, ends, widths) + widths = make_widths_table() + test(widths) table_file = f"""# Auto generated by make_terminal_widths.py -CELL_WIDTH_RANGE_STARTS = {starts!r} - - -CELL_WIDTH_RANGE_ENDS = {ends!r} - - CELL_WIDTHS = {widths!r} """ with open("../rich/_cell_widths.py", "wt") as fh: