Datasets:
Add files using upload-large-folder tool
Browse files- data.yaml +16 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_cell_widths.py +454 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_export_format.py +76 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_extension.py +10 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_inspect.py +268 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_loop.py +43 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_null_file.py +69 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_palettes.py +309 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_pick.py +17 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_ratio.py +159 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_stack.py +16 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_timer.py +19 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_windows_renderer.py +56 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/_wrap.py +93 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/align.py +312 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/ansi.py +241 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/bar.py +93 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/color.py +621 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/color_triplet.py +38 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/console.py +0 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/containers.py +167 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/default_styles.py +193 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/diagnose.py +38 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/errors.py +34 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/file_proxy.py +57 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/filesize.py +88 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/jupyter.py +101 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/live.py +375 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/live_render.py +112 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/logging.py +297 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/markup.py +251 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/measure.py +151 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/padding.py +141 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/palette.py +100 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/panel.py +318 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/pretty.py +1016 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/prompt.py +400 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/protocol.py +42 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/region.py +10 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/repr.py +149 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/rule.py +130 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/scope.py +86 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/screen.py +54 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/segment.py +752 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/status.py +131 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/styled.py +42 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/table.py +1006 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/text.py +1361 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/themes.py +5 -0
- env/lib/python3.13/site-packages/pip/_vendor/rich/tree.py +257 -0
data.yaml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SAINetset - YOLO Configuration
|
| 2 |
+
# Wildfire smoke detection dataset from SAI nodes
|
| 3 |
+
# https://huggingface.co/datasets/SAINetset/SAINetset_v8.0
|
| 4 |
+
|
| 5 |
+
path: .
|
| 6 |
+
train: data/train/images
|
| 7 |
+
val: data/val/images
|
| 8 |
+
|
| 9 |
+
# Classes
|
| 10 |
+
names:
|
| 11 |
+
0: smoke
|
| 12 |
+
|
| 13 |
+
# Dataset info
|
| 14 |
+
# Total: 5,596 images (245 positives, 5,351 negatives)
|
| 15 |
+
# Train: 4,462 images
|
| 16 |
+
# Val: 1,134 images
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_cell_widths.py
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
| 1 |
+
# Auto generated by make_terminal_widths.py
|
| 2 |
+
|
| 3 |
+
CELL_WIDTHS = [
|
| 4 |
+
(0, 0, 0),
|
| 5 |
+
(1, 31, -1),
|
| 6 |
+
(127, 159, -1),
|
| 7 |
+
(173, 173, 0),
|
| 8 |
+
(768, 879, 0),
|
| 9 |
+
(1155, 1161, 0),
|
| 10 |
+
(1425, 1469, 0),
|
| 11 |
+
(1471, 1471, 0),
|
| 12 |
+
(1473, 1474, 0),
|
| 13 |
+
(1476, 1477, 0),
|
| 14 |
+
(1479, 1479, 0),
|
| 15 |
+
(1536, 1541, 0),
|
| 16 |
+
(1552, 1562, 0),
|
| 17 |
+
(1564, 1564, 0),
|
| 18 |
+
(1611, 1631, 0),
|
| 19 |
+
(1648, 1648, 0),
|
| 20 |
+
(1750, 1757, 0),
|
| 21 |
+
(1759, 1764, 0),
|
| 22 |
+
(1767, 1768, 0),
|
| 23 |
+
(1770, 1773, 0),
|
| 24 |
+
(1807, 1807, 0),
|
| 25 |
+
(1809, 1809, 0),
|
| 26 |
+
(1840, 1866, 0),
|
| 27 |
+
(1958, 1968, 0),
|
| 28 |
+
(2027, 2035, 0),
|
| 29 |
+
(2045, 2045, 0),
|
| 30 |
+
(2070, 2073, 0),
|
| 31 |
+
(2075, 2083, 0),
|
| 32 |
+
(2085, 2087, 0),
|
| 33 |
+
(2089, 2093, 0),
|
| 34 |
+
(2137, 2139, 0),
|
| 35 |
+
(2192, 2193, 0),
|
| 36 |
+
(2200, 2207, 0),
|
| 37 |
+
(2250, 2307, 0),
|
| 38 |
+
(2362, 2364, 0),
|
| 39 |
+
(2366, 2383, 0),
|
| 40 |
+
(2385, 2391, 0),
|
| 41 |
+
(2402, 2403, 0),
|
| 42 |
+
(2433, 2435, 0),
|
| 43 |
+
(2492, 2492, 0),
|
| 44 |
+
(2494, 2500, 0),
|
| 45 |
+
(2503, 2504, 0),
|
| 46 |
+
(2507, 2509, 0),
|
| 47 |
+
(2519, 2519, 0),
|
| 48 |
+
(2530, 2531, 0),
|
| 49 |
+
(2558, 2558, 0),
|
| 50 |
+
(2561, 2563, 0),
|
| 51 |
+
(2620, 2620, 0),
|
| 52 |
+
(2622, 2626, 0),
|
| 53 |
+
(2631, 2632, 0),
|
| 54 |
+
(2635, 2637, 0),
|
| 55 |
+
(2641, 2641, 0),
|
| 56 |
+
(2672, 2673, 0),
|
| 57 |
+
(2677, 2677, 0),
|
| 58 |
+
(2689, 2691, 0),
|
| 59 |
+
(2748, 2748, 0),
|
| 60 |
+
(2750, 2757, 0),
|
| 61 |
+
(2759, 2761, 0),
|
| 62 |
+
(2763, 2765, 0),
|
| 63 |
+
(2786, 2787, 0),
|
| 64 |
+
(2810, 2815, 0),
|
| 65 |
+
(2817, 2819, 0),
|
| 66 |
+
(2876, 2876, 0),
|
| 67 |
+
(2878, 2884, 0),
|
| 68 |
+
(2887, 2888, 0),
|
| 69 |
+
(2891, 2893, 0),
|
| 70 |
+
(2901, 2903, 0),
|
| 71 |
+
(2914, 2915, 0),
|
| 72 |
+
(2946, 2946, 0),
|
| 73 |
+
(3006, 3010, 0),
|
| 74 |
+
(3014, 3016, 0),
|
| 75 |
+
(3018, 3021, 0),
|
| 76 |
+
(3031, 3031, 0),
|
| 77 |
+
(3072, 3076, 0),
|
| 78 |
+
(3132, 3132, 0),
|
| 79 |
+
(3134, 3140, 0),
|
| 80 |
+
(3142, 3144, 0),
|
| 81 |
+
(3146, 3149, 0),
|
| 82 |
+
(3157, 3158, 0),
|
| 83 |
+
(3170, 3171, 0),
|
| 84 |
+
(3201, 3203, 0),
|
| 85 |
+
(3260, 3260, 0),
|
| 86 |
+
(3262, 3268, 0),
|
| 87 |
+
(3270, 3272, 0),
|
| 88 |
+
(3274, 3277, 0),
|
| 89 |
+
(3285, 3286, 0),
|
| 90 |
+
(3298, 3299, 0),
|
| 91 |
+
(3315, 3315, 0),
|
| 92 |
+
(3328, 3331, 0),
|
| 93 |
+
(3387, 3388, 0),
|
| 94 |
+
(3390, 3396, 0),
|
| 95 |
+
(3398, 3400, 0),
|
| 96 |
+
(3402, 3405, 0),
|
| 97 |
+
(3415, 3415, 0),
|
| 98 |
+
(3426, 3427, 0),
|
| 99 |
+
(3457, 3459, 0),
|
| 100 |
+
(3530, 3530, 0),
|
| 101 |
+
(3535, 3540, 0),
|
| 102 |
+
(3542, 3542, 0),
|
| 103 |
+
(3544, 3551, 0),
|
| 104 |
+
(3570, 3571, 0),
|
| 105 |
+
(3633, 3633, 0),
|
| 106 |
+
(3636, 3642, 0),
|
| 107 |
+
(3655, 3662, 0),
|
| 108 |
+
(3761, 3761, 0),
|
| 109 |
+
(3764, 3772, 0),
|
| 110 |
+
(3784, 3790, 0),
|
| 111 |
+
(3864, 3865, 0),
|
| 112 |
+
(3893, 3893, 0),
|
| 113 |
+
(3895, 3895, 0),
|
| 114 |
+
(3897, 3897, 0),
|
| 115 |
+
(3902, 3903, 0),
|
| 116 |
+
(3953, 3972, 0),
|
| 117 |
+
(3974, 3975, 0),
|
| 118 |
+
(3981, 3991, 0),
|
| 119 |
+
(3993, 4028, 0),
|
| 120 |
+
(4038, 4038, 0),
|
| 121 |
+
(4139, 4158, 0),
|
| 122 |
+
(4182, 4185, 0),
|
| 123 |
+
(4190, 4192, 0),
|
| 124 |
+
(4194, 4196, 0),
|
| 125 |
+
(4199, 4205, 0),
|
| 126 |
+
(4209, 4212, 0),
|
| 127 |
+
(4226, 4237, 0),
|
| 128 |
+
(4239, 4239, 0),
|
| 129 |
+
(4250, 4253, 0),
|
| 130 |
+
(4352, 4447, 2),
|
| 131 |
+
(4448, 4607, 0),
|
| 132 |
+
(4957, 4959, 0),
|
| 133 |
+
(5906, 5909, 0),
|
| 134 |
+
(5938, 5940, 0),
|
| 135 |
+
(5970, 5971, 0),
|
| 136 |
+
(6002, 6003, 0),
|
| 137 |
+
(6068, 6099, 0),
|
| 138 |
+
(6109, 6109, 0),
|
| 139 |
+
(6155, 6159, 0),
|
| 140 |
+
(6277, 6278, 0),
|
| 141 |
+
(6313, 6313, 0),
|
| 142 |
+
(6432, 6443, 0),
|
| 143 |
+
(6448, 6459, 0),
|
| 144 |
+
(6679, 6683, 0),
|
| 145 |
+
(6741, 6750, 0),
|
| 146 |
+
(6752, 6780, 0),
|
| 147 |
+
(6783, 6783, 0),
|
| 148 |
+
(6832, 6862, 0),
|
| 149 |
+
(6912, 6916, 0),
|
| 150 |
+
(6964, 6980, 0),
|
| 151 |
+
(7019, 7027, 0),
|
| 152 |
+
(7040, 7042, 0),
|
| 153 |
+
(7073, 7085, 0),
|
| 154 |
+
(7142, 7155, 0),
|
| 155 |
+
(7204, 7223, 0),
|
| 156 |
+
(7376, 7378, 0),
|
| 157 |
+
(7380, 7400, 0),
|
| 158 |
+
(7405, 7405, 0),
|
| 159 |
+
(7412, 7412, 0),
|
| 160 |
+
(7415, 7417, 0),
|
| 161 |
+
(7616, 7679, 0),
|
| 162 |
+
(8203, 8207, 0),
|
| 163 |
+
(8232, 8238, 0),
|
| 164 |
+
(8288, 8292, 0),
|
| 165 |
+
(8294, 8303, 0),
|
| 166 |
+
(8400, 8432, 0),
|
| 167 |
+
(8986, 8987, 2),
|
| 168 |
+
(9001, 9002, 2),
|
| 169 |
+
(9193, 9196, 2),
|
| 170 |
+
(9200, 9200, 2),
|
| 171 |
+
(9203, 9203, 2),
|
| 172 |
+
(9725, 9726, 2),
|
| 173 |
+
(9748, 9749, 2),
|
| 174 |
+
(9800, 9811, 2),
|
| 175 |
+
(9855, 9855, 2),
|
| 176 |
+
(9875, 9875, 2),
|
| 177 |
+
(9889, 9889, 2),
|
| 178 |
+
(9898, 9899, 2),
|
| 179 |
+
(9917, 9918, 2),
|
| 180 |
+
(9924, 9925, 2),
|
| 181 |
+
(9934, 9934, 2),
|
| 182 |
+
(9940, 9940, 2),
|
| 183 |
+
(9962, 9962, 2),
|
| 184 |
+
(9970, 9971, 2),
|
| 185 |
+
(9973, 9973, 2),
|
| 186 |
+
(9978, 9978, 2),
|
| 187 |
+
(9981, 9981, 2),
|
| 188 |
+
(9989, 9989, 2),
|
| 189 |
+
(9994, 9995, 2),
|
| 190 |
+
(10024, 10024, 2),
|
| 191 |
+
(10060, 10060, 2),
|
| 192 |
+
(10062, 10062, 2),
|
| 193 |
+
(10067, 10069, 2),
|
| 194 |
+
(10071, 10071, 2),
|
| 195 |
+
(10133, 10135, 2),
|
| 196 |
+
(10160, 10160, 2),
|
| 197 |
+
(10175, 10175, 2),
|
| 198 |
+
(11035, 11036, 2),
|
| 199 |
+
(11088, 11088, 2),
|
| 200 |
+
(11093, 11093, 2),
|
| 201 |
+
(11503, 11505, 0),
|
| 202 |
+
(11647, 11647, 0),
|
| 203 |
+
(11744, 11775, 0),
|
| 204 |
+
(11904, 11929, 2),
|
| 205 |
+
(11931, 12019, 2),
|
| 206 |
+
(12032, 12245, 2),
|
| 207 |
+
(12272, 12329, 2),
|
| 208 |
+
(12330, 12335, 0),
|
| 209 |
+
(12336, 12350, 2),
|
| 210 |
+
(12353, 12438, 2),
|
| 211 |
+
(12441, 12442, 0),
|
| 212 |
+
(12443, 12543, 2),
|
| 213 |
+
(12549, 12591, 2),
|
| 214 |
+
(12593, 12686, 2),
|
| 215 |
+
(12688, 12771, 2),
|
| 216 |
+
(12783, 12830, 2),
|
| 217 |
+
(12832, 12871, 2),
|
| 218 |
+
(12880, 19903, 2),
|
| 219 |
+
(19968, 42124, 2),
|
| 220 |
+
(42128, 42182, 2),
|
| 221 |
+
(42607, 42610, 0),
|
| 222 |
+
(42612, 42621, 0),
|
| 223 |
+
(42654, 42655, 0),
|
| 224 |
+
(42736, 42737, 0),
|
| 225 |
+
(43010, 43010, 0),
|
| 226 |
+
(43014, 43014, 0),
|
| 227 |
+
(43019, 43019, 0),
|
| 228 |
+
(43043, 43047, 0),
|
| 229 |
+
(43052, 43052, 0),
|
| 230 |
+
(43136, 43137, 0),
|
| 231 |
+
(43188, 43205, 0),
|
| 232 |
+
(43232, 43249, 0),
|
| 233 |
+
(43263, 43263, 0),
|
| 234 |
+
(43302, 43309, 0),
|
| 235 |
+
(43335, 43347, 0),
|
| 236 |
+
(43360, 43388, 2),
|
| 237 |
+
(43392, 43395, 0),
|
| 238 |
+
(43443, 43456, 0),
|
| 239 |
+
(43493, 43493, 0),
|
| 240 |
+
(43561, 43574, 0),
|
| 241 |
+
(43587, 43587, 0),
|
| 242 |
+
(43596, 43597, 0),
|
| 243 |
+
(43643, 43645, 0),
|
| 244 |
+
(43696, 43696, 0),
|
| 245 |
+
(43698, 43700, 0),
|
| 246 |
+
(43703, 43704, 0),
|
| 247 |
+
(43710, 43711, 0),
|
| 248 |
+
(43713, 43713, 0),
|
| 249 |
+
(43755, 43759, 0),
|
| 250 |
+
(43765, 43766, 0),
|
| 251 |
+
(44003, 44010, 0),
|
| 252 |
+
(44012, 44013, 0),
|
| 253 |
+
(44032, 55203, 2),
|
| 254 |
+
(55216, 55295, 0),
|
| 255 |
+
(63744, 64255, 2),
|
| 256 |
+
(64286, 64286, 0),
|
| 257 |
+
(65024, 65039, 0),
|
| 258 |
+
(65040, 65049, 2),
|
| 259 |
+
(65056, 65071, 0),
|
| 260 |
+
(65072, 65106, 2),
|
| 261 |
+
(65108, 65126, 2),
|
| 262 |
+
(65128, 65131, 2),
|
| 263 |
+
(65279, 65279, 0),
|
| 264 |
+
(65281, 65376, 2),
|
| 265 |
+
(65504, 65510, 2),
|
| 266 |
+
(65529, 65531, 0),
|
| 267 |
+
(66045, 66045, 0),
|
| 268 |
+
(66272, 66272, 0),
|
| 269 |
+
(66422, 66426, 0),
|
| 270 |
+
(68097, 68099, 0),
|
| 271 |
+
(68101, 68102, 0),
|
| 272 |
+
(68108, 68111, 0),
|
| 273 |
+
(68152, 68154, 0),
|
| 274 |
+
(68159, 68159, 0),
|
| 275 |
+
(68325, 68326, 0),
|
| 276 |
+
(68900, 68903, 0),
|
| 277 |
+
(69291, 69292, 0),
|
| 278 |
+
(69373, 69375, 0),
|
| 279 |
+
(69446, 69456, 0),
|
| 280 |
+
(69506, 69509, 0),
|
| 281 |
+
(69632, 69634, 0),
|
| 282 |
+
(69688, 69702, 0),
|
| 283 |
+
(69744, 69744, 0),
|
| 284 |
+
(69747, 69748, 0),
|
| 285 |
+
(69759, 69762, 0),
|
| 286 |
+
(69808, 69818, 0),
|
| 287 |
+
(69821, 69821, 0),
|
| 288 |
+
(69826, 69826, 0),
|
| 289 |
+
(69837, 69837, 0),
|
| 290 |
+
(69888, 69890, 0),
|
| 291 |
+
(69927, 69940, 0),
|
| 292 |
+
(69957, 69958, 0),
|
| 293 |
+
(70003, 70003, 0),
|
| 294 |
+
(70016, 70018, 0),
|
| 295 |
+
(70067, 70080, 0),
|
| 296 |
+
(70089, 70092, 0),
|
| 297 |
+
(70094, 70095, 0),
|
| 298 |
+
(70188, 70199, 0),
|
| 299 |
+
(70206, 70206, 0),
|
| 300 |
+
(70209, 70209, 0),
|
| 301 |
+
(70367, 70378, 0),
|
| 302 |
+
(70400, 70403, 0),
|
| 303 |
+
(70459, 70460, 0),
|
| 304 |
+
(70462, 70468, 0),
|
| 305 |
+
(70471, 70472, 0),
|
| 306 |
+
(70475, 70477, 0),
|
| 307 |
+
(70487, 70487, 0),
|
| 308 |
+
(70498, 70499, 0),
|
| 309 |
+
(70502, 70508, 0),
|
| 310 |
+
(70512, 70516, 0),
|
| 311 |
+
(70709, 70726, 0),
|
| 312 |
+
(70750, 70750, 0),
|
| 313 |
+
(70832, 70851, 0),
|
| 314 |
+
(71087, 71093, 0),
|
| 315 |
+
(71096, 71104, 0),
|
| 316 |
+
(71132, 71133, 0),
|
| 317 |
+
(71216, 71232, 0),
|
| 318 |
+
(71339, 71351, 0),
|
| 319 |
+
(71453, 71467, 0),
|
| 320 |
+
(71724, 71738, 0),
|
| 321 |
+
(71984, 71989, 0),
|
| 322 |
+
(71991, 71992, 0),
|
| 323 |
+
(71995, 71998, 0),
|
| 324 |
+
(72000, 72000, 0),
|
| 325 |
+
(72002, 72003, 0),
|
| 326 |
+
(72145, 72151, 0),
|
| 327 |
+
(72154, 72160, 0),
|
| 328 |
+
(72164, 72164, 0),
|
| 329 |
+
(72193, 72202, 0),
|
| 330 |
+
(72243, 72249, 0),
|
| 331 |
+
(72251, 72254, 0),
|
| 332 |
+
(72263, 72263, 0),
|
| 333 |
+
(72273, 72283, 0),
|
| 334 |
+
(72330, 72345, 0),
|
| 335 |
+
(72751, 72758, 0),
|
| 336 |
+
(72760, 72767, 0),
|
| 337 |
+
(72850, 72871, 0),
|
| 338 |
+
(72873, 72886, 0),
|
| 339 |
+
(73009, 73014, 0),
|
| 340 |
+
(73018, 73018, 0),
|
| 341 |
+
(73020, 73021, 0),
|
| 342 |
+
(73023, 73029, 0),
|
| 343 |
+
(73031, 73031, 0),
|
| 344 |
+
(73098, 73102, 0),
|
| 345 |
+
(73104, 73105, 0),
|
| 346 |
+
(73107, 73111, 0),
|
| 347 |
+
(73459, 73462, 0),
|
| 348 |
+
(73472, 73473, 0),
|
| 349 |
+
(73475, 73475, 0),
|
| 350 |
+
(73524, 73530, 0),
|
| 351 |
+
(73534, 73538, 0),
|
| 352 |
+
(78896, 78912, 0),
|
| 353 |
+
(78919, 78933, 0),
|
| 354 |
+
(92912, 92916, 0),
|
| 355 |
+
(92976, 92982, 0),
|
| 356 |
+
(94031, 94031, 0),
|
| 357 |
+
(94033, 94087, 0),
|
| 358 |
+
(94095, 94098, 0),
|
| 359 |
+
(94176, 94179, 2),
|
| 360 |
+
(94180, 94180, 0),
|
| 361 |
+
(94192, 94193, 0),
|
| 362 |
+
(94208, 100343, 2),
|
| 363 |
+
(100352, 101589, 2),
|
| 364 |
+
(101632, 101640, 2),
|
| 365 |
+
(110576, 110579, 2),
|
| 366 |
+
(110581, 110587, 2),
|
| 367 |
+
(110589, 110590, 2),
|
| 368 |
+
(110592, 110882, 2),
|
| 369 |
+
(110898, 110898, 2),
|
| 370 |
+
(110928, 110930, 2),
|
| 371 |
+
(110933, 110933, 2),
|
| 372 |
+
(110948, 110951, 2),
|
| 373 |
+
(110960, 111355, 2),
|
| 374 |
+
(113821, 113822, 0),
|
| 375 |
+
(113824, 113827, 0),
|
| 376 |
+
(118528, 118573, 0),
|
| 377 |
+
(118576, 118598, 0),
|
| 378 |
+
(119141, 119145, 0),
|
| 379 |
+
(119149, 119170, 0),
|
| 380 |
+
(119173, 119179, 0),
|
| 381 |
+
(119210, 119213, 0),
|
| 382 |
+
(119362, 119364, 0),
|
| 383 |
+
(121344, 121398, 0),
|
| 384 |
+
(121403, 121452, 0),
|
| 385 |
+
(121461, 121461, 0),
|
| 386 |
+
(121476, 121476, 0),
|
| 387 |
+
(121499, 121503, 0),
|
| 388 |
+
(121505, 121519, 0),
|
| 389 |
+
(122880, 122886, 0),
|
| 390 |
+
(122888, 122904, 0),
|
| 391 |
+
(122907, 122913, 0),
|
| 392 |
+
(122915, 122916, 0),
|
| 393 |
+
(122918, 122922, 0),
|
| 394 |
+
(123023, 123023, 0),
|
| 395 |
+
(123184, 123190, 0),
|
| 396 |
+
(123566, 123566, 0),
|
| 397 |
+
(123628, 123631, 0),
|
| 398 |
+
(124140, 124143, 0),
|
| 399 |
+
(125136, 125142, 0),
|
| 400 |
+
(125252, 125258, 0),
|
| 401 |
+
(126980, 126980, 2),
|
| 402 |
+
(127183, 127183, 2),
|
| 403 |
+
(127374, 127374, 2),
|
| 404 |
+
(127377, 127386, 2),
|
| 405 |
+
(127488, 127490, 2),
|
| 406 |
+
(127504, 127547, 2),
|
| 407 |
+
(127552, 127560, 2),
|
| 408 |
+
(127568, 127569, 2),
|
| 409 |
+
(127584, 127589, 2),
|
| 410 |
+
(127744, 127776, 2),
|
| 411 |
+
(127789, 127797, 2),
|
| 412 |
+
(127799, 127868, 2),
|
| 413 |
+
(127870, 127891, 2),
|
| 414 |
+
(127904, 127946, 2),
|
| 415 |
+
(127951, 127955, 2),
|
| 416 |
+
(127968, 127984, 2),
|
| 417 |
+
(127988, 127988, 2),
|
| 418 |
+
(127992, 127994, 2),
|
| 419 |
+
(127995, 127999, 0),
|
| 420 |
+
(128000, 128062, 2),
|
| 421 |
+
(128064, 128064, 2),
|
| 422 |
+
(128066, 128252, 2),
|
| 423 |
+
(128255, 128317, 2),
|
| 424 |
+
(128331, 128334, 2),
|
| 425 |
+
(128336, 128359, 2),
|
| 426 |
+
(128378, 128378, 2),
|
| 427 |
+
(128405, 128406, 2),
|
| 428 |
+
(128420, 128420, 2),
|
| 429 |
+
(128507, 128591, 2),
|
| 430 |
+
(128640, 128709, 2),
|
| 431 |
+
(128716, 128716, 2),
|
| 432 |
+
(128720, 128722, 2),
|
| 433 |
+
(128725, 128727, 2),
|
| 434 |
+
(128732, 128735, 2),
|
| 435 |
+
(128747, 128748, 2),
|
| 436 |
+
(128756, 128764, 2),
|
| 437 |
+
(128992, 129003, 2),
|
| 438 |
+
(129008, 129008, 2),
|
| 439 |
+
(129292, 129338, 2),
|
| 440 |
+
(129340, 129349, 2),
|
| 441 |
+
(129351, 129535, 2),
|
| 442 |
+
(129648, 129660, 2),
|
| 443 |
+
(129664, 129672, 2),
|
| 444 |
+
(129680, 129725, 2),
|
| 445 |
+
(129727, 129733, 2),
|
| 446 |
+
(129742, 129755, 2),
|
| 447 |
+
(129760, 129768, 2),
|
| 448 |
+
(129776, 129784, 2),
|
| 449 |
+
(131072, 196605, 2),
|
| 450 |
+
(196608, 262141, 2),
|
| 451 |
+
(917505, 917505, 0),
|
| 452 |
+
(917536, 917631, 0),
|
| 453 |
+
(917760, 917999, 0),
|
| 454 |
+
]
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_export_format.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
CONSOLE_HTML_FORMAT = """\
|
| 2 |
+
<!DOCTYPE html>
|
| 3 |
+
<html>
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="UTF-8">
|
| 6 |
+
<style>
|
| 7 |
+
{stylesheet}
|
| 8 |
+
body {{
|
| 9 |
+
color: {foreground};
|
| 10 |
+
background-color: {background};
|
| 11 |
+
}}
|
| 12 |
+
</style>
|
| 13 |
+
</head>
|
| 14 |
+
<body>
|
| 15 |
+
<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><code style="font-family:inherit">{code}</code></pre>
|
| 16 |
+
</body>
|
| 17 |
+
</html>
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
CONSOLE_SVG_FORMAT = """\
|
| 21 |
+
<svg class="rich-terminal" viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg">
|
| 22 |
+
<!-- Generated with Rich https://www.textualize.io -->
|
| 23 |
+
<style>
|
| 24 |
+
|
| 25 |
+
@font-face {{
|
| 26 |
+
font-family: "Fira Code";
|
| 27 |
+
src: local("FiraCode-Regular"),
|
| 28 |
+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
|
| 29 |
+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
|
| 30 |
+
font-style: normal;
|
| 31 |
+
font-weight: 400;
|
| 32 |
+
}}
|
| 33 |
+
@font-face {{
|
| 34 |
+
font-family: "Fira Code";
|
| 35 |
+
src: local("FiraCode-Bold"),
|
| 36 |
+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
|
| 37 |
+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
|
| 38 |
+
font-style: bold;
|
| 39 |
+
font-weight: 700;
|
| 40 |
+
}}
|
| 41 |
+
|
| 42 |
+
.{unique_id}-matrix {{
|
| 43 |
+
font-family: Fira Code, monospace;
|
| 44 |
+
font-size: {char_height}px;
|
| 45 |
+
line-height: {line_height}px;
|
| 46 |
+
font-variant-east-asian: full-width;
|
| 47 |
+
}}
|
| 48 |
+
|
| 49 |
+
.{unique_id}-title {{
|
| 50 |
+
font-size: 18px;
|
| 51 |
+
font-weight: bold;
|
| 52 |
+
font-family: arial;
|
| 53 |
+
}}
|
| 54 |
+
|
| 55 |
+
{styles}
|
| 56 |
+
</style>
|
| 57 |
+
|
| 58 |
+
<defs>
|
| 59 |
+
<clipPath id="{unique_id}-clip-terminal">
|
| 60 |
+
<rect x="0" y="0" width="{terminal_width}" height="{terminal_height}" />
|
| 61 |
+
</clipPath>
|
| 62 |
+
{lines}
|
| 63 |
+
</defs>
|
| 64 |
+
|
| 65 |
+
{chrome}
|
| 66 |
+
<g transform="translate({terminal_x}, {terminal_y})" clip-path="url(#{unique_id}-clip-terminal)">
|
| 67 |
+
{backgrounds}
|
| 68 |
+
<g class="{unique_id}-matrix">
|
| 69 |
+
{matrix}
|
| 70 |
+
</g>
|
| 71 |
+
</g>
|
| 72 |
+
</svg>
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
_SVG_FONT_FAMILY = "Rich Fira Code"
|
| 76 |
+
_SVG_CLASSES_PREFIX = "rich-svg"
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_extension.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def load_ipython_extension(ip: Any) -> None: # pragma: no cover
|
| 5 |
+
# prevent circular import
|
| 6 |
+
from pip._vendor.rich.pretty import install
|
| 7 |
+
from pip._vendor.rich.traceback import install as tr_install
|
| 8 |
+
|
| 9 |
+
install()
|
| 10 |
+
tr_install()
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_inspect.py
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
+
from inspect import cleandoc, getdoc, getfile, isclass, ismodule, signature
|
| 3 |
+
from typing import Any, Collection, Iterable, Optional, Tuple, Type, Union
|
| 4 |
+
|
| 5 |
+
from .console import Group, RenderableType
|
| 6 |
+
from .control import escape_control_codes
|
| 7 |
+
from .highlighter import ReprHighlighter
|
| 8 |
+
from .jupyter import JupyterMixin
|
| 9 |
+
from .panel import Panel
|
| 10 |
+
from .pretty import Pretty
|
| 11 |
+
from .table import Table
|
| 12 |
+
from .text import Text, TextType
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _first_paragraph(doc: str) -> str:
|
| 16 |
+
"""Get the first paragraph from a docstring."""
|
| 17 |
+
paragraph, _, _ = doc.partition("\n\n")
|
| 18 |
+
return paragraph
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class Inspect(JupyterMixin):
|
| 22 |
+
"""A renderable to inspect any Python Object.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
obj (Any): An object to inspect.
|
| 26 |
+
title (str, optional): Title to display over inspect result, or None use type. Defaults to None.
|
| 27 |
+
help (bool, optional): Show full help text rather than just first paragraph. Defaults to False.
|
| 28 |
+
methods (bool, optional): Enable inspection of callables. Defaults to False.
|
| 29 |
+
docs (bool, optional): Also render doc strings. Defaults to True.
|
| 30 |
+
private (bool, optional): Show private attributes (beginning with underscore). Defaults to False.
|
| 31 |
+
dunder (bool, optional): Show attributes starting with double underscore. Defaults to False.
|
| 32 |
+
sort (bool, optional): Sort attributes alphabetically. Defaults to True.
|
| 33 |
+
all (bool, optional): Show all attributes. Defaults to False.
|
| 34 |
+
value (bool, optional): Pretty print value of object. Defaults to True.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
def __init__(
|
| 38 |
+
self,
|
| 39 |
+
obj: Any,
|
| 40 |
+
*,
|
| 41 |
+
title: Optional[TextType] = None,
|
| 42 |
+
help: bool = False,
|
| 43 |
+
methods: bool = False,
|
| 44 |
+
docs: bool = True,
|
| 45 |
+
private: bool = False,
|
| 46 |
+
dunder: bool = False,
|
| 47 |
+
sort: bool = True,
|
| 48 |
+
all: bool = True,
|
| 49 |
+
value: bool = True,
|
| 50 |
+
) -> None:
|
| 51 |
+
self.highlighter = ReprHighlighter()
|
| 52 |
+
self.obj = obj
|
| 53 |
+
self.title = title or self._make_title(obj)
|
| 54 |
+
if all:
|
| 55 |
+
methods = private = dunder = True
|
| 56 |
+
self.help = help
|
| 57 |
+
self.methods = methods
|
| 58 |
+
self.docs = docs or help
|
| 59 |
+
self.private = private or dunder
|
| 60 |
+
self.dunder = dunder
|
| 61 |
+
self.sort = sort
|
| 62 |
+
self.value = value
|
| 63 |
+
|
| 64 |
+
def _make_title(self, obj: Any) -> Text:
|
| 65 |
+
"""Make a default title."""
|
| 66 |
+
title_str = (
|
| 67 |
+
str(obj)
|
| 68 |
+
if (isclass(obj) or callable(obj) or ismodule(obj))
|
| 69 |
+
else str(type(obj))
|
| 70 |
+
)
|
| 71 |
+
title_text = self.highlighter(title_str)
|
| 72 |
+
return title_text
|
| 73 |
+
|
| 74 |
+
def __rich__(self) -> Panel:
|
| 75 |
+
return Panel.fit(
|
| 76 |
+
Group(*self._render()),
|
| 77 |
+
title=self.title,
|
| 78 |
+
border_style="scope.border",
|
| 79 |
+
padding=(0, 1),
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
def _get_signature(self, name: str, obj: Any) -> Optional[Text]:
|
| 83 |
+
"""Get a signature for a callable."""
|
| 84 |
+
try:
|
| 85 |
+
_signature = str(signature(obj)) + ":"
|
| 86 |
+
except ValueError:
|
| 87 |
+
_signature = "(...)"
|
| 88 |
+
except TypeError:
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
source_filename: Optional[str] = None
|
| 92 |
+
try:
|
| 93 |
+
source_filename = getfile(obj)
|
| 94 |
+
except (OSError, TypeError):
|
| 95 |
+
# OSError is raised if obj has no source file, e.g. when defined in REPL.
|
| 96 |
+
pass
|
| 97 |
+
|
| 98 |
+
callable_name = Text(name, style="inspect.callable")
|
| 99 |
+
if source_filename:
|
| 100 |
+
callable_name.stylize(f"link file://{source_filename}")
|
| 101 |
+
signature_text = self.highlighter(_signature)
|
| 102 |
+
|
| 103 |
+
qualname = name or getattr(obj, "__qualname__", name)
|
| 104 |
+
|
| 105 |
+
# If obj is a module, there may be classes (which are callable) to display
|
| 106 |
+
if inspect.isclass(obj):
|
| 107 |
+
prefix = "class"
|
| 108 |
+
elif inspect.iscoroutinefunction(obj):
|
| 109 |
+
prefix = "async def"
|
| 110 |
+
else:
|
| 111 |
+
prefix = "def"
|
| 112 |
+
|
| 113 |
+
qual_signature = Text.assemble(
|
| 114 |
+
(f"{prefix} ", f"inspect.{prefix.replace(' ', '_')}"),
|
| 115 |
+
(qualname, "inspect.callable"),
|
| 116 |
+
signature_text,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
return qual_signature
|
| 120 |
+
|
| 121 |
+
def _render(self) -> Iterable[RenderableType]:
|
| 122 |
+
"""Render object."""
|
| 123 |
+
|
| 124 |
+
def sort_items(item: Tuple[str, Any]) -> Tuple[bool, str]:
|
| 125 |
+
key, (_error, value) = item
|
| 126 |
+
return (callable(value), key.strip("_").lower())
|
| 127 |
+
|
| 128 |
+
def safe_getattr(attr_name: str) -> Tuple[Any, Any]:
|
| 129 |
+
"""Get attribute or any exception."""
|
| 130 |
+
try:
|
| 131 |
+
return (None, getattr(obj, attr_name))
|
| 132 |
+
except Exception as error:
|
| 133 |
+
return (error, None)
|
| 134 |
+
|
| 135 |
+
obj = self.obj
|
| 136 |
+
keys = dir(obj)
|
| 137 |
+
total_items = len(keys)
|
| 138 |
+
if not self.dunder:
|
| 139 |
+
keys = [key for key in keys if not key.startswith("__")]
|
| 140 |
+
if not self.private:
|
| 141 |
+
keys = [key for key in keys if not key.startswith("_")]
|
| 142 |
+
not_shown_count = total_items - len(keys)
|
| 143 |
+
items = [(key, safe_getattr(key)) for key in keys]
|
| 144 |
+
if self.sort:
|
| 145 |
+
items.sort(key=sort_items)
|
| 146 |
+
|
| 147 |
+
items_table = Table.grid(padding=(0, 1), expand=False)
|
| 148 |
+
items_table.add_column(justify="right")
|
| 149 |
+
add_row = items_table.add_row
|
| 150 |
+
highlighter = self.highlighter
|
| 151 |
+
|
| 152 |
+
if callable(obj):
|
| 153 |
+
signature = self._get_signature("", obj)
|
| 154 |
+
if signature is not None:
|
| 155 |
+
yield signature
|
| 156 |
+
yield ""
|
| 157 |
+
|
| 158 |
+
if self.docs:
|
| 159 |
+
_doc = self._get_formatted_doc(obj)
|
| 160 |
+
if _doc is not None:
|
| 161 |
+
doc_text = Text(_doc, style="inspect.help")
|
| 162 |
+
doc_text = highlighter(doc_text)
|
| 163 |
+
yield doc_text
|
| 164 |
+
yield ""
|
| 165 |
+
|
| 166 |
+
if self.value and not (isclass(obj) or callable(obj) or ismodule(obj)):
|
| 167 |
+
yield Panel(
|
| 168 |
+
Pretty(obj, indent_guides=True, max_length=10, max_string=60),
|
| 169 |
+
border_style="inspect.value.border",
|
| 170 |
+
)
|
| 171 |
+
yield ""
|
| 172 |
+
|
| 173 |
+
for key, (error, value) in items:
|
| 174 |
+
key_text = Text.assemble(
|
| 175 |
+
(
|
| 176 |
+
key,
|
| 177 |
+
"inspect.attr.dunder" if key.startswith("__") else "inspect.attr",
|
| 178 |
+
),
|
| 179 |
+
(" =", "inspect.equals"),
|
| 180 |
+
)
|
| 181 |
+
if error is not None:
|
| 182 |
+
warning = key_text.copy()
|
| 183 |
+
warning.stylize("inspect.error")
|
| 184 |
+
add_row(warning, highlighter(repr(error)))
|
| 185 |
+
continue
|
| 186 |
+
|
| 187 |
+
if callable(value):
|
| 188 |
+
if not self.methods:
|
| 189 |
+
continue
|
| 190 |
+
|
| 191 |
+
_signature_text = self._get_signature(key, value)
|
| 192 |
+
if _signature_text is None:
|
| 193 |
+
add_row(key_text, Pretty(value, highlighter=highlighter))
|
| 194 |
+
else:
|
| 195 |
+
if self.docs:
|
| 196 |
+
docs = self._get_formatted_doc(value)
|
| 197 |
+
if docs is not None:
|
| 198 |
+
_signature_text.append("\n" if "\n" in docs else " ")
|
| 199 |
+
doc = highlighter(docs)
|
| 200 |
+
doc.stylize("inspect.doc")
|
| 201 |
+
_signature_text.append(doc)
|
| 202 |
+
|
| 203 |
+
add_row(key_text, _signature_text)
|
| 204 |
+
else:
|
| 205 |
+
add_row(key_text, Pretty(value, highlighter=highlighter))
|
| 206 |
+
if items_table.row_count:
|
| 207 |
+
yield items_table
|
| 208 |
+
elif not_shown_count:
|
| 209 |
+
yield Text.from_markup(
|
| 210 |
+
f"[b cyan]{not_shown_count}[/][i] attribute(s) not shown.[/i] "
|
| 211 |
+
f"Run [b][magenta]inspect[/]([not b]inspect[/])[/b] for options."
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
def _get_formatted_doc(self, object_: Any) -> Optional[str]:
|
| 215 |
+
"""
|
| 216 |
+
Extract the docstring of an object, process it and returns it.
|
| 217 |
+
The processing consists in cleaning up the doctring's indentation,
|
| 218 |
+
taking only its 1st paragraph if `self.help` is not True,
|
| 219 |
+
and escape its control codes.
|
| 220 |
+
|
| 221 |
+
Args:
|
| 222 |
+
object_ (Any): the object to get the docstring from.
|
| 223 |
+
|
| 224 |
+
Returns:
|
| 225 |
+
Optional[str]: the processed docstring, or None if no docstring was found.
|
| 226 |
+
"""
|
| 227 |
+
docs = getdoc(object_)
|
| 228 |
+
if docs is None:
|
| 229 |
+
return None
|
| 230 |
+
docs = cleandoc(docs).strip()
|
| 231 |
+
if not self.help:
|
| 232 |
+
docs = _first_paragraph(docs)
|
| 233 |
+
return escape_control_codes(docs)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def get_object_types_mro(obj: Union[object, Type[Any]]) -> Tuple[type, ...]:
|
| 237 |
+
"""Returns the MRO of an object's class, or of the object itself if it's a class."""
|
| 238 |
+
if not hasattr(obj, "__mro__"):
|
| 239 |
+
# N.B. we cannot use `if type(obj) is type` here because it doesn't work with
|
| 240 |
+
# some types of classes, such as the ones that use abc.ABCMeta.
|
| 241 |
+
obj = type(obj)
|
| 242 |
+
return getattr(obj, "__mro__", ())
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def get_object_types_mro_as_strings(obj: object) -> Collection[str]:
|
| 246 |
+
"""
|
| 247 |
+
Returns the MRO of an object's class as full qualified names, or of the object itself if it's a class.
|
| 248 |
+
|
| 249 |
+
Examples:
|
| 250 |
+
`object_types_mro_as_strings(JSONDecoder)` will return `['json.decoder.JSONDecoder', 'builtins.object']`
|
| 251 |
+
"""
|
| 252 |
+
return [
|
| 253 |
+
f'{getattr(type_, "__module__", "")}.{getattr(type_, "__qualname__", "")}'
|
| 254 |
+
for type_ in get_object_types_mro(obj)
|
| 255 |
+
]
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def is_object_one_of_types(
|
| 259 |
+
obj: object, fully_qualified_types_names: Collection[str]
|
| 260 |
+
) -> bool:
|
| 261 |
+
"""
|
| 262 |
+
Returns `True` if the given object's class (or the object itself, if it's a class) has one of the
|
| 263 |
+
fully qualified names in its MRO.
|
| 264 |
+
"""
|
| 265 |
+
for type_name in get_object_types_mro_as_strings(obj):
|
| 266 |
+
if type_name in fully_qualified_types_names:
|
| 267 |
+
return True
|
| 268 |
+
return False
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_loop.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Iterable, Tuple, TypeVar
|
| 2 |
+
|
| 3 |
+
T = TypeVar("T")
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def loop_first(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
|
| 7 |
+
"""Iterate and generate a tuple with a flag for first value."""
|
| 8 |
+
iter_values = iter(values)
|
| 9 |
+
try:
|
| 10 |
+
value = next(iter_values)
|
| 11 |
+
except StopIteration:
|
| 12 |
+
return
|
| 13 |
+
yield True, value
|
| 14 |
+
for value in iter_values:
|
| 15 |
+
yield False, value
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
|
| 19 |
+
"""Iterate and generate a tuple with a flag for last value."""
|
| 20 |
+
iter_values = iter(values)
|
| 21 |
+
try:
|
| 22 |
+
previous_value = next(iter_values)
|
| 23 |
+
except StopIteration:
|
| 24 |
+
return
|
| 25 |
+
for value in iter_values:
|
| 26 |
+
yield False, previous_value
|
| 27 |
+
previous_value = value
|
| 28 |
+
yield True, previous_value
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def loop_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]:
|
| 32 |
+
"""Iterate and generate a tuple with a flag for first and last value."""
|
| 33 |
+
iter_values = iter(values)
|
| 34 |
+
try:
|
| 35 |
+
previous_value = next(iter_values)
|
| 36 |
+
except StopIteration:
|
| 37 |
+
return
|
| 38 |
+
first = True
|
| 39 |
+
for value in iter_values:
|
| 40 |
+
yield first, False, previous_value
|
| 41 |
+
first = False
|
| 42 |
+
previous_value = value
|
| 43 |
+
yield first, True, previous_value
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_null_file.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from types import TracebackType
|
| 2 |
+
from typing import IO, Iterable, Iterator, List, Optional, Type
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class NullFile(IO[str]):
|
| 6 |
+
def close(self) -> None:
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
def isatty(self) -> bool:
|
| 10 |
+
return False
|
| 11 |
+
|
| 12 |
+
def read(self, __n: int = 1) -> str:
|
| 13 |
+
return ""
|
| 14 |
+
|
| 15 |
+
def readable(self) -> bool:
|
| 16 |
+
return False
|
| 17 |
+
|
| 18 |
+
def readline(self, __limit: int = 1) -> str:
|
| 19 |
+
return ""
|
| 20 |
+
|
| 21 |
+
def readlines(self, __hint: int = 1) -> List[str]:
|
| 22 |
+
return []
|
| 23 |
+
|
| 24 |
+
def seek(self, __offset: int, __whence: int = 1) -> int:
|
| 25 |
+
return 0
|
| 26 |
+
|
| 27 |
+
def seekable(self) -> bool:
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
def tell(self) -> int:
|
| 31 |
+
return 0
|
| 32 |
+
|
| 33 |
+
def truncate(self, __size: Optional[int] = 1) -> int:
|
| 34 |
+
return 0
|
| 35 |
+
|
| 36 |
+
def writable(self) -> bool:
|
| 37 |
+
return False
|
| 38 |
+
|
| 39 |
+
def writelines(self, __lines: Iterable[str]) -> None:
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
def __next__(self) -> str:
|
| 43 |
+
return ""
|
| 44 |
+
|
| 45 |
+
def __iter__(self) -> Iterator[str]:
|
| 46 |
+
return iter([""])
|
| 47 |
+
|
| 48 |
+
def __enter__(self) -> IO[str]:
|
| 49 |
+
return self
|
| 50 |
+
|
| 51 |
+
def __exit__(
|
| 52 |
+
self,
|
| 53 |
+
__t: Optional[Type[BaseException]],
|
| 54 |
+
__value: Optional[BaseException],
|
| 55 |
+
__traceback: Optional[TracebackType],
|
| 56 |
+
) -> None:
|
| 57 |
+
pass
|
| 58 |
+
|
| 59 |
+
def write(self, text: str) -> int:
|
| 60 |
+
return 0
|
| 61 |
+
|
| 62 |
+
def flush(self) -> None:
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
def fileno(self) -> int:
|
| 66 |
+
return -1
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
NULL_FILE = NullFile()
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_palettes.py
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .palette import Palette
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# Taken from https://en.wikipedia.org/wiki/ANSI_escape_code (Windows 10 column)
|
| 5 |
+
WINDOWS_PALETTE = Palette(
|
| 6 |
+
[
|
| 7 |
+
(12, 12, 12),
|
| 8 |
+
(197, 15, 31),
|
| 9 |
+
(19, 161, 14),
|
| 10 |
+
(193, 156, 0),
|
| 11 |
+
(0, 55, 218),
|
| 12 |
+
(136, 23, 152),
|
| 13 |
+
(58, 150, 221),
|
| 14 |
+
(204, 204, 204),
|
| 15 |
+
(118, 118, 118),
|
| 16 |
+
(231, 72, 86),
|
| 17 |
+
(22, 198, 12),
|
| 18 |
+
(249, 241, 165),
|
| 19 |
+
(59, 120, 255),
|
| 20 |
+
(180, 0, 158),
|
| 21 |
+
(97, 214, 214),
|
| 22 |
+
(242, 242, 242),
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# # The standard ansi colors (including bright variants)
|
| 27 |
+
STANDARD_PALETTE = Palette(
|
| 28 |
+
[
|
| 29 |
+
(0, 0, 0),
|
| 30 |
+
(170, 0, 0),
|
| 31 |
+
(0, 170, 0),
|
| 32 |
+
(170, 85, 0),
|
| 33 |
+
(0, 0, 170),
|
| 34 |
+
(170, 0, 170),
|
| 35 |
+
(0, 170, 170),
|
| 36 |
+
(170, 170, 170),
|
| 37 |
+
(85, 85, 85),
|
| 38 |
+
(255, 85, 85),
|
| 39 |
+
(85, 255, 85),
|
| 40 |
+
(255, 255, 85),
|
| 41 |
+
(85, 85, 255),
|
| 42 |
+
(255, 85, 255),
|
| 43 |
+
(85, 255, 255),
|
| 44 |
+
(255, 255, 255),
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# The 256 color palette
|
| 50 |
+
EIGHT_BIT_PALETTE = Palette(
|
| 51 |
+
[
|
| 52 |
+
(0, 0, 0),
|
| 53 |
+
(128, 0, 0),
|
| 54 |
+
(0, 128, 0),
|
| 55 |
+
(128, 128, 0),
|
| 56 |
+
(0, 0, 128),
|
| 57 |
+
(128, 0, 128),
|
| 58 |
+
(0, 128, 128),
|
| 59 |
+
(192, 192, 192),
|
| 60 |
+
(128, 128, 128),
|
| 61 |
+
(255, 0, 0),
|
| 62 |
+
(0, 255, 0),
|
| 63 |
+
(255, 255, 0),
|
| 64 |
+
(0, 0, 255),
|
| 65 |
+
(255, 0, 255),
|
| 66 |
+
(0, 255, 255),
|
| 67 |
+
(255, 255, 255),
|
| 68 |
+
(0, 0, 0),
|
| 69 |
+
(0, 0, 95),
|
| 70 |
+
(0, 0, 135),
|
| 71 |
+
(0, 0, 175),
|
| 72 |
+
(0, 0, 215),
|
| 73 |
+
(0, 0, 255),
|
| 74 |
+
(0, 95, 0),
|
| 75 |
+
(0, 95, 95),
|
| 76 |
+
(0, 95, 135),
|
| 77 |
+
(0, 95, 175),
|
| 78 |
+
(0, 95, 215),
|
| 79 |
+
(0, 95, 255),
|
| 80 |
+
(0, 135, 0),
|
| 81 |
+
(0, 135, 95),
|
| 82 |
+
(0, 135, 135),
|
| 83 |
+
(0, 135, 175),
|
| 84 |
+
(0, 135, 215),
|
| 85 |
+
(0, 135, 255),
|
| 86 |
+
(0, 175, 0),
|
| 87 |
+
(0, 175, 95),
|
| 88 |
+
(0, 175, 135),
|
| 89 |
+
(0, 175, 175),
|
| 90 |
+
(0, 175, 215),
|
| 91 |
+
(0, 175, 255),
|
| 92 |
+
(0, 215, 0),
|
| 93 |
+
(0, 215, 95),
|
| 94 |
+
(0, 215, 135),
|
| 95 |
+
(0, 215, 175),
|
| 96 |
+
(0, 215, 215),
|
| 97 |
+
(0, 215, 255),
|
| 98 |
+
(0, 255, 0),
|
| 99 |
+
(0, 255, 95),
|
| 100 |
+
(0, 255, 135),
|
| 101 |
+
(0, 255, 175),
|
| 102 |
+
(0, 255, 215),
|
| 103 |
+
(0, 255, 255),
|
| 104 |
+
(95, 0, 0),
|
| 105 |
+
(95, 0, 95),
|
| 106 |
+
(95, 0, 135),
|
| 107 |
+
(95, 0, 175),
|
| 108 |
+
(95, 0, 215),
|
| 109 |
+
(95, 0, 255),
|
| 110 |
+
(95, 95, 0),
|
| 111 |
+
(95, 95, 95),
|
| 112 |
+
(95, 95, 135),
|
| 113 |
+
(95, 95, 175),
|
| 114 |
+
(95, 95, 215),
|
| 115 |
+
(95, 95, 255),
|
| 116 |
+
(95, 135, 0),
|
| 117 |
+
(95, 135, 95),
|
| 118 |
+
(95, 135, 135),
|
| 119 |
+
(95, 135, 175),
|
| 120 |
+
(95, 135, 215),
|
| 121 |
+
(95, 135, 255),
|
| 122 |
+
(95, 175, 0),
|
| 123 |
+
(95, 175, 95),
|
| 124 |
+
(95, 175, 135),
|
| 125 |
+
(95, 175, 175),
|
| 126 |
+
(95, 175, 215),
|
| 127 |
+
(95, 175, 255),
|
| 128 |
+
(95, 215, 0),
|
| 129 |
+
(95, 215, 95),
|
| 130 |
+
(95, 215, 135),
|
| 131 |
+
(95, 215, 175),
|
| 132 |
+
(95, 215, 215),
|
| 133 |
+
(95, 215, 255),
|
| 134 |
+
(95, 255, 0),
|
| 135 |
+
(95, 255, 95),
|
| 136 |
+
(95, 255, 135),
|
| 137 |
+
(95, 255, 175),
|
| 138 |
+
(95, 255, 215),
|
| 139 |
+
(95, 255, 255),
|
| 140 |
+
(135, 0, 0),
|
| 141 |
+
(135, 0, 95),
|
| 142 |
+
(135, 0, 135),
|
| 143 |
+
(135, 0, 175),
|
| 144 |
+
(135, 0, 215),
|
| 145 |
+
(135, 0, 255),
|
| 146 |
+
(135, 95, 0),
|
| 147 |
+
(135, 95, 95),
|
| 148 |
+
(135, 95, 135),
|
| 149 |
+
(135, 95, 175),
|
| 150 |
+
(135, 95, 215),
|
| 151 |
+
(135, 95, 255),
|
| 152 |
+
(135, 135, 0),
|
| 153 |
+
(135, 135, 95),
|
| 154 |
+
(135, 135, 135),
|
| 155 |
+
(135, 135, 175),
|
| 156 |
+
(135, 135, 215),
|
| 157 |
+
(135, 135, 255),
|
| 158 |
+
(135, 175, 0),
|
| 159 |
+
(135, 175, 95),
|
| 160 |
+
(135, 175, 135),
|
| 161 |
+
(135, 175, 175),
|
| 162 |
+
(135, 175, 215),
|
| 163 |
+
(135, 175, 255),
|
| 164 |
+
(135, 215, 0),
|
| 165 |
+
(135, 215, 95),
|
| 166 |
+
(135, 215, 135),
|
| 167 |
+
(135, 215, 175),
|
| 168 |
+
(135, 215, 215),
|
| 169 |
+
(135, 215, 255),
|
| 170 |
+
(135, 255, 0),
|
| 171 |
+
(135, 255, 95),
|
| 172 |
+
(135, 255, 135),
|
| 173 |
+
(135, 255, 175),
|
| 174 |
+
(135, 255, 215),
|
| 175 |
+
(135, 255, 255),
|
| 176 |
+
(175, 0, 0),
|
| 177 |
+
(175, 0, 95),
|
| 178 |
+
(175, 0, 135),
|
| 179 |
+
(175, 0, 175),
|
| 180 |
+
(175, 0, 215),
|
| 181 |
+
(175, 0, 255),
|
| 182 |
+
(175, 95, 0),
|
| 183 |
+
(175, 95, 95),
|
| 184 |
+
(175, 95, 135),
|
| 185 |
+
(175, 95, 175),
|
| 186 |
+
(175, 95, 215),
|
| 187 |
+
(175, 95, 255),
|
| 188 |
+
(175, 135, 0),
|
| 189 |
+
(175, 135, 95),
|
| 190 |
+
(175, 135, 135),
|
| 191 |
+
(175, 135, 175),
|
| 192 |
+
(175, 135, 215),
|
| 193 |
+
(175, 135, 255),
|
| 194 |
+
(175, 175, 0),
|
| 195 |
+
(175, 175, 95),
|
| 196 |
+
(175, 175, 135),
|
| 197 |
+
(175, 175, 175),
|
| 198 |
+
(175, 175, 215),
|
| 199 |
+
(175, 175, 255),
|
| 200 |
+
(175, 215, 0),
|
| 201 |
+
(175, 215, 95),
|
| 202 |
+
(175, 215, 135),
|
| 203 |
+
(175, 215, 175),
|
| 204 |
+
(175, 215, 215),
|
| 205 |
+
(175, 215, 255),
|
| 206 |
+
(175, 255, 0),
|
| 207 |
+
(175, 255, 95),
|
| 208 |
+
(175, 255, 135),
|
| 209 |
+
(175, 255, 175),
|
| 210 |
+
(175, 255, 215),
|
| 211 |
+
(175, 255, 255),
|
| 212 |
+
(215, 0, 0),
|
| 213 |
+
(215, 0, 95),
|
| 214 |
+
(215, 0, 135),
|
| 215 |
+
(215, 0, 175),
|
| 216 |
+
(215, 0, 215),
|
| 217 |
+
(215, 0, 255),
|
| 218 |
+
(215, 95, 0),
|
| 219 |
+
(215, 95, 95),
|
| 220 |
+
(215, 95, 135),
|
| 221 |
+
(215, 95, 175),
|
| 222 |
+
(215, 95, 215),
|
| 223 |
+
(215, 95, 255),
|
| 224 |
+
(215, 135, 0),
|
| 225 |
+
(215, 135, 95),
|
| 226 |
+
(215, 135, 135),
|
| 227 |
+
(215, 135, 175),
|
| 228 |
+
(215, 135, 215),
|
| 229 |
+
(215, 135, 255),
|
| 230 |
+
(215, 175, 0),
|
| 231 |
+
(215, 175, 95),
|
| 232 |
+
(215, 175, 135),
|
| 233 |
+
(215, 175, 175),
|
| 234 |
+
(215, 175, 215),
|
| 235 |
+
(215, 175, 255),
|
| 236 |
+
(215, 215, 0),
|
| 237 |
+
(215, 215, 95),
|
| 238 |
+
(215, 215, 135),
|
| 239 |
+
(215, 215, 175),
|
| 240 |
+
(215, 215, 215),
|
| 241 |
+
(215, 215, 255),
|
| 242 |
+
(215, 255, 0),
|
| 243 |
+
(215, 255, 95),
|
| 244 |
+
(215, 255, 135),
|
| 245 |
+
(215, 255, 175),
|
| 246 |
+
(215, 255, 215),
|
| 247 |
+
(215, 255, 255),
|
| 248 |
+
(255, 0, 0),
|
| 249 |
+
(255, 0, 95),
|
| 250 |
+
(255, 0, 135),
|
| 251 |
+
(255, 0, 175),
|
| 252 |
+
(255, 0, 215),
|
| 253 |
+
(255, 0, 255),
|
| 254 |
+
(255, 95, 0),
|
| 255 |
+
(255, 95, 95),
|
| 256 |
+
(255, 95, 135),
|
| 257 |
+
(255, 95, 175),
|
| 258 |
+
(255, 95, 215),
|
| 259 |
+
(255, 95, 255),
|
| 260 |
+
(255, 135, 0),
|
| 261 |
+
(255, 135, 95),
|
| 262 |
+
(255, 135, 135),
|
| 263 |
+
(255, 135, 175),
|
| 264 |
+
(255, 135, 215),
|
| 265 |
+
(255, 135, 255),
|
| 266 |
+
(255, 175, 0),
|
| 267 |
+
(255, 175, 95),
|
| 268 |
+
(255, 175, 135),
|
| 269 |
+
(255, 175, 175),
|
| 270 |
+
(255, 175, 215),
|
| 271 |
+
(255, 175, 255),
|
| 272 |
+
(255, 215, 0),
|
| 273 |
+
(255, 215, 95),
|
| 274 |
+
(255, 215, 135),
|
| 275 |
+
(255, 215, 175),
|
| 276 |
+
(255, 215, 215),
|
| 277 |
+
(255, 215, 255),
|
| 278 |
+
(255, 255, 0),
|
| 279 |
+
(255, 255, 95),
|
| 280 |
+
(255, 255, 135),
|
| 281 |
+
(255, 255, 175),
|
| 282 |
+
(255, 255, 215),
|
| 283 |
+
(255, 255, 255),
|
| 284 |
+
(8, 8, 8),
|
| 285 |
+
(18, 18, 18),
|
| 286 |
+
(28, 28, 28),
|
| 287 |
+
(38, 38, 38),
|
| 288 |
+
(48, 48, 48),
|
| 289 |
+
(58, 58, 58),
|
| 290 |
+
(68, 68, 68),
|
| 291 |
+
(78, 78, 78),
|
| 292 |
+
(88, 88, 88),
|
| 293 |
+
(98, 98, 98),
|
| 294 |
+
(108, 108, 108),
|
| 295 |
+
(118, 118, 118),
|
| 296 |
+
(128, 128, 128),
|
| 297 |
+
(138, 138, 138),
|
| 298 |
+
(148, 148, 148),
|
| 299 |
+
(158, 158, 158),
|
| 300 |
+
(168, 168, 168),
|
| 301 |
+
(178, 178, 178),
|
| 302 |
+
(188, 188, 188),
|
| 303 |
+
(198, 198, 198),
|
| 304 |
+
(208, 208, 208),
|
| 305 |
+
(218, 218, 218),
|
| 306 |
+
(228, 228, 228),
|
| 307 |
+
(238, 238, 238),
|
| 308 |
+
]
|
| 309 |
+
)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_pick.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def pick_bool(*values: Optional[bool]) -> bool:
|
| 5 |
+
"""Pick the first non-none bool or return the last value.
|
| 6 |
+
|
| 7 |
+
Args:
|
| 8 |
+
*values (bool): Any number of boolean or None values.
|
| 9 |
+
|
| 10 |
+
Returns:
|
| 11 |
+
bool: First non-none boolean.
|
| 12 |
+
"""
|
| 13 |
+
assert values, "1 or more values required"
|
| 14 |
+
for value in values:
|
| 15 |
+
if value is not None:
|
| 16 |
+
return value
|
| 17 |
+
return bool(value)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_ratio.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from fractions import Fraction
|
| 3 |
+
from math import ceil
|
| 4 |
+
from typing import cast, List, Optional, Sequence
|
| 5 |
+
|
| 6 |
+
if sys.version_info >= (3, 8):
|
| 7 |
+
from typing import Protocol
|
| 8 |
+
else:
|
| 9 |
+
from pip._vendor.typing_extensions import Protocol # pragma: no cover
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Edge(Protocol):
|
| 13 |
+
"""Any object that defines an edge (such as Layout)."""
|
| 14 |
+
|
| 15 |
+
size: Optional[int] = None
|
| 16 |
+
ratio: int = 1
|
| 17 |
+
minimum_size: int = 1
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def ratio_resolve(total: int, edges: Sequence[Edge]) -> List[int]:
|
| 21 |
+
"""Divide total space to satisfy size, ratio, and minimum_size, constraints.
|
| 22 |
+
|
| 23 |
+
The returned list of integers should add up to total in most cases, unless it is
|
| 24 |
+
impossible to satisfy all the constraints. For instance, if there are two edges
|
| 25 |
+
with a minimum size of 20 each and `total` is 30 then the returned list will be
|
| 26 |
+
greater than total. In practice, this would mean that a Layout object would
|
| 27 |
+
clip the rows that would overflow the screen height.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
total (int): Total number of characters.
|
| 31 |
+
edges (List[Edge]): Edges within total space.
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
List[int]: Number of characters for each edge.
|
| 35 |
+
"""
|
| 36 |
+
# Size of edge or None for yet to be determined
|
| 37 |
+
sizes = [(edge.size or None) for edge in edges]
|
| 38 |
+
|
| 39 |
+
_Fraction = Fraction
|
| 40 |
+
|
| 41 |
+
# While any edges haven't been calculated
|
| 42 |
+
while None in sizes:
|
| 43 |
+
# Get flexible edges and index to map these back on to sizes list
|
| 44 |
+
flexible_edges = [
|
| 45 |
+
(index, edge)
|
| 46 |
+
for index, (size, edge) in enumerate(zip(sizes, edges))
|
| 47 |
+
if size is None
|
| 48 |
+
]
|
| 49 |
+
# Remaining space in total
|
| 50 |
+
remaining = total - sum(size or 0 for size in sizes)
|
| 51 |
+
if remaining <= 0:
|
| 52 |
+
# No room for flexible edges
|
| 53 |
+
return [
|
| 54 |
+
((edge.minimum_size or 1) if size is None else size)
|
| 55 |
+
for size, edge in zip(sizes, edges)
|
| 56 |
+
]
|
| 57 |
+
# Calculate number of characters in a ratio portion
|
| 58 |
+
portion = _Fraction(
|
| 59 |
+
remaining, sum((edge.ratio or 1) for _, edge in flexible_edges)
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# If any edges will be less than their minimum, replace size with the minimum
|
| 63 |
+
for index, edge in flexible_edges:
|
| 64 |
+
if portion * edge.ratio <= edge.minimum_size:
|
| 65 |
+
sizes[index] = edge.minimum_size
|
| 66 |
+
# New fixed size will invalidate calculations, so we need to repeat the process
|
| 67 |
+
break
|
| 68 |
+
else:
|
| 69 |
+
# Distribute flexible space and compensate for rounding error
|
| 70 |
+
# Since edge sizes can only be integers we need to add the remainder
|
| 71 |
+
# to the following line
|
| 72 |
+
remainder = _Fraction(0)
|
| 73 |
+
for index, edge in flexible_edges:
|
| 74 |
+
size, remainder = divmod(portion * edge.ratio + remainder, 1)
|
| 75 |
+
sizes[index] = size
|
| 76 |
+
break
|
| 77 |
+
# Sizes now contains integers only
|
| 78 |
+
return cast(List[int], sizes)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def ratio_reduce(
|
| 82 |
+
total: int, ratios: List[int], maximums: List[int], values: List[int]
|
| 83 |
+
) -> List[int]:
|
| 84 |
+
"""Divide an integer total in to parts based on ratios.
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
total (int): The total to divide.
|
| 88 |
+
ratios (List[int]): A list of integer ratios.
|
| 89 |
+
maximums (List[int]): List of maximums values for each slot.
|
| 90 |
+
values (List[int]): List of values
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
List[int]: A list of integers guaranteed to sum to total.
|
| 94 |
+
"""
|
| 95 |
+
ratios = [ratio if _max else 0 for ratio, _max in zip(ratios, maximums)]
|
| 96 |
+
total_ratio = sum(ratios)
|
| 97 |
+
if not total_ratio:
|
| 98 |
+
return values[:]
|
| 99 |
+
total_remaining = total
|
| 100 |
+
result: List[int] = []
|
| 101 |
+
append = result.append
|
| 102 |
+
for ratio, maximum, value in zip(ratios, maximums, values):
|
| 103 |
+
if ratio and total_ratio > 0:
|
| 104 |
+
distributed = min(maximum, round(ratio * total_remaining / total_ratio))
|
| 105 |
+
append(value - distributed)
|
| 106 |
+
total_remaining -= distributed
|
| 107 |
+
total_ratio -= ratio
|
| 108 |
+
else:
|
| 109 |
+
append(value)
|
| 110 |
+
return result
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def ratio_distribute(
|
| 114 |
+
total: int, ratios: List[int], minimums: Optional[List[int]] = None
|
| 115 |
+
) -> List[int]:
|
| 116 |
+
"""Distribute an integer total in to parts based on ratios.
|
| 117 |
+
|
| 118 |
+
Args:
|
| 119 |
+
total (int): The total to divide.
|
| 120 |
+
ratios (List[int]): A list of integer ratios.
|
| 121 |
+
minimums (List[int]): List of minimum values for each slot.
|
| 122 |
+
|
| 123 |
+
Returns:
|
| 124 |
+
List[int]: A list of integers guaranteed to sum to total.
|
| 125 |
+
"""
|
| 126 |
+
if minimums:
|
| 127 |
+
ratios = [ratio if _min else 0 for ratio, _min in zip(ratios, minimums)]
|
| 128 |
+
total_ratio = sum(ratios)
|
| 129 |
+
assert total_ratio > 0, "Sum of ratios must be > 0"
|
| 130 |
+
|
| 131 |
+
total_remaining = total
|
| 132 |
+
distributed_total: List[int] = []
|
| 133 |
+
append = distributed_total.append
|
| 134 |
+
if minimums is None:
|
| 135 |
+
_minimums = [0] * len(ratios)
|
| 136 |
+
else:
|
| 137 |
+
_minimums = minimums
|
| 138 |
+
for ratio, minimum in zip(ratios, _minimums):
|
| 139 |
+
if total_ratio > 0:
|
| 140 |
+
distributed = max(minimum, ceil(ratio * total_remaining / total_ratio))
|
| 141 |
+
else:
|
| 142 |
+
distributed = total_remaining
|
| 143 |
+
append(distributed)
|
| 144 |
+
total_ratio -= ratio
|
| 145 |
+
total_remaining -= distributed
|
| 146 |
+
return distributed_total
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
from dataclasses import dataclass
|
| 151 |
+
|
| 152 |
+
@dataclass
|
| 153 |
+
class E:
|
| 154 |
+
size: Optional[int] = None
|
| 155 |
+
ratio: int = 1
|
| 156 |
+
minimum_size: int = 1
|
| 157 |
+
|
| 158 |
+
resolved = ratio_resolve(110, [E(None, 1, 1), E(None, 1, 1), E(None, 1, 1)])
|
| 159 |
+
print(sum(resolved))
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_stack.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, TypeVar
|
| 2 |
+
|
| 3 |
+
T = TypeVar("T")
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Stack(List[T]):
|
| 7 |
+
"""A small shim over builtin list."""
|
| 8 |
+
|
| 9 |
+
@property
|
| 10 |
+
def top(self) -> T:
|
| 11 |
+
"""Get top of stack."""
|
| 12 |
+
return self[-1]
|
| 13 |
+
|
| 14 |
+
def push(self, item: T) -> None:
|
| 15 |
+
"""Push an item on to the stack (append in stack nomenclature)."""
|
| 16 |
+
self.append(item)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_timer.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Timer context manager, only used in debug.
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from time import time
|
| 7 |
+
|
| 8 |
+
import contextlib
|
| 9 |
+
from typing import Generator
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@contextlib.contextmanager
|
| 13 |
+
def timer(subject: str = "time") -> Generator[None, None, None]:
|
| 14 |
+
"""print the elapsed time. (only used in debugging)"""
|
| 15 |
+
start = time()
|
| 16 |
+
yield
|
| 17 |
+
elapsed = time() - start
|
| 18 |
+
elapsed_ms = elapsed * 1000
|
| 19 |
+
print(f"{subject} elapsed {elapsed_ms:.1f}ms")
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_windows_renderer.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Iterable, Sequence, Tuple, cast
|
| 2 |
+
|
| 3 |
+
from pip._vendor.rich._win32_console import LegacyWindowsTerm, WindowsCoordinates
|
| 4 |
+
from pip._vendor.rich.segment import ControlCode, ControlType, Segment
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def legacy_windows_render(buffer: Iterable[Segment], term: LegacyWindowsTerm) -> None:
|
| 8 |
+
"""Makes appropriate Windows Console API calls based on the segments in the buffer.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
buffer (Iterable[Segment]): Iterable of Segments to convert to Win32 API calls.
|
| 12 |
+
term (LegacyWindowsTerm): Used to call the Windows Console API.
|
| 13 |
+
"""
|
| 14 |
+
for text, style, control in buffer:
|
| 15 |
+
if not control:
|
| 16 |
+
if style:
|
| 17 |
+
term.write_styled(text, style)
|
| 18 |
+
else:
|
| 19 |
+
term.write_text(text)
|
| 20 |
+
else:
|
| 21 |
+
control_codes: Sequence[ControlCode] = control
|
| 22 |
+
for control_code in control_codes:
|
| 23 |
+
control_type = control_code[0]
|
| 24 |
+
if control_type == ControlType.CURSOR_MOVE_TO:
|
| 25 |
+
_, x, y = cast(Tuple[ControlType, int, int], control_code)
|
| 26 |
+
term.move_cursor_to(WindowsCoordinates(row=y - 1, col=x - 1))
|
| 27 |
+
elif control_type == ControlType.CARRIAGE_RETURN:
|
| 28 |
+
term.write_text("\r")
|
| 29 |
+
elif control_type == ControlType.HOME:
|
| 30 |
+
term.move_cursor_to(WindowsCoordinates(0, 0))
|
| 31 |
+
elif control_type == ControlType.CURSOR_UP:
|
| 32 |
+
term.move_cursor_up()
|
| 33 |
+
elif control_type == ControlType.CURSOR_DOWN:
|
| 34 |
+
term.move_cursor_down()
|
| 35 |
+
elif control_type == ControlType.CURSOR_FORWARD:
|
| 36 |
+
term.move_cursor_forward()
|
| 37 |
+
elif control_type == ControlType.CURSOR_BACKWARD:
|
| 38 |
+
term.move_cursor_backward()
|
| 39 |
+
elif control_type == ControlType.CURSOR_MOVE_TO_COLUMN:
|
| 40 |
+
_, column = cast(Tuple[ControlType, int], control_code)
|
| 41 |
+
term.move_cursor_to_column(column - 1)
|
| 42 |
+
elif control_type == ControlType.HIDE_CURSOR:
|
| 43 |
+
term.hide_cursor()
|
| 44 |
+
elif control_type == ControlType.SHOW_CURSOR:
|
| 45 |
+
term.show_cursor()
|
| 46 |
+
elif control_type == ControlType.ERASE_IN_LINE:
|
| 47 |
+
_, mode = cast(Tuple[ControlType, int], control_code)
|
| 48 |
+
if mode == 0:
|
| 49 |
+
term.erase_end_of_line()
|
| 50 |
+
elif mode == 1:
|
| 51 |
+
term.erase_start_of_line()
|
| 52 |
+
elif mode == 2:
|
| 53 |
+
term.erase_line()
|
| 54 |
+
elif control_type == ControlType.SET_WINDOW_TITLE:
|
| 55 |
+
_, title = cast(Tuple[ControlType, str], control_code)
|
| 56 |
+
term.set_title(title)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/_wrap.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from typing import Iterable
|
| 5 |
+
|
| 6 |
+
from ._loop import loop_last
|
| 7 |
+
from .cells import cell_len, chop_cells
|
| 8 |
+
|
| 9 |
+
re_word = re.compile(r"\s*\S+\s*")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def words(text: str) -> Iterable[tuple[int, int, str]]:
|
| 13 |
+
"""Yields each word from the text as a tuple
|
| 14 |
+
containing (start_index, end_index, word). A "word" in this context may
|
| 15 |
+
include the actual word and any whitespace to the right.
|
| 16 |
+
"""
|
| 17 |
+
position = 0
|
| 18 |
+
word_match = re_word.match(text, position)
|
| 19 |
+
while word_match is not None:
|
| 20 |
+
start, end = word_match.span()
|
| 21 |
+
word = word_match.group(0)
|
| 22 |
+
yield start, end, word
|
| 23 |
+
word_match = re_word.match(text, end)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def divide_line(text: str, width: int, fold: bool = True) -> list[int]:
|
| 27 |
+
"""Given a string of text, and a width (measured in cells), return a list
|
| 28 |
+
of cell offsets which the string should be split at in order for it to fit
|
| 29 |
+
within the given width.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
text: The text to examine.
|
| 33 |
+
width: The available cell width.
|
| 34 |
+
fold: If True, words longer than `width` will be folded onto a new line.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
A list of indices to break the line at.
|
| 38 |
+
"""
|
| 39 |
+
break_positions: list[int] = [] # offsets to insert the breaks at
|
| 40 |
+
append = break_positions.append
|
| 41 |
+
cell_offset = 0
|
| 42 |
+
_cell_len = cell_len
|
| 43 |
+
|
| 44 |
+
for start, _end, word in words(text):
|
| 45 |
+
word_length = _cell_len(word.rstrip())
|
| 46 |
+
remaining_space = width - cell_offset
|
| 47 |
+
word_fits_remaining_space = remaining_space >= word_length
|
| 48 |
+
|
| 49 |
+
if word_fits_remaining_space:
|
| 50 |
+
# Simplest case - the word fits within the remaining width for this line.
|
| 51 |
+
cell_offset += _cell_len(word)
|
| 52 |
+
else:
|
| 53 |
+
# Not enough space remaining for this word on the current line.
|
| 54 |
+
if word_length > width:
|
| 55 |
+
# The word doesn't fit on any line, so we can't simply
|
| 56 |
+
# place it on the next line...
|
| 57 |
+
if fold:
|
| 58 |
+
# Fold the word across multiple lines.
|
| 59 |
+
folded_word = chop_cells(word, width=width)
|
| 60 |
+
for last, line in loop_last(folded_word):
|
| 61 |
+
if start:
|
| 62 |
+
append(start)
|
| 63 |
+
if last:
|
| 64 |
+
cell_offset = _cell_len(line)
|
| 65 |
+
else:
|
| 66 |
+
start += len(line)
|
| 67 |
+
else:
|
| 68 |
+
# Folding isn't allowed, so crop the word.
|
| 69 |
+
if start:
|
| 70 |
+
append(start)
|
| 71 |
+
cell_offset = _cell_len(word)
|
| 72 |
+
elif cell_offset and start:
|
| 73 |
+
# The word doesn't fit within the remaining space on the current
|
| 74 |
+
# line, but it *can* fit on to the next (empty) line.
|
| 75 |
+
append(start)
|
| 76 |
+
cell_offset = _cell_len(word)
|
| 77 |
+
|
| 78 |
+
return break_positions
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__": # pragma: no cover
|
| 82 |
+
from .console import Console
|
| 83 |
+
|
| 84 |
+
console = Console(width=10)
|
| 85 |
+
console.print("12345 abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ 12345")
|
| 86 |
+
print(chop_cells("abcdefghijklmnopqrstuvwxyz", 10))
|
| 87 |
+
|
| 88 |
+
console = Console(width=20)
|
| 89 |
+
console.rule()
|
| 90 |
+
console.print("TextualはPythonの高速アプリケーション開発フレームワークです")
|
| 91 |
+
|
| 92 |
+
console.rule()
|
| 93 |
+
console.print("アプリケーションは1670万色を使用でき")
|
env/lib/python3.13/site-packages/pip/_vendor/rich/align.py
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from itertools import chain
|
| 3 |
+
from typing import TYPE_CHECKING, Iterable, Optional
|
| 4 |
+
|
| 5 |
+
if sys.version_info >= (3, 8):
|
| 6 |
+
from typing import Literal
|
| 7 |
+
else:
|
| 8 |
+
from pip._vendor.typing_extensions import Literal # pragma: no cover
|
| 9 |
+
|
| 10 |
+
from .constrain import Constrain
|
| 11 |
+
from .jupyter import JupyterMixin
|
| 12 |
+
from .measure import Measurement
|
| 13 |
+
from .segment import Segment
|
| 14 |
+
from .style import StyleType
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from .console import Console, ConsoleOptions, RenderableType, RenderResult
|
| 18 |
+
|
| 19 |
+
AlignMethod = Literal["left", "center", "right"]
|
| 20 |
+
VerticalAlignMethod = Literal["top", "middle", "bottom"]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class Align(JupyterMixin):
|
| 24 |
+
"""Align a renderable by adding spaces if necessary.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
renderable (RenderableType): A console renderable.
|
| 28 |
+
align (AlignMethod): One of "left", "center", or "right""
|
| 29 |
+
style (StyleType, optional): An optional style to apply to the background.
|
| 30 |
+
vertical (Optional[VerticalAlignMethod], optional): Optional vertical align, one of "top", "middle", or "bottom". Defaults to None.
|
| 31 |
+
pad (bool, optional): Pad the right with spaces. Defaults to True.
|
| 32 |
+
width (int, optional): Restrict contents to given width, or None to use default width. Defaults to None.
|
| 33 |
+
height (int, optional): Set height of align renderable, or None to fit to contents. Defaults to None.
|
| 34 |
+
|
| 35 |
+
Raises:
|
| 36 |
+
ValueError: if ``align`` is not one of the expected values.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
def __init__(
|
| 40 |
+
self,
|
| 41 |
+
renderable: "RenderableType",
|
| 42 |
+
align: AlignMethod = "left",
|
| 43 |
+
style: Optional[StyleType] = None,
|
| 44 |
+
*,
|
| 45 |
+
vertical: Optional[VerticalAlignMethod] = None,
|
| 46 |
+
pad: bool = True,
|
| 47 |
+
width: Optional[int] = None,
|
| 48 |
+
height: Optional[int] = None,
|
| 49 |
+
) -> None:
|
| 50 |
+
if align not in ("left", "center", "right"):
|
| 51 |
+
raise ValueError(
|
| 52 |
+
f'invalid value for align, expected "left", "center", or "right" (not {align!r})'
|
| 53 |
+
)
|
| 54 |
+
if vertical is not None and vertical not in ("top", "middle", "bottom"):
|
| 55 |
+
raise ValueError(
|
| 56 |
+
f'invalid value for vertical, expected "top", "middle", or "bottom" (not {vertical!r})'
|
| 57 |
+
)
|
| 58 |
+
self.renderable = renderable
|
| 59 |
+
self.align = align
|
| 60 |
+
self.style = style
|
| 61 |
+
self.vertical = vertical
|
| 62 |
+
self.pad = pad
|
| 63 |
+
self.width = width
|
| 64 |
+
self.height = height
|
| 65 |
+
|
| 66 |
+
def __repr__(self) -> str:
|
| 67 |
+
return f"Align({self.renderable!r}, {self.align!r})"
|
| 68 |
+
|
| 69 |
+
@classmethod
|
| 70 |
+
def left(
|
| 71 |
+
cls,
|
| 72 |
+
renderable: "RenderableType",
|
| 73 |
+
style: Optional[StyleType] = None,
|
| 74 |
+
*,
|
| 75 |
+
vertical: Optional[VerticalAlignMethod] = None,
|
| 76 |
+
pad: bool = True,
|
| 77 |
+
width: Optional[int] = None,
|
| 78 |
+
height: Optional[int] = None,
|
| 79 |
+
) -> "Align":
|
| 80 |
+
"""Align a renderable to the left."""
|
| 81 |
+
return cls(
|
| 82 |
+
renderable,
|
| 83 |
+
"left",
|
| 84 |
+
style=style,
|
| 85 |
+
vertical=vertical,
|
| 86 |
+
pad=pad,
|
| 87 |
+
width=width,
|
| 88 |
+
height=height,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
@classmethod
|
| 92 |
+
def center(
|
| 93 |
+
cls,
|
| 94 |
+
renderable: "RenderableType",
|
| 95 |
+
style: Optional[StyleType] = None,
|
| 96 |
+
*,
|
| 97 |
+
vertical: Optional[VerticalAlignMethod] = None,
|
| 98 |
+
pad: bool = True,
|
| 99 |
+
width: Optional[int] = None,
|
| 100 |
+
height: Optional[int] = None,
|
| 101 |
+
) -> "Align":
|
| 102 |
+
"""Align a renderable to the center."""
|
| 103 |
+
return cls(
|
| 104 |
+
renderable,
|
| 105 |
+
"center",
|
| 106 |
+
style=style,
|
| 107 |
+
vertical=vertical,
|
| 108 |
+
pad=pad,
|
| 109 |
+
width=width,
|
| 110 |
+
height=height,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
@classmethod
|
| 114 |
+
def right(
|
| 115 |
+
cls,
|
| 116 |
+
renderable: "RenderableType",
|
| 117 |
+
style: Optional[StyleType] = None,
|
| 118 |
+
*,
|
| 119 |
+
vertical: Optional[VerticalAlignMethod] = None,
|
| 120 |
+
pad: bool = True,
|
| 121 |
+
width: Optional[int] = None,
|
| 122 |
+
height: Optional[int] = None,
|
| 123 |
+
) -> "Align":
|
| 124 |
+
"""Align a renderable to the right."""
|
| 125 |
+
return cls(
|
| 126 |
+
renderable,
|
| 127 |
+
"right",
|
| 128 |
+
style=style,
|
| 129 |
+
vertical=vertical,
|
| 130 |
+
pad=pad,
|
| 131 |
+
width=width,
|
| 132 |
+
height=height,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
def __rich_console__(
|
| 136 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 137 |
+
) -> "RenderResult":
|
| 138 |
+
align = self.align
|
| 139 |
+
width = console.measure(self.renderable, options=options).maximum
|
| 140 |
+
rendered = console.render(
|
| 141 |
+
Constrain(
|
| 142 |
+
self.renderable, width if self.width is None else min(width, self.width)
|
| 143 |
+
),
|
| 144 |
+
options.update(height=None),
|
| 145 |
+
)
|
| 146 |
+
lines = list(Segment.split_lines(rendered))
|
| 147 |
+
width, height = Segment.get_shape(lines)
|
| 148 |
+
lines = Segment.set_shape(lines, width, height)
|
| 149 |
+
new_line = Segment.line()
|
| 150 |
+
excess_space = options.max_width - width
|
| 151 |
+
style = console.get_style(self.style) if self.style is not None else None
|
| 152 |
+
|
| 153 |
+
def generate_segments() -> Iterable[Segment]:
|
| 154 |
+
if excess_space <= 0:
|
| 155 |
+
# Exact fit
|
| 156 |
+
for line in lines:
|
| 157 |
+
yield from line
|
| 158 |
+
yield new_line
|
| 159 |
+
|
| 160 |
+
elif align == "left":
|
| 161 |
+
# Pad on the right
|
| 162 |
+
pad = Segment(" " * excess_space, style) if self.pad else None
|
| 163 |
+
for line in lines:
|
| 164 |
+
yield from line
|
| 165 |
+
if pad:
|
| 166 |
+
yield pad
|
| 167 |
+
yield new_line
|
| 168 |
+
|
| 169 |
+
elif align == "center":
|
| 170 |
+
# Pad left and right
|
| 171 |
+
left = excess_space // 2
|
| 172 |
+
pad = Segment(" " * left, style)
|
| 173 |
+
pad_right = (
|
| 174 |
+
Segment(" " * (excess_space - left), style) if self.pad else None
|
| 175 |
+
)
|
| 176 |
+
for line in lines:
|
| 177 |
+
if left:
|
| 178 |
+
yield pad
|
| 179 |
+
yield from line
|
| 180 |
+
if pad_right:
|
| 181 |
+
yield pad_right
|
| 182 |
+
yield new_line
|
| 183 |
+
|
| 184 |
+
elif align == "right":
|
| 185 |
+
# Padding on left
|
| 186 |
+
pad = Segment(" " * excess_space, style)
|
| 187 |
+
for line in lines:
|
| 188 |
+
yield pad
|
| 189 |
+
yield from line
|
| 190 |
+
yield new_line
|
| 191 |
+
|
| 192 |
+
blank_line = (
|
| 193 |
+
Segment(f"{' ' * (self.width or options.max_width)}\n", style)
|
| 194 |
+
if self.pad
|
| 195 |
+
else Segment("\n")
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
def blank_lines(count: int) -> Iterable[Segment]:
|
| 199 |
+
if count > 0:
|
| 200 |
+
for _ in range(count):
|
| 201 |
+
yield blank_line
|
| 202 |
+
|
| 203 |
+
vertical_height = self.height or options.height
|
| 204 |
+
iter_segments: Iterable[Segment]
|
| 205 |
+
if self.vertical and vertical_height is not None:
|
| 206 |
+
if self.vertical == "top":
|
| 207 |
+
bottom_space = vertical_height - height
|
| 208 |
+
iter_segments = chain(generate_segments(), blank_lines(bottom_space))
|
| 209 |
+
elif self.vertical == "middle":
|
| 210 |
+
top_space = (vertical_height - height) // 2
|
| 211 |
+
bottom_space = vertical_height - top_space - height
|
| 212 |
+
iter_segments = chain(
|
| 213 |
+
blank_lines(top_space),
|
| 214 |
+
generate_segments(),
|
| 215 |
+
blank_lines(bottom_space),
|
| 216 |
+
)
|
| 217 |
+
else: # self.vertical == "bottom":
|
| 218 |
+
top_space = vertical_height - height
|
| 219 |
+
iter_segments = chain(blank_lines(top_space), generate_segments())
|
| 220 |
+
else:
|
| 221 |
+
iter_segments = generate_segments()
|
| 222 |
+
if self.style:
|
| 223 |
+
style = console.get_style(self.style)
|
| 224 |
+
iter_segments = Segment.apply_style(iter_segments, style)
|
| 225 |
+
yield from iter_segments
|
| 226 |
+
|
| 227 |
+
def __rich_measure__(
|
| 228 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 229 |
+
) -> Measurement:
|
| 230 |
+
measurement = Measurement.get(console, options, self.renderable)
|
| 231 |
+
return measurement
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
class VerticalCenter(JupyterMixin):
|
| 235 |
+
"""Vertically aligns a renderable.
|
| 236 |
+
|
| 237 |
+
Warn:
|
| 238 |
+
This class is deprecated and may be removed in a future version. Use Align class with
|
| 239 |
+
`vertical="middle"`.
|
| 240 |
+
|
| 241 |
+
Args:
|
| 242 |
+
renderable (RenderableType): A renderable object.
|
| 243 |
+
style (StyleType, optional): An optional style to apply to the background. Defaults to None.
|
| 244 |
+
"""
|
| 245 |
+
|
| 246 |
+
def __init__(
|
| 247 |
+
self,
|
| 248 |
+
renderable: "RenderableType",
|
| 249 |
+
style: Optional[StyleType] = None,
|
| 250 |
+
) -> None:
|
| 251 |
+
self.renderable = renderable
|
| 252 |
+
self.style = style
|
| 253 |
+
|
| 254 |
+
def __repr__(self) -> str:
|
| 255 |
+
return f"VerticalCenter({self.renderable!r})"
|
| 256 |
+
|
| 257 |
+
def __rich_console__(
|
| 258 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 259 |
+
) -> "RenderResult":
|
| 260 |
+
style = console.get_style(self.style) if self.style is not None else None
|
| 261 |
+
lines = console.render_lines(
|
| 262 |
+
self.renderable, options.update(height=None), pad=False
|
| 263 |
+
)
|
| 264 |
+
width, _height = Segment.get_shape(lines)
|
| 265 |
+
new_line = Segment.line()
|
| 266 |
+
height = options.height or options.size.height
|
| 267 |
+
top_space = (height - len(lines)) // 2
|
| 268 |
+
bottom_space = height - top_space - len(lines)
|
| 269 |
+
blank_line = Segment(f"{' ' * width}", style)
|
| 270 |
+
|
| 271 |
+
def blank_lines(count: int) -> Iterable[Segment]:
|
| 272 |
+
for _ in range(count):
|
| 273 |
+
yield blank_line
|
| 274 |
+
yield new_line
|
| 275 |
+
|
| 276 |
+
if top_space > 0:
|
| 277 |
+
yield from blank_lines(top_space)
|
| 278 |
+
for line in lines:
|
| 279 |
+
yield from line
|
| 280 |
+
yield new_line
|
| 281 |
+
if bottom_space > 0:
|
| 282 |
+
yield from blank_lines(bottom_space)
|
| 283 |
+
|
| 284 |
+
def __rich_measure__(
|
| 285 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 286 |
+
) -> Measurement:
|
| 287 |
+
measurement = Measurement.get(console, options, self.renderable)
|
| 288 |
+
return measurement
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
if __name__ == "__main__": # pragma: no cover
|
| 292 |
+
from pip._vendor.rich.console import Console, Group
|
| 293 |
+
from pip._vendor.rich.highlighter import ReprHighlighter
|
| 294 |
+
from pip._vendor.rich.panel import Panel
|
| 295 |
+
|
| 296 |
+
highlighter = ReprHighlighter()
|
| 297 |
+
console = Console()
|
| 298 |
+
|
| 299 |
+
panel = Panel(
|
| 300 |
+
Group(
|
| 301 |
+
Align.left(highlighter("align='left'")),
|
| 302 |
+
Align.center(highlighter("align='center'")),
|
| 303 |
+
Align.right(highlighter("align='right'")),
|
| 304 |
+
),
|
| 305 |
+
width=60,
|
| 306 |
+
style="on dark_blue",
|
| 307 |
+
title="Align",
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
console.print(
|
| 311 |
+
Align.center(panel, vertical="middle", style="on red", height=console.height)
|
| 312 |
+
)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/ansi.py
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import sys
|
| 3 |
+
from contextlib import suppress
|
| 4 |
+
from typing import Iterable, NamedTuple, Optional
|
| 5 |
+
|
| 6 |
+
from .color import Color
|
| 7 |
+
from .style import Style
|
| 8 |
+
from .text import Text
|
| 9 |
+
|
| 10 |
+
re_ansi = re.compile(
|
| 11 |
+
r"""
|
| 12 |
+
(?:\x1b[0-?])|
|
| 13 |
+
(?:\x1b\](.*?)\x1b\\)|
|
| 14 |
+
(?:\x1b([(@-Z\\-_]|\[[0-?]*[ -/]*[@-~]))
|
| 15 |
+
""",
|
| 16 |
+
re.VERBOSE,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class _AnsiToken(NamedTuple):
|
| 21 |
+
"""Result of ansi tokenized string."""
|
| 22 |
+
|
| 23 |
+
plain: str = ""
|
| 24 |
+
sgr: Optional[str] = ""
|
| 25 |
+
osc: Optional[str] = ""
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _ansi_tokenize(ansi_text: str) -> Iterable[_AnsiToken]:
|
| 29 |
+
"""Tokenize a string in to plain text and ANSI codes.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
ansi_text (str): A String containing ANSI codes.
|
| 33 |
+
|
| 34 |
+
Yields:
|
| 35 |
+
AnsiToken: A named tuple of (plain, sgr, osc)
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
position = 0
|
| 39 |
+
sgr: Optional[str]
|
| 40 |
+
osc: Optional[str]
|
| 41 |
+
for match in re_ansi.finditer(ansi_text):
|
| 42 |
+
start, end = match.span(0)
|
| 43 |
+
osc, sgr = match.groups()
|
| 44 |
+
if start > position:
|
| 45 |
+
yield _AnsiToken(ansi_text[position:start])
|
| 46 |
+
if sgr:
|
| 47 |
+
if sgr == "(":
|
| 48 |
+
position = end + 1
|
| 49 |
+
continue
|
| 50 |
+
if sgr.endswith("m"):
|
| 51 |
+
yield _AnsiToken("", sgr[1:-1], osc)
|
| 52 |
+
else:
|
| 53 |
+
yield _AnsiToken("", sgr, osc)
|
| 54 |
+
position = end
|
| 55 |
+
if position < len(ansi_text):
|
| 56 |
+
yield _AnsiToken(ansi_text[position:])
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
SGR_STYLE_MAP = {
|
| 60 |
+
1: "bold",
|
| 61 |
+
2: "dim",
|
| 62 |
+
3: "italic",
|
| 63 |
+
4: "underline",
|
| 64 |
+
5: "blink",
|
| 65 |
+
6: "blink2",
|
| 66 |
+
7: "reverse",
|
| 67 |
+
8: "conceal",
|
| 68 |
+
9: "strike",
|
| 69 |
+
21: "underline2",
|
| 70 |
+
22: "not dim not bold",
|
| 71 |
+
23: "not italic",
|
| 72 |
+
24: "not underline",
|
| 73 |
+
25: "not blink",
|
| 74 |
+
26: "not blink2",
|
| 75 |
+
27: "not reverse",
|
| 76 |
+
28: "not conceal",
|
| 77 |
+
29: "not strike",
|
| 78 |
+
30: "color(0)",
|
| 79 |
+
31: "color(1)",
|
| 80 |
+
32: "color(2)",
|
| 81 |
+
33: "color(3)",
|
| 82 |
+
34: "color(4)",
|
| 83 |
+
35: "color(5)",
|
| 84 |
+
36: "color(6)",
|
| 85 |
+
37: "color(7)",
|
| 86 |
+
39: "default",
|
| 87 |
+
40: "on color(0)",
|
| 88 |
+
41: "on color(1)",
|
| 89 |
+
42: "on color(2)",
|
| 90 |
+
43: "on color(3)",
|
| 91 |
+
44: "on color(4)",
|
| 92 |
+
45: "on color(5)",
|
| 93 |
+
46: "on color(6)",
|
| 94 |
+
47: "on color(7)",
|
| 95 |
+
49: "on default",
|
| 96 |
+
51: "frame",
|
| 97 |
+
52: "encircle",
|
| 98 |
+
53: "overline",
|
| 99 |
+
54: "not frame not encircle",
|
| 100 |
+
55: "not overline",
|
| 101 |
+
90: "color(8)",
|
| 102 |
+
91: "color(9)",
|
| 103 |
+
92: "color(10)",
|
| 104 |
+
93: "color(11)",
|
| 105 |
+
94: "color(12)",
|
| 106 |
+
95: "color(13)",
|
| 107 |
+
96: "color(14)",
|
| 108 |
+
97: "color(15)",
|
| 109 |
+
100: "on color(8)",
|
| 110 |
+
101: "on color(9)",
|
| 111 |
+
102: "on color(10)",
|
| 112 |
+
103: "on color(11)",
|
| 113 |
+
104: "on color(12)",
|
| 114 |
+
105: "on color(13)",
|
| 115 |
+
106: "on color(14)",
|
| 116 |
+
107: "on color(15)",
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
class AnsiDecoder:
|
| 121 |
+
"""Translate ANSI code in to styled Text."""
|
| 122 |
+
|
| 123 |
+
def __init__(self) -> None:
|
| 124 |
+
self.style = Style.null()
|
| 125 |
+
|
| 126 |
+
def decode(self, terminal_text: str) -> Iterable[Text]:
|
| 127 |
+
"""Decode ANSI codes in an iterable of lines.
|
| 128 |
+
|
| 129 |
+
Args:
|
| 130 |
+
lines (Iterable[str]): An iterable of lines of terminal output.
|
| 131 |
+
|
| 132 |
+
Yields:
|
| 133 |
+
Text: Marked up Text.
|
| 134 |
+
"""
|
| 135 |
+
for line in terminal_text.splitlines():
|
| 136 |
+
yield self.decode_line(line)
|
| 137 |
+
|
| 138 |
+
def decode_line(self, line: str) -> Text:
|
| 139 |
+
"""Decode a line containing ansi codes.
|
| 140 |
+
|
| 141 |
+
Args:
|
| 142 |
+
line (str): A line of terminal output.
|
| 143 |
+
|
| 144 |
+
Returns:
|
| 145 |
+
Text: A Text instance marked up according to ansi codes.
|
| 146 |
+
"""
|
| 147 |
+
from_ansi = Color.from_ansi
|
| 148 |
+
from_rgb = Color.from_rgb
|
| 149 |
+
_Style = Style
|
| 150 |
+
text = Text()
|
| 151 |
+
append = text.append
|
| 152 |
+
line = line.rsplit("\r", 1)[-1]
|
| 153 |
+
for plain_text, sgr, osc in _ansi_tokenize(line):
|
| 154 |
+
if plain_text:
|
| 155 |
+
append(plain_text, self.style or None)
|
| 156 |
+
elif osc is not None:
|
| 157 |
+
if osc.startswith("8;"):
|
| 158 |
+
_params, semicolon, link = osc[2:].partition(";")
|
| 159 |
+
if semicolon:
|
| 160 |
+
self.style = self.style.update_link(link or None)
|
| 161 |
+
elif sgr is not None:
|
| 162 |
+
# Translate in to semi-colon separated codes
|
| 163 |
+
# Ignore invalid codes, because we want to be lenient
|
| 164 |
+
codes = [
|
| 165 |
+
min(255, int(_code) if _code else 0)
|
| 166 |
+
for _code in sgr.split(";")
|
| 167 |
+
if _code.isdigit() or _code == ""
|
| 168 |
+
]
|
| 169 |
+
iter_codes = iter(codes)
|
| 170 |
+
for code in iter_codes:
|
| 171 |
+
if code == 0:
|
| 172 |
+
# reset
|
| 173 |
+
self.style = _Style.null()
|
| 174 |
+
elif code in SGR_STYLE_MAP:
|
| 175 |
+
# styles
|
| 176 |
+
self.style += _Style.parse(SGR_STYLE_MAP[code])
|
| 177 |
+
elif code == 38:
|
| 178 |
+
# Foreground
|
| 179 |
+
with suppress(StopIteration):
|
| 180 |
+
color_type = next(iter_codes)
|
| 181 |
+
if color_type == 5:
|
| 182 |
+
self.style += _Style.from_color(
|
| 183 |
+
from_ansi(next(iter_codes))
|
| 184 |
+
)
|
| 185 |
+
elif color_type == 2:
|
| 186 |
+
self.style += _Style.from_color(
|
| 187 |
+
from_rgb(
|
| 188 |
+
next(iter_codes),
|
| 189 |
+
next(iter_codes),
|
| 190 |
+
next(iter_codes),
|
| 191 |
+
)
|
| 192 |
+
)
|
| 193 |
+
elif code == 48:
|
| 194 |
+
# Background
|
| 195 |
+
with suppress(StopIteration):
|
| 196 |
+
color_type = next(iter_codes)
|
| 197 |
+
if color_type == 5:
|
| 198 |
+
self.style += _Style.from_color(
|
| 199 |
+
None, from_ansi(next(iter_codes))
|
| 200 |
+
)
|
| 201 |
+
elif color_type == 2:
|
| 202 |
+
self.style += _Style.from_color(
|
| 203 |
+
None,
|
| 204 |
+
from_rgb(
|
| 205 |
+
next(iter_codes),
|
| 206 |
+
next(iter_codes),
|
| 207 |
+
next(iter_codes),
|
| 208 |
+
),
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
return text
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
if sys.platform != "win32" and __name__ == "__main__": # pragma: no cover
|
| 215 |
+
import io
|
| 216 |
+
import os
|
| 217 |
+
import pty
|
| 218 |
+
import sys
|
| 219 |
+
|
| 220 |
+
decoder = AnsiDecoder()
|
| 221 |
+
|
| 222 |
+
stdout = io.BytesIO()
|
| 223 |
+
|
| 224 |
+
def read(fd: int) -> bytes:
|
| 225 |
+
data = os.read(fd, 1024)
|
| 226 |
+
stdout.write(data)
|
| 227 |
+
return data
|
| 228 |
+
|
| 229 |
+
pty.spawn(sys.argv[1:], read)
|
| 230 |
+
|
| 231 |
+
from .console import Console
|
| 232 |
+
|
| 233 |
+
console = Console(record=True)
|
| 234 |
+
|
| 235 |
+
stdout_result = stdout.getvalue().decode("utf-8")
|
| 236 |
+
print(stdout_result)
|
| 237 |
+
|
| 238 |
+
for line in decoder.decode(stdout_result):
|
| 239 |
+
console.print(line)
|
| 240 |
+
|
| 241 |
+
console.save_html("stdout.html")
|
env/lib/python3.13/site-packages/pip/_vendor/rich/bar.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional, Union
|
| 2 |
+
|
| 3 |
+
from .color import Color
|
| 4 |
+
from .console import Console, ConsoleOptions, RenderResult
|
| 5 |
+
from .jupyter import JupyterMixin
|
| 6 |
+
from .measure import Measurement
|
| 7 |
+
from .segment import Segment
|
| 8 |
+
from .style import Style
|
| 9 |
+
|
| 10 |
+
# There are left-aligned characters for 1/8 to 7/8, but
|
| 11 |
+
# the right-aligned characters exist only for 1/8 and 4/8.
|
| 12 |
+
BEGIN_BLOCK_ELEMENTS = ["█", "█", "█", "▐", "▐", "▐", "▕", "▕"]
|
| 13 |
+
END_BLOCK_ELEMENTS = [" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"]
|
| 14 |
+
FULL_BLOCK = "█"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Bar(JupyterMixin):
|
| 18 |
+
"""Renders a solid block bar.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
size (float): Value for the end of the bar.
|
| 22 |
+
begin (float): Begin point (between 0 and size, inclusive).
|
| 23 |
+
end (float): End point (between 0 and size, inclusive).
|
| 24 |
+
width (int, optional): Width of the bar, or ``None`` for maximum width. Defaults to None.
|
| 25 |
+
color (Union[Color, str], optional): Color of the bar. Defaults to "default".
|
| 26 |
+
bgcolor (Union[Color, str], optional): Color of bar background. Defaults to "default".
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
def __init__(
|
| 30 |
+
self,
|
| 31 |
+
size: float,
|
| 32 |
+
begin: float,
|
| 33 |
+
end: float,
|
| 34 |
+
*,
|
| 35 |
+
width: Optional[int] = None,
|
| 36 |
+
color: Union[Color, str] = "default",
|
| 37 |
+
bgcolor: Union[Color, str] = "default",
|
| 38 |
+
):
|
| 39 |
+
self.size = size
|
| 40 |
+
self.begin = max(begin, 0)
|
| 41 |
+
self.end = min(end, size)
|
| 42 |
+
self.width = width
|
| 43 |
+
self.style = Style(color=color, bgcolor=bgcolor)
|
| 44 |
+
|
| 45 |
+
def __repr__(self) -> str:
|
| 46 |
+
return f"Bar({self.size}, {self.begin}, {self.end})"
|
| 47 |
+
|
| 48 |
+
def __rich_console__(
|
| 49 |
+
self, console: Console, options: ConsoleOptions
|
| 50 |
+
) -> RenderResult:
|
| 51 |
+
width = min(
|
| 52 |
+
self.width if self.width is not None else options.max_width,
|
| 53 |
+
options.max_width,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if self.begin >= self.end:
|
| 57 |
+
yield Segment(" " * width, self.style)
|
| 58 |
+
yield Segment.line()
|
| 59 |
+
return
|
| 60 |
+
|
| 61 |
+
prefix_complete_eights = int(width * 8 * self.begin / self.size)
|
| 62 |
+
prefix_bar_count = prefix_complete_eights // 8
|
| 63 |
+
prefix_eights_count = prefix_complete_eights % 8
|
| 64 |
+
|
| 65 |
+
body_complete_eights = int(width * 8 * self.end / self.size)
|
| 66 |
+
body_bar_count = body_complete_eights // 8
|
| 67 |
+
body_eights_count = body_complete_eights % 8
|
| 68 |
+
|
| 69 |
+
# When start and end fall into the same cell, we ideally should render
|
| 70 |
+
# a symbol that's "center-aligned", but there is no good symbol in Unicode.
|
| 71 |
+
# In this case, we fall back to right-aligned block symbol for simplicity.
|
| 72 |
+
|
| 73 |
+
prefix = " " * prefix_bar_count
|
| 74 |
+
if prefix_eights_count:
|
| 75 |
+
prefix += BEGIN_BLOCK_ELEMENTS[prefix_eights_count]
|
| 76 |
+
|
| 77 |
+
body = FULL_BLOCK * body_bar_count
|
| 78 |
+
if body_eights_count:
|
| 79 |
+
body += END_BLOCK_ELEMENTS[body_eights_count]
|
| 80 |
+
|
| 81 |
+
suffix = " " * (width - len(body))
|
| 82 |
+
|
| 83 |
+
yield Segment(prefix + body[len(prefix) :] + suffix, self.style)
|
| 84 |
+
yield Segment.line()
|
| 85 |
+
|
| 86 |
+
def __rich_measure__(
|
| 87 |
+
self, console: Console, options: ConsoleOptions
|
| 88 |
+
) -> Measurement:
|
| 89 |
+
return (
|
| 90 |
+
Measurement(self.width, self.width)
|
| 91 |
+
if self.width is not None
|
| 92 |
+
else Measurement(4, options.max_width)
|
| 93 |
+
)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/color.py
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import sys
|
| 3 |
+
from colorsys import rgb_to_hls
|
| 4 |
+
from enum import IntEnum
|
| 5 |
+
from functools import lru_cache
|
| 6 |
+
from typing import TYPE_CHECKING, NamedTuple, Optional, Tuple
|
| 7 |
+
|
| 8 |
+
from ._palettes import EIGHT_BIT_PALETTE, STANDARD_PALETTE, WINDOWS_PALETTE
|
| 9 |
+
from .color_triplet import ColorTriplet
|
| 10 |
+
from .repr import Result, rich_repr
|
| 11 |
+
from .terminal_theme import DEFAULT_TERMINAL_THEME
|
| 12 |
+
|
| 13 |
+
if TYPE_CHECKING: # pragma: no cover
|
| 14 |
+
from .terminal_theme import TerminalTheme
|
| 15 |
+
from .text import Text
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
WINDOWS = sys.platform == "win32"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class ColorSystem(IntEnum):
|
| 22 |
+
"""One of the 3 color system supported by terminals."""
|
| 23 |
+
|
| 24 |
+
STANDARD = 1
|
| 25 |
+
EIGHT_BIT = 2
|
| 26 |
+
TRUECOLOR = 3
|
| 27 |
+
WINDOWS = 4
|
| 28 |
+
|
| 29 |
+
def __repr__(self) -> str:
|
| 30 |
+
return f"ColorSystem.{self.name}"
|
| 31 |
+
|
| 32 |
+
def __str__(self) -> str:
|
| 33 |
+
return repr(self)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class ColorType(IntEnum):
|
| 37 |
+
"""Type of color stored in Color class."""
|
| 38 |
+
|
| 39 |
+
DEFAULT = 0
|
| 40 |
+
STANDARD = 1
|
| 41 |
+
EIGHT_BIT = 2
|
| 42 |
+
TRUECOLOR = 3
|
| 43 |
+
WINDOWS = 4
|
| 44 |
+
|
| 45 |
+
def __repr__(self) -> str:
|
| 46 |
+
return f"ColorType.{self.name}"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
ANSI_COLOR_NAMES = {
|
| 50 |
+
"black": 0,
|
| 51 |
+
"red": 1,
|
| 52 |
+
"green": 2,
|
| 53 |
+
"yellow": 3,
|
| 54 |
+
"blue": 4,
|
| 55 |
+
"magenta": 5,
|
| 56 |
+
"cyan": 6,
|
| 57 |
+
"white": 7,
|
| 58 |
+
"bright_black": 8,
|
| 59 |
+
"bright_red": 9,
|
| 60 |
+
"bright_green": 10,
|
| 61 |
+
"bright_yellow": 11,
|
| 62 |
+
"bright_blue": 12,
|
| 63 |
+
"bright_magenta": 13,
|
| 64 |
+
"bright_cyan": 14,
|
| 65 |
+
"bright_white": 15,
|
| 66 |
+
"grey0": 16,
|
| 67 |
+
"gray0": 16,
|
| 68 |
+
"navy_blue": 17,
|
| 69 |
+
"dark_blue": 18,
|
| 70 |
+
"blue3": 20,
|
| 71 |
+
"blue1": 21,
|
| 72 |
+
"dark_green": 22,
|
| 73 |
+
"deep_sky_blue4": 25,
|
| 74 |
+
"dodger_blue3": 26,
|
| 75 |
+
"dodger_blue2": 27,
|
| 76 |
+
"green4": 28,
|
| 77 |
+
"spring_green4": 29,
|
| 78 |
+
"turquoise4": 30,
|
| 79 |
+
"deep_sky_blue3": 32,
|
| 80 |
+
"dodger_blue1": 33,
|
| 81 |
+
"green3": 40,
|
| 82 |
+
"spring_green3": 41,
|
| 83 |
+
"dark_cyan": 36,
|
| 84 |
+
"light_sea_green": 37,
|
| 85 |
+
"deep_sky_blue2": 38,
|
| 86 |
+
"deep_sky_blue1": 39,
|
| 87 |
+
"spring_green2": 47,
|
| 88 |
+
"cyan3": 43,
|
| 89 |
+
"dark_turquoise": 44,
|
| 90 |
+
"turquoise2": 45,
|
| 91 |
+
"green1": 46,
|
| 92 |
+
"spring_green1": 48,
|
| 93 |
+
"medium_spring_green": 49,
|
| 94 |
+
"cyan2": 50,
|
| 95 |
+
"cyan1": 51,
|
| 96 |
+
"dark_red": 88,
|
| 97 |
+
"deep_pink4": 125,
|
| 98 |
+
"purple4": 55,
|
| 99 |
+
"purple3": 56,
|
| 100 |
+
"blue_violet": 57,
|
| 101 |
+
"orange4": 94,
|
| 102 |
+
"grey37": 59,
|
| 103 |
+
"gray37": 59,
|
| 104 |
+
"medium_purple4": 60,
|
| 105 |
+
"slate_blue3": 62,
|
| 106 |
+
"royal_blue1": 63,
|
| 107 |
+
"chartreuse4": 64,
|
| 108 |
+
"dark_sea_green4": 71,
|
| 109 |
+
"pale_turquoise4": 66,
|
| 110 |
+
"steel_blue": 67,
|
| 111 |
+
"steel_blue3": 68,
|
| 112 |
+
"cornflower_blue": 69,
|
| 113 |
+
"chartreuse3": 76,
|
| 114 |
+
"cadet_blue": 73,
|
| 115 |
+
"sky_blue3": 74,
|
| 116 |
+
"steel_blue1": 81,
|
| 117 |
+
"pale_green3": 114,
|
| 118 |
+
"sea_green3": 78,
|
| 119 |
+
"aquamarine3": 79,
|
| 120 |
+
"medium_turquoise": 80,
|
| 121 |
+
"chartreuse2": 112,
|
| 122 |
+
"sea_green2": 83,
|
| 123 |
+
"sea_green1": 85,
|
| 124 |
+
"aquamarine1": 122,
|
| 125 |
+
"dark_slate_gray2": 87,
|
| 126 |
+
"dark_magenta": 91,
|
| 127 |
+
"dark_violet": 128,
|
| 128 |
+
"purple": 129,
|
| 129 |
+
"light_pink4": 95,
|
| 130 |
+
"plum4": 96,
|
| 131 |
+
"medium_purple3": 98,
|
| 132 |
+
"slate_blue1": 99,
|
| 133 |
+
"yellow4": 106,
|
| 134 |
+
"wheat4": 101,
|
| 135 |
+
"grey53": 102,
|
| 136 |
+
"gray53": 102,
|
| 137 |
+
"light_slate_grey": 103,
|
| 138 |
+
"light_slate_gray": 103,
|
| 139 |
+
"medium_purple": 104,
|
| 140 |
+
"light_slate_blue": 105,
|
| 141 |
+
"dark_olive_green3": 149,
|
| 142 |
+
"dark_sea_green": 108,
|
| 143 |
+
"light_sky_blue3": 110,
|
| 144 |
+
"sky_blue2": 111,
|
| 145 |
+
"dark_sea_green3": 150,
|
| 146 |
+
"dark_slate_gray3": 116,
|
| 147 |
+
"sky_blue1": 117,
|
| 148 |
+
"chartreuse1": 118,
|
| 149 |
+
"light_green": 120,
|
| 150 |
+
"pale_green1": 156,
|
| 151 |
+
"dark_slate_gray1": 123,
|
| 152 |
+
"red3": 160,
|
| 153 |
+
"medium_violet_red": 126,
|
| 154 |
+
"magenta3": 164,
|
| 155 |
+
"dark_orange3": 166,
|
| 156 |
+
"indian_red": 167,
|
| 157 |
+
"hot_pink3": 168,
|
| 158 |
+
"medium_orchid3": 133,
|
| 159 |
+
"medium_orchid": 134,
|
| 160 |
+
"medium_purple2": 140,
|
| 161 |
+
"dark_goldenrod": 136,
|
| 162 |
+
"light_salmon3": 173,
|
| 163 |
+
"rosy_brown": 138,
|
| 164 |
+
"grey63": 139,
|
| 165 |
+
"gray63": 139,
|
| 166 |
+
"medium_purple1": 141,
|
| 167 |
+
"gold3": 178,
|
| 168 |
+
"dark_khaki": 143,
|
| 169 |
+
"navajo_white3": 144,
|
| 170 |
+
"grey69": 145,
|
| 171 |
+
"gray69": 145,
|
| 172 |
+
"light_steel_blue3": 146,
|
| 173 |
+
"light_steel_blue": 147,
|
| 174 |
+
"yellow3": 184,
|
| 175 |
+
"dark_sea_green2": 157,
|
| 176 |
+
"light_cyan3": 152,
|
| 177 |
+
"light_sky_blue1": 153,
|
| 178 |
+
"green_yellow": 154,
|
| 179 |
+
"dark_olive_green2": 155,
|
| 180 |
+
"dark_sea_green1": 193,
|
| 181 |
+
"pale_turquoise1": 159,
|
| 182 |
+
"deep_pink3": 162,
|
| 183 |
+
"magenta2": 200,
|
| 184 |
+
"hot_pink2": 169,
|
| 185 |
+
"orchid": 170,
|
| 186 |
+
"medium_orchid1": 207,
|
| 187 |
+
"orange3": 172,
|
| 188 |
+
"light_pink3": 174,
|
| 189 |
+
"pink3": 175,
|
| 190 |
+
"plum3": 176,
|
| 191 |
+
"violet": 177,
|
| 192 |
+
"light_goldenrod3": 179,
|
| 193 |
+
"tan": 180,
|
| 194 |
+
"misty_rose3": 181,
|
| 195 |
+
"thistle3": 182,
|
| 196 |
+
"plum2": 183,
|
| 197 |
+
"khaki3": 185,
|
| 198 |
+
"light_goldenrod2": 222,
|
| 199 |
+
"light_yellow3": 187,
|
| 200 |
+
"grey84": 188,
|
| 201 |
+
"gray84": 188,
|
| 202 |
+
"light_steel_blue1": 189,
|
| 203 |
+
"yellow2": 190,
|
| 204 |
+
"dark_olive_green1": 192,
|
| 205 |
+
"honeydew2": 194,
|
| 206 |
+
"light_cyan1": 195,
|
| 207 |
+
"red1": 196,
|
| 208 |
+
"deep_pink2": 197,
|
| 209 |
+
"deep_pink1": 199,
|
| 210 |
+
"magenta1": 201,
|
| 211 |
+
"orange_red1": 202,
|
| 212 |
+
"indian_red1": 204,
|
| 213 |
+
"hot_pink": 206,
|
| 214 |
+
"dark_orange": 208,
|
| 215 |
+
"salmon1": 209,
|
| 216 |
+
"light_coral": 210,
|
| 217 |
+
"pale_violet_red1": 211,
|
| 218 |
+
"orchid2": 212,
|
| 219 |
+
"orchid1": 213,
|
| 220 |
+
"orange1": 214,
|
| 221 |
+
"sandy_brown": 215,
|
| 222 |
+
"light_salmon1": 216,
|
| 223 |
+
"light_pink1": 217,
|
| 224 |
+
"pink1": 218,
|
| 225 |
+
"plum1": 219,
|
| 226 |
+
"gold1": 220,
|
| 227 |
+
"navajo_white1": 223,
|
| 228 |
+
"misty_rose1": 224,
|
| 229 |
+
"thistle1": 225,
|
| 230 |
+
"yellow1": 226,
|
| 231 |
+
"light_goldenrod1": 227,
|
| 232 |
+
"khaki1": 228,
|
| 233 |
+
"wheat1": 229,
|
| 234 |
+
"cornsilk1": 230,
|
| 235 |
+
"grey100": 231,
|
| 236 |
+
"gray100": 231,
|
| 237 |
+
"grey3": 232,
|
| 238 |
+
"gray3": 232,
|
| 239 |
+
"grey7": 233,
|
| 240 |
+
"gray7": 233,
|
| 241 |
+
"grey11": 234,
|
| 242 |
+
"gray11": 234,
|
| 243 |
+
"grey15": 235,
|
| 244 |
+
"gray15": 235,
|
| 245 |
+
"grey19": 236,
|
| 246 |
+
"gray19": 236,
|
| 247 |
+
"grey23": 237,
|
| 248 |
+
"gray23": 237,
|
| 249 |
+
"grey27": 238,
|
| 250 |
+
"gray27": 238,
|
| 251 |
+
"grey30": 239,
|
| 252 |
+
"gray30": 239,
|
| 253 |
+
"grey35": 240,
|
| 254 |
+
"gray35": 240,
|
| 255 |
+
"grey39": 241,
|
| 256 |
+
"gray39": 241,
|
| 257 |
+
"grey42": 242,
|
| 258 |
+
"gray42": 242,
|
| 259 |
+
"grey46": 243,
|
| 260 |
+
"gray46": 243,
|
| 261 |
+
"grey50": 244,
|
| 262 |
+
"gray50": 244,
|
| 263 |
+
"grey54": 245,
|
| 264 |
+
"gray54": 245,
|
| 265 |
+
"grey58": 246,
|
| 266 |
+
"gray58": 246,
|
| 267 |
+
"grey62": 247,
|
| 268 |
+
"gray62": 247,
|
| 269 |
+
"grey66": 248,
|
| 270 |
+
"gray66": 248,
|
| 271 |
+
"grey70": 249,
|
| 272 |
+
"gray70": 249,
|
| 273 |
+
"grey74": 250,
|
| 274 |
+
"gray74": 250,
|
| 275 |
+
"grey78": 251,
|
| 276 |
+
"gray78": 251,
|
| 277 |
+
"grey82": 252,
|
| 278 |
+
"gray82": 252,
|
| 279 |
+
"grey85": 253,
|
| 280 |
+
"gray85": 253,
|
| 281 |
+
"grey89": 254,
|
| 282 |
+
"gray89": 254,
|
| 283 |
+
"grey93": 255,
|
| 284 |
+
"gray93": 255,
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
class ColorParseError(Exception):
|
| 289 |
+
"""The color could not be parsed."""
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
RE_COLOR = re.compile(
|
| 293 |
+
r"""^
|
| 294 |
+
\#([0-9a-f]{6})$|
|
| 295 |
+
color\(([0-9]{1,3})\)$|
|
| 296 |
+
rgb\(([\d\s,]+)\)$
|
| 297 |
+
""",
|
| 298 |
+
re.VERBOSE,
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
@rich_repr
|
| 303 |
+
class Color(NamedTuple):
|
| 304 |
+
"""Terminal color definition."""
|
| 305 |
+
|
| 306 |
+
name: str
|
| 307 |
+
"""The name of the color (typically the input to Color.parse)."""
|
| 308 |
+
type: ColorType
|
| 309 |
+
"""The type of the color."""
|
| 310 |
+
number: Optional[int] = None
|
| 311 |
+
"""The color number, if a standard color, or None."""
|
| 312 |
+
triplet: Optional[ColorTriplet] = None
|
| 313 |
+
"""A triplet of color components, if an RGB color."""
|
| 314 |
+
|
| 315 |
+
def __rich__(self) -> "Text":
|
| 316 |
+
"""Displays the actual color if Rich printed."""
|
| 317 |
+
from .style import Style
|
| 318 |
+
from .text import Text
|
| 319 |
+
|
| 320 |
+
return Text.assemble(
|
| 321 |
+
f"<color {self.name!r} ({self.type.name.lower()})",
|
| 322 |
+
("⬤", Style(color=self)),
|
| 323 |
+
" >",
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
def __rich_repr__(self) -> Result:
|
| 327 |
+
yield self.name
|
| 328 |
+
yield self.type
|
| 329 |
+
yield "number", self.number, None
|
| 330 |
+
yield "triplet", self.triplet, None
|
| 331 |
+
|
| 332 |
+
@property
|
| 333 |
+
def system(self) -> ColorSystem:
|
| 334 |
+
"""Get the native color system for this color."""
|
| 335 |
+
if self.type == ColorType.DEFAULT:
|
| 336 |
+
return ColorSystem.STANDARD
|
| 337 |
+
return ColorSystem(int(self.type))
|
| 338 |
+
|
| 339 |
+
@property
|
| 340 |
+
def is_system_defined(self) -> bool:
|
| 341 |
+
"""Check if the color is ultimately defined by the system."""
|
| 342 |
+
return self.system not in (ColorSystem.EIGHT_BIT, ColorSystem.TRUECOLOR)
|
| 343 |
+
|
| 344 |
+
@property
|
| 345 |
+
def is_default(self) -> bool:
|
| 346 |
+
"""Check if the color is a default color."""
|
| 347 |
+
return self.type == ColorType.DEFAULT
|
| 348 |
+
|
| 349 |
+
def get_truecolor(
|
| 350 |
+
self, theme: Optional["TerminalTheme"] = None, foreground: bool = True
|
| 351 |
+
) -> ColorTriplet:
|
| 352 |
+
"""Get an equivalent color triplet for this color.
|
| 353 |
+
|
| 354 |
+
Args:
|
| 355 |
+
theme (TerminalTheme, optional): Optional terminal theme, or None to use default. Defaults to None.
|
| 356 |
+
foreground (bool, optional): True for a foreground color, or False for background. Defaults to True.
|
| 357 |
+
|
| 358 |
+
Returns:
|
| 359 |
+
ColorTriplet: A color triplet containing RGB components.
|
| 360 |
+
"""
|
| 361 |
+
|
| 362 |
+
if theme is None:
|
| 363 |
+
theme = DEFAULT_TERMINAL_THEME
|
| 364 |
+
if self.type == ColorType.TRUECOLOR:
|
| 365 |
+
assert self.triplet is not None
|
| 366 |
+
return self.triplet
|
| 367 |
+
elif self.type == ColorType.EIGHT_BIT:
|
| 368 |
+
assert self.number is not None
|
| 369 |
+
return EIGHT_BIT_PALETTE[self.number]
|
| 370 |
+
elif self.type == ColorType.STANDARD:
|
| 371 |
+
assert self.number is not None
|
| 372 |
+
return theme.ansi_colors[self.number]
|
| 373 |
+
elif self.type == ColorType.WINDOWS:
|
| 374 |
+
assert self.number is not None
|
| 375 |
+
return WINDOWS_PALETTE[self.number]
|
| 376 |
+
else: # self.type == ColorType.DEFAULT:
|
| 377 |
+
assert self.number is None
|
| 378 |
+
return theme.foreground_color if foreground else theme.background_color
|
| 379 |
+
|
| 380 |
+
@classmethod
|
| 381 |
+
def from_ansi(cls, number: int) -> "Color":
|
| 382 |
+
"""Create a Color number from it's 8-bit ansi number.
|
| 383 |
+
|
| 384 |
+
Args:
|
| 385 |
+
number (int): A number between 0-255 inclusive.
|
| 386 |
+
|
| 387 |
+
Returns:
|
| 388 |
+
Color: A new Color instance.
|
| 389 |
+
"""
|
| 390 |
+
return cls(
|
| 391 |
+
name=f"color({number})",
|
| 392 |
+
type=(ColorType.STANDARD if number < 16 else ColorType.EIGHT_BIT),
|
| 393 |
+
number=number,
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
@classmethod
|
| 397 |
+
def from_triplet(cls, triplet: "ColorTriplet") -> "Color":
|
| 398 |
+
"""Create a truecolor RGB color from a triplet of values.
|
| 399 |
+
|
| 400 |
+
Args:
|
| 401 |
+
triplet (ColorTriplet): A color triplet containing red, green and blue components.
|
| 402 |
+
|
| 403 |
+
Returns:
|
| 404 |
+
Color: A new color object.
|
| 405 |
+
"""
|
| 406 |
+
return cls(name=triplet.hex, type=ColorType.TRUECOLOR, triplet=triplet)
|
| 407 |
+
|
| 408 |
+
@classmethod
|
| 409 |
+
def from_rgb(cls, red: float, green: float, blue: float) -> "Color":
|
| 410 |
+
"""Create a truecolor from three color components in the range(0->255).
|
| 411 |
+
|
| 412 |
+
Args:
|
| 413 |
+
red (float): Red component in range 0-255.
|
| 414 |
+
green (float): Green component in range 0-255.
|
| 415 |
+
blue (float): Blue component in range 0-255.
|
| 416 |
+
|
| 417 |
+
Returns:
|
| 418 |
+
Color: A new color object.
|
| 419 |
+
"""
|
| 420 |
+
return cls.from_triplet(ColorTriplet(int(red), int(green), int(blue)))
|
| 421 |
+
|
| 422 |
+
@classmethod
|
| 423 |
+
def default(cls) -> "Color":
|
| 424 |
+
"""Get a Color instance representing the default color.
|
| 425 |
+
|
| 426 |
+
Returns:
|
| 427 |
+
Color: Default color.
|
| 428 |
+
"""
|
| 429 |
+
return cls(name="default", type=ColorType.DEFAULT)
|
| 430 |
+
|
| 431 |
+
@classmethod
|
| 432 |
+
@lru_cache(maxsize=1024)
|
| 433 |
+
def parse(cls, color: str) -> "Color":
|
| 434 |
+
"""Parse a color definition."""
|
| 435 |
+
original_color = color
|
| 436 |
+
color = color.lower().strip()
|
| 437 |
+
|
| 438 |
+
if color == "default":
|
| 439 |
+
return cls(color, type=ColorType.DEFAULT)
|
| 440 |
+
|
| 441 |
+
color_number = ANSI_COLOR_NAMES.get(color)
|
| 442 |
+
if color_number is not None:
|
| 443 |
+
return cls(
|
| 444 |
+
color,
|
| 445 |
+
type=(ColorType.STANDARD if color_number < 16 else ColorType.EIGHT_BIT),
|
| 446 |
+
number=color_number,
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
color_match = RE_COLOR.match(color)
|
| 450 |
+
if color_match is None:
|
| 451 |
+
raise ColorParseError(f"{original_color!r} is not a valid color")
|
| 452 |
+
|
| 453 |
+
color_24, color_8, color_rgb = color_match.groups()
|
| 454 |
+
if color_24:
|
| 455 |
+
triplet = ColorTriplet(
|
| 456 |
+
int(color_24[0:2], 16), int(color_24[2:4], 16), int(color_24[4:6], 16)
|
| 457 |
+
)
|
| 458 |
+
return cls(color, ColorType.TRUECOLOR, triplet=triplet)
|
| 459 |
+
|
| 460 |
+
elif color_8:
|
| 461 |
+
number = int(color_8)
|
| 462 |
+
if number > 255:
|
| 463 |
+
raise ColorParseError(f"color number must be <= 255 in {color!r}")
|
| 464 |
+
return cls(
|
| 465 |
+
color,
|
| 466 |
+
type=(ColorType.STANDARD if number < 16 else ColorType.EIGHT_BIT),
|
| 467 |
+
number=number,
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
else: # color_rgb:
|
| 471 |
+
components = color_rgb.split(",")
|
| 472 |
+
if len(components) != 3:
|
| 473 |
+
raise ColorParseError(
|
| 474 |
+
f"expected three components in {original_color!r}"
|
| 475 |
+
)
|
| 476 |
+
red, green, blue = components
|
| 477 |
+
triplet = ColorTriplet(int(red), int(green), int(blue))
|
| 478 |
+
if not all(component <= 255 for component in triplet):
|
| 479 |
+
raise ColorParseError(
|
| 480 |
+
f"color components must be <= 255 in {original_color!r}"
|
| 481 |
+
)
|
| 482 |
+
return cls(color, ColorType.TRUECOLOR, triplet=triplet)
|
| 483 |
+
|
| 484 |
+
@lru_cache(maxsize=1024)
|
| 485 |
+
def get_ansi_codes(self, foreground: bool = True) -> Tuple[str, ...]:
|
| 486 |
+
"""Get the ANSI escape codes for this color."""
|
| 487 |
+
_type = self.type
|
| 488 |
+
if _type == ColorType.DEFAULT:
|
| 489 |
+
return ("39" if foreground else "49",)
|
| 490 |
+
|
| 491 |
+
elif _type == ColorType.WINDOWS:
|
| 492 |
+
number = self.number
|
| 493 |
+
assert number is not None
|
| 494 |
+
fore, back = (30, 40) if number < 8 else (82, 92)
|
| 495 |
+
return (str(fore + number if foreground else back + number),)
|
| 496 |
+
|
| 497 |
+
elif _type == ColorType.STANDARD:
|
| 498 |
+
number = self.number
|
| 499 |
+
assert number is not None
|
| 500 |
+
fore, back = (30, 40) if number < 8 else (82, 92)
|
| 501 |
+
return (str(fore + number if foreground else back + number),)
|
| 502 |
+
|
| 503 |
+
elif _type == ColorType.EIGHT_BIT:
|
| 504 |
+
assert self.number is not None
|
| 505 |
+
return ("38" if foreground else "48", "5", str(self.number))
|
| 506 |
+
|
| 507 |
+
else: # self.standard == ColorStandard.TRUECOLOR:
|
| 508 |
+
assert self.triplet is not None
|
| 509 |
+
red, green, blue = self.triplet
|
| 510 |
+
return ("38" if foreground else "48", "2", str(red), str(green), str(blue))
|
| 511 |
+
|
| 512 |
+
@lru_cache(maxsize=1024)
|
| 513 |
+
def downgrade(self, system: ColorSystem) -> "Color":
|
| 514 |
+
"""Downgrade a color system to a system with fewer colors."""
|
| 515 |
+
|
| 516 |
+
if self.type in (ColorType.DEFAULT, system):
|
| 517 |
+
return self
|
| 518 |
+
# Convert to 8-bit color from truecolor color
|
| 519 |
+
if system == ColorSystem.EIGHT_BIT and self.system == ColorSystem.TRUECOLOR:
|
| 520 |
+
assert self.triplet is not None
|
| 521 |
+
_h, l, s = rgb_to_hls(*self.triplet.normalized)
|
| 522 |
+
# If saturation is under 15% assume it is grayscale
|
| 523 |
+
if s < 0.15:
|
| 524 |
+
gray = round(l * 25.0)
|
| 525 |
+
if gray == 0:
|
| 526 |
+
color_number = 16
|
| 527 |
+
elif gray == 25:
|
| 528 |
+
color_number = 231
|
| 529 |
+
else:
|
| 530 |
+
color_number = 231 + gray
|
| 531 |
+
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)
|
| 532 |
+
|
| 533 |
+
red, green, blue = self.triplet
|
| 534 |
+
six_red = red / 95 if red < 95 else 1 + (red - 95) / 40
|
| 535 |
+
six_green = green / 95 if green < 95 else 1 + (green - 95) / 40
|
| 536 |
+
six_blue = blue / 95 if blue < 95 else 1 + (blue - 95) / 40
|
| 537 |
+
|
| 538 |
+
color_number = (
|
| 539 |
+
16 + 36 * round(six_red) + 6 * round(six_green) + round(six_blue)
|
| 540 |
+
)
|
| 541 |
+
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)
|
| 542 |
+
|
| 543 |
+
# Convert to standard from truecolor or 8-bit
|
| 544 |
+
elif system == ColorSystem.STANDARD:
|
| 545 |
+
if self.system == ColorSystem.TRUECOLOR:
|
| 546 |
+
assert self.triplet is not None
|
| 547 |
+
triplet = self.triplet
|
| 548 |
+
else: # self.system == ColorSystem.EIGHT_BIT
|
| 549 |
+
assert self.number is not None
|
| 550 |
+
triplet = ColorTriplet(*EIGHT_BIT_PALETTE[self.number])
|
| 551 |
+
|
| 552 |
+
color_number = STANDARD_PALETTE.match(triplet)
|
| 553 |
+
return Color(self.name, ColorType.STANDARD, number=color_number)
|
| 554 |
+
|
| 555 |
+
elif system == ColorSystem.WINDOWS:
|
| 556 |
+
if self.system == ColorSystem.TRUECOLOR:
|
| 557 |
+
assert self.triplet is not None
|
| 558 |
+
triplet = self.triplet
|
| 559 |
+
else: # self.system == ColorSystem.EIGHT_BIT
|
| 560 |
+
assert self.number is not None
|
| 561 |
+
if self.number < 16:
|
| 562 |
+
return Color(self.name, ColorType.WINDOWS, number=self.number)
|
| 563 |
+
triplet = ColorTriplet(*EIGHT_BIT_PALETTE[self.number])
|
| 564 |
+
|
| 565 |
+
color_number = WINDOWS_PALETTE.match(triplet)
|
| 566 |
+
return Color(self.name, ColorType.WINDOWS, number=color_number)
|
| 567 |
+
|
| 568 |
+
return self
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
def parse_rgb_hex(hex_color: str) -> ColorTriplet:
|
| 572 |
+
"""Parse six hex characters in to RGB triplet."""
|
| 573 |
+
assert len(hex_color) == 6, "must be 6 characters"
|
| 574 |
+
color = ColorTriplet(
|
| 575 |
+
int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
|
| 576 |
+
)
|
| 577 |
+
return color
|
| 578 |
+
|
| 579 |
+
|
| 580 |
+
def blend_rgb(
|
| 581 |
+
color1: ColorTriplet, color2: ColorTriplet, cross_fade: float = 0.5
|
| 582 |
+
) -> ColorTriplet:
|
| 583 |
+
"""Blend one RGB color in to another."""
|
| 584 |
+
r1, g1, b1 = color1
|
| 585 |
+
r2, g2, b2 = color2
|
| 586 |
+
new_color = ColorTriplet(
|
| 587 |
+
int(r1 + (r2 - r1) * cross_fade),
|
| 588 |
+
int(g1 + (g2 - g1) * cross_fade),
|
| 589 |
+
int(b1 + (b2 - b1) * cross_fade),
|
| 590 |
+
)
|
| 591 |
+
return new_color
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
if __name__ == "__main__": # pragma: no cover
|
| 595 |
+
from .console import Console
|
| 596 |
+
from .table import Table
|
| 597 |
+
from .text import Text
|
| 598 |
+
|
| 599 |
+
console = Console()
|
| 600 |
+
|
| 601 |
+
table = Table(show_footer=False, show_edge=True)
|
| 602 |
+
table.add_column("Color", width=10, overflow="ellipsis")
|
| 603 |
+
table.add_column("Number", justify="right", style="yellow")
|
| 604 |
+
table.add_column("Name", style="green")
|
| 605 |
+
table.add_column("Hex", style="blue")
|
| 606 |
+
table.add_column("RGB", style="magenta")
|
| 607 |
+
|
| 608 |
+
colors = sorted((v, k) for k, v in ANSI_COLOR_NAMES.items())
|
| 609 |
+
for color_number, name in colors:
|
| 610 |
+
if "grey" in name:
|
| 611 |
+
continue
|
| 612 |
+
color_cell = Text(" " * 10, style=f"on {name}")
|
| 613 |
+
if color_number < 16:
|
| 614 |
+
table.add_row(color_cell, f"{color_number}", Text(f'"{name}"'))
|
| 615 |
+
else:
|
| 616 |
+
color = EIGHT_BIT_PALETTE[color_number] # type: ignore[has-type]
|
| 617 |
+
table.add_row(
|
| 618 |
+
color_cell, str(color_number), Text(f'"{name}"'), color.hex, color.rgb
|
| 619 |
+
)
|
| 620 |
+
|
| 621 |
+
console.print(table)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/color_triplet.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import NamedTuple, Tuple
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class ColorTriplet(NamedTuple):
|
| 5 |
+
"""The red, green, and blue components of a color."""
|
| 6 |
+
|
| 7 |
+
red: int
|
| 8 |
+
"""Red component in 0 to 255 range."""
|
| 9 |
+
green: int
|
| 10 |
+
"""Green component in 0 to 255 range."""
|
| 11 |
+
blue: int
|
| 12 |
+
"""Blue component in 0 to 255 range."""
|
| 13 |
+
|
| 14 |
+
@property
|
| 15 |
+
def hex(self) -> str:
|
| 16 |
+
"""get the color triplet in CSS style."""
|
| 17 |
+
red, green, blue = self
|
| 18 |
+
return f"#{red:02x}{green:02x}{blue:02x}"
|
| 19 |
+
|
| 20 |
+
@property
|
| 21 |
+
def rgb(self) -> str:
|
| 22 |
+
"""The color in RGB format.
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
str: An rgb color, e.g. ``"rgb(100,23,255)"``.
|
| 26 |
+
"""
|
| 27 |
+
red, green, blue = self
|
| 28 |
+
return f"rgb({red},{green},{blue})"
|
| 29 |
+
|
| 30 |
+
@property
|
| 31 |
+
def normalized(self) -> Tuple[float, float, float]:
|
| 32 |
+
"""Convert components into floats between 0 and 1.
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
Tuple[float, float, float]: A tuple of three normalized colour components.
|
| 36 |
+
"""
|
| 37 |
+
red, green, blue = self
|
| 38 |
+
return red / 255.0, green / 255.0, blue / 255.0
|
env/lib/python3.13/site-packages/pip/_vendor/rich/console.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
env/lib/python3.13/site-packages/pip/_vendor/rich/containers.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 15 |
+
Console,
|
| 16 |
+
ConsoleOptions,
|
| 17 |
+
JustifyMethod,
|
| 18 |
+
OverflowMethod,
|
| 19 |
+
RenderResult,
|
| 20 |
+
RenderableType,
|
| 21 |
+
)
|
| 22 |
+
from .text import Text
|
| 23 |
+
|
| 24 |
+
from .cells import cell_len
|
| 25 |
+
from .measure import Measurement
|
| 26 |
+
|
| 27 |
+
T = TypeVar("T")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class Renderables:
|
| 31 |
+
"""A list subclass which renders its contents to the console."""
|
| 32 |
+
|
| 33 |
+
def __init__(
|
| 34 |
+
self, renderables: Optional[Iterable["RenderableType"]] = None
|
| 35 |
+
) -> None:
|
| 36 |
+
self._renderables: List["RenderableType"] = (
|
| 37 |
+
list(renderables) if renderables is not None else []
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def __rich_console__(
|
| 41 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 42 |
+
) -> "RenderResult":
|
| 43 |
+
"""Console render method to insert line-breaks."""
|
| 44 |
+
yield from self._renderables
|
| 45 |
+
|
| 46 |
+
def __rich_measure__(
|
| 47 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 48 |
+
) -> "Measurement":
|
| 49 |
+
dimensions = [
|
| 50 |
+
Measurement.get(console, options, renderable)
|
| 51 |
+
for renderable in self._renderables
|
| 52 |
+
]
|
| 53 |
+
if not dimensions:
|
| 54 |
+
return Measurement(1, 1)
|
| 55 |
+
_min = max(dimension.minimum for dimension in dimensions)
|
| 56 |
+
_max = max(dimension.maximum for dimension in dimensions)
|
| 57 |
+
return Measurement(_min, _max)
|
| 58 |
+
|
| 59 |
+
def append(self, renderable: "RenderableType") -> None:
|
| 60 |
+
self._renderables.append(renderable)
|
| 61 |
+
|
| 62 |
+
def __iter__(self) -> Iterable["RenderableType"]:
|
| 63 |
+
return iter(self._renderables)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class Lines:
|
| 67 |
+
"""A list subclass which can render to the console."""
|
| 68 |
+
|
| 69 |
+
def __init__(self, lines: Iterable["Text"] = ()) -> None:
|
| 70 |
+
self._lines: List["Text"] = list(lines)
|
| 71 |
+
|
| 72 |
+
def __repr__(self) -> str:
|
| 73 |
+
return f"Lines({self._lines!r})"
|
| 74 |
+
|
| 75 |
+
def __iter__(self) -> Iterator["Text"]:
|
| 76 |
+
return iter(self._lines)
|
| 77 |
+
|
| 78 |
+
@overload
|
| 79 |
+
def __getitem__(self, index: int) -> "Text":
|
| 80 |
+
...
|
| 81 |
+
|
| 82 |
+
@overload
|
| 83 |
+
def __getitem__(self, index: slice) -> List["Text"]:
|
| 84 |
+
...
|
| 85 |
+
|
| 86 |
+
def __getitem__(self, index: Union[slice, int]) -> Union["Text", List["Text"]]:
|
| 87 |
+
return self._lines[index]
|
| 88 |
+
|
| 89 |
+
def __setitem__(self, index: int, value: "Text") -> "Lines":
|
| 90 |
+
self._lines[index] = value
|
| 91 |
+
return self
|
| 92 |
+
|
| 93 |
+
def __len__(self) -> int:
|
| 94 |
+
return self._lines.__len__()
|
| 95 |
+
|
| 96 |
+
def __rich_console__(
|
| 97 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 98 |
+
) -> "RenderResult":
|
| 99 |
+
"""Console render method to insert line-breaks."""
|
| 100 |
+
yield from self._lines
|
| 101 |
+
|
| 102 |
+
def append(self, line: "Text") -> None:
|
| 103 |
+
self._lines.append(line)
|
| 104 |
+
|
| 105 |
+
def extend(self, lines: Iterable["Text"]) -> None:
|
| 106 |
+
self._lines.extend(lines)
|
| 107 |
+
|
| 108 |
+
def pop(self, index: int = -1) -> "Text":
|
| 109 |
+
return self._lines.pop(index)
|
| 110 |
+
|
| 111 |
+
def justify(
|
| 112 |
+
self,
|
| 113 |
+
console: "Console",
|
| 114 |
+
width: int,
|
| 115 |
+
justify: "JustifyMethod" = "left",
|
| 116 |
+
overflow: "OverflowMethod" = "fold",
|
| 117 |
+
) -> None:
|
| 118 |
+
"""Justify and overflow text to a given width.
|
| 119 |
+
|
| 120 |
+
Args:
|
| 121 |
+
console (Console): Console instance.
|
| 122 |
+
width (int): Number of cells available per line.
|
| 123 |
+
justify (str, optional): Default justify method for text: "left", "center", "full" or "right". Defaults to "left".
|
| 124 |
+
overflow (str, optional): Default overflow for text: "crop", "fold", or "ellipsis". Defaults to "fold".
|
| 125 |
+
|
| 126 |
+
"""
|
| 127 |
+
from .text import Text
|
| 128 |
+
|
| 129 |
+
if justify == "left":
|
| 130 |
+
for line in self._lines:
|
| 131 |
+
line.truncate(width, overflow=overflow, pad=True)
|
| 132 |
+
elif justify == "center":
|
| 133 |
+
for line in self._lines:
|
| 134 |
+
line.rstrip()
|
| 135 |
+
line.truncate(width, overflow=overflow)
|
| 136 |
+
line.pad_left((width - cell_len(line.plain)) // 2)
|
| 137 |
+
line.pad_right(width - cell_len(line.plain))
|
| 138 |
+
elif justify == "right":
|
| 139 |
+
for line in self._lines:
|
| 140 |
+
line.rstrip()
|
| 141 |
+
line.truncate(width, overflow=overflow)
|
| 142 |
+
line.pad_left(width - cell_len(line.plain))
|
| 143 |
+
elif justify == "full":
|
| 144 |
+
for line_index, line in enumerate(self._lines):
|
| 145 |
+
if line_index == len(self._lines) - 1:
|
| 146 |
+
break
|
| 147 |
+
words = line.split(" ")
|
| 148 |
+
words_size = sum(cell_len(word.plain) for word in words)
|
| 149 |
+
num_spaces = len(words) - 1
|
| 150 |
+
spaces = [1 for _ in range(num_spaces)]
|
| 151 |
+
index = 0
|
| 152 |
+
if spaces:
|
| 153 |
+
while words_size + num_spaces < width:
|
| 154 |
+
spaces[len(spaces) - index - 1] += 1
|
| 155 |
+
num_spaces += 1
|
| 156 |
+
index = (index + 1) % len(spaces)
|
| 157 |
+
tokens: List[Text] = []
|
| 158 |
+
for index, (word, next_word) in enumerate(
|
| 159 |
+
zip_longest(words, words[1:])
|
| 160 |
+
):
|
| 161 |
+
tokens.append(word)
|
| 162 |
+
if index < len(spaces):
|
| 163 |
+
style = word.get_style_at_offset(console, -1)
|
| 164 |
+
next_style = next_word.get_style_at_offset(console, 0)
|
| 165 |
+
space_style = style if style == next_style else line.style
|
| 166 |
+
tokens.append(Text(" " * spaces[index], style=space_style))
|
| 167 |
+
self[line_index] = Text("").join(tokens)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/default_styles.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict
|
| 2 |
+
|
| 3 |
+
from .style import Style
|
| 4 |
+
|
| 5 |
+
DEFAULT_STYLES: Dict[str, Style] = {
|
| 6 |
+
"none": Style.null(),
|
| 7 |
+
"reset": Style(
|
| 8 |
+
color="default",
|
| 9 |
+
bgcolor="default",
|
| 10 |
+
dim=False,
|
| 11 |
+
bold=False,
|
| 12 |
+
italic=False,
|
| 13 |
+
underline=False,
|
| 14 |
+
blink=False,
|
| 15 |
+
blink2=False,
|
| 16 |
+
reverse=False,
|
| 17 |
+
conceal=False,
|
| 18 |
+
strike=False,
|
| 19 |
+
),
|
| 20 |
+
"dim": Style(dim=True),
|
| 21 |
+
"bright": Style(dim=False),
|
| 22 |
+
"bold": Style(bold=True),
|
| 23 |
+
"strong": Style(bold=True),
|
| 24 |
+
"code": Style(reverse=True, bold=True),
|
| 25 |
+
"italic": Style(italic=True),
|
| 26 |
+
"emphasize": Style(italic=True),
|
| 27 |
+
"underline": Style(underline=True),
|
| 28 |
+
"blink": Style(blink=True),
|
| 29 |
+
"blink2": Style(blink2=True),
|
| 30 |
+
"reverse": Style(reverse=True),
|
| 31 |
+
"strike": Style(strike=True),
|
| 32 |
+
"black": Style(color="black"),
|
| 33 |
+
"red": Style(color="red"),
|
| 34 |
+
"green": Style(color="green"),
|
| 35 |
+
"yellow": Style(color="yellow"),
|
| 36 |
+
"magenta": Style(color="magenta"),
|
| 37 |
+
"cyan": Style(color="cyan"),
|
| 38 |
+
"white": Style(color="white"),
|
| 39 |
+
"inspect.attr": Style(color="yellow", italic=True),
|
| 40 |
+
"inspect.attr.dunder": Style(color="yellow", italic=True, dim=True),
|
| 41 |
+
"inspect.callable": Style(bold=True, color="red"),
|
| 42 |
+
"inspect.async_def": Style(italic=True, color="bright_cyan"),
|
| 43 |
+
"inspect.def": Style(italic=True, color="bright_cyan"),
|
| 44 |
+
"inspect.class": Style(italic=True, color="bright_cyan"),
|
| 45 |
+
"inspect.error": Style(bold=True, color="red"),
|
| 46 |
+
"inspect.equals": Style(),
|
| 47 |
+
"inspect.help": Style(color="cyan"),
|
| 48 |
+
"inspect.doc": Style(dim=True),
|
| 49 |
+
"inspect.value.border": Style(color="green"),
|
| 50 |
+
"live.ellipsis": Style(bold=True, color="red"),
|
| 51 |
+
"layout.tree.row": Style(dim=False, color="red"),
|
| 52 |
+
"layout.tree.column": Style(dim=False, color="blue"),
|
| 53 |
+
"logging.keyword": Style(bold=True, color="yellow"),
|
| 54 |
+
"logging.level.notset": Style(dim=True),
|
| 55 |
+
"logging.level.debug": Style(color="green"),
|
| 56 |
+
"logging.level.info": Style(color="blue"),
|
| 57 |
+
"logging.level.warning": Style(color="yellow"),
|
| 58 |
+
"logging.level.error": Style(color="red", bold=True),
|
| 59 |
+
"logging.level.critical": Style(color="red", bold=True, reverse=True),
|
| 60 |
+
"log.level": Style.null(),
|
| 61 |
+
"log.time": Style(color="cyan", dim=True),
|
| 62 |
+
"log.message": Style.null(),
|
| 63 |
+
"log.path": Style(dim=True),
|
| 64 |
+
"repr.ellipsis": Style(color="yellow"),
|
| 65 |
+
"repr.indent": Style(color="green", dim=True),
|
| 66 |
+
"repr.error": Style(color="red", bold=True),
|
| 67 |
+
"repr.str": Style(color="green", italic=False, bold=False),
|
| 68 |
+
"repr.brace": Style(bold=True),
|
| 69 |
+
"repr.comma": Style(bold=True),
|
| 70 |
+
"repr.ipv4": Style(bold=True, color="bright_green"),
|
| 71 |
+
"repr.ipv6": Style(bold=True, color="bright_green"),
|
| 72 |
+
"repr.eui48": Style(bold=True, color="bright_green"),
|
| 73 |
+
"repr.eui64": Style(bold=True, color="bright_green"),
|
| 74 |
+
"repr.tag_start": Style(bold=True),
|
| 75 |
+
"repr.tag_name": Style(color="bright_magenta", bold=True),
|
| 76 |
+
"repr.tag_contents": Style(color="default"),
|
| 77 |
+
"repr.tag_end": Style(bold=True),
|
| 78 |
+
"repr.attrib_name": Style(color="yellow", italic=False),
|
| 79 |
+
"repr.attrib_equal": Style(bold=True),
|
| 80 |
+
"repr.attrib_value": Style(color="magenta", italic=False),
|
| 81 |
+
"repr.number": Style(color="cyan", bold=True, italic=False),
|
| 82 |
+
"repr.number_complex": Style(color="cyan", bold=True, italic=False), # same
|
| 83 |
+
"repr.bool_true": Style(color="bright_green", italic=True),
|
| 84 |
+
"repr.bool_false": Style(color="bright_red", italic=True),
|
| 85 |
+
"repr.none": Style(color="magenta", italic=True),
|
| 86 |
+
"repr.url": Style(underline=True, color="bright_blue", italic=False, bold=False),
|
| 87 |
+
"repr.uuid": Style(color="bright_yellow", bold=False),
|
| 88 |
+
"repr.call": Style(color="magenta", bold=True),
|
| 89 |
+
"repr.path": Style(color="magenta"),
|
| 90 |
+
"repr.filename": Style(color="bright_magenta"),
|
| 91 |
+
"rule.line": Style(color="bright_green"),
|
| 92 |
+
"rule.text": Style.null(),
|
| 93 |
+
"json.brace": Style(bold=True),
|
| 94 |
+
"json.bool_true": Style(color="bright_green", italic=True),
|
| 95 |
+
"json.bool_false": Style(color="bright_red", italic=True),
|
| 96 |
+
"json.null": Style(color="magenta", italic=True),
|
| 97 |
+
"json.number": Style(color="cyan", bold=True, italic=False),
|
| 98 |
+
"json.str": Style(color="green", italic=False, bold=False),
|
| 99 |
+
"json.key": Style(color="blue", bold=True),
|
| 100 |
+
"prompt": Style.null(),
|
| 101 |
+
"prompt.choices": Style(color="magenta", bold=True),
|
| 102 |
+
"prompt.default": Style(color="cyan", bold=True),
|
| 103 |
+
"prompt.invalid": Style(color="red"),
|
| 104 |
+
"prompt.invalid.choice": Style(color="red"),
|
| 105 |
+
"pretty": Style.null(),
|
| 106 |
+
"scope.border": Style(color="blue"),
|
| 107 |
+
"scope.key": Style(color="yellow", italic=True),
|
| 108 |
+
"scope.key.special": Style(color="yellow", italic=True, dim=True),
|
| 109 |
+
"scope.equals": Style(color="red"),
|
| 110 |
+
"table.header": Style(bold=True),
|
| 111 |
+
"table.footer": Style(bold=True),
|
| 112 |
+
"table.cell": Style.null(),
|
| 113 |
+
"table.title": Style(italic=True),
|
| 114 |
+
"table.caption": Style(italic=True, dim=True),
|
| 115 |
+
"traceback.error": Style(color="red", italic=True),
|
| 116 |
+
"traceback.border.syntax_error": Style(color="bright_red"),
|
| 117 |
+
"traceback.border": Style(color="red"),
|
| 118 |
+
"traceback.text": Style.null(),
|
| 119 |
+
"traceback.title": Style(color="red", bold=True),
|
| 120 |
+
"traceback.exc_type": Style(color="bright_red", bold=True),
|
| 121 |
+
"traceback.exc_value": Style.null(),
|
| 122 |
+
"traceback.offset": Style(color="bright_red", bold=True),
|
| 123 |
+
"traceback.error_range": Style(underline=True, bold=True),
|
| 124 |
+
"traceback.note": Style(color="green", bold=True),
|
| 125 |
+
"traceback.group.border": Style(color="magenta"),
|
| 126 |
+
"bar.back": Style(color="grey23"),
|
| 127 |
+
"bar.complete": Style(color="rgb(249,38,114)"),
|
| 128 |
+
"bar.finished": Style(color="rgb(114,156,31)"),
|
| 129 |
+
"bar.pulse": Style(color="rgb(249,38,114)"),
|
| 130 |
+
"progress.description": Style.null(),
|
| 131 |
+
"progress.filesize": Style(color="green"),
|
| 132 |
+
"progress.filesize.total": Style(color="green"),
|
| 133 |
+
"progress.download": Style(color="green"),
|
| 134 |
+
"progress.elapsed": Style(color="yellow"),
|
| 135 |
+
"progress.percentage": Style(color="magenta"),
|
| 136 |
+
"progress.remaining": Style(color="cyan"),
|
| 137 |
+
"progress.data.speed": Style(color="red"),
|
| 138 |
+
"progress.spinner": Style(color="green"),
|
| 139 |
+
"status.spinner": Style(color="green"),
|
| 140 |
+
"tree": Style(),
|
| 141 |
+
"tree.line": Style(),
|
| 142 |
+
"markdown.paragraph": Style(),
|
| 143 |
+
"markdown.text": Style(),
|
| 144 |
+
"markdown.em": Style(italic=True),
|
| 145 |
+
"markdown.emph": Style(italic=True), # For commonmark backwards compatibility
|
| 146 |
+
"markdown.strong": Style(bold=True),
|
| 147 |
+
"markdown.code": Style(bold=True, color="cyan", bgcolor="black"),
|
| 148 |
+
"markdown.code_block": Style(color="cyan", bgcolor="black"),
|
| 149 |
+
"markdown.block_quote": Style(color="magenta"),
|
| 150 |
+
"markdown.list": Style(color="cyan"),
|
| 151 |
+
"markdown.item": Style(),
|
| 152 |
+
"markdown.item.bullet": Style(color="yellow", bold=True),
|
| 153 |
+
"markdown.item.number": Style(color="yellow", bold=True),
|
| 154 |
+
"markdown.hr": Style(color="yellow"),
|
| 155 |
+
"markdown.h1.border": Style(),
|
| 156 |
+
"markdown.h1": Style(bold=True),
|
| 157 |
+
"markdown.h2": Style(bold=True, underline=True),
|
| 158 |
+
"markdown.h3": Style(bold=True),
|
| 159 |
+
"markdown.h4": Style(bold=True, dim=True),
|
| 160 |
+
"markdown.h5": Style(underline=True),
|
| 161 |
+
"markdown.h6": Style(italic=True),
|
| 162 |
+
"markdown.h7": Style(italic=True, dim=True),
|
| 163 |
+
"markdown.link": Style(color="bright_blue"),
|
| 164 |
+
"markdown.link_url": Style(color="blue", underline=True),
|
| 165 |
+
"markdown.s": Style(strike=True),
|
| 166 |
+
"iso8601.date": Style(color="blue"),
|
| 167 |
+
"iso8601.time": Style(color="magenta"),
|
| 168 |
+
"iso8601.timezone": Style(color="yellow"),
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
if __name__ == "__main__": # pragma: no cover
|
| 173 |
+
import argparse
|
| 174 |
+
import io
|
| 175 |
+
|
| 176 |
+
from pip._vendor.rich.console import Console
|
| 177 |
+
from pip._vendor.rich.table import Table
|
| 178 |
+
from pip._vendor.rich.text import Text
|
| 179 |
+
|
| 180 |
+
parser = argparse.ArgumentParser()
|
| 181 |
+
parser.add_argument("--html", action="store_true", help="Export as HTML table")
|
| 182 |
+
args = parser.parse_args()
|
| 183 |
+
html: bool = args.html
|
| 184 |
+
console = Console(record=True, width=70, file=io.StringIO()) if html else Console()
|
| 185 |
+
|
| 186 |
+
table = Table("Name", "Styling")
|
| 187 |
+
|
| 188 |
+
for style_name, style in DEFAULT_STYLES.items():
|
| 189 |
+
table.add_row(Text(style_name, style=style), str(style))
|
| 190 |
+
|
| 191 |
+
console.print(table)
|
| 192 |
+
if html:
|
| 193 |
+
print(console.export_html(inline_styles=True))
|
env/lib/python3.13/site-packages/pip/_vendor/rich/diagnose.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import platform
|
| 3 |
+
|
| 4 |
+
from pip._vendor.rich import inspect
|
| 5 |
+
from pip._vendor.rich.console import Console, get_windows_console_features
|
| 6 |
+
from pip._vendor.rich.panel import Panel
|
| 7 |
+
from pip._vendor.rich.pretty import Pretty
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def report() -> None: # pragma: no cover
|
| 11 |
+
"""Print a report to the terminal with debugging information"""
|
| 12 |
+
console = Console()
|
| 13 |
+
inspect(console)
|
| 14 |
+
features = get_windows_console_features()
|
| 15 |
+
inspect(features)
|
| 16 |
+
|
| 17 |
+
env_names = (
|
| 18 |
+
"CLICOLOR",
|
| 19 |
+
"COLORTERM",
|
| 20 |
+
"COLUMNS",
|
| 21 |
+
"JPY_PARENT_PID",
|
| 22 |
+
"JUPYTER_COLUMNS",
|
| 23 |
+
"JUPYTER_LINES",
|
| 24 |
+
"LINES",
|
| 25 |
+
"NO_COLOR",
|
| 26 |
+
"TERM_PROGRAM",
|
| 27 |
+
"TERM",
|
| 28 |
+
"TTY_COMPATIBLE",
|
| 29 |
+
"VSCODE_VERBOSE_LOGGING",
|
| 30 |
+
)
|
| 31 |
+
env = {name: os.getenv(name) for name in env_names}
|
| 32 |
+
console.print(Panel.fit((Pretty(env)), title="[b]Environment Variables"))
|
| 33 |
+
|
| 34 |
+
console.print(f'platform="{platform.system()}"')
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__": # pragma: no cover
|
| 38 |
+
report()
|
env/lib/python3.13/site-packages/pip/_vendor/rich/errors.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class ConsoleError(Exception):
|
| 2 |
+
"""An error in console operation."""
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class StyleError(Exception):
|
| 6 |
+
"""An error in styles."""
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class StyleSyntaxError(ConsoleError):
|
| 10 |
+
"""Style was badly formatted."""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class MissingStyle(StyleError):
|
| 14 |
+
"""No such style."""
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class StyleStackError(ConsoleError):
|
| 18 |
+
"""Style stack is invalid."""
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class NotRenderableError(ConsoleError):
|
| 22 |
+
"""Object is not renderable."""
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class MarkupError(ConsoleError):
|
| 26 |
+
"""Markup was badly formatted."""
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class LiveError(ConsoleError):
|
| 30 |
+
"""Error related to Live display."""
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class NoAltScreen(ConsoleError):
|
| 34 |
+
"""Alt screen mode was required."""
|
env/lib/python3.13/site-packages/pip/_vendor/rich/file_proxy.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
from typing import IO, TYPE_CHECKING, Any, List
|
| 3 |
+
|
| 4 |
+
from .ansi import AnsiDecoder
|
| 5 |
+
from .text import Text
|
| 6 |
+
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
from .console import Console
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class FileProxy(io.TextIOBase):
|
| 12 |
+
"""Wraps a file (e.g. sys.stdout) and redirects writes to a console."""
|
| 13 |
+
|
| 14 |
+
def __init__(self, console: "Console", file: IO[str]) -> None:
|
| 15 |
+
self.__console = console
|
| 16 |
+
self.__file = file
|
| 17 |
+
self.__buffer: List[str] = []
|
| 18 |
+
self.__ansi_decoder = AnsiDecoder()
|
| 19 |
+
|
| 20 |
+
@property
|
| 21 |
+
def rich_proxied_file(self) -> IO[str]:
|
| 22 |
+
"""Get proxied file."""
|
| 23 |
+
return self.__file
|
| 24 |
+
|
| 25 |
+
def __getattr__(self, name: str) -> Any:
|
| 26 |
+
return getattr(self.__file, name)
|
| 27 |
+
|
| 28 |
+
def write(self, text: str) -> int:
|
| 29 |
+
if not isinstance(text, str):
|
| 30 |
+
raise TypeError(f"write() argument must be str, not {type(text).__name__}")
|
| 31 |
+
buffer = self.__buffer
|
| 32 |
+
lines: List[str] = []
|
| 33 |
+
while text:
|
| 34 |
+
line, new_line, text = text.partition("\n")
|
| 35 |
+
if new_line:
|
| 36 |
+
lines.append("".join(buffer) + line)
|
| 37 |
+
buffer.clear()
|
| 38 |
+
else:
|
| 39 |
+
buffer.append(line)
|
| 40 |
+
break
|
| 41 |
+
if lines:
|
| 42 |
+
console = self.__console
|
| 43 |
+
with console:
|
| 44 |
+
output = Text("\n").join(
|
| 45 |
+
self.__ansi_decoder.decode_line(line) for line in lines
|
| 46 |
+
)
|
| 47 |
+
console.print(output)
|
| 48 |
+
return len(text)
|
| 49 |
+
|
| 50 |
+
def flush(self) -> None:
|
| 51 |
+
output = "".join(self.__buffer)
|
| 52 |
+
if output:
|
| 53 |
+
self.__console.print(output)
|
| 54 |
+
del self.__buffer[:]
|
| 55 |
+
|
| 56 |
+
def fileno(self) -> int:
|
| 57 |
+
return self.__file.fileno()
|
env/lib/python3.13/site-packages/pip/_vendor/rich/filesize.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Functions for reporting filesizes. Borrowed from https://github.com/PyFilesystem/pyfilesystem2
|
| 2 |
+
|
| 3 |
+
The functions declared in this module should cover the different
|
| 4 |
+
use cases needed to generate a string representation of a file size
|
| 5 |
+
using several different units. Since there are many standards regarding
|
| 6 |
+
file size units, three different functions have been implemented.
|
| 7 |
+
|
| 8 |
+
See Also:
|
| 9 |
+
* `Wikipedia: Binary prefix <https://en.wikipedia.org/wiki/Binary_prefix>`_
|
| 10 |
+
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
__all__ = ["decimal"]
|
| 14 |
+
|
| 15 |
+
from typing import Iterable, List, Optional, Tuple
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _to_str(
|
| 19 |
+
size: int,
|
| 20 |
+
suffixes: Iterable[str],
|
| 21 |
+
base: int,
|
| 22 |
+
*,
|
| 23 |
+
precision: Optional[int] = 1,
|
| 24 |
+
separator: Optional[str] = " ",
|
| 25 |
+
) -> str:
|
| 26 |
+
if size == 1:
|
| 27 |
+
return "1 byte"
|
| 28 |
+
elif size < base:
|
| 29 |
+
return f"{size:,} bytes"
|
| 30 |
+
|
| 31 |
+
for i, suffix in enumerate(suffixes, 2): # noqa: B007
|
| 32 |
+
unit = base**i
|
| 33 |
+
if size < unit:
|
| 34 |
+
break
|
| 35 |
+
return "{:,.{precision}f}{separator}{}".format(
|
| 36 |
+
(base * size / unit),
|
| 37 |
+
suffix,
|
| 38 |
+
precision=precision,
|
| 39 |
+
separator=separator,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def pick_unit_and_suffix(size: int, suffixes: List[str], base: int) -> Tuple[int, str]:
|
| 44 |
+
"""Pick a suffix and base for the given size."""
|
| 45 |
+
for i, suffix in enumerate(suffixes):
|
| 46 |
+
unit = base**i
|
| 47 |
+
if size < unit * base:
|
| 48 |
+
break
|
| 49 |
+
return unit, suffix
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def decimal(
|
| 53 |
+
size: int,
|
| 54 |
+
*,
|
| 55 |
+
precision: Optional[int] = 1,
|
| 56 |
+
separator: Optional[str] = " ",
|
| 57 |
+
) -> str:
|
| 58 |
+
"""Convert a filesize in to a string (powers of 1000, SI prefixes).
|
| 59 |
+
|
| 60 |
+
In this convention, ``1000 B = 1 kB``.
|
| 61 |
+
|
| 62 |
+
This is typically the format used to advertise the storage
|
| 63 |
+
capacity of USB flash drives and the like (*256 MB* meaning
|
| 64 |
+
actually a storage capacity of more than *256 000 000 B*),
|
| 65 |
+
or used by **Mac OS X** since v10.6 to report file sizes.
|
| 66 |
+
|
| 67 |
+
Arguments:
|
| 68 |
+
int (size): A file size.
|
| 69 |
+
int (precision): The number of decimal places to include (default = 1).
|
| 70 |
+
str (separator): The string to separate the value from the units (default = " ").
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
`str`: A string containing a abbreviated file size and units.
|
| 74 |
+
|
| 75 |
+
Example:
|
| 76 |
+
>>> filesize.decimal(30000)
|
| 77 |
+
'30.0 kB'
|
| 78 |
+
>>> filesize.decimal(30000, precision=2, separator="")
|
| 79 |
+
'30.00kB'
|
| 80 |
+
|
| 81 |
+
"""
|
| 82 |
+
return _to_str(
|
| 83 |
+
size,
|
| 84 |
+
("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"),
|
| 85 |
+
1000,
|
| 86 |
+
precision=precision,
|
| 87 |
+
separator=separator,
|
| 88 |
+
)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/jupyter.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Sequence
|
| 2 |
+
|
| 3 |
+
if TYPE_CHECKING:
|
| 4 |
+
from pip._vendor.rich.console import ConsoleRenderable
|
| 5 |
+
|
| 6 |
+
from . import get_console
|
| 7 |
+
from .segment import Segment
|
| 8 |
+
from .terminal_theme import DEFAULT_TERMINAL_THEME
|
| 9 |
+
|
| 10 |
+
if TYPE_CHECKING:
|
| 11 |
+
from pip._vendor.rich.console import ConsoleRenderable
|
| 12 |
+
|
| 13 |
+
JUPYTER_HTML_FORMAT = """\
|
| 14 |
+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">{code}</pre>
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class JupyterRenderable:
|
| 19 |
+
"""A shim to write html to Jupyter notebook."""
|
| 20 |
+
|
| 21 |
+
def __init__(self, html: str, text: str) -> None:
|
| 22 |
+
self.html = html
|
| 23 |
+
self.text = text
|
| 24 |
+
|
| 25 |
+
def _repr_mimebundle_(
|
| 26 |
+
self, include: Sequence[str], exclude: Sequence[str], **kwargs: Any
|
| 27 |
+
) -> Dict[str, str]:
|
| 28 |
+
data = {"text/plain": self.text, "text/html": self.html}
|
| 29 |
+
if include:
|
| 30 |
+
data = {k: v for (k, v) in data.items() if k in include}
|
| 31 |
+
if exclude:
|
| 32 |
+
data = {k: v for (k, v) in data.items() if k not in exclude}
|
| 33 |
+
return data
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class JupyterMixin:
|
| 37 |
+
"""Add to an Rich renderable to make it render in Jupyter notebook."""
|
| 38 |
+
|
| 39 |
+
__slots__ = ()
|
| 40 |
+
|
| 41 |
+
def _repr_mimebundle_(
|
| 42 |
+
self: "ConsoleRenderable",
|
| 43 |
+
include: Sequence[str],
|
| 44 |
+
exclude: Sequence[str],
|
| 45 |
+
**kwargs: Any,
|
| 46 |
+
) -> Dict[str, str]:
|
| 47 |
+
console = get_console()
|
| 48 |
+
segments = list(console.render(self, console.options))
|
| 49 |
+
html = _render_segments(segments)
|
| 50 |
+
text = console._render_buffer(segments)
|
| 51 |
+
data = {"text/plain": text, "text/html": html}
|
| 52 |
+
if include:
|
| 53 |
+
data = {k: v for (k, v) in data.items() if k in include}
|
| 54 |
+
if exclude:
|
| 55 |
+
data = {k: v for (k, v) in data.items() if k not in exclude}
|
| 56 |
+
return data
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _render_segments(segments: Iterable[Segment]) -> str:
|
| 60 |
+
def escape(text: str) -> str:
|
| 61 |
+
"""Escape html."""
|
| 62 |
+
return text.replace("&", "&").replace("<", "<").replace(">", ">")
|
| 63 |
+
|
| 64 |
+
fragments: List[str] = []
|
| 65 |
+
append_fragment = fragments.append
|
| 66 |
+
theme = DEFAULT_TERMINAL_THEME
|
| 67 |
+
for text, style, control in Segment.simplify(segments):
|
| 68 |
+
if control:
|
| 69 |
+
continue
|
| 70 |
+
text = escape(text)
|
| 71 |
+
if style:
|
| 72 |
+
rule = style.get_html_style(theme)
|
| 73 |
+
text = f'<span style="{rule}">{text}</span>' if rule else text
|
| 74 |
+
if style.link:
|
| 75 |
+
text = f'<a href="{style.link}" target="_blank">{text}</a>'
|
| 76 |
+
append_fragment(text)
|
| 77 |
+
|
| 78 |
+
code = "".join(fragments)
|
| 79 |
+
html = JUPYTER_HTML_FORMAT.format(code=code)
|
| 80 |
+
|
| 81 |
+
return html
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def display(segments: Iterable[Segment], text: str) -> None:
|
| 85 |
+
"""Render segments to Jupyter."""
|
| 86 |
+
html = _render_segments(segments)
|
| 87 |
+
jupyter_renderable = JupyterRenderable(html, text)
|
| 88 |
+
try:
|
| 89 |
+
from IPython.display import display as ipython_display
|
| 90 |
+
|
| 91 |
+
ipython_display(jupyter_renderable)
|
| 92 |
+
except ModuleNotFoundError:
|
| 93 |
+
# Handle the case where the Console has force_jupyter=True,
|
| 94 |
+
# but IPython is not installed.
|
| 95 |
+
pass
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def print(*args: Any, **kwargs: Any) -> None:
|
| 99 |
+
"""Proxy for Console print."""
|
| 100 |
+
console = get_console()
|
| 101 |
+
return console.print(*args, **kwargs)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/live.py
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from threading import Event, RLock, Thread
|
| 3 |
+
from types import TracebackType
|
| 4 |
+
from typing import IO, Any, Callable, List, Optional, TextIO, Type, cast
|
| 5 |
+
|
| 6 |
+
from . import get_console
|
| 7 |
+
from .console import Console, ConsoleRenderable, RenderableType, RenderHook
|
| 8 |
+
from .control import Control
|
| 9 |
+
from .file_proxy import FileProxy
|
| 10 |
+
from .jupyter import JupyterMixin
|
| 11 |
+
from .live_render import LiveRender, VerticalOverflowMethod
|
| 12 |
+
from .screen import Screen
|
| 13 |
+
from .text import Text
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class _RefreshThread(Thread):
|
| 17 |
+
"""A thread that calls refresh() at regular intervals."""
|
| 18 |
+
|
| 19 |
+
def __init__(self, live: "Live", refresh_per_second: float) -> None:
|
| 20 |
+
self.live = live
|
| 21 |
+
self.refresh_per_second = refresh_per_second
|
| 22 |
+
self.done = Event()
|
| 23 |
+
super().__init__(daemon=True)
|
| 24 |
+
|
| 25 |
+
def stop(self) -> None:
|
| 26 |
+
self.done.set()
|
| 27 |
+
|
| 28 |
+
def run(self) -> None:
|
| 29 |
+
while not self.done.wait(1 / self.refresh_per_second):
|
| 30 |
+
with self.live._lock:
|
| 31 |
+
if not self.done.is_set():
|
| 32 |
+
self.live.refresh()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class Live(JupyterMixin, RenderHook):
|
| 36 |
+
"""Renders an auto-updating live display of any given renderable.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
renderable (RenderableType, optional): The renderable to live display. Defaults to displaying nothing.
|
| 40 |
+
console (Console, optional): Optional Console instance. Defaults to an internal Console instance writing to stdout.
|
| 41 |
+
screen (bool, optional): Enable alternate screen mode. Defaults to False.
|
| 42 |
+
auto_refresh (bool, optional): Enable auto refresh. If disabled, you will need to call `refresh()` or `update()` with refresh flag. Defaults to True
|
| 43 |
+
refresh_per_second (float, optional): Number of times per second to refresh the live display. Defaults to 4.
|
| 44 |
+
transient (bool, optional): Clear the renderable on exit (has no effect when screen=True). Defaults to False.
|
| 45 |
+
redirect_stdout (bool, optional): Enable redirection of stdout, so ``print`` may be used. Defaults to True.
|
| 46 |
+
redirect_stderr (bool, optional): Enable redirection of stderr. Defaults to True.
|
| 47 |
+
vertical_overflow (VerticalOverflowMethod, optional): How to handle renderable when it is too tall for the console. Defaults to "ellipsis".
|
| 48 |
+
get_renderable (Callable[[], RenderableType], optional): Optional callable to get renderable. Defaults to None.
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
def __init__(
|
| 52 |
+
self,
|
| 53 |
+
renderable: Optional[RenderableType] = None,
|
| 54 |
+
*,
|
| 55 |
+
console: Optional[Console] = None,
|
| 56 |
+
screen: bool = False,
|
| 57 |
+
auto_refresh: bool = True,
|
| 58 |
+
refresh_per_second: float = 4,
|
| 59 |
+
transient: bool = False,
|
| 60 |
+
redirect_stdout: bool = True,
|
| 61 |
+
redirect_stderr: bool = True,
|
| 62 |
+
vertical_overflow: VerticalOverflowMethod = "ellipsis",
|
| 63 |
+
get_renderable: Optional[Callable[[], RenderableType]] = None,
|
| 64 |
+
) -> None:
|
| 65 |
+
assert refresh_per_second > 0, "refresh_per_second must be > 0"
|
| 66 |
+
self._renderable = renderable
|
| 67 |
+
self.console = console if console is not None else get_console()
|
| 68 |
+
self._screen = screen
|
| 69 |
+
self._alt_screen = False
|
| 70 |
+
|
| 71 |
+
self._redirect_stdout = redirect_stdout
|
| 72 |
+
self._redirect_stderr = redirect_stderr
|
| 73 |
+
self._restore_stdout: Optional[IO[str]] = None
|
| 74 |
+
self._restore_stderr: Optional[IO[str]] = None
|
| 75 |
+
|
| 76 |
+
self._lock = RLock()
|
| 77 |
+
self.ipy_widget: Optional[Any] = None
|
| 78 |
+
self.auto_refresh = auto_refresh
|
| 79 |
+
self._started: bool = False
|
| 80 |
+
self.transient = True if screen else transient
|
| 81 |
+
|
| 82 |
+
self._refresh_thread: Optional[_RefreshThread] = None
|
| 83 |
+
self.refresh_per_second = refresh_per_second
|
| 84 |
+
|
| 85 |
+
self.vertical_overflow = vertical_overflow
|
| 86 |
+
self._get_renderable = get_renderable
|
| 87 |
+
self._live_render = LiveRender(
|
| 88 |
+
self.get_renderable(), vertical_overflow=vertical_overflow
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
@property
|
| 92 |
+
def is_started(self) -> bool:
|
| 93 |
+
"""Check if live display has been started."""
|
| 94 |
+
return self._started
|
| 95 |
+
|
| 96 |
+
def get_renderable(self) -> RenderableType:
|
| 97 |
+
renderable = (
|
| 98 |
+
self._get_renderable()
|
| 99 |
+
if self._get_renderable is not None
|
| 100 |
+
else self._renderable
|
| 101 |
+
)
|
| 102 |
+
return renderable or ""
|
| 103 |
+
|
| 104 |
+
def start(self, refresh: bool = False) -> None:
|
| 105 |
+
"""Start live rendering display.
|
| 106 |
+
|
| 107 |
+
Args:
|
| 108 |
+
refresh (bool, optional): Also refresh. Defaults to False.
|
| 109 |
+
"""
|
| 110 |
+
with self._lock:
|
| 111 |
+
if self._started:
|
| 112 |
+
return
|
| 113 |
+
self.console.set_live(self)
|
| 114 |
+
self._started = True
|
| 115 |
+
if self._screen:
|
| 116 |
+
self._alt_screen = self.console.set_alt_screen(True)
|
| 117 |
+
self.console.show_cursor(False)
|
| 118 |
+
self._enable_redirect_io()
|
| 119 |
+
self.console.push_render_hook(self)
|
| 120 |
+
if refresh:
|
| 121 |
+
try:
|
| 122 |
+
self.refresh()
|
| 123 |
+
except Exception:
|
| 124 |
+
# If refresh fails, we want to stop the redirection of sys.stderr,
|
| 125 |
+
# so the error stacktrace is properly displayed in the terminal.
|
| 126 |
+
# (or, if the code that calls Rich captures the exception and wants to display something,
|
| 127 |
+
# let this be displayed in the terminal).
|
| 128 |
+
self.stop()
|
| 129 |
+
raise
|
| 130 |
+
if self.auto_refresh:
|
| 131 |
+
self._refresh_thread = _RefreshThread(self, self.refresh_per_second)
|
| 132 |
+
self._refresh_thread.start()
|
| 133 |
+
|
| 134 |
+
def stop(self) -> None:
|
| 135 |
+
"""Stop live rendering display."""
|
| 136 |
+
with self._lock:
|
| 137 |
+
if not self._started:
|
| 138 |
+
return
|
| 139 |
+
self.console.clear_live()
|
| 140 |
+
self._started = False
|
| 141 |
+
|
| 142 |
+
if self.auto_refresh and self._refresh_thread is not None:
|
| 143 |
+
self._refresh_thread.stop()
|
| 144 |
+
self._refresh_thread = None
|
| 145 |
+
# allow it to fully render on the last even if overflow
|
| 146 |
+
self.vertical_overflow = "visible"
|
| 147 |
+
with self.console:
|
| 148 |
+
try:
|
| 149 |
+
if not self._alt_screen and not self.console.is_jupyter:
|
| 150 |
+
self.refresh()
|
| 151 |
+
finally:
|
| 152 |
+
self._disable_redirect_io()
|
| 153 |
+
self.console.pop_render_hook()
|
| 154 |
+
if not self._alt_screen and self.console.is_terminal:
|
| 155 |
+
self.console.line()
|
| 156 |
+
self.console.show_cursor(True)
|
| 157 |
+
if self._alt_screen:
|
| 158 |
+
self.console.set_alt_screen(False)
|
| 159 |
+
|
| 160 |
+
if self.transient and not self._alt_screen:
|
| 161 |
+
self.console.control(self._live_render.restore_cursor())
|
| 162 |
+
if self.ipy_widget is not None and self.transient:
|
| 163 |
+
self.ipy_widget.close() # pragma: no cover
|
| 164 |
+
|
| 165 |
+
def __enter__(self) -> "Live":
|
| 166 |
+
self.start(refresh=self._renderable is not None)
|
| 167 |
+
return self
|
| 168 |
+
|
| 169 |
+
def __exit__(
|
| 170 |
+
self,
|
| 171 |
+
exc_type: Optional[Type[BaseException]],
|
| 172 |
+
exc_val: Optional[BaseException],
|
| 173 |
+
exc_tb: Optional[TracebackType],
|
| 174 |
+
) -> None:
|
| 175 |
+
self.stop()
|
| 176 |
+
|
| 177 |
+
def _enable_redirect_io(self) -> None:
|
| 178 |
+
"""Enable redirecting of stdout / stderr."""
|
| 179 |
+
if self.console.is_terminal or self.console.is_jupyter:
|
| 180 |
+
if self._redirect_stdout and not isinstance(sys.stdout, FileProxy):
|
| 181 |
+
self._restore_stdout = sys.stdout
|
| 182 |
+
sys.stdout = cast("TextIO", FileProxy(self.console, sys.stdout))
|
| 183 |
+
if self._redirect_stderr and not isinstance(sys.stderr, FileProxy):
|
| 184 |
+
self._restore_stderr = sys.stderr
|
| 185 |
+
sys.stderr = cast("TextIO", FileProxy(self.console, sys.stderr))
|
| 186 |
+
|
| 187 |
+
def _disable_redirect_io(self) -> None:
|
| 188 |
+
"""Disable redirecting of stdout / stderr."""
|
| 189 |
+
if self._restore_stdout:
|
| 190 |
+
sys.stdout = cast("TextIO", self._restore_stdout)
|
| 191 |
+
self._restore_stdout = None
|
| 192 |
+
if self._restore_stderr:
|
| 193 |
+
sys.stderr = cast("TextIO", self._restore_stderr)
|
| 194 |
+
self._restore_stderr = None
|
| 195 |
+
|
| 196 |
+
@property
|
| 197 |
+
def renderable(self) -> RenderableType:
|
| 198 |
+
"""Get the renderable that is being displayed
|
| 199 |
+
|
| 200 |
+
Returns:
|
| 201 |
+
RenderableType: Displayed renderable.
|
| 202 |
+
"""
|
| 203 |
+
renderable = self.get_renderable()
|
| 204 |
+
return Screen(renderable) if self._alt_screen else renderable
|
| 205 |
+
|
| 206 |
+
def update(self, renderable: RenderableType, *, refresh: bool = False) -> None:
|
| 207 |
+
"""Update the renderable that is being displayed
|
| 208 |
+
|
| 209 |
+
Args:
|
| 210 |
+
renderable (RenderableType): New renderable to use.
|
| 211 |
+
refresh (bool, optional): Refresh the display. Defaults to False.
|
| 212 |
+
"""
|
| 213 |
+
if isinstance(renderable, str):
|
| 214 |
+
renderable = self.console.render_str(renderable)
|
| 215 |
+
with self._lock:
|
| 216 |
+
self._renderable = renderable
|
| 217 |
+
if refresh:
|
| 218 |
+
self.refresh()
|
| 219 |
+
|
| 220 |
+
def refresh(self) -> None:
|
| 221 |
+
"""Update the display of the Live Render."""
|
| 222 |
+
with self._lock:
|
| 223 |
+
self._live_render.set_renderable(self.renderable)
|
| 224 |
+
if self.console.is_jupyter: # pragma: no cover
|
| 225 |
+
try:
|
| 226 |
+
from IPython.display import display
|
| 227 |
+
from ipywidgets import Output
|
| 228 |
+
except ImportError:
|
| 229 |
+
import warnings
|
| 230 |
+
|
| 231 |
+
warnings.warn('install "ipywidgets" for Jupyter support')
|
| 232 |
+
else:
|
| 233 |
+
if self.ipy_widget is None:
|
| 234 |
+
self.ipy_widget = Output()
|
| 235 |
+
display(self.ipy_widget)
|
| 236 |
+
|
| 237 |
+
with self.ipy_widget:
|
| 238 |
+
self.ipy_widget.clear_output(wait=True)
|
| 239 |
+
self.console.print(self._live_render.renderable)
|
| 240 |
+
elif self.console.is_terminal and not self.console.is_dumb_terminal:
|
| 241 |
+
with self.console:
|
| 242 |
+
self.console.print(Control())
|
| 243 |
+
elif (
|
| 244 |
+
not self._started and not self.transient
|
| 245 |
+
): # if it is finished allow files or dumb-terminals to see final result
|
| 246 |
+
with self.console:
|
| 247 |
+
self.console.print(Control())
|
| 248 |
+
|
| 249 |
+
def process_renderables(
|
| 250 |
+
self, renderables: List[ConsoleRenderable]
|
| 251 |
+
) -> List[ConsoleRenderable]:
|
| 252 |
+
"""Process renderables to restore cursor and display progress."""
|
| 253 |
+
self._live_render.vertical_overflow = self.vertical_overflow
|
| 254 |
+
if self.console.is_interactive:
|
| 255 |
+
# lock needs acquiring as user can modify live_render renderable at any time unlike in Progress.
|
| 256 |
+
with self._lock:
|
| 257 |
+
reset = (
|
| 258 |
+
Control.home()
|
| 259 |
+
if self._alt_screen
|
| 260 |
+
else self._live_render.position_cursor()
|
| 261 |
+
)
|
| 262 |
+
renderables = [reset, *renderables, self._live_render]
|
| 263 |
+
elif (
|
| 264 |
+
not self._started and not self.transient
|
| 265 |
+
): # if it is finished render the final output for files or dumb_terminals
|
| 266 |
+
renderables = [*renderables, self._live_render]
|
| 267 |
+
|
| 268 |
+
return renderables
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
if __name__ == "__main__": # pragma: no cover
|
| 272 |
+
import random
|
| 273 |
+
import time
|
| 274 |
+
from itertools import cycle
|
| 275 |
+
from typing import Dict, List, Tuple
|
| 276 |
+
|
| 277 |
+
from .align import Align
|
| 278 |
+
from .console import Console
|
| 279 |
+
from .live import Live as Live
|
| 280 |
+
from .panel import Panel
|
| 281 |
+
from .rule import Rule
|
| 282 |
+
from .syntax import Syntax
|
| 283 |
+
from .table import Table
|
| 284 |
+
|
| 285 |
+
console = Console()
|
| 286 |
+
|
| 287 |
+
syntax = Syntax(
|
| 288 |
+
'''def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
|
| 289 |
+
"""Iterate and generate a tuple with a flag for last value."""
|
| 290 |
+
iter_values = iter(values)
|
| 291 |
+
try:
|
| 292 |
+
previous_value = next(iter_values)
|
| 293 |
+
except StopIteration:
|
| 294 |
+
return
|
| 295 |
+
for value in iter_values:
|
| 296 |
+
yield False, previous_value
|
| 297 |
+
previous_value = value
|
| 298 |
+
yield True, previous_value''',
|
| 299 |
+
"python",
|
| 300 |
+
line_numbers=True,
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
table = Table("foo", "bar", "baz")
|
| 304 |
+
table.add_row("1", "2", "3")
|
| 305 |
+
|
| 306 |
+
progress_renderables = [
|
| 307 |
+
"You can make the terminal shorter and taller to see the live table hide"
|
| 308 |
+
"Text may be printed while the progress bars are rendering.",
|
| 309 |
+
Panel("In fact, [i]any[/i] renderable will work"),
|
| 310 |
+
"Such as [magenta]tables[/]...",
|
| 311 |
+
table,
|
| 312 |
+
"Pretty printed structures...",
|
| 313 |
+
{"type": "example", "text": "Pretty printed"},
|
| 314 |
+
"Syntax...",
|
| 315 |
+
syntax,
|
| 316 |
+
Rule("Give it a try!"),
|
| 317 |
+
]
|
| 318 |
+
|
| 319 |
+
examples = cycle(progress_renderables)
|
| 320 |
+
|
| 321 |
+
exchanges = [
|
| 322 |
+
"SGD",
|
| 323 |
+
"MYR",
|
| 324 |
+
"EUR",
|
| 325 |
+
"USD",
|
| 326 |
+
"AUD",
|
| 327 |
+
"JPY",
|
| 328 |
+
"CNH",
|
| 329 |
+
"HKD",
|
| 330 |
+
"CAD",
|
| 331 |
+
"INR",
|
| 332 |
+
"DKK",
|
| 333 |
+
"GBP",
|
| 334 |
+
"RUB",
|
| 335 |
+
"NZD",
|
| 336 |
+
"MXN",
|
| 337 |
+
"IDR",
|
| 338 |
+
"TWD",
|
| 339 |
+
"THB",
|
| 340 |
+
"VND",
|
| 341 |
+
]
|
| 342 |
+
with Live(console=console) as live_table:
|
| 343 |
+
exchange_rate_dict: Dict[Tuple[str, str], float] = {}
|
| 344 |
+
|
| 345 |
+
for index in range(100):
|
| 346 |
+
select_exchange = exchanges[index % len(exchanges)]
|
| 347 |
+
|
| 348 |
+
for exchange in exchanges:
|
| 349 |
+
if exchange == select_exchange:
|
| 350 |
+
continue
|
| 351 |
+
time.sleep(0.4)
|
| 352 |
+
if random.randint(0, 10) < 1:
|
| 353 |
+
console.log(next(examples))
|
| 354 |
+
exchange_rate_dict[(select_exchange, exchange)] = 200 / (
|
| 355 |
+
(random.random() * 320) + 1
|
| 356 |
+
)
|
| 357 |
+
if len(exchange_rate_dict) > len(exchanges) - 1:
|
| 358 |
+
exchange_rate_dict.pop(list(exchange_rate_dict.keys())[0])
|
| 359 |
+
table = Table(title="Exchange Rates")
|
| 360 |
+
|
| 361 |
+
table.add_column("Source Currency")
|
| 362 |
+
table.add_column("Destination Currency")
|
| 363 |
+
table.add_column("Exchange Rate")
|
| 364 |
+
|
| 365 |
+
for (source, dest), exchange_rate in exchange_rate_dict.items():
|
| 366 |
+
table.add_row(
|
| 367 |
+
source,
|
| 368 |
+
dest,
|
| 369 |
+
Text(
|
| 370 |
+
f"{exchange_rate:.4f}",
|
| 371 |
+
style="red" if exchange_rate < 1.0 else "green",
|
| 372 |
+
),
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
live_table.update(Align.center(table))
|
env/lib/python3.13/site-packages/pip/_vendor/rich/live_render.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from typing import Optional, Tuple
|
| 3 |
+
|
| 4 |
+
if sys.version_info >= (3, 8):
|
| 5 |
+
from typing import Literal
|
| 6 |
+
else:
|
| 7 |
+
from pip._vendor.typing_extensions import Literal # pragma: no cover
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
from ._loop import loop_last
|
| 11 |
+
from .console import Console, ConsoleOptions, RenderableType, RenderResult
|
| 12 |
+
from .control import Control
|
| 13 |
+
from .segment import ControlType, Segment
|
| 14 |
+
from .style import StyleType
|
| 15 |
+
from .text import Text
|
| 16 |
+
|
| 17 |
+
VerticalOverflowMethod = Literal["crop", "ellipsis", "visible"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class LiveRender:
|
| 21 |
+
"""Creates a renderable that may be updated.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
renderable (RenderableType): Any renderable object.
|
| 25 |
+
style (StyleType, optional): An optional style to apply to the renderable. Defaults to "".
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
def __init__(
|
| 29 |
+
self,
|
| 30 |
+
renderable: RenderableType,
|
| 31 |
+
style: StyleType = "",
|
| 32 |
+
vertical_overflow: VerticalOverflowMethod = "ellipsis",
|
| 33 |
+
) -> None:
|
| 34 |
+
self.renderable = renderable
|
| 35 |
+
self.style = style
|
| 36 |
+
self.vertical_overflow = vertical_overflow
|
| 37 |
+
self._shape: Optional[Tuple[int, int]] = None
|
| 38 |
+
|
| 39 |
+
def set_renderable(self, renderable: RenderableType) -> None:
|
| 40 |
+
"""Set a new renderable.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
renderable (RenderableType): Any renderable object, including str.
|
| 44 |
+
"""
|
| 45 |
+
self.renderable = renderable
|
| 46 |
+
|
| 47 |
+
def position_cursor(self) -> Control:
|
| 48 |
+
"""Get control codes to move cursor to beginning of live render.
|
| 49 |
+
|
| 50 |
+
Returns:
|
| 51 |
+
Control: A control instance that may be printed.
|
| 52 |
+
"""
|
| 53 |
+
if self._shape is not None:
|
| 54 |
+
_, height = self._shape
|
| 55 |
+
return Control(
|
| 56 |
+
ControlType.CARRIAGE_RETURN,
|
| 57 |
+
(ControlType.ERASE_IN_LINE, 2),
|
| 58 |
+
*(
|
| 59 |
+
(
|
| 60 |
+
(ControlType.CURSOR_UP, 1),
|
| 61 |
+
(ControlType.ERASE_IN_LINE, 2),
|
| 62 |
+
)
|
| 63 |
+
* (height - 1)
|
| 64 |
+
)
|
| 65 |
+
)
|
| 66 |
+
return Control()
|
| 67 |
+
|
| 68 |
+
def restore_cursor(self) -> Control:
|
| 69 |
+
"""Get control codes to clear the render and restore the cursor to its previous position.
|
| 70 |
+
|
| 71 |
+
Returns:
|
| 72 |
+
Control: A Control instance that may be printed.
|
| 73 |
+
"""
|
| 74 |
+
if self._shape is not None:
|
| 75 |
+
_, height = self._shape
|
| 76 |
+
return Control(
|
| 77 |
+
ControlType.CARRIAGE_RETURN,
|
| 78 |
+
*((ControlType.CURSOR_UP, 1), (ControlType.ERASE_IN_LINE, 2)) * height
|
| 79 |
+
)
|
| 80 |
+
return Control()
|
| 81 |
+
|
| 82 |
+
def __rich_console__(
|
| 83 |
+
self, console: Console, options: ConsoleOptions
|
| 84 |
+
) -> RenderResult:
|
| 85 |
+
renderable = self.renderable
|
| 86 |
+
style = console.get_style(self.style)
|
| 87 |
+
lines = console.render_lines(renderable, options, style=style, pad=False)
|
| 88 |
+
shape = Segment.get_shape(lines)
|
| 89 |
+
|
| 90 |
+
_, height = shape
|
| 91 |
+
if height > options.size.height:
|
| 92 |
+
if self.vertical_overflow == "crop":
|
| 93 |
+
lines = lines[: options.size.height]
|
| 94 |
+
shape = Segment.get_shape(lines)
|
| 95 |
+
elif self.vertical_overflow == "ellipsis":
|
| 96 |
+
lines = lines[: (options.size.height - 1)]
|
| 97 |
+
overflow_text = Text(
|
| 98 |
+
"...",
|
| 99 |
+
overflow="crop",
|
| 100 |
+
justify="center",
|
| 101 |
+
end="",
|
| 102 |
+
style="live.ellipsis",
|
| 103 |
+
)
|
| 104 |
+
lines.append(list(console.render(overflow_text)))
|
| 105 |
+
shape = Segment.get_shape(lines)
|
| 106 |
+
self._shape = shape
|
| 107 |
+
|
| 108 |
+
new_line = Segment.line()
|
| 109 |
+
for last, line in loop_last(lines):
|
| 110 |
+
yield from line
|
| 111 |
+
if not last:
|
| 112 |
+
yield new_line
|
env/lib/python3.13/site-packages/pip/_vendor/rich/log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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from logging import Handler, LogRecord
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from types import ModuleType
|
| 6 |
+
from typing import ClassVar, Iterable, List, Optional, Type, Union
|
| 7 |
+
|
| 8 |
+
from pip._vendor.rich._null_file import NullFile
|
| 9 |
+
|
| 10 |
+
from . import get_console
|
| 11 |
+
from ._log_render import FormatTimeCallable, LogRender
|
| 12 |
+
from .console import Console, ConsoleRenderable
|
| 13 |
+
from .highlighter import Highlighter, ReprHighlighter
|
| 14 |
+
from .text import Text
|
| 15 |
+
from .traceback import Traceback
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class RichHandler(Handler):
|
| 19 |
+
"""A logging handler that renders output with Rich. The time / level / message and file are displayed in columns.
|
| 20 |
+
The level is color coded, and the message is syntax highlighted.
|
| 21 |
+
|
| 22 |
+
Note:
|
| 23 |
+
Be careful when enabling console markup in log messages if you have configured logging for libraries not
|
| 24 |
+
under your control. If a dependency writes messages containing square brackets, it may not produce the intended output.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
level (Union[int, str], optional): Log level. Defaults to logging.NOTSET.
|
| 28 |
+
console (:class:`~rich.console.Console`, optional): Optional console instance to write logs.
|
| 29 |
+
Default will use a global console instance writing to stdout.
|
| 30 |
+
show_time (bool, optional): Show a column for the time. Defaults to True.
|
| 31 |
+
omit_repeated_times (bool, optional): Omit repetition of the same time. Defaults to True.
|
| 32 |
+
show_level (bool, optional): Show a column for the level. Defaults to True.
|
| 33 |
+
show_path (bool, optional): Show the path to the original log call. Defaults to True.
|
| 34 |
+
enable_link_path (bool, optional): Enable terminal link of path column to file. Defaults to True.
|
| 35 |
+
highlighter (Highlighter, optional): Highlighter to style log messages, or None to use ReprHighlighter. Defaults to None.
|
| 36 |
+
markup (bool, optional): Enable console markup in log messages. Defaults to False.
|
| 37 |
+
rich_tracebacks (bool, optional): Enable rich tracebacks with syntax highlighting and formatting. Defaults to False.
|
| 38 |
+
tracebacks_width (Optional[int], optional): Number of characters used to render tracebacks, or None for full width. Defaults to None.
|
| 39 |
+
tracebacks_code_width (int, optional): Number of code characters used to render tracebacks, or None for full width. Defaults to 88.
|
| 40 |
+
tracebacks_extra_lines (int, optional): Additional lines of code to render tracebacks, or None for full width. Defaults to None.
|
| 41 |
+
tracebacks_theme (str, optional): Override pygments theme used in traceback.
|
| 42 |
+
tracebacks_word_wrap (bool, optional): Enable word wrapping of long tracebacks lines. Defaults to True.
|
| 43 |
+
tracebacks_show_locals (bool, optional): Enable display of locals in tracebacks. Defaults to False.
|
| 44 |
+
tracebacks_suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.
|
| 45 |
+
tracebacks_max_frames (int, optional): Optional maximum number of frames returned by traceback.
|
| 46 |
+
locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
|
| 47 |
+
Defaults to 10.
|
| 48 |
+
locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.
|
| 49 |
+
log_time_format (Union[str, TimeFormatterCallable], optional): If ``log_time`` is enabled, either string for strftime or callable that formats the time. Defaults to "[%x %X] ".
|
| 50 |
+
keywords (List[str], optional): List of words to highlight instead of ``RichHandler.KEYWORDS``.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
KEYWORDS: ClassVar[Optional[List[str]]] = [
|
| 54 |
+
"GET",
|
| 55 |
+
"POST",
|
| 56 |
+
"HEAD",
|
| 57 |
+
"PUT",
|
| 58 |
+
"DELETE",
|
| 59 |
+
"OPTIONS",
|
| 60 |
+
"TRACE",
|
| 61 |
+
"PATCH",
|
| 62 |
+
]
|
| 63 |
+
HIGHLIGHTER_CLASS: ClassVar[Type[Highlighter]] = ReprHighlighter
|
| 64 |
+
|
| 65 |
+
def __init__(
|
| 66 |
+
self,
|
| 67 |
+
level: Union[int, str] = logging.NOTSET,
|
| 68 |
+
console: Optional[Console] = None,
|
| 69 |
+
*,
|
| 70 |
+
show_time: bool = True,
|
| 71 |
+
omit_repeated_times: bool = True,
|
| 72 |
+
show_level: bool = True,
|
| 73 |
+
show_path: bool = True,
|
| 74 |
+
enable_link_path: bool = True,
|
| 75 |
+
highlighter: Optional[Highlighter] = None,
|
| 76 |
+
markup: bool = False,
|
| 77 |
+
rich_tracebacks: bool = False,
|
| 78 |
+
tracebacks_width: Optional[int] = None,
|
| 79 |
+
tracebacks_code_width: int = 88,
|
| 80 |
+
tracebacks_extra_lines: int = 3,
|
| 81 |
+
tracebacks_theme: Optional[str] = None,
|
| 82 |
+
tracebacks_word_wrap: bool = True,
|
| 83 |
+
tracebacks_show_locals: bool = False,
|
| 84 |
+
tracebacks_suppress: Iterable[Union[str, ModuleType]] = (),
|
| 85 |
+
tracebacks_max_frames: int = 100,
|
| 86 |
+
locals_max_length: int = 10,
|
| 87 |
+
locals_max_string: int = 80,
|
| 88 |
+
log_time_format: Union[str, FormatTimeCallable] = "[%x %X]",
|
| 89 |
+
keywords: Optional[List[str]] = None,
|
| 90 |
+
) -> None:
|
| 91 |
+
super().__init__(level=level)
|
| 92 |
+
self.console = console or get_console()
|
| 93 |
+
self.highlighter = highlighter or self.HIGHLIGHTER_CLASS()
|
| 94 |
+
self._log_render = LogRender(
|
| 95 |
+
show_time=show_time,
|
| 96 |
+
show_level=show_level,
|
| 97 |
+
show_path=show_path,
|
| 98 |
+
time_format=log_time_format,
|
| 99 |
+
omit_repeated_times=omit_repeated_times,
|
| 100 |
+
level_width=None,
|
| 101 |
+
)
|
| 102 |
+
self.enable_link_path = enable_link_path
|
| 103 |
+
self.markup = markup
|
| 104 |
+
self.rich_tracebacks = rich_tracebacks
|
| 105 |
+
self.tracebacks_width = tracebacks_width
|
| 106 |
+
self.tracebacks_extra_lines = tracebacks_extra_lines
|
| 107 |
+
self.tracebacks_theme = tracebacks_theme
|
| 108 |
+
self.tracebacks_word_wrap = tracebacks_word_wrap
|
| 109 |
+
self.tracebacks_show_locals = tracebacks_show_locals
|
| 110 |
+
self.tracebacks_suppress = tracebacks_suppress
|
| 111 |
+
self.tracebacks_max_frames = tracebacks_max_frames
|
| 112 |
+
self.tracebacks_code_width = tracebacks_code_width
|
| 113 |
+
self.locals_max_length = locals_max_length
|
| 114 |
+
self.locals_max_string = locals_max_string
|
| 115 |
+
self.keywords = keywords
|
| 116 |
+
|
| 117 |
+
def get_level_text(self, record: LogRecord) -> Text:
|
| 118 |
+
"""Get the level name from the record.
|
| 119 |
+
|
| 120 |
+
Args:
|
| 121 |
+
record (LogRecord): LogRecord instance.
|
| 122 |
+
|
| 123 |
+
Returns:
|
| 124 |
+
Text: A tuple of the style and level name.
|
| 125 |
+
"""
|
| 126 |
+
level_name = record.levelname
|
| 127 |
+
level_text = Text.styled(
|
| 128 |
+
level_name.ljust(8), f"logging.level.{level_name.lower()}"
|
| 129 |
+
)
|
| 130 |
+
return level_text
|
| 131 |
+
|
| 132 |
+
def emit(self, record: LogRecord) -> None:
|
| 133 |
+
"""Invoked by logging."""
|
| 134 |
+
message = self.format(record)
|
| 135 |
+
traceback = None
|
| 136 |
+
if (
|
| 137 |
+
self.rich_tracebacks
|
| 138 |
+
and record.exc_info
|
| 139 |
+
and record.exc_info != (None, None, None)
|
| 140 |
+
):
|
| 141 |
+
exc_type, exc_value, exc_traceback = record.exc_info
|
| 142 |
+
assert exc_type is not None
|
| 143 |
+
assert exc_value is not None
|
| 144 |
+
traceback = Traceback.from_exception(
|
| 145 |
+
exc_type,
|
| 146 |
+
exc_value,
|
| 147 |
+
exc_traceback,
|
| 148 |
+
width=self.tracebacks_width,
|
| 149 |
+
code_width=self.tracebacks_code_width,
|
| 150 |
+
extra_lines=self.tracebacks_extra_lines,
|
| 151 |
+
theme=self.tracebacks_theme,
|
| 152 |
+
word_wrap=self.tracebacks_word_wrap,
|
| 153 |
+
show_locals=self.tracebacks_show_locals,
|
| 154 |
+
locals_max_length=self.locals_max_length,
|
| 155 |
+
locals_max_string=self.locals_max_string,
|
| 156 |
+
suppress=self.tracebacks_suppress,
|
| 157 |
+
max_frames=self.tracebacks_max_frames,
|
| 158 |
+
)
|
| 159 |
+
message = record.getMessage()
|
| 160 |
+
if self.formatter:
|
| 161 |
+
record.message = record.getMessage()
|
| 162 |
+
formatter = self.formatter
|
| 163 |
+
if hasattr(formatter, "usesTime") and formatter.usesTime():
|
| 164 |
+
record.asctime = formatter.formatTime(record, formatter.datefmt)
|
| 165 |
+
message = formatter.formatMessage(record)
|
| 166 |
+
|
| 167 |
+
message_renderable = self.render_message(record, message)
|
| 168 |
+
log_renderable = self.render(
|
| 169 |
+
record=record, traceback=traceback, message_renderable=message_renderable
|
| 170 |
+
)
|
| 171 |
+
if isinstance(self.console.file, NullFile):
|
| 172 |
+
# Handles pythonw, where stdout/stderr are null, and we return NullFile
|
| 173 |
+
# instance from Console.file. In this case, we still want to make a log record
|
| 174 |
+
# even though we won't be writing anything to a file.
|
| 175 |
+
self.handleError(record)
|
| 176 |
+
else:
|
| 177 |
+
try:
|
| 178 |
+
self.console.print(log_renderable)
|
| 179 |
+
except Exception:
|
| 180 |
+
self.handleError(record)
|
| 181 |
+
|
| 182 |
+
def render_message(self, record: LogRecord, message: str) -> "ConsoleRenderable":
|
| 183 |
+
"""Render message text in to Text.
|
| 184 |
+
|
| 185 |
+
Args:
|
| 186 |
+
record (LogRecord): logging Record.
|
| 187 |
+
message (str): String containing log message.
|
| 188 |
+
|
| 189 |
+
Returns:
|
| 190 |
+
ConsoleRenderable: Renderable to display log message.
|
| 191 |
+
"""
|
| 192 |
+
use_markup = getattr(record, "markup", self.markup)
|
| 193 |
+
message_text = Text.from_markup(message) if use_markup else Text(message)
|
| 194 |
+
|
| 195 |
+
highlighter = getattr(record, "highlighter", self.highlighter)
|
| 196 |
+
if highlighter:
|
| 197 |
+
message_text = highlighter(message_text)
|
| 198 |
+
|
| 199 |
+
if self.keywords is None:
|
| 200 |
+
self.keywords = self.KEYWORDS
|
| 201 |
+
|
| 202 |
+
if self.keywords:
|
| 203 |
+
message_text.highlight_words(self.keywords, "logging.keyword")
|
| 204 |
+
|
| 205 |
+
return message_text
|
| 206 |
+
|
| 207 |
+
def render(
|
| 208 |
+
self,
|
| 209 |
+
*,
|
| 210 |
+
record: LogRecord,
|
| 211 |
+
traceback: Optional[Traceback],
|
| 212 |
+
message_renderable: "ConsoleRenderable",
|
| 213 |
+
) -> "ConsoleRenderable":
|
| 214 |
+
"""Render log for display.
|
| 215 |
+
|
| 216 |
+
Args:
|
| 217 |
+
record (LogRecord): logging Record.
|
| 218 |
+
traceback (Optional[Traceback]): Traceback instance or None for no Traceback.
|
| 219 |
+
message_renderable (ConsoleRenderable): Renderable (typically Text) containing log message contents.
|
| 220 |
+
|
| 221 |
+
Returns:
|
| 222 |
+
ConsoleRenderable: Renderable to display log.
|
| 223 |
+
"""
|
| 224 |
+
path = Path(record.pathname).name
|
| 225 |
+
level = self.get_level_text(record)
|
| 226 |
+
time_format = None if self.formatter is None else self.formatter.datefmt
|
| 227 |
+
log_time = datetime.fromtimestamp(record.created)
|
| 228 |
+
|
| 229 |
+
log_renderable = self._log_render(
|
| 230 |
+
self.console,
|
| 231 |
+
[message_renderable] if not traceback else [message_renderable, traceback],
|
| 232 |
+
log_time=log_time,
|
| 233 |
+
time_format=time_format,
|
| 234 |
+
level=level,
|
| 235 |
+
path=path,
|
| 236 |
+
line_no=record.lineno,
|
| 237 |
+
link_path=record.pathname if self.enable_link_path else None,
|
| 238 |
+
)
|
| 239 |
+
return log_renderable
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
if __name__ == "__main__": # pragma: no cover
|
| 243 |
+
from time import sleep
|
| 244 |
+
|
| 245 |
+
FORMAT = "%(message)s"
|
| 246 |
+
# FORMAT = "%(asctime)-15s - %(levelname)s - %(message)s"
|
| 247 |
+
logging.basicConfig(
|
| 248 |
+
level="NOTSET",
|
| 249 |
+
format=FORMAT,
|
| 250 |
+
datefmt="[%X]",
|
| 251 |
+
handlers=[RichHandler(rich_tracebacks=True, tracebacks_show_locals=True)],
|
| 252 |
+
)
|
| 253 |
+
log = logging.getLogger("rich")
|
| 254 |
+
|
| 255 |
+
log.info("Server starting...")
|
| 256 |
+
log.info("Listening on http://127.0.0.1:8080")
|
| 257 |
+
sleep(1)
|
| 258 |
+
|
| 259 |
+
log.info("GET /index.html 200 1298")
|
| 260 |
+
log.info("GET /imgs/backgrounds/back1.jpg 200 54386")
|
| 261 |
+
log.info("GET /css/styles.css 200 54386")
|
| 262 |
+
log.warning("GET /favicon.ico 404 242")
|
| 263 |
+
sleep(1)
|
| 264 |
+
|
| 265 |
+
log.debug(
|
| 266 |
+
"JSONRPC request\n--> %r\n<-- %r",
|
| 267 |
+
{
|
| 268 |
+
"version": "1.1",
|
| 269 |
+
"method": "confirmFruitPurchase",
|
| 270 |
+
"params": [["apple", "orange", "mangoes", "pomelo"], 1.123],
|
| 271 |
+
"id": "194521489",
|
| 272 |
+
},
|
| 273 |
+
{"version": "1.1", "result": True, "error": None, "id": "194521489"},
|
| 274 |
+
)
|
| 275 |
+
log.debug(
|
| 276 |
+
"Loading configuration file /adasd/asdasd/qeqwe/qwrqwrqwr/sdgsdgsdg/werwerwer/dfgerert/ertertert/ertetert/werwerwer"
|
| 277 |
+
)
|
| 278 |
+
log.error("Unable to find 'pomelo' in database!")
|
| 279 |
+
log.info("POST /jsonrpc/ 200 65532")
|
| 280 |
+
log.info("POST /admin/ 401 42234")
|
| 281 |
+
log.warning("password was rejected for admin site.")
|
| 282 |
+
|
| 283 |
+
def divide() -> None:
|
| 284 |
+
number = 1
|
| 285 |
+
divisor = 0
|
| 286 |
+
foos = ["foo"] * 100
|
| 287 |
+
log.debug("in divide")
|
| 288 |
+
try:
|
| 289 |
+
number / divisor
|
| 290 |
+
except:
|
| 291 |
+
log.exception("An error of some kind occurred!")
|
| 292 |
+
|
| 293 |
+
divide()
|
| 294 |
+
sleep(1)
|
| 295 |
+
log.critical("Out of memory!")
|
| 296 |
+
log.info("Server exited with code=-1")
|
| 297 |
+
log.info("[bold]EXITING...[/bold]", extra=dict(markup=True))
|
env/lib/python3.13/site-packages/pip/_vendor/rich/markup.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from ast import literal_eval
|
| 3 |
+
from operator import attrgetter
|
| 4 |
+
from typing import Callable, Iterable, List, Match, NamedTuple, Optional, Tuple, Union
|
| 5 |
+
|
| 6 |
+
from ._emoji_replace import _emoji_replace
|
| 7 |
+
from .emoji import EmojiVariant
|
| 8 |
+
from .errors import MarkupError
|
| 9 |
+
from .style import Style
|
| 10 |
+
from .text import Span, Text
|
| 11 |
+
|
| 12 |
+
RE_TAGS = re.compile(
|
| 13 |
+
r"""((\\*)\[([a-z#/@][^[]*?)])""",
|
| 14 |
+
re.VERBOSE,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
RE_HANDLER = re.compile(r"^([\w.]*?)(\(.*?\))?$")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class Tag(NamedTuple):
|
| 21 |
+
"""A tag in console markup."""
|
| 22 |
+
|
| 23 |
+
name: str
|
| 24 |
+
"""The tag name. e.g. 'bold'."""
|
| 25 |
+
parameters: Optional[str]
|
| 26 |
+
"""Any additional parameters after the name."""
|
| 27 |
+
|
| 28 |
+
def __str__(self) -> str:
|
| 29 |
+
return (
|
| 30 |
+
self.name if self.parameters is None else f"{self.name} {self.parameters}"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
@property
|
| 34 |
+
def markup(self) -> str:
|
| 35 |
+
"""Get the string representation of this tag."""
|
| 36 |
+
return (
|
| 37 |
+
f"[{self.name}]"
|
| 38 |
+
if self.parameters is None
|
| 39 |
+
else f"[{self.name}={self.parameters}]"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
_ReStringMatch = Match[str] # regex match object
|
| 44 |
+
_ReSubCallable = Callable[[_ReStringMatch], str] # Callable invoked by re.sub
|
| 45 |
+
_EscapeSubMethod = Callable[[_ReSubCallable, str], str] # Sub method of a compiled re
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def escape(
|
| 49 |
+
markup: str,
|
| 50 |
+
_escape: _EscapeSubMethod = re.compile(r"(\\*)(\[[a-z#/@][^[]*?])").sub,
|
| 51 |
+
) -> str:
|
| 52 |
+
"""Escapes text so that it won't be interpreted as markup.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
markup (str): Content to be inserted in to markup.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
str: Markup with square brackets escaped.
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
def escape_backslashes(match: Match[str]) -> str:
|
| 62 |
+
"""Called by re.sub replace matches."""
|
| 63 |
+
backslashes, text = match.groups()
|
| 64 |
+
return f"{backslashes}{backslashes}\\{text}"
|
| 65 |
+
|
| 66 |
+
markup = _escape(escape_backslashes, markup)
|
| 67 |
+
if markup.endswith("\\") and not markup.endswith("\\\\"):
|
| 68 |
+
return markup + "\\"
|
| 69 |
+
|
| 70 |
+
return markup
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _parse(markup: str) -> Iterable[Tuple[int, Optional[str], Optional[Tag]]]:
|
| 74 |
+
"""Parse markup in to an iterable of tuples of (position, text, tag).
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
markup (str): A string containing console markup
|
| 78 |
+
|
| 79 |
+
"""
|
| 80 |
+
position = 0
|
| 81 |
+
_divmod = divmod
|
| 82 |
+
_Tag = Tag
|
| 83 |
+
for match in RE_TAGS.finditer(markup):
|
| 84 |
+
full_text, escapes, tag_text = match.groups()
|
| 85 |
+
start, end = match.span()
|
| 86 |
+
if start > position:
|
| 87 |
+
yield start, markup[position:start], None
|
| 88 |
+
if escapes:
|
| 89 |
+
backslashes, escaped = _divmod(len(escapes), 2)
|
| 90 |
+
if backslashes:
|
| 91 |
+
# Literal backslashes
|
| 92 |
+
yield start, "\\" * backslashes, None
|
| 93 |
+
start += backslashes * 2
|
| 94 |
+
if escaped:
|
| 95 |
+
# Escape of tag
|
| 96 |
+
yield start, full_text[len(escapes) :], None
|
| 97 |
+
position = end
|
| 98 |
+
continue
|
| 99 |
+
text, equals, parameters = tag_text.partition("=")
|
| 100 |
+
yield start, None, _Tag(text, parameters if equals else None)
|
| 101 |
+
position = end
|
| 102 |
+
if position < len(markup):
|
| 103 |
+
yield position, markup[position:], None
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def render(
|
| 107 |
+
markup: str,
|
| 108 |
+
style: Union[str, Style] = "",
|
| 109 |
+
emoji: bool = True,
|
| 110 |
+
emoji_variant: Optional[EmojiVariant] = None,
|
| 111 |
+
) -> Text:
|
| 112 |
+
"""Render console markup in to a Text instance.
|
| 113 |
+
|
| 114 |
+
Args:
|
| 115 |
+
markup (str): A string containing console markup.
|
| 116 |
+
style: (Union[str, Style]): The style to use.
|
| 117 |
+
emoji (bool, optional): Also render emoji code. Defaults to True.
|
| 118 |
+
emoji_variant (str, optional): Optional emoji variant, either "text" or "emoji". Defaults to None.
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
Raises:
|
| 122 |
+
MarkupError: If there is a syntax error in the markup.
|
| 123 |
+
|
| 124 |
+
Returns:
|
| 125 |
+
Text: A test instance.
|
| 126 |
+
"""
|
| 127 |
+
emoji_replace = _emoji_replace
|
| 128 |
+
if "[" not in markup:
|
| 129 |
+
return Text(
|
| 130 |
+
emoji_replace(markup, default_variant=emoji_variant) if emoji else markup,
|
| 131 |
+
style=style,
|
| 132 |
+
)
|
| 133 |
+
text = Text(style=style)
|
| 134 |
+
append = text.append
|
| 135 |
+
normalize = Style.normalize
|
| 136 |
+
|
| 137 |
+
style_stack: List[Tuple[int, Tag]] = []
|
| 138 |
+
pop = style_stack.pop
|
| 139 |
+
|
| 140 |
+
spans: List[Span] = []
|
| 141 |
+
append_span = spans.append
|
| 142 |
+
|
| 143 |
+
_Span = Span
|
| 144 |
+
_Tag = Tag
|
| 145 |
+
|
| 146 |
+
def pop_style(style_name: str) -> Tuple[int, Tag]:
|
| 147 |
+
"""Pop tag matching given style name."""
|
| 148 |
+
for index, (_, tag) in enumerate(reversed(style_stack), 1):
|
| 149 |
+
if tag.name == style_name:
|
| 150 |
+
return pop(-index)
|
| 151 |
+
raise KeyError(style_name)
|
| 152 |
+
|
| 153 |
+
for position, plain_text, tag in _parse(markup):
|
| 154 |
+
if plain_text is not None:
|
| 155 |
+
# Handle open brace escapes, where the brace is not part of a tag.
|
| 156 |
+
plain_text = plain_text.replace("\\[", "[")
|
| 157 |
+
append(emoji_replace(plain_text) if emoji else plain_text)
|
| 158 |
+
elif tag is not None:
|
| 159 |
+
if tag.name.startswith("/"): # Closing tag
|
| 160 |
+
style_name = tag.name[1:].strip()
|
| 161 |
+
|
| 162 |
+
if style_name: # explicit close
|
| 163 |
+
style_name = normalize(style_name)
|
| 164 |
+
try:
|
| 165 |
+
start, open_tag = pop_style(style_name)
|
| 166 |
+
except KeyError:
|
| 167 |
+
raise MarkupError(
|
| 168 |
+
f"closing tag '{tag.markup}' at position {position} doesn't match any open tag"
|
| 169 |
+
) from None
|
| 170 |
+
else: # implicit close
|
| 171 |
+
try:
|
| 172 |
+
start, open_tag = pop()
|
| 173 |
+
except IndexError:
|
| 174 |
+
raise MarkupError(
|
| 175 |
+
f"closing tag '[/]' at position {position} has nothing to close"
|
| 176 |
+
) from None
|
| 177 |
+
|
| 178 |
+
if open_tag.name.startswith("@"):
|
| 179 |
+
if open_tag.parameters:
|
| 180 |
+
handler_name = ""
|
| 181 |
+
parameters = open_tag.parameters.strip()
|
| 182 |
+
handler_match = RE_HANDLER.match(parameters)
|
| 183 |
+
if handler_match is not None:
|
| 184 |
+
handler_name, match_parameters = handler_match.groups()
|
| 185 |
+
parameters = (
|
| 186 |
+
"()" if match_parameters is None else match_parameters
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
try:
|
| 190 |
+
meta_params = literal_eval(parameters)
|
| 191 |
+
except SyntaxError as error:
|
| 192 |
+
raise MarkupError(
|
| 193 |
+
f"error parsing {parameters!r} in {open_tag.parameters!r}; {error.msg}"
|
| 194 |
+
)
|
| 195 |
+
except Exception as error:
|
| 196 |
+
raise MarkupError(
|
| 197 |
+
f"error parsing {open_tag.parameters!r}; {error}"
|
| 198 |
+
) from None
|
| 199 |
+
|
| 200 |
+
if handler_name:
|
| 201 |
+
meta_params = (
|
| 202 |
+
handler_name,
|
| 203 |
+
meta_params
|
| 204 |
+
if isinstance(meta_params, tuple)
|
| 205 |
+
else (meta_params,),
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
else:
|
| 209 |
+
meta_params = ()
|
| 210 |
+
|
| 211 |
+
append_span(
|
| 212 |
+
_Span(
|
| 213 |
+
start, len(text), Style(meta={open_tag.name: meta_params})
|
| 214 |
+
)
|
| 215 |
+
)
|
| 216 |
+
else:
|
| 217 |
+
append_span(_Span(start, len(text), str(open_tag)))
|
| 218 |
+
|
| 219 |
+
else: # Opening tag
|
| 220 |
+
normalized_tag = _Tag(normalize(tag.name), tag.parameters)
|
| 221 |
+
style_stack.append((len(text), normalized_tag))
|
| 222 |
+
|
| 223 |
+
text_length = len(text)
|
| 224 |
+
while style_stack:
|
| 225 |
+
start, tag = style_stack.pop()
|
| 226 |
+
style = str(tag)
|
| 227 |
+
if style:
|
| 228 |
+
append_span(_Span(start, text_length, style))
|
| 229 |
+
|
| 230 |
+
text.spans = sorted(spans[::-1], key=attrgetter("start"))
|
| 231 |
+
return text
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
if __name__ == "__main__": # pragma: no cover
|
| 235 |
+
MARKUP = [
|
| 236 |
+
"[red]Hello World[/red]",
|
| 237 |
+
"[magenta]Hello [b]World[/b]",
|
| 238 |
+
"[bold]Bold[italic] bold and italic [/bold]italic[/italic]",
|
| 239 |
+
"Click [link=https://www.willmcgugan.com]here[/link] to visit my Blog",
|
| 240 |
+
":warning-emoji: [bold red blink] DANGER![/]",
|
| 241 |
+
]
|
| 242 |
+
|
| 243 |
+
from pip._vendor.rich import print
|
| 244 |
+
from pip._vendor.rich.table import Table
|
| 245 |
+
|
| 246 |
+
grid = Table("Markup", "Result", padding=(0, 1))
|
| 247 |
+
|
| 248 |
+
for markup in MARKUP:
|
| 249 |
+
grid.add_row(Text(markup), markup)
|
| 250 |
+
|
| 251 |
+
print(grid)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/measure.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from operator import itemgetter
|
| 2 |
+
from typing import TYPE_CHECKING, Callable, NamedTuple, Optional, Sequence
|
| 3 |
+
|
| 4 |
+
from . import errors
|
| 5 |
+
from .protocol import is_renderable, rich_cast
|
| 6 |
+
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
from .console import Console, ConsoleOptions, RenderableType
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Measurement(NamedTuple):
|
| 12 |
+
"""Stores the minimum and maximum widths (in characters) required to render an object."""
|
| 13 |
+
|
| 14 |
+
minimum: int
|
| 15 |
+
"""Minimum number of cells required to render."""
|
| 16 |
+
maximum: int
|
| 17 |
+
"""Maximum number of cells required to render."""
|
| 18 |
+
|
| 19 |
+
@property
|
| 20 |
+
def span(self) -> int:
|
| 21 |
+
"""Get difference between maximum and minimum."""
|
| 22 |
+
return self.maximum - self.minimum
|
| 23 |
+
|
| 24 |
+
def normalize(self) -> "Measurement":
|
| 25 |
+
"""Get measurement that ensures that minimum <= maximum and minimum >= 0
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
Measurement: A normalized measurement.
|
| 29 |
+
"""
|
| 30 |
+
minimum, maximum = self
|
| 31 |
+
minimum = min(max(0, minimum), maximum)
|
| 32 |
+
return Measurement(max(0, minimum), max(0, max(minimum, maximum)))
|
| 33 |
+
|
| 34 |
+
def with_maximum(self, width: int) -> "Measurement":
|
| 35 |
+
"""Get a RenderableWith where the widths are <= width.
|
| 36 |
+
|
| 37 |
+
Args:
|
| 38 |
+
width (int): Maximum desired width.
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
Measurement: New Measurement object.
|
| 42 |
+
"""
|
| 43 |
+
minimum, maximum = self
|
| 44 |
+
return Measurement(min(minimum, width), min(maximum, width))
|
| 45 |
+
|
| 46 |
+
def with_minimum(self, width: int) -> "Measurement":
|
| 47 |
+
"""Get a RenderableWith where the widths are >= width.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
width (int): Minimum desired width.
|
| 51 |
+
|
| 52 |
+
Returns:
|
| 53 |
+
Measurement: New Measurement object.
|
| 54 |
+
"""
|
| 55 |
+
minimum, maximum = self
|
| 56 |
+
width = max(0, width)
|
| 57 |
+
return Measurement(max(minimum, width), max(maximum, width))
|
| 58 |
+
|
| 59 |
+
def clamp(
|
| 60 |
+
self, min_width: Optional[int] = None, max_width: Optional[int] = None
|
| 61 |
+
) -> "Measurement":
|
| 62 |
+
"""Clamp a measurement within the specified range.
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
min_width (int): Minimum desired width, or ``None`` for no minimum. Defaults to None.
|
| 66 |
+
max_width (int): Maximum desired width, or ``None`` for no maximum. Defaults to None.
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
Measurement: New Measurement object.
|
| 70 |
+
"""
|
| 71 |
+
measurement = self
|
| 72 |
+
if min_width is not None:
|
| 73 |
+
measurement = measurement.with_minimum(min_width)
|
| 74 |
+
if max_width is not None:
|
| 75 |
+
measurement = measurement.with_maximum(max_width)
|
| 76 |
+
return measurement
|
| 77 |
+
|
| 78 |
+
@classmethod
|
| 79 |
+
def get(
|
| 80 |
+
cls, console: "Console", options: "ConsoleOptions", renderable: "RenderableType"
|
| 81 |
+
) -> "Measurement":
|
| 82 |
+
"""Get a measurement for a renderable.
|
| 83 |
+
|
| 84 |
+
Args:
|
| 85 |
+
console (~rich.console.Console): Console instance.
|
| 86 |
+
options (~rich.console.ConsoleOptions): Console options.
|
| 87 |
+
renderable (RenderableType): An object that may be rendered with Rich.
|
| 88 |
+
|
| 89 |
+
Raises:
|
| 90 |
+
errors.NotRenderableError: If the object is not renderable.
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
Measurement: Measurement object containing range of character widths required to render the object.
|
| 94 |
+
"""
|
| 95 |
+
_max_width = options.max_width
|
| 96 |
+
if _max_width < 1:
|
| 97 |
+
return Measurement(0, 0)
|
| 98 |
+
if isinstance(renderable, str):
|
| 99 |
+
renderable = console.render_str(
|
| 100 |
+
renderable, markup=options.markup, highlight=False
|
| 101 |
+
)
|
| 102 |
+
renderable = rich_cast(renderable)
|
| 103 |
+
if is_renderable(renderable):
|
| 104 |
+
get_console_width: Optional[
|
| 105 |
+
Callable[["Console", "ConsoleOptions"], "Measurement"]
|
| 106 |
+
] = getattr(renderable, "__rich_measure__", None)
|
| 107 |
+
if get_console_width is not None:
|
| 108 |
+
render_width = (
|
| 109 |
+
get_console_width(console, options)
|
| 110 |
+
.normalize()
|
| 111 |
+
.with_maximum(_max_width)
|
| 112 |
+
)
|
| 113 |
+
if render_width.maximum < 1:
|
| 114 |
+
return Measurement(0, 0)
|
| 115 |
+
return render_width.normalize()
|
| 116 |
+
else:
|
| 117 |
+
return Measurement(0, _max_width)
|
| 118 |
+
else:
|
| 119 |
+
raise errors.NotRenderableError(
|
| 120 |
+
f"Unable to get render width for {renderable!r}; "
|
| 121 |
+
"a str, Segment, or object with __rich_console__ method is required"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def measure_renderables(
|
| 126 |
+
console: "Console",
|
| 127 |
+
options: "ConsoleOptions",
|
| 128 |
+
renderables: Sequence["RenderableType"],
|
| 129 |
+
) -> "Measurement":
|
| 130 |
+
"""Get a measurement that would fit a number of renderables.
|
| 131 |
+
|
| 132 |
+
Args:
|
| 133 |
+
console (~rich.console.Console): Console instance.
|
| 134 |
+
options (~rich.console.ConsoleOptions): Console options.
|
| 135 |
+
renderables (Iterable[RenderableType]): One or more renderable objects.
|
| 136 |
+
|
| 137 |
+
Returns:
|
| 138 |
+
Measurement: Measurement object containing range of character widths required to
|
| 139 |
+
contain all given renderables.
|
| 140 |
+
"""
|
| 141 |
+
if not renderables:
|
| 142 |
+
return Measurement(0, 0)
|
| 143 |
+
get_measurement = Measurement.get
|
| 144 |
+
measurements = [
|
| 145 |
+
get_measurement(console, options, renderable) for renderable in renderables
|
| 146 |
+
]
|
| 147 |
+
measured_width = Measurement(
|
| 148 |
+
max(measurements, key=itemgetter(0)).minimum,
|
| 149 |
+
max(measurements, key=itemgetter(1)).maximum,
|
| 150 |
+
)
|
| 151 |
+
return measured_width
|
env/lib/python3.13/site-packages/pip/_vendor/rich/padding.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TYPE_CHECKING, List, Optional, Tuple, Union
|
| 2 |
+
|
| 3 |
+
if TYPE_CHECKING:
|
| 4 |
+
from .console import (
|
| 5 |
+
Console,
|
| 6 |
+
ConsoleOptions,
|
| 7 |
+
RenderableType,
|
| 8 |
+
RenderResult,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
from .jupyter import JupyterMixin
|
| 12 |
+
from .measure import Measurement
|
| 13 |
+
from .segment import Segment
|
| 14 |
+
from .style import Style
|
| 15 |
+
|
| 16 |
+
PaddingDimensions = Union[int, Tuple[int], Tuple[int, int], Tuple[int, int, int, int]]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class Padding(JupyterMixin):
|
| 20 |
+
"""Draw space around content.
|
| 21 |
+
|
| 22 |
+
Example:
|
| 23 |
+
>>> print(Padding("Hello", (2, 4), style="on blue"))
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
renderable (RenderableType): String or other renderable.
|
| 27 |
+
pad (Union[int, Tuple[int]]): Padding for top, right, bottom, and left borders.
|
| 28 |
+
May be specified with 1, 2, or 4 integers (CSS style).
|
| 29 |
+
style (Union[str, Style], optional): Style for padding characters. Defaults to "none".
|
| 30 |
+
expand (bool, optional): Expand padding to fit available width. Defaults to True.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
def __init__(
|
| 34 |
+
self,
|
| 35 |
+
renderable: "RenderableType",
|
| 36 |
+
pad: "PaddingDimensions" = (0, 0, 0, 0),
|
| 37 |
+
*,
|
| 38 |
+
style: Union[str, Style] = "none",
|
| 39 |
+
expand: bool = True,
|
| 40 |
+
):
|
| 41 |
+
self.renderable = renderable
|
| 42 |
+
self.top, self.right, self.bottom, self.left = self.unpack(pad)
|
| 43 |
+
self.style = style
|
| 44 |
+
self.expand = expand
|
| 45 |
+
|
| 46 |
+
@classmethod
|
| 47 |
+
def indent(cls, renderable: "RenderableType", level: int) -> "Padding":
|
| 48 |
+
"""Make padding instance to render an indent.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
renderable (RenderableType): String or other renderable.
|
| 52 |
+
level (int): Number of characters to indent.
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
Padding: A Padding instance.
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
return Padding(renderable, pad=(0, 0, 0, level), expand=False)
|
| 59 |
+
|
| 60 |
+
@staticmethod
|
| 61 |
+
def unpack(pad: "PaddingDimensions") -> Tuple[int, int, int, int]:
|
| 62 |
+
"""Unpack padding specified in CSS style."""
|
| 63 |
+
if isinstance(pad, int):
|
| 64 |
+
return (pad, pad, pad, pad)
|
| 65 |
+
if len(pad) == 1:
|
| 66 |
+
_pad = pad[0]
|
| 67 |
+
return (_pad, _pad, _pad, _pad)
|
| 68 |
+
if len(pad) == 2:
|
| 69 |
+
pad_top, pad_right = pad
|
| 70 |
+
return (pad_top, pad_right, pad_top, pad_right)
|
| 71 |
+
if len(pad) == 4:
|
| 72 |
+
top, right, bottom, left = pad
|
| 73 |
+
return (top, right, bottom, left)
|
| 74 |
+
raise ValueError(f"1, 2 or 4 integers required for padding; {len(pad)} given")
|
| 75 |
+
|
| 76 |
+
def __repr__(self) -> str:
|
| 77 |
+
return f"Padding({self.renderable!r}, ({self.top},{self.right},{self.bottom},{self.left}))"
|
| 78 |
+
|
| 79 |
+
def __rich_console__(
|
| 80 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 81 |
+
) -> "RenderResult":
|
| 82 |
+
style = console.get_style(self.style)
|
| 83 |
+
if self.expand:
|
| 84 |
+
width = options.max_width
|
| 85 |
+
else:
|
| 86 |
+
width = min(
|
| 87 |
+
Measurement.get(console, options, self.renderable).maximum
|
| 88 |
+
+ self.left
|
| 89 |
+
+ self.right,
|
| 90 |
+
options.max_width,
|
| 91 |
+
)
|
| 92 |
+
render_options = options.update_width(width - self.left - self.right)
|
| 93 |
+
if render_options.height is not None:
|
| 94 |
+
render_options = render_options.update_height(
|
| 95 |
+
height=render_options.height - self.top - self.bottom
|
| 96 |
+
)
|
| 97 |
+
lines = console.render_lines(
|
| 98 |
+
self.renderable, render_options, style=style, pad=True
|
| 99 |
+
)
|
| 100 |
+
_Segment = Segment
|
| 101 |
+
|
| 102 |
+
left = _Segment(" " * self.left, style) if self.left else None
|
| 103 |
+
right = (
|
| 104 |
+
[_Segment(f'{" " * self.right}', style), _Segment.line()]
|
| 105 |
+
if self.right
|
| 106 |
+
else [_Segment.line()]
|
| 107 |
+
)
|
| 108 |
+
blank_line: Optional[List[Segment]] = None
|
| 109 |
+
if self.top:
|
| 110 |
+
blank_line = [_Segment(f'{" " * width}\n', style)]
|
| 111 |
+
yield from blank_line * self.top
|
| 112 |
+
if left:
|
| 113 |
+
for line in lines:
|
| 114 |
+
yield left
|
| 115 |
+
yield from line
|
| 116 |
+
yield from right
|
| 117 |
+
else:
|
| 118 |
+
for line in lines:
|
| 119 |
+
yield from line
|
| 120 |
+
yield from right
|
| 121 |
+
if self.bottom:
|
| 122 |
+
blank_line = blank_line or [_Segment(f'{" " * width}\n', style)]
|
| 123 |
+
yield from blank_line * self.bottom
|
| 124 |
+
|
| 125 |
+
def __rich_measure__(
|
| 126 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 127 |
+
) -> "Measurement":
|
| 128 |
+
max_width = options.max_width
|
| 129 |
+
extra_width = self.left + self.right
|
| 130 |
+
if max_width - extra_width < 1:
|
| 131 |
+
return Measurement(max_width, max_width)
|
| 132 |
+
measure_min, measure_max = Measurement.get(console, options, self.renderable)
|
| 133 |
+
measurement = Measurement(measure_min + extra_width, measure_max + extra_width)
|
| 134 |
+
measurement = measurement.with_maximum(max_width)
|
| 135 |
+
return measurement
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__": # pragma: no cover
|
| 139 |
+
from pip._vendor.rich import print
|
| 140 |
+
|
| 141 |
+
print(Padding("Hello, World", (2, 4), style="on blue"))
|
env/lib/python3.13/site-packages/pip/_vendor/rich/palette.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from math import sqrt
|
| 2 |
+
from functools import lru_cache
|
| 3 |
+
from typing import Sequence, Tuple, TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
from .color_triplet import ColorTriplet
|
| 6 |
+
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
from pip._vendor.rich.table import Table
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Palette:
|
| 12 |
+
"""A palette of available colors."""
|
| 13 |
+
|
| 14 |
+
def __init__(self, colors: Sequence[Tuple[int, int, int]]):
|
| 15 |
+
self._colors = colors
|
| 16 |
+
|
| 17 |
+
def __getitem__(self, number: int) -> ColorTriplet:
|
| 18 |
+
return ColorTriplet(*self._colors[number])
|
| 19 |
+
|
| 20 |
+
def __rich__(self) -> "Table":
|
| 21 |
+
from pip._vendor.rich.color import Color
|
| 22 |
+
from pip._vendor.rich.style import Style
|
| 23 |
+
from pip._vendor.rich.text import Text
|
| 24 |
+
from pip._vendor.rich.table import Table
|
| 25 |
+
|
| 26 |
+
table = Table(
|
| 27 |
+
"index",
|
| 28 |
+
"RGB",
|
| 29 |
+
"Color",
|
| 30 |
+
title="Palette",
|
| 31 |
+
caption=f"{len(self._colors)} colors",
|
| 32 |
+
highlight=True,
|
| 33 |
+
caption_justify="right",
|
| 34 |
+
)
|
| 35 |
+
for index, color in enumerate(self._colors):
|
| 36 |
+
table.add_row(
|
| 37 |
+
str(index),
|
| 38 |
+
repr(color),
|
| 39 |
+
Text(" " * 16, style=Style(bgcolor=Color.from_rgb(*color))),
|
| 40 |
+
)
|
| 41 |
+
return table
|
| 42 |
+
|
| 43 |
+
# This is somewhat inefficient and needs caching
|
| 44 |
+
@lru_cache(maxsize=1024)
|
| 45 |
+
def match(self, color: Tuple[int, int, int]) -> int:
|
| 46 |
+
"""Find a color from a palette that most closely matches a given color.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
color (Tuple[int, int, int]): RGB components in range 0 > 255.
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
int: Index of closes matching color.
|
| 53 |
+
"""
|
| 54 |
+
red1, green1, blue1 = color
|
| 55 |
+
_sqrt = sqrt
|
| 56 |
+
get_color = self._colors.__getitem__
|
| 57 |
+
|
| 58 |
+
def get_color_distance(index: int) -> float:
|
| 59 |
+
"""Get the distance to a color."""
|
| 60 |
+
red2, green2, blue2 = get_color(index)
|
| 61 |
+
red_mean = (red1 + red2) // 2
|
| 62 |
+
red = red1 - red2
|
| 63 |
+
green = green1 - green2
|
| 64 |
+
blue = blue1 - blue2
|
| 65 |
+
return _sqrt(
|
| 66 |
+
(((512 + red_mean) * red * red) >> 8)
|
| 67 |
+
+ 4 * green * green
|
| 68 |
+
+ (((767 - red_mean) * blue * blue) >> 8)
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
min_index = min(range(len(self._colors)), key=get_color_distance)
|
| 72 |
+
return min_index
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__": # pragma: no cover
|
| 76 |
+
import colorsys
|
| 77 |
+
from typing import Iterable
|
| 78 |
+
from pip._vendor.rich.color import Color
|
| 79 |
+
from pip._vendor.rich.console import Console, ConsoleOptions
|
| 80 |
+
from pip._vendor.rich.segment import Segment
|
| 81 |
+
from pip._vendor.rich.style import Style
|
| 82 |
+
|
| 83 |
+
class ColorBox:
|
| 84 |
+
def __rich_console__(
|
| 85 |
+
self, console: Console, options: ConsoleOptions
|
| 86 |
+
) -> Iterable[Segment]:
|
| 87 |
+
height = console.size.height - 3
|
| 88 |
+
for y in range(0, height):
|
| 89 |
+
for x in range(options.max_width):
|
| 90 |
+
h = x / options.max_width
|
| 91 |
+
l = y / (height + 1)
|
| 92 |
+
r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
|
| 93 |
+
r2, g2, b2 = colorsys.hls_to_rgb(h, l + (1 / height / 2), 1.0)
|
| 94 |
+
bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
|
| 95 |
+
color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
|
| 96 |
+
yield Segment("▄", Style(color=color, bgcolor=bgcolor))
|
| 97 |
+
yield Segment.line()
|
| 98 |
+
|
|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 |
+
from .align import AlignMethod
|
| 4 |
+
from .box import ROUNDED, Box
|
| 5 |
+
from .cells import cell_len
|
| 6 |
+
from .jupyter import JupyterMixin
|
| 7 |
+
from .measure import Measurement, measure_renderables
|
| 8 |
+
from .padding import Padding, PaddingDimensions
|
| 9 |
+
from .segment import Segment
|
| 10 |
+
from .style import Style, StyleType
|
| 11 |
+
from .text import Text, TextType
|
| 12 |
+
|
| 13 |
+
if TYPE_CHECKING:
|
| 14 |
+
from .console import Console, ConsoleOptions, RenderableType, RenderResult
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Panel(JupyterMixin):
|
| 18 |
+
"""A console renderable that draws a border around its contents.
|
| 19 |
+
|
| 20 |
+
Example:
|
| 21 |
+
>>> console.print(Panel("Hello, World!"))
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
renderable (RenderableType): A console renderable object.
|
| 25 |
+
box (Box): A Box instance that defines the look of the border (see :ref:`appendix_box`. Defaults to box.ROUNDED.
|
| 26 |
+
title (Optional[TextType], optional): Optional title displayed in panel header. Defaults to None.
|
| 27 |
+
title_align (AlignMethod, optional): Alignment of title. Defaults to "center".
|
| 28 |
+
subtitle (Optional[TextType], optional): Optional subtitle displayed in panel footer. Defaults to None.
|
| 29 |
+
subtitle_align (AlignMethod, optional): Alignment of subtitle. Defaults to "center".
|
| 30 |
+
safe_box (bool, optional): Disable box characters that don't display on windows legacy terminal with *raster* fonts. Defaults to True.
|
| 31 |
+
expand (bool, optional): If True the panel will stretch to fill the console width, otherwise it will be sized to fit the contents. Defaults to True.
|
| 32 |
+
style (str, optional): The style of the panel (border and contents). Defaults to "none".
|
| 33 |
+
border_style (str, optional): The style of the border. Defaults to "none".
|
| 34 |
+
width (Optional[int], optional): Optional width of panel. Defaults to None to auto-detect.
|
| 35 |
+
height (Optional[int], optional): Optional height of panel. Defaults to None to auto-detect.
|
| 36 |
+
padding (Optional[PaddingDimensions]): Optional padding around renderable. Defaults to 0.
|
| 37 |
+
highlight (bool, optional): Enable automatic highlighting of panel title (if str). Defaults to False.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
def __init__(
|
| 41 |
+
self,
|
| 42 |
+
renderable: "RenderableType",
|
| 43 |
+
box: Box = ROUNDED,
|
| 44 |
+
*,
|
| 45 |
+
title: Optional[TextType] = None,
|
| 46 |
+
title_align: AlignMethod = "center",
|
| 47 |
+
subtitle: Optional[TextType] = None,
|
| 48 |
+
subtitle_align: AlignMethod = "center",
|
| 49 |
+
safe_box: Optional[bool] = None,
|
| 50 |
+
expand: bool = True,
|
| 51 |
+
style: StyleType = "none",
|
| 52 |
+
border_style: StyleType = "none",
|
| 53 |
+
width: Optional[int] = None,
|
| 54 |
+
height: Optional[int] = None,
|
| 55 |
+
padding: PaddingDimensions = (0, 1),
|
| 56 |
+
highlight: bool = False,
|
| 57 |
+
) -> None:
|
| 58 |
+
self.renderable = renderable
|
| 59 |
+
self.box = box
|
| 60 |
+
self.title = title
|
| 61 |
+
self.title_align: AlignMethod = title_align
|
| 62 |
+
self.subtitle = subtitle
|
| 63 |
+
self.subtitle_align = subtitle_align
|
| 64 |
+
self.safe_box = safe_box
|
| 65 |
+
self.expand = expand
|
| 66 |
+
self.style = style
|
| 67 |
+
self.border_style = border_style
|
| 68 |
+
self.width = width
|
| 69 |
+
self.height = height
|
| 70 |
+
self.padding = padding
|
| 71 |
+
self.highlight = highlight
|
| 72 |
+
|
| 73 |
+
@classmethod
|
| 74 |
+
def fit(
|
| 75 |
+
cls,
|
| 76 |
+
renderable: "RenderableType",
|
| 77 |
+
box: Box = ROUNDED,
|
| 78 |
+
*,
|
| 79 |
+
title: Optional[TextType] = None,
|
| 80 |
+
title_align: AlignMethod = "center",
|
| 81 |
+
subtitle: Optional[TextType] = None,
|
| 82 |
+
subtitle_align: AlignMethod = "center",
|
| 83 |
+
safe_box: Optional[bool] = None,
|
| 84 |
+
style: StyleType = "none",
|
| 85 |
+
border_style: StyleType = "none",
|
| 86 |
+
width: Optional[int] = None,
|
| 87 |
+
height: Optional[int] = None,
|
| 88 |
+
padding: PaddingDimensions = (0, 1),
|
| 89 |
+
highlight: bool = False,
|
| 90 |
+
) -> "Panel":
|
| 91 |
+
"""An alternative constructor that sets expand=False."""
|
| 92 |
+
return cls(
|
| 93 |
+
renderable,
|
| 94 |
+
box,
|
| 95 |
+
title=title,
|
| 96 |
+
title_align=title_align,
|
| 97 |
+
subtitle=subtitle,
|
| 98 |
+
subtitle_align=subtitle_align,
|
| 99 |
+
safe_box=safe_box,
|
| 100 |
+
style=style,
|
| 101 |
+
border_style=border_style,
|
| 102 |
+
width=width,
|
| 103 |
+
height=height,
|
| 104 |
+
padding=padding,
|
| 105 |
+
highlight=highlight,
|
| 106 |
+
expand=False,
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
@property
|
| 110 |
+
def _title(self) -> Optional[Text]:
|
| 111 |
+
if self.title:
|
| 112 |
+
title_text = (
|
| 113 |
+
Text.from_markup(self.title)
|
| 114 |
+
if isinstance(self.title, str)
|
| 115 |
+
else self.title.copy()
|
| 116 |
+
)
|
| 117 |
+
title_text.end = ""
|
| 118 |
+
title_text.plain = title_text.plain.replace("\n", " ")
|
| 119 |
+
title_text.no_wrap = True
|
| 120 |
+
title_text.expand_tabs()
|
| 121 |
+
title_text.pad(1)
|
| 122 |
+
return title_text
|
| 123 |
+
return None
|
| 124 |
+
|
| 125 |
+
@property
|
| 126 |
+
def _subtitle(self) -> Optional[Text]:
|
| 127 |
+
if self.subtitle:
|
| 128 |
+
subtitle_text = (
|
| 129 |
+
Text.from_markup(self.subtitle)
|
| 130 |
+
if isinstance(self.subtitle, str)
|
| 131 |
+
else self.subtitle.copy()
|
| 132 |
+
)
|
| 133 |
+
subtitle_text.end = ""
|
| 134 |
+
subtitle_text.plain = subtitle_text.plain.replace("\n", " ")
|
| 135 |
+
subtitle_text.no_wrap = True
|
| 136 |
+
subtitle_text.expand_tabs()
|
| 137 |
+
subtitle_text.pad(1)
|
| 138 |
+
return subtitle_text
|
| 139 |
+
return None
|
| 140 |
+
|
| 141 |
+
def __rich_console__(
|
| 142 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 143 |
+
) -> "RenderResult":
|
| 144 |
+
_padding = Padding.unpack(self.padding)
|
| 145 |
+
renderable = (
|
| 146 |
+
Padding(self.renderable, _padding) if any(_padding) else self.renderable
|
| 147 |
+
)
|
| 148 |
+
style = console.get_style(self.style)
|
| 149 |
+
partial_border_style = console.get_style(self.border_style)
|
| 150 |
+
border_style = style + partial_border_style
|
| 151 |
+
width = (
|
| 152 |
+
options.max_width
|
| 153 |
+
if self.width is None
|
| 154 |
+
else min(options.max_width, self.width)
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
safe_box: bool = console.safe_box if self.safe_box is None else self.safe_box
|
| 158 |
+
box = self.box.substitute(options, safe=safe_box)
|
| 159 |
+
|
| 160 |
+
def align_text(
|
| 161 |
+
text: Text, width: int, align: str, character: str, style: Style
|
| 162 |
+
) -> Text:
|
| 163 |
+
"""Gets new aligned text.
|
| 164 |
+
|
| 165 |
+
Args:
|
| 166 |
+
text (Text): Title or subtitle text.
|
| 167 |
+
width (int): Desired width.
|
| 168 |
+
align (str): Alignment.
|
| 169 |
+
character (str): Character for alignment.
|
| 170 |
+
style (Style): Border style
|
| 171 |
+
|
| 172 |
+
Returns:
|
| 173 |
+
Text: New text instance
|
| 174 |
+
"""
|
| 175 |
+
text = text.copy()
|
| 176 |
+
text.truncate(width)
|
| 177 |
+
excess_space = width - cell_len(text.plain)
|
| 178 |
+
if text.style:
|
| 179 |
+
text.stylize(console.get_style(text.style))
|
| 180 |
+
|
| 181 |
+
if excess_space:
|
| 182 |
+
if align == "left":
|
| 183 |
+
return Text.assemble(
|
| 184 |
+
text,
|
| 185 |
+
(character * excess_space, style),
|
| 186 |
+
no_wrap=True,
|
| 187 |
+
end="",
|
| 188 |
+
)
|
| 189 |
+
elif align == "center":
|
| 190 |
+
left = excess_space // 2
|
| 191 |
+
return Text.assemble(
|
| 192 |
+
(character * left, style),
|
| 193 |
+
text,
|
| 194 |
+
(character * (excess_space - left), style),
|
| 195 |
+
no_wrap=True,
|
| 196 |
+
end="",
|
| 197 |
+
)
|
| 198 |
+
else:
|
| 199 |
+
return Text.assemble(
|
| 200 |
+
(character * excess_space, style),
|
| 201 |
+
text,
|
| 202 |
+
no_wrap=True,
|
| 203 |
+
end="",
|
| 204 |
+
)
|
| 205 |
+
return text
|
| 206 |
+
|
| 207 |
+
title_text = self._title
|
| 208 |
+
if title_text is not None:
|
| 209 |
+
title_text.stylize_before(partial_border_style)
|
| 210 |
+
|
| 211 |
+
child_width = (
|
| 212 |
+
width - 2
|
| 213 |
+
if self.expand
|
| 214 |
+
else console.measure(
|
| 215 |
+
renderable, options=options.update_width(width - 2)
|
| 216 |
+
).maximum
|
| 217 |
+
)
|
| 218 |
+
child_height = self.height or options.height or None
|
| 219 |
+
if child_height:
|
| 220 |
+
child_height -= 2
|
| 221 |
+
if title_text is not None:
|
| 222 |
+
child_width = min(
|
| 223 |
+
options.max_width - 2, max(child_width, title_text.cell_len + 2)
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
width = child_width + 2
|
| 227 |
+
child_options = options.update(
|
| 228 |
+
width=child_width, height=child_height, highlight=self.highlight
|
| 229 |
+
)
|
| 230 |
+
lines = console.render_lines(renderable, child_options, style=style)
|
| 231 |
+
|
| 232 |
+
line_start = Segment(box.mid_left, border_style)
|
| 233 |
+
line_end = Segment(f"{box.mid_right}", border_style)
|
| 234 |
+
new_line = Segment.line()
|
| 235 |
+
if title_text is None or width <= 4:
|
| 236 |
+
yield Segment(box.get_top([width - 2]), border_style)
|
| 237 |
+
else:
|
| 238 |
+
title_text = align_text(
|
| 239 |
+
title_text,
|
| 240 |
+
width - 4,
|
| 241 |
+
self.title_align,
|
| 242 |
+
box.top,
|
| 243 |
+
border_style,
|
| 244 |
+
)
|
| 245 |
+
yield Segment(box.top_left + box.top, border_style)
|
| 246 |
+
yield from console.render(title_text, child_options.update_width(width - 4))
|
| 247 |
+
yield Segment(box.top + box.top_right, border_style)
|
| 248 |
+
|
| 249 |
+
yield new_line
|
| 250 |
+
for line in lines:
|
| 251 |
+
yield line_start
|
| 252 |
+
yield from line
|
| 253 |
+
yield line_end
|
| 254 |
+
yield new_line
|
| 255 |
+
|
| 256 |
+
subtitle_text = self._subtitle
|
| 257 |
+
if subtitle_text is not None:
|
| 258 |
+
subtitle_text.stylize_before(partial_border_style)
|
| 259 |
+
|
| 260 |
+
if subtitle_text is None or width <= 4:
|
| 261 |
+
yield Segment(box.get_bottom([width - 2]), border_style)
|
| 262 |
+
else:
|
| 263 |
+
subtitle_text = align_text(
|
| 264 |
+
subtitle_text,
|
| 265 |
+
width - 4,
|
| 266 |
+
self.subtitle_align,
|
| 267 |
+
box.bottom,
|
| 268 |
+
border_style,
|
| 269 |
+
)
|
| 270 |
+
yield Segment(box.bottom_left + box.bottom, border_style)
|
| 271 |
+
yield from console.render(
|
| 272 |
+
subtitle_text, child_options.update_width(width - 4)
|
| 273 |
+
)
|
| 274 |
+
yield Segment(box.bottom + box.bottom_right, border_style)
|
| 275 |
+
|
| 276 |
+
yield new_line
|
| 277 |
+
|
| 278 |
+
def __rich_measure__(
|
| 279 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 280 |
+
) -> "Measurement":
|
| 281 |
+
_title = self._title
|
| 282 |
+
_, right, _, left = Padding.unpack(self.padding)
|
| 283 |
+
padding = left + right
|
| 284 |
+
renderables = [self.renderable, _title] if _title else [self.renderable]
|
| 285 |
+
|
| 286 |
+
if self.width is None:
|
| 287 |
+
width = (
|
| 288 |
+
measure_renderables(
|
| 289 |
+
console,
|
| 290 |
+
options.update_width(options.max_width - padding - 2),
|
| 291 |
+
renderables,
|
| 292 |
+
).maximum
|
| 293 |
+
+ padding
|
| 294 |
+
+ 2
|
| 295 |
+
)
|
| 296 |
+
else:
|
| 297 |
+
width = self.width
|
| 298 |
+
return Measurement(width, width)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
if __name__ == "__main__": # pragma: no cover
|
| 302 |
+
from .console import Console
|
| 303 |
+
|
| 304 |
+
c = Console()
|
| 305 |
+
|
| 306 |
+
from .box import DOUBLE, ROUNDED
|
| 307 |
+
from .padding import Padding
|
| 308 |
+
|
| 309 |
+
p = Panel(
|
| 310 |
+
"Hello, World!",
|
| 311 |
+
title="rich.Panel",
|
| 312 |
+
style="white on blue",
|
| 313 |
+
box=DOUBLE,
|
| 314 |
+
padding=1,
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
c.print()
|
| 318 |
+
c.print(p)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/pretty.py
ADDED
|
@@ -0,0 +1,1016 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import builtins
|
| 2 |
+
import collections
|
| 3 |
+
import dataclasses
|
| 4 |
+
import inspect
|
| 5 |
+
import os
|
| 6 |
+
import reprlib
|
| 7 |
+
import sys
|
| 8 |
+
from array import array
|
| 9 |
+
from collections import Counter, UserDict, UserList, defaultdict, deque
|
| 10 |
+
from dataclasses import dataclass, fields, is_dataclass
|
| 11 |
+
from inspect import isclass
|
| 12 |
+
from itertools import islice
|
| 13 |
+
from types import MappingProxyType
|
| 14 |
+
from typing import (
|
| 15 |
+
TYPE_CHECKING,
|
| 16 |
+
Any,
|
| 17 |
+
Callable,
|
| 18 |
+
DefaultDict,
|
| 19 |
+
Deque,
|
| 20 |
+
Dict,
|
| 21 |
+
Iterable,
|
| 22 |
+
List,
|
| 23 |
+
Optional,
|
| 24 |
+
Sequence,
|
| 25 |
+
Set,
|
| 26 |
+
Tuple,
|
| 27 |
+
Union,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
from pip._vendor.rich.repr import RichReprResult
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
import attr as _attr_module
|
| 34 |
+
|
| 35 |
+
_has_attrs = hasattr(_attr_module, "ib")
|
| 36 |
+
except ImportError: # pragma: no cover
|
| 37 |
+
_has_attrs = False
|
| 38 |
+
|
| 39 |
+
from . import get_console
|
| 40 |
+
from ._loop import loop_last
|
| 41 |
+
from ._pick import pick_bool
|
| 42 |
+
from .abc import RichRenderable
|
| 43 |
+
from .cells import cell_len
|
| 44 |
+
from .highlighter import ReprHighlighter
|
| 45 |
+
from .jupyter import JupyterMixin, JupyterRenderable
|
| 46 |
+
from .measure import Measurement
|
| 47 |
+
from .text import Text
|
| 48 |
+
|
| 49 |
+
if TYPE_CHECKING:
|
| 50 |
+
from .console import (
|
| 51 |
+
Console,
|
| 52 |
+
ConsoleOptions,
|
| 53 |
+
HighlighterType,
|
| 54 |
+
JustifyMethod,
|
| 55 |
+
OverflowMethod,
|
| 56 |
+
RenderResult,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _is_attr_object(obj: Any) -> bool:
|
| 61 |
+
"""Check if an object was created with attrs module."""
|
| 62 |
+
return _has_attrs and _attr_module.has(type(obj))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def _get_attr_fields(obj: Any) -> Sequence["_attr_module.Attribute[Any]"]:
|
| 66 |
+
"""Get fields for an attrs object."""
|
| 67 |
+
return _attr_module.fields(type(obj)) if _has_attrs else []
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def _is_dataclass_repr(obj: object) -> bool:
|
| 71 |
+
"""Check if an instance of a dataclass contains the default repr.
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
obj (object): A dataclass instance.
|
| 75 |
+
|
| 76 |
+
Returns:
|
| 77 |
+
bool: True if the default repr is used, False if there is a custom repr.
|
| 78 |
+
"""
|
| 79 |
+
# Digging in to a lot of internals here
|
| 80 |
+
# Catching all exceptions in case something is missing on a non CPython implementation
|
| 81 |
+
try:
|
| 82 |
+
return obj.__repr__.__code__.co_filename in (
|
| 83 |
+
dataclasses.__file__,
|
| 84 |
+
reprlib.__file__,
|
| 85 |
+
)
|
| 86 |
+
except Exception: # pragma: no coverage
|
| 87 |
+
return False
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
_dummy_namedtuple = collections.namedtuple("_dummy_namedtuple", [])
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def _has_default_namedtuple_repr(obj: object) -> bool:
|
| 94 |
+
"""Check if an instance of namedtuple contains the default repr
|
| 95 |
+
|
| 96 |
+
Args:
|
| 97 |
+
obj (object): A namedtuple
|
| 98 |
+
|
| 99 |
+
Returns:
|
| 100 |
+
bool: True if the default repr is used, False if there's a custom repr.
|
| 101 |
+
"""
|
| 102 |
+
obj_file = None
|
| 103 |
+
try:
|
| 104 |
+
obj_file = inspect.getfile(obj.__repr__)
|
| 105 |
+
except (OSError, TypeError):
|
| 106 |
+
# OSError handles case where object is defined in __main__ scope, e.g. REPL - no filename available.
|
| 107 |
+
# TypeError trapped defensively, in case of object without filename slips through.
|
| 108 |
+
pass
|
| 109 |
+
default_repr_file = inspect.getfile(_dummy_namedtuple.__repr__)
|
| 110 |
+
return obj_file == default_repr_file
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def _ipy_display_hook(
|
| 114 |
+
value: Any,
|
| 115 |
+
console: Optional["Console"] = None,
|
| 116 |
+
overflow: "OverflowMethod" = "ignore",
|
| 117 |
+
crop: bool = False,
|
| 118 |
+
indent_guides: bool = False,
|
| 119 |
+
max_length: Optional[int] = None,
|
| 120 |
+
max_string: Optional[int] = None,
|
| 121 |
+
max_depth: Optional[int] = None,
|
| 122 |
+
expand_all: bool = False,
|
| 123 |
+
) -> Union[str, None]:
|
| 124 |
+
# needed here to prevent circular import:
|
| 125 |
+
from .console import ConsoleRenderable
|
| 126 |
+
|
| 127 |
+
# always skip rich generated jupyter renderables or None values
|
| 128 |
+
if _safe_isinstance(value, JupyterRenderable) or value is None:
|
| 129 |
+
return None
|
| 130 |
+
|
| 131 |
+
console = console or get_console()
|
| 132 |
+
|
| 133 |
+
with console.capture() as capture:
|
| 134 |
+
# certain renderables should start on a new line
|
| 135 |
+
if _safe_isinstance(value, ConsoleRenderable):
|
| 136 |
+
console.line()
|
| 137 |
+
console.print(
|
| 138 |
+
(
|
| 139 |
+
value
|
| 140 |
+
if _safe_isinstance(value, RichRenderable)
|
| 141 |
+
else Pretty(
|
| 142 |
+
value,
|
| 143 |
+
overflow=overflow,
|
| 144 |
+
indent_guides=indent_guides,
|
| 145 |
+
max_length=max_length,
|
| 146 |
+
max_string=max_string,
|
| 147 |
+
max_depth=max_depth,
|
| 148 |
+
expand_all=expand_all,
|
| 149 |
+
margin=12,
|
| 150 |
+
)
|
| 151 |
+
),
|
| 152 |
+
crop=crop,
|
| 153 |
+
new_line_start=True,
|
| 154 |
+
end="",
|
| 155 |
+
)
|
| 156 |
+
# strip trailing newline, not usually part of a text repr
|
| 157 |
+
# I'm not sure if this should be prevented at a lower level
|
| 158 |
+
return capture.get().rstrip("\n")
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def _safe_isinstance(
|
| 162 |
+
obj: object, class_or_tuple: Union[type, Tuple[type, ...]]
|
| 163 |
+
) -> bool:
|
| 164 |
+
"""isinstance can fail in rare cases, for example types with no __class__"""
|
| 165 |
+
try:
|
| 166 |
+
return isinstance(obj, class_or_tuple)
|
| 167 |
+
except Exception:
|
| 168 |
+
return False
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def install(
|
| 172 |
+
console: Optional["Console"] = None,
|
| 173 |
+
overflow: "OverflowMethod" = "ignore",
|
| 174 |
+
crop: bool = False,
|
| 175 |
+
indent_guides: bool = False,
|
| 176 |
+
max_length: Optional[int] = None,
|
| 177 |
+
max_string: Optional[int] = None,
|
| 178 |
+
max_depth: Optional[int] = None,
|
| 179 |
+
expand_all: bool = False,
|
| 180 |
+
) -> None:
|
| 181 |
+
"""Install automatic pretty printing in the Python REPL.
|
| 182 |
+
|
| 183 |
+
Args:
|
| 184 |
+
console (Console, optional): Console instance or ``None`` to use global console. Defaults to None.
|
| 185 |
+
overflow (Optional[OverflowMethod], optional): Overflow method. Defaults to "ignore".
|
| 186 |
+
crop (Optional[bool], optional): Enable cropping of long lines. Defaults to False.
|
| 187 |
+
indent_guides (bool, optional): Enable indentation guides. Defaults to False.
|
| 188 |
+
max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
|
| 189 |
+
Defaults to None.
|
| 190 |
+
max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.
|
| 191 |
+
max_depth (int, optional): Maximum depth of nested data structures, or None for no maximum. Defaults to None.
|
| 192 |
+
expand_all (bool, optional): Expand all containers. Defaults to False.
|
| 193 |
+
max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.
|
| 194 |
+
"""
|
| 195 |
+
from pip._vendor.rich import get_console
|
| 196 |
+
|
| 197 |
+
console = console or get_console()
|
| 198 |
+
assert console is not None
|
| 199 |
+
|
| 200 |
+
def display_hook(value: Any) -> None:
|
| 201 |
+
"""Replacement sys.displayhook which prettifies objects with Rich."""
|
| 202 |
+
if value is not None:
|
| 203 |
+
assert console is not None
|
| 204 |
+
builtins._ = None # type: ignore[attr-defined]
|
| 205 |
+
console.print(
|
| 206 |
+
(
|
| 207 |
+
value
|
| 208 |
+
if _safe_isinstance(value, RichRenderable)
|
| 209 |
+
else Pretty(
|
| 210 |
+
value,
|
| 211 |
+
overflow=overflow,
|
| 212 |
+
indent_guides=indent_guides,
|
| 213 |
+
max_length=max_length,
|
| 214 |
+
max_string=max_string,
|
| 215 |
+
max_depth=max_depth,
|
| 216 |
+
expand_all=expand_all,
|
| 217 |
+
)
|
| 218 |
+
),
|
| 219 |
+
crop=crop,
|
| 220 |
+
)
|
| 221 |
+
builtins._ = value # type: ignore[attr-defined]
|
| 222 |
+
|
| 223 |
+
try:
|
| 224 |
+
ip = get_ipython() # type: ignore[name-defined]
|
| 225 |
+
except NameError:
|
| 226 |
+
sys.displayhook = display_hook
|
| 227 |
+
else:
|
| 228 |
+
from IPython.core.formatters import BaseFormatter
|
| 229 |
+
|
| 230 |
+
class RichFormatter(BaseFormatter): # type: ignore[misc]
|
| 231 |
+
pprint: bool = True
|
| 232 |
+
|
| 233 |
+
def __call__(self, value: Any) -> Any:
|
| 234 |
+
if self.pprint:
|
| 235 |
+
return _ipy_display_hook(
|
| 236 |
+
value,
|
| 237 |
+
console=get_console(),
|
| 238 |
+
overflow=overflow,
|
| 239 |
+
indent_guides=indent_guides,
|
| 240 |
+
max_length=max_length,
|
| 241 |
+
max_string=max_string,
|
| 242 |
+
max_depth=max_depth,
|
| 243 |
+
expand_all=expand_all,
|
| 244 |
+
)
|
| 245 |
+
else:
|
| 246 |
+
return repr(value)
|
| 247 |
+
|
| 248 |
+
# replace plain text formatter with rich formatter
|
| 249 |
+
rich_formatter = RichFormatter()
|
| 250 |
+
ip.display_formatter.formatters["text/plain"] = rich_formatter
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
class Pretty(JupyterMixin):
|
| 254 |
+
"""A rich renderable that pretty prints an object.
|
| 255 |
+
|
| 256 |
+
Args:
|
| 257 |
+
_object (Any): An object to pretty print.
|
| 258 |
+
highlighter (HighlighterType, optional): Highlighter object to apply to result, or None for ReprHighlighter. Defaults to None.
|
| 259 |
+
indent_size (int, optional): Number of spaces in indent. Defaults to 4.
|
| 260 |
+
justify (JustifyMethod, optional): Justify method, or None for default. Defaults to None.
|
| 261 |
+
overflow (OverflowMethod, optional): Overflow method, or None for default. Defaults to None.
|
| 262 |
+
no_wrap (Optional[bool], optional): Disable word wrapping. Defaults to False.
|
| 263 |
+
indent_guides (bool, optional): Enable indentation guides. Defaults to False.
|
| 264 |
+
max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
|
| 265 |
+
Defaults to None.
|
| 266 |
+
max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.
|
| 267 |
+
max_depth (int, optional): Maximum depth of nested data structures, or None for no maximum. Defaults to None.
|
| 268 |
+
expand_all (bool, optional): Expand all containers. Defaults to False.
|
| 269 |
+
margin (int, optional): Subtrace a margin from width to force containers to expand earlier. Defaults to 0.
|
| 270 |
+
insert_line (bool, optional): Insert a new line if the output has multiple new lines. Defaults to False.
|
| 271 |
+
"""
|
| 272 |
+
|
| 273 |
+
def __init__(
|
| 274 |
+
self,
|
| 275 |
+
_object: Any,
|
| 276 |
+
highlighter: Optional["HighlighterType"] = None,
|
| 277 |
+
*,
|
| 278 |
+
indent_size: int = 4,
|
| 279 |
+
justify: Optional["JustifyMethod"] = None,
|
| 280 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 281 |
+
no_wrap: Optional[bool] = False,
|
| 282 |
+
indent_guides: bool = False,
|
| 283 |
+
max_length: Optional[int] = None,
|
| 284 |
+
max_string: Optional[int] = None,
|
| 285 |
+
max_depth: Optional[int] = None,
|
| 286 |
+
expand_all: bool = False,
|
| 287 |
+
margin: int = 0,
|
| 288 |
+
insert_line: bool = False,
|
| 289 |
+
) -> None:
|
| 290 |
+
self._object = _object
|
| 291 |
+
self.highlighter = highlighter or ReprHighlighter()
|
| 292 |
+
self.indent_size = indent_size
|
| 293 |
+
self.justify: Optional["JustifyMethod"] = justify
|
| 294 |
+
self.overflow: Optional["OverflowMethod"] = overflow
|
| 295 |
+
self.no_wrap = no_wrap
|
| 296 |
+
self.indent_guides = indent_guides
|
| 297 |
+
self.max_length = max_length
|
| 298 |
+
self.max_string = max_string
|
| 299 |
+
self.max_depth = max_depth
|
| 300 |
+
self.expand_all = expand_all
|
| 301 |
+
self.margin = margin
|
| 302 |
+
self.insert_line = insert_line
|
| 303 |
+
|
| 304 |
+
def __rich_console__(
|
| 305 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 306 |
+
) -> "RenderResult":
|
| 307 |
+
pretty_str = pretty_repr(
|
| 308 |
+
self._object,
|
| 309 |
+
max_width=options.max_width - self.margin,
|
| 310 |
+
indent_size=self.indent_size,
|
| 311 |
+
max_length=self.max_length,
|
| 312 |
+
max_string=self.max_string,
|
| 313 |
+
max_depth=self.max_depth,
|
| 314 |
+
expand_all=self.expand_all,
|
| 315 |
+
)
|
| 316 |
+
pretty_text = Text.from_ansi(
|
| 317 |
+
pretty_str,
|
| 318 |
+
justify=self.justify or options.justify,
|
| 319 |
+
overflow=self.overflow or options.overflow,
|
| 320 |
+
no_wrap=pick_bool(self.no_wrap, options.no_wrap),
|
| 321 |
+
style="pretty",
|
| 322 |
+
)
|
| 323 |
+
pretty_text = (
|
| 324 |
+
self.highlighter(pretty_text)
|
| 325 |
+
if pretty_text
|
| 326 |
+
else Text(
|
| 327 |
+
f"{type(self._object)}.__repr__ returned empty string",
|
| 328 |
+
style="dim italic",
|
| 329 |
+
)
|
| 330 |
+
)
|
| 331 |
+
if self.indent_guides and not options.ascii_only:
|
| 332 |
+
pretty_text = pretty_text.with_indent_guides(
|
| 333 |
+
self.indent_size, style="repr.indent"
|
| 334 |
+
)
|
| 335 |
+
if self.insert_line and "\n" in pretty_text:
|
| 336 |
+
yield ""
|
| 337 |
+
yield pretty_text
|
| 338 |
+
|
| 339 |
+
def __rich_measure__(
|
| 340 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 341 |
+
) -> "Measurement":
|
| 342 |
+
pretty_str = pretty_repr(
|
| 343 |
+
self._object,
|
| 344 |
+
max_width=options.max_width,
|
| 345 |
+
indent_size=self.indent_size,
|
| 346 |
+
max_length=self.max_length,
|
| 347 |
+
max_string=self.max_string,
|
| 348 |
+
max_depth=self.max_depth,
|
| 349 |
+
expand_all=self.expand_all,
|
| 350 |
+
)
|
| 351 |
+
text_width = (
|
| 352 |
+
max(cell_len(line) for line in pretty_str.splitlines()) if pretty_str else 0
|
| 353 |
+
)
|
| 354 |
+
return Measurement(text_width, text_width)
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
def _get_braces_for_defaultdict(_object: DefaultDict[Any, Any]) -> Tuple[str, str, str]:
|
| 358 |
+
return (
|
| 359 |
+
f"defaultdict({_object.default_factory!r}, {{",
|
| 360 |
+
"})",
|
| 361 |
+
f"defaultdict({_object.default_factory!r}, {{}})",
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
def _get_braces_for_deque(_object: Deque[Any]) -> Tuple[str, str, str]:
|
| 366 |
+
if _object.maxlen is None:
|
| 367 |
+
return ("deque([", "])", "deque()")
|
| 368 |
+
return (
|
| 369 |
+
"deque([",
|
| 370 |
+
f"], maxlen={_object.maxlen})",
|
| 371 |
+
f"deque(maxlen={_object.maxlen})",
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
def _get_braces_for_array(_object: "array[Any]") -> Tuple[str, str, str]:
|
| 376 |
+
return (f"array({_object.typecode!r}, [", "])", f"array({_object.typecode!r})")
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
_BRACES: Dict[type, Callable[[Any], Tuple[str, str, str]]] = {
|
| 380 |
+
os._Environ: lambda _object: ("environ({", "})", "environ({})"),
|
| 381 |
+
array: _get_braces_for_array,
|
| 382 |
+
defaultdict: _get_braces_for_defaultdict,
|
| 383 |
+
Counter: lambda _object: ("Counter({", "})", "Counter()"),
|
| 384 |
+
deque: _get_braces_for_deque,
|
| 385 |
+
dict: lambda _object: ("{", "}", "{}"),
|
| 386 |
+
UserDict: lambda _object: ("{", "}", "{}"),
|
| 387 |
+
frozenset: lambda _object: ("frozenset({", "})", "frozenset()"),
|
| 388 |
+
list: lambda _object: ("[", "]", "[]"),
|
| 389 |
+
UserList: lambda _object: ("[", "]", "[]"),
|
| 390 |
+
set: lambda _object: ("{", "}", "set()"),
|
| 391 |
+
tuple: lambda _object: ("(", ")", "()"),
|
| 392 |
+
MappingProxyType: lambda _object: ("mappingproxy({", "})", "mappingproxy({})"),
|
| 393 |
+
}
|
| 394 |
+
_CONTAINERS = tuple(_BRACES.keys())
|
| 395 |
+
_MAPPING_CONTAINERS = (dict, os._Environ, MappingProxyType, UserDict)
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
def is_expandable(obj: Any) -> bool:
|
| 399 |
+
"""Check if an object may be expanded by pretty print."""
|
| 400 |
+
return (
|
| 401 |
+
_safe_isinstance(obj, _CONTAINERS)
|
| 402 |
+
or (is_dataclass(obj))
|
| 403 |
+
or (hasattr(obj, "__rich_repr__"))
|
| 404 |
+
or _is_attr_object(obj)
|
| 405 |
+
) and not isclass(obj)
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
@dataclass
|
| 409 |
+
class Node:
|
| 410 |
+
"""A node in a repr tree. May be atomic or a container."""
|
| 411 |
+
|
| 412 |
+
key_repr: str = ""
|
| 413 |
+
value_repr: str = ""
|
| 414 |
+
open_brace: str = ""
|
| 415 |
+
close_brace: str = ""
|
| 416 |
+
empty: str = ""
|
| 417 |
+
last: bool = False
|
| 418 |
+
is_tuple: bool = False
|
| 419 |
+
is_namedtuple: bool = False
|
| 420 |
+
children: Optional[List["Node"]] = None
|
| 421 |
+
key_separator: str = ": "
|
| 422 |
+
separator: str = ", "
|
| 423 |
+
|
| 424 |
+
def iter_tokens(self) -> Iterable[str]:
|
| 425 |
+
"""Generate tokens for this node."""
|
| 426 |
+
if self.key_repr:
|
| 427 |
+
yield self.key_repr
|
| 428 |
+
yield self.key_separator
|
| 429 |
+
if self.value_repr:
|
| 430 |
+
yield self.value_repr
|
| 431 |
+
elif self.children is not None:
|
| 432 |
+
if self.children:
|
| 433 |
+
yield self.open_brace
|
| 434 |
+
if self.is_tuple and not self.is_namedtuple and len(self.children) == 1:
|
| 435 |
+
yield from self.children[0].iter_tokens()
|
| 436 |
+
yield ","
|
| 437 |
+
else:
|
| 438 |
+
for child in self.children:
|
| 439 |
+
yield from child.iter_tokens()
|
| 440 |
+
if not child.last:
|
| 441 |
+
yield self.separator
|
| 442 |
+
yield self.close_brace
|
| 443 |
+
else:
|
| 444 |
+
yield self.empty
|
| 445 |
+
|
| 446 |
+
def check_length(self, start_length: int, max_length: int) -> bool:
|
| 447 |
+
"""Check the length fits within a limit.
|
| 448 |
+
|
| 449 |
+
Args:
|
| 450 |
+
start_length (int): Starting length of the line (indent, prefix, suffix).
|
| 451 |
+
max_length (int): Maximum length.
|
| 452 |
+
|
| 453 |
+
Returns:
|
| 454 |
+
bool: True if the node can be rendered within max length, otherwise False.
|
| 455 |
+
"""
|
| 456 |
+
total_length = start_length
|
| 457 |
+
for token in self.iter_tokens():
|
| 458 |
+
total_length += cell_len(token)
|
| 459 |
+
if total_length > max_length:
|
| 460 |
+
return False
|
| 461 |
+
return True
|
| 462 |
+
|
| 463 |
+
def __str__(self) -> str:
|
| 464 |
+
repr_text = "".join(self.iter_tokens())
|
| 465 |
+
return repr_text
|
| 466 |
+
|
| 467 |
+
def render(
|
| 468 |
+
self, max_width: int = 80, indent_size: int = 4, expand_all: bool = False
|
| 469 |
+
) -> str:
|
| 470 |
+
"""Render the node to a pretty repr.
|
| 471 |
+
|
| 472 |
+
Args:
|
| 473 |
+
max_width (int, optional): Maximum width of the repr. Defaults to 80.
|
| 474 |
+
indent_size (int, optional): Size of indents. Defaults to 4.
|
| 475 |
+
expand_all (bool, optional): Expand all levels. Defaults to False.
|
| 476 |
+
|
| 477 |
+
Returns:
|
| 478 |
+
str: A repr string of the original object.
|
| 479 |
+
"""
|
| 480 |
+
lines = [_Line(node=self, is_root=True)]
|
| 481 |
+
line_no = 0
|
| 482 |
+
while line_no < len(lines):
|
| 483 |
+
line = lines[line_no]
|
| 484 |
+
if line.expandable and not line.expanded:
|
| 485 |
+
if expand_all or not line.check_length(max_width):
|
| 486 |
+
lines[line_no : line_no + 1] = line.expand(indent_size)
|
| 487 |
+
line_no += 1
|
| 488 |
+
|
| 489 |
+
repr_str = "\n".join(str(line) for line in lines)
|
| 490 |
+
return repr_str
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
@dataclass
|
| 494 |
+
class _Line:
|
| 495 |
+
"""A line in repr output."""
|
| 496 |
+
|
| 497 |
+
parent: Optional["_Line"] = None
|
| 498 |
+
is_root: bool = False
|
| 499 |
+
node: Optional[Node] = None
|
| 500 |
+
text: str = ""
|
| 501 |
+
suffix: str = ""
|
| 502 |
+
whitespace: str = ""
|
| 503 |
+
expanded: bool = False
|
| 504 |
+
last: bool = False
|
| 505 |
+
|
| 506 |
+
@property
|
| 507 |
+
def expandable(self) -> bool:
|
| 508 |
+
"""Check if the line may be expanded."""
|
| 509 |
+
return bool(self.node is not None and self.node.children)
|
| 510 |
+
|
| 511 |
+
def check_length(self, max_length: int) -> bool:
|
| 512 |
+
"""Check this line fits within a given number of cells."""
|
| 513 |
+
start_length = (
|
| 514 |
+
len(self.whitespace) + cell_len(self.text) + cell_len(self.suffix)
|
| 515 |
+
)
|
| 516 |
+
assert self.node is not None
|
| 517 |
+
return self.node.check_length(start_length, max_length)
|
| 518 |
+
|
| 519 |
+
def expand(self, indent_size: int) -> Iterable["_Line"]:
|
| 520 |
+
"""Expand this line by adding children on their own line."""
|
| 521 |
+
node = self.node
|
| 522 |
+
assert node is not None
|
| 523 |
+
whitespace = self.whitespace
|
| 524 |
+
assert node.children
|
| 525 |
+
if node.key_repr:
|
| 526 |
+
new_line = yield _Line(
|
| 527 |
+
text=f"{node.key_repr}{node.key_separator}{node.open_brace}",
|
| 528 |
+
whitespace=whitespace,
|
| 529 |
+
)
|
| 530 |
+
else:
|
| 531 |
+
new_line = yield _Line(text=node.open_brace, whitespace=whitespace)
|
| 532 |
+
child_whitespace = self.whitespace + " " * indent_size
|
| 533 |
+
tuple_of_one = node.is_tuple and len(node.children) == 1
|
| 534 |
+
for last, child in loop_last(node.children):
|
| 535 |
+
separator = "," if tuple_of_one else node.separator
|
| 536 |
+
line = _Line(
|
| 537 |
+
parent=new_line,
|
| 538 |
+
node=child,
|
| 539 |
+
whitespace=child_whitespace,
|
| 540 |
+
suffix=separator,
|
| 541 |
+
last=last and not tuple_of_one,
|
| 542 |
+
)
|
| 543 |
+
yield line
|
| 544 |
+
|
| 545 |
+
yield _Line(
|
| 546 |
+
text=node.close_brace,
|
| 547 |
+
whitespace=whitespace,
|
| 548 |
+
suffix=self.suffix,
|
| 549 |
+
last=self.last,
|
| 550 |
+
)
|
| 551 |
+
|
| 552 |
+
def __str__(self) -> str:
|
| 553 |
+
if self.last:
|
| 554 |
+
return f"{self.whitespace}{self.text}{self.node or ''}"
|
| 555 |
+
else:
|
| 556 |
+
return (
|
| 557 |
+
f"{self.whitespace}{self.text}{self.node or ''}{self.suffix.rstrip()}"
|
| 558 |
+
)
|
| 559 |
+
|
| 560 |
+
|
| 561 |
+
def _is_namedtuple(obj: Any) -> bool:
|
| 562 |
+
"""Checks if an object is most likely a namedtuple. It is possible
|
| 563 |
+
to craft an object that passes this check and isn't a namedtuple, but
|
| 564 |
+
there is only a minuscule chance of this happening unintentionally.
|
| 565 |
+
|
| 566 |
+
Args:
|
| 567 |
+
obj (Any): The object to test
|
| 568 |
+
|
| 569 |
+
Returns:
|
| 570 |
+
bool: True if the object is a namedtuple. False otherwise.
|
| 571 |
+
"""
|
| 572 |
+
try:
|
| 573 |
+
fields = getattr(obj, "_fields", None)
|
| 574 |
+
except Exception:
|
| 575 |
+
# Being very defensive - if we cannot get the attr then its not a namedtuple
|
| 576 |
+
return False
|
| 577 |
+
return isinstance(obj, tuple) and isinstance(fields, tuple)
|
| 578 |
+
|
| 579 |
+
|
| 580 |
+
def traverse(
|
| 581 |
+
_object: Any,
|
| 582 |
+
max_length: Optional[int] = None,
|
| 583 |
+
max_string: Optional[int] = None,
|
| 584 |
+
max_depth: Optional[int] = None,
|
| 585 |
+
) -> Node:
|
| 586 |
+
"""Traverse object and generate a tree.
|
| 587 |
+
|
| 588 |
+
Args:
|
| 589 |
+
_object (Any): Object to be traversed.
|
| 590 |
+
max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
|
| 591 |
+
Defaults to None.
|
| 592 |
+
max_string (int, optional): Maximum length of string before truncating, or None to disable truncating.
|
| 593 |
+
Defaults to None.
|
| 594 |
+
max_depth (int, optional): Maximum depth of data structures, or None for no maximum.
|
| 595 |
+
Defaults to None.
|
| 596 |
+
|
| 597 |
+
Returns:
|
| 598 |
+
Node: The root of a tree structure which can be used to render a pretty repr.
|
| 599 |
+
"""
|
| 600 |
+
|
| 601 |
+
def to_repr(obj: Any) -> str:
|
| 602 |
+
"""Get repr string for an object, but catch errors."""
|
| 603 |
+
if (
|
| 604 |
+
max_string is not None
|
| 605 |
+
and _safe_isinstance(obj, (bytes, str))
|
| 606 |
+
and len(obj) > max_string
|
| 607 |
+
):
|
| 608 |
+
truncated = len(obj) - max_string
|
| 609 |
+
obj_repr = f"{obj[:max_string]!r}+{truncated}"
|
| 610 |
+
else:
|
| 611 |
+
try:
|
| 612 |
+
obj_repr = repr(obj)
|
| 613 |
+
except Exception as error:
|
| 614 |
+
obj_repr = f"<repr-error {str(error)!r}>"
|
| 615 |
+
return obj_repr
|
| 616 |
+
|
| 617 |
+
visited_ids: Set[int] = set()
|
| 618 |
+
push_visited = visited_ids.add
|
| 619 |
+
pop_visited = visited_ids.remove
|
| 620 |
+
|
| 621 |
+
def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
|
| 622 |
+
"""Walk the object depth first."""
|
| 623 |
+
|
| 624 |
+
obj_id = id(obj)
|
| 625 |
+
if obj_id in visited_ids:
|
| 626 |
+
# Recursion detected
|
| 627 |
+
return Node(value_repr="...")
|
| 628 |
+
|
| 629 |
+
obj_type = type(obj)
|
| 630 |
+
children: List[Node]
|
| 631 |
+
reached_max_depth = max_depth is not None and depth >= max_depth
|
| 632 |
+
|
| 633 |
+
def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
|
| 634 |
+
for arg in rich_args:
|
| 635 |
+
if _safe_isinstance(arg, tuple):
|
| 636 |
+
if len(arg) == 3:
|
| 637 |
+
key, child, default = arg
|
| 638 |
+
if default == child:
|
| 639 |
+
continue
|
| 640 |
+
yield key, child
|
| 641 |
+
elif len(arg) == 2:
|
| 642 |
+
key, child = arg
|
| 643 |
+
yield key, child
|
| 644 |
+
elif len(arg) == 1:
|
| 645 |
+
yield arg[0]
|
| 646 |
+
else:
|
| 647 |
+
yield arg
|
| 648 |
+
|
| 649 |
+
try:
|
| 650 |
+
fake_attributes = hasattr(
|
| 651 |
+
obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
|
| 652 |
+
)
|
| 653 |
+
except Exception:
|
| 654 |
+
fake_attributes = False
|
| 655 |
+
|
| 656 |
+
rich_repr_result: Optional[RichReprResult] = None
|
| 657 |
+
if not fake_attributes:
|
| 658 |
+
try:
|
| 659 |
+
if hasattr(obj, "__rich_repr__") and not isclass(obj):
|
| 660 |
+
rich_repr_result = obj.__rich_repr__()
|
| 661 |
+
except Exception:
|
| 662 |
+
pass
|
| 663 |
+
|
| 664 |
+
if rich_repr_result is not None:
|
| 665 |
+
push_visited(obj_id)
|
| 666 |
+
angular = getattr(obj.__rich_repr__, "angular", False)
|
| 667 |
+
args = list(iter_rich_args(rich_repr_result))
|
| 668 |
+
class_name = obj.__class__.__name__
|
| 669 |
+
|
| 670 |
+
if args:
|
| 671 |
+
children = []
|
| 672 |
+
append = children.append
|
| 673 |
+
|
| 674 |
+
if reached_max_depth:
|
| 675 |
+
if angular:
|
| 676 |
+
node = Node(value_repr=f"<{class_name}...>")
|
| 677 |
+
else:
|
| 678 |
+
node = Node(value_repr=f"{class_name}(...)")
|
| 679 |
+
else:
|
| 680 |
+
if angular:
|
| 681 |
+
node = Node(
|
| 682 |
+
open_brace=f"<{class_name} ",
|
| 683 |
+
close_brace=">",
|
| 684 |
+
children=children,
|
| 685 |
+
last=root,
|
| 686 |
+
separator=" ",
|
| 687 |
+
)
|
| 688 |
+
else:
|
| 689 |
+
node = Node(
|
| 690 |
+
open_brace=f"{class_name}(",
|
| 691 |
+
close_brace=")",
|
| 692 |
+
children=children,
|
| 693 |
+
last=root,
|
| 694 |
+
)
|
| 695 |
+
for last, arg in loop_last(args):
|
| 696 |
+
if _safe_isinstance(arg, tuple):
|
| 697 |
+
key, child = arg
|
| 698 |
+
child_node = _traverse(child, depth=depth + 1)
|
| 699 |
+
child_node.last = last
|
| 700 |
+
child_node.key_repr = key
|
| 701 |
+
child_node.key_separator = "="
|
| 702 |
+
append(child_node)
|
| 703 |
+
else:
|
| 704 |
+
child_node = _traverse(arg, depth=depth + 1)
|
| 705 |
+
child_node.last = last
|
| 706 |
+
append(child_node)
|
| 707 |
+
else:
|
| 708 |
+
node = Node(
|
| 709 |
+
value_repr=f"<{class_name}>" if angular else f"{class_name}()",
|
| 710 |
+
children=[],
|
| 711 |
+
last=root,
|
| 712 |
+
)
|
| 713 |
+
pop_visited(obj_id)
|
| 714 |
+
elif _is_attr_object(obj) and not fake_attributes:
|
| 715 |
+
push_visited(obj_id)
|
| 716 |
+
children = []
|
| 717 |
+
append = children.append
|
| 718 |
+
|
| 719 |
+
attr_fields = _get_attr_fields(obj)
|
| 720 |
+
if attr_fields:
|
| 721 |
+
if reached_max_depth:
|
| 722 |
+
node = Node(value_repr=f"{obj.__class__.__name__}(...)")
|
| 723 |
+
else:
|
| 724 |
+
node = Node(
|
| 725 |
+
open_brace=f"{obj.__class__.__name__}(",
|
| 726 |
+
close_brace=")",
|
| 727 |
+
children=children,
|
| 728 |
+
last=root,
|
| 729 |
+
)
|
| 730 |
+
|
| 731 |
+
def iter_attrs() -> (
|
| 732 |
+
Iterable[Tuple[str, Any, Optional[Callable[[Any], str]]]]
|
| 733 |
+
):
|
| 734 |
+
"""Iterate over attr fields and values."""
|
| 735 |
+
for attr in attr_fields:
|
| 736 |
+
if attr.repr:
|
| 737 |
+
try:
|
| 738 |
+
value = getattr(obj, attr.name)
|
| 739 |
+
except Exception as error:
|
| 740 |
+
# Can happen, albeit rarely
|
| 741 |
+
yield (attr.name, error, None)
|
| 742 |
+
else:
|
| 743 |
+
yield (
|
| 744 |
+
attr.name,
|
| 745 |
+
value,
|
| 746 |
+
attr.repr if callable(attr.repr) else None,
|
| 747 |
+
)
|
| 748 |
+
|
| 749 |
+
for last, (name, value, repr_callable) in loop_last(iter_attrs()):
|
| 750 |
+
if repr_callable:
|
| 751 |
+
child_node = Node(value_repr=str(repr_callable(value)))
|
| 752 |
+
else:
|
| 753 |
+
child_node = _traverse(value, depth=depth + 1)
|
| 754 |
+
child_node.last = last
|
| 755 |
+
child_node.key_repr = name
|
| 756 |
+
child_node.key_separator = "="
|
| 757 |
+
append(child_node)
|
| 758 |
+
else:
|
| 759 |
+
node = Node(
|
| 760 |
+
value_repr=f"{obj.__class__.__name__}()", children=[], last=root
|
| 761 |
+
)
|
| 762 |
+
pop_visited(obj_id)
|
| 763 |
+
elif (
|
| 764 |
+
is_dataclass(obj)
|
| 765 |
+
and not _safe_isinstance(obj, type)
|
| 766 |
+
and not fake_attributes
|
| 767 |
+
and _is_dataclass_repr(obj)
|
| 768 |
+
):
|
| 769 |
+
push_visited(obj_id)
|
| 770 |
+
children = []
|
| 771 |
+
append = children.append
|
| 772 |
+
if reached_max_depth:
|
| 773 |
+
node = Node(value_repr=f"{obj.__class__.__name__}(...)")
|
| 774 |
+
else:
|
| 775 |
+
node = Node(
|
| 776 |
+
open_brace=f"{obj.__class__.__name__}(",
|
| 777 |
+
close_brace=")",
|
| 778 |
+
children=children,
|
| 779 |
+
last=root,
|
| 780 |
+
empty=f"{obj.__class__.__name__}()",
|
| 781 |
+
)
|
| 782 |
+
|
| 783 |
+
for last, field in loop_last(
|
| 784 |
+
field
|
| 785 |
+
for field in fields(obj)
|
| 786 |
+
if field.repr and hasattr(obj, field.name)
|
| 787 |
+
):
|
| 788 |
+
child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
|
| 789 |
+
child_node.key_repr = field.name
|
| 790 |
+
child_node.last = last
|
| 791 |
+
child_node.key_separator = "="
|
| 792 |
+
append(child_node)
|
| 793 |
+
|
| 794 |
+
pop_visited(obj_id)
|
| 795 |
+
elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
|
| 796 |
+
push_visited(obj_id)
|
| 797 |
+
class_name = obj.__class__.__name__
|
| 798 |
+
if reached_max_depth:
|
| 799 |
+
# If we've reached the max depth, we still show the class name, but not its contents
|
| 800 |
+
node = Node(
|
| 801 |
+
value_repr=f"{class_name}(...)",
|
| 802 |
+
)
|
| 803 |
+
else:
|
| 804 |
+
children = []
|
| 805 |
+
append = children.append
|
| 806 |
+
node = Node(
|
| 807 |
+
open_brace=f"{class_name}(",
|
| 808 |
+
close_brace=")",
|
| 809 |
+
children=children,
|
| 810 |
+
empty=f"{class_name}()",
|
| 811 |
+
)
|
| 812 |
+
for last, (key, value) in loop_last(obj._asdict().items()):
|
| 813 |
+
child_node = _traverse(value, depth=depth + 1)
|
| 814 |
+
child_node.key_repr = key
|
| 815 |
+
child_node.last = last
|
| 816 |
+
child_node.key_separator = "="
|
| 817 |
+
append(child_node)
|
| 818 |
+
pop_visited(obj_id)
|
| 819 |
+
elif _safe_isinstance(obj, _CONTAINERS):
|
| 820 |
+
for container_type in _CONTAINERS:
|
| 821 |
+
if _safe_isinstance(obj, container_type):
|
| 822 |
+
obj_type = container_type
|
| 823 |
+
break
|
| 824 |
+
|
| 825 |
+
push_visited(obj_id)
|
| 826 |
+
|
| 827 |
+
open_brace, close_brace, empty = _BRACES[obj_type](obj)
|
| 828 |
+
|
| 829 |
+
if reached_max_depth:
|
| 830 |
+
node = Node(value_repr=f"{open_brace}...{close_brace}")
|
| 831 |
+
elif obj_type.__repr__ != type(obj).__repr__:
|
| 832 |
+
node = Node(value_repr=to_repr(obj), last=root)
|
| 833 |
+
elif obj:
|
| 834 |
+
children = []
|
| 835 |
+
node = Node(
|
| 836 |
+
open_brace=open_brace,
|
| 837 |
+
close_brace=close_brace,
|
| 838 |
+
children=children,
|
| 839 |
+
last=root,
|
| 840 |
+
)
|
| 841 |
+
append = children.append
|
| 842 |
+
num_items = len(obj)
|
| 843 |
+
last_item_index = num_items - 1
|
| 844 |
+
|
| 845 |
+
if _safe_isinstance(obj, _MAPPING_CONTAINERS):
|
| 846 |
+
iter_items = iter(obj.items())
|
| 847 |
+
if max_length is not None:
|
| 848 |
+
iter_items = islice(iter_items, max_length)
|
| 849 |
+
for index, (key, child) in enumerate(iter_items):
|
| 850 |
+
child_node = _traverse(child, depth=depth + 1)
|
| 851 |
+
child_node.key_repr = to_repr(key)
|
| 852 |
+
child_node.last = index == last_item_index
|
| 853 |
+
append(child_node)
|
| 854 |
+
else:
|
| 855 |
+
iter_values = iter(obj)
|
| 856 |
+
if max_length is not None:
|
| 857 |
+
iter_values = islice(iter_values, max_length)
|
| 858 |
+
for index, child in enumerate(iter_values):
|
| 859 |
+
child_node = _traverse(child, depth=depth + 1)
|
| 860 |
+
child_node.last = index == last_item_index
|
| 861 |
+
append(child_node)
|
| 862 |
+
if max_length is not None and num_items > max_length:
|
| 863 |
+
append(Node(value_repr=f"... +{num_items - max_length}", last=True))
|
| 864 |
+
else:
|
| 865 |
+
node = Node(empty=empty, children=[], last=root)
|
| 866 |
+
|
| 867 |
+
pop_visited(obj_id)
|
| 868 |
+
else:
|
| 869 |
+
node = Node(value_repr=to_repr(obj), last=root)
|
| 870 |
+
node.is_tuple = type(obj) == tuple
|
| 871 |
+
node.is_namedtuple = _is_namedtuple(obj)
|
| 872 |
+
return node
|
| 873 |
+
|
| 874 |
+
node = _traverse(_object, root=True)
|
| 875 |
+
return node
|
| 876 |
+
|
| 877 |
+
|
| 878 |
+
def pretty_repr(
|
| 879 |
+
_object: Any,
|
| 880 |
+
*,
|
| 881 |
+
max_width: int = 80,
|
| 882 |
+
indent_size: int = 4,
|
| 883 |
+
max_length: Optional[int] = None,
|
| 884 |
+
max_string: Optional[int] = None,
|
| 885 |
+
max_depth: Optional[int] = None,
|
| 886 |
+
expand_all: bool = False,
|
| 887 |
+
) -> str:
|
| 888 |
+
"""Prettify repr string by expanding on to new lines to fit within a given width.
|
| 889 |
+
|
| 890 |
+
Args:
|
| 891 |
+
_object (Any): Object to repr.
|
| 892 |
+
max_width (int, optional): Desired maximum width of repr string. Defaults to 80.
|
| 893 |
+
indent_size (int, optional): Number of spaces to indent. Defaults to 4.
|
| 894 |
+
max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
|
| 895 |
+
Defaults to None.
|
| 896 |
+
max_string (int, optional): Maximum length of string before truncating, or None to disable truncating.
|
| 897 |
+
Defaults to None.
|
| 898 |
+
max_depth (int, optional): Maximum depth of nested data structure, or None for no depth.
|
| 899 |
+
Defaults to None.
|
| 900 |
+
expand_all (bool, optional): Expand all containers regardless of available width. Defaults to False.
|
| 901 |
+
|
| 902 |
+
Returns:
|
| 903 |
+
str: A possibly multi-line representation of the object.
|
| 904 |
+
"""
|
| 905 |
+
|
| 906 |
+
if _safe_isinstance(_object, Node):
|
| 907 |
+
node = _object
|
| 908 |
+
else:
|
| 909 |
+
node = traverse(
|
| 910 |
+
_object, max_length=max_length, max_string=max_string, max_depth=max_depth
|
| 911 |
+
)
|
| 912 |
+
repr_str: str = node.render(
|
| 913 |
+
max_width=max_width, indent_size=indent_size, expand_all=expand_all
|
| 914 |
+
)
|
| 915 |
+
return repr_str
|
| 916 |
+
|
| 917 |
+
|
| 918 |
+
def pprint(
|
| 919 |
+
_object: Any,
|
| 920 |
+
*,
|
| 921 |
+
console: Optional["Console"] = None,
|
| 922 |
+
indent_guides: bool = True,
|
| 923 |
+
max_length: Optional[int] = None,
|
| 924 |
+
max_string: Optional[int] = None,
|
| 925 |
+
max_depth: Optional[int] = None,
|
| 926 |
+
expand_all: bool = False,
|
| 927 |
+
) -> None:
|
| 928 |
+
"""A convenience function for pretty printing.
|
| 929 |
+
|
| 930 |
+
Args:
|
| 931 |
+
_object (Any): Object to pretty print.
|
| 932 |
+
console (Console, optional): Console instance, or None to use default. Defaults to None.
|
| 933 |
+
max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
|
| 934 |
+
Defaults to None.
|
| 935 |
+
max_string (int, optional): Maximum length of strings before truncating, or None to disable. Defaults to None.
|
| 936 |
+
max_depth (int, optional): Maximum depth for nested data structures, or None for unlimited depth. Defaults to None.
|
| 937 |
+
indent_guides (bool, optional): Enable indentation guides. Defaults to True.
|
| 938 |
+
expand_all (bool, optional): Expand all containers. Defaults to False.
|
| 939 |
+
"""
|
| 940 |
+
_console = get_console() if console is None else console
|
| 941 |
+
_console.print(
|
| 942 |
+
Pretty(
|
| 943 |
+
_object,
|
| 944 |
+
max_length=max_length,
|
| 945 |
+
max_string=max_string,
|
| 946 |
+
max_depth=max_depth,
|
| 947 |
+
indent_guides=indent_guides,
|
| 948 |
+
expand_all=expand_all,
|
| 949 |
+
overflow="ignore",
|
| 950 |
+
),
|
| 951 |
+
soft_wrap=True,
|
| 952 |
+
)
|
| 953 |
+
|
| 954 |
+
|
| 955 |
+
if __name__ == "__main__": # pragma: no cover
|
| 956 |
+
|
| 957 |
+
class BrokenRepr:
|
| 958 |
+
def __repr__(self) -> str:
|
| 959 |
+
1 / 0
|
| 960 |
+
return "this will fail"
|
| 961 |
+
|
| 962 |
+
from typing import NamedTuple
|
| 963 |
+
|
| 964 |
+
class StockKeepingUnit(NamedTuple):
|
| 965 |
+
name: str
|
| 966 |
+
description: str
|
| 967 |
+
price: float
|
| 968 |
+
category: str
|
| 969 |
+
reviews: List[str]
|
| 970 |
+
|
| 971 |
+
d = defaultdict(int)
|
| 972 |
+
d["foo"] = 5
|
| 973 |
+
data = {
|
| 974 |
+
"foo": [
|
| 975 |
+
1,
|
| 976 |
+
"Hello World!",
|
| 977 |
+
100.123,
|
| 978 |
+
323.232,
|
| 979 |
+
432324.0,
|
| 980 |
+
{5, 6, 7, (1, 2, 3, 4), 8},
|
| 981 |
+
],
|
| 982 |
+
"bar": frozenset({1, 2, 3}),
|
| 983 |
+
"defaultdict": defaultdict(
|
| 984 |
+
list, {"crumble": ["apple", "rhubarb", "butter", "sugar", "flour"]}
|
| 985 |
+
),
|
| 986 |
+
"counter": Counter(
|
| 987 |
+
[
|
| 988 |
+
"apple",
|
| 989 |
+
"orange",
|
| 990 |
+
"pear",
|
| 991 |
+
"kumquat",
|
| 992 |
+
"kumquat",
|
| 993 |
+
"durian" * 100,
|
| 994 |
+
]
|
| 995 |
+
),
|
| 996 |
+
"atomic": (False, True, None),
|
| 997 |
+
"namedtuple": StockKeepingUnit(
|
| 998 |
+
"Sparkling British Spring Water",
|
| 999 |
+
"Carbonated spring water",
|
| 1000 |
+
0.9,
|
| 1001 |
+
"water",
|
| 1002 |
+
["its amazing!", "its terrible!"],
|
| 1003 |
+
),
|
| 1004 |
+
"Broken": BrokenRepr(),
|
| 1005 |
+
}
|
| 1006 |
+
data["foo"].append(data) # type: ignore[attr-defined]
|
| 1007 |
+
|
| 1008 |
+
from pip._vendor.rich import print
|
| 1009 |
+
|
| 1010 |
+
print(Pretty(data, indent_guides=True, max_string=20))
|
| 1011 |
+
|
| 1012 |
+
class Thing:
|
| 1013 |
+
def __repr__(self) -> str:
|
| 1014 |
+
return "Hello\x1b[38;5;239m World!"
|
| 1015 |
+
|
| 1016 |
+
print(Pretty(Thing()))
|
env/lib/python3.13/site-packages/pip/_vendor/rich/prompt.py
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Generic, List, Optional, TextIO, TypeVar, Union, overload
|
| 2 |
+
|
| 3 |
+
from . import get_console
|
| 4 |
+
from .console import Console
|
| 5 |
+
from .text import Text, TextType
|
| 6 |
+
|
| 7 |
+
PromptType = TypeVar("PromptType")
|
| 8 |
+
DefaultType = TypeVar("DefaultType")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class PromptError(Exception):
|
| 12 |
+
"""Exception base class for prompt related errors."""
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class InvalidResponse(PromptError):
|
| 16 |
+
"""Exception to indicate a response was invalid. Raise this within process_response() to indicate an error
|
| 17 |
+
and provide an error message.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
message (Union[str, Text]): Error message.
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
def __init__(self, message: TextType) -> None:
|
| 24 |
+
self.message = message
|
| 25 |
+
|
| 26 |
+
def __rich__(self) -> TextType:
|
| 27 |
+
return self.message
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class PromptBase(Generic[PromptType]):
|
| 31 |
+
"""Ask the user for input until a valid response is received. This is the base class, see one of
|
| 32 |
+
the concrete classes for examples.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
prompt (TextType, optional): Prompt text. Defaults to "".
|
| 36 |
+
console (Console, optional): A Console instance or None to use global console. Defaults to None.
|
| 37 |
+
password (bool, optional): Enable password input. Defaults to False.
|
| 38 |
+
choices (List[str], optional): A list of valid choices. Defaults to None.
|
| 39 |
+
case_sensitive (bool, optional): Matching of choices should be case-sensitive. Defaults to True.
|
| 40 |
+
show_default (bool, optional): Show default in prompt. Defaults to True.
|
| 41 |
+
show_choices (bool, optional): Show choices in prompt. Defaults to True.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
response_type: type = str
|
| 45 |
+
|
| 46 |
+
validate_error_message = "[prompt.invalid]Please enter a valid value"
|
| 47 |
+
illegal_choice_message = (
|
| 48 |
+
"[prompt.invalid.choice]Please select one of the available options"
|
| 49 |
+
)
|
| 50 |
+
prompt_suffix = ": "
|
| 51 |
+
|
| 52 |
+
choices: Optional[List[str]] = None
|
| 53 |
+
|
| 54 |
+
def __init__(
|
| 55 |
+
self,
|
| 56 |
+
prompt: TextType = "",
|
| 57 |
+
*,
|
| 58 |
+
console: Optional[Console] = None,
|
| 59 |
+
password: bool = False,
|
| 60 |
+
choices: Optional[List[str]] = None,
|
| 61 |
+
case_sensitive: bool = True,
|
| 62 |
+
show_default: bool = True,
|
| 63 |
+
show_choices: bool = True,
|
| 64 |
+
) -> None:
|
| 65 |
+
self.console = console or get_console()
|
| 66 |
+
self.prompt = (
|
| 67 |
+
Text.from_markup(prompt, style="prompt")
|
| 68 |
+
if isinstance(prompt, str)
|
| 69 |
+
else prompt
|
| 70 |
+
)
|
| 71 |
+
self.password = password
|
| 72 |
+
if choices is not None:
|
| 73 |
+
self.choices = choices
|
| 74 |
+
self.case_sensitive = case_sensitive
|
| 75 |
+
self.show_default = show_default
|
| 76 |
+
self.show_choices = show_choices
|
| 77 |
+
|
| 78 |
+
@classmethod
|
| 79 |
+
@overload
|
| 80 |
+
def ask(
|
| 81 |
+
cls,
|
| 82 |
+
prompt: TextType = "",
|
| 83 |
+
*,
|
| 84 |
+
console: Optional[Console] = None,
|
| 85 |
+
password: bool = False,
|
| 86 |
+
choices: Optional[List[str]] = None,
|
| 87 |
+
case_sensitive: bool = True,
|
| 88 |
+
show_default: bool = True,
|
| 89 |
+
show_choices: bool = True,
|
| 90 |
+
default: DefaultType,
|
| 91 |
+
stream: Optional[TextIO] = None,
|
| 92 |
+
) -> Union[DefaultType, PromptType]:
|
| 93 |
+
...
|
| 94 |
+
|
| 95 |
+
@classmethod
|
| 96 |
+
@overload
|
| 97 |
+
def ask(
|
| 98 |
+
cls,
|
| 99 |
+
prompt: TextType = "",
|
| 100 |
+
*,
|
| 101 |
+
console: Optional[Console] = None,
|
| 102 |
+
password: bool = False,
|
| 103 |
+
choices: Optional[List[str]] = None,
|
| 104 |
+
case_sensitive: bool = True,
|
| 105 |
+
show_default: bool = True,
|
| 106 |
+
show_choices: bool = True,
|
| 107 |
+
stream: Optional[TextIO] = None,
|
| 108 |
+
) -> PromptType:
|
| 109 |
+
...
|
| 110 |
+
|
| 111 |
+
@classmethod
|
| 112 |
+
def ask(
|
| 113 |
+
cls,
|
| 114 |
+
prompt: TextType = "",
|
| 115 |
+
*,
|
| 116 |
+
console: Optional[Console] = None,
|
| 117 |
+
password: bool = False,
|
| 118 |
+
choices: Optional[List[str]] = None,
|
| 119 |
+
case_sensitive: bool = True,
|
| 120 |
+
show_default: bool = True,
|
| 121 |
+
show_choices: bool = True,
|
| 122 |
+
default: Any = ...,
|
| 123 |
+
stream: Optional[TextIO] = None,
|
| 124 |
+
) -> Any:
|
| 125 |
+
"""Shortcut to construct and run a prompt loop and return the result.
|
| 126 |
+
|
| 127 |
+
Example:
|
| 128 |
+
>>> filename = Prompt.ask("Enter a filename")
|
| 129 |
+
|
| 130 |
+
Args:
|
| 131 |
+
prompt (TextType, optional): Prompt text. Defaults to "".
|
| 132 |
+
console (Console, optional): A Console instance or None to use global console. Defaults to None.
|
| 133 |
+
password (bool, optional): Enable password input. Defaults to False.
|
| 134 |
+
choices (List[str], optional): A list of valid choices. Defaults to None.
|
| 135 |
+
case_sensitive (bool, optional): Matching of choices should be case-sensitive. Defaults to True.
|
| 136 |
+
show_default (bool, optional): Show default in prompt. Defaults to True.
|
| 137 |
+
show_choices (bool, optional): Show choices in prompt. Defaults to True.
|
| 138 |
+
stream (TextIO, optional): Optional text file open for reading to get input. Defaults to None.
|
| 139 |
+
"""
|
| 140 |
+
_prompt = cls(
|
| 141 |
+
prompt,
|
| 142 |
+
console=console,
|
| 143 |
+
password=password,
|
| 144 |
+
choices=choices,
|
| 145 |
+
case_sensitive=case_sensitive,
|
| 146 |
+
show_default=show_default,
|
| 147 |
+
show_choices=show_choices,
|
| 148 |
+
)
|
| 149 |
+
return _prompt(default=default, stream=stream)
|
| 150 |
+
|
| 151 |
+
def render_default(self, default: DefaultType) -> Text:
|
| 152 |
+
"""Turn the supplied default in to a Text instance.
|
| 153 |
+
|
| 154 |
+
Args:
|
| 155 |
+
default (DefaultType): Default value.
|
| 156 |
+
|
| 157 |
+
Returns:
|
| 158 |
+
Text: Text containing rendering of default value.
|
| 159 |
+
"""
|
| 160 |
+
return Text(f"({default})", "prompt.default")
|
| 161 |
+
|
| 162 |
+
def make_prompt(self, default: DefaultType) -> Text:
|
| 163 |
+
"""Make prompt text.
|
| 164 |
+
|
| 165 |
+
Args:
|
| 166 |
+
default (DefaultType): Default value.
|
| 167 |
+
|
| 168 |
+
Returns:
|
| 169 |
+
Text: Text to display in prompt.
|
| 170 |
+
"""
|
| 171 |
+
prompt = self.prompt.copy()
|
| 172 |
+
prompt.end = ""
|
| 173 |
+
|
| 174 |
+
if self.show_choices and self.choices:
|
| 175 |
+
_choices = "/".join(self.choices)
|
| 176 |
+
choices = f"[{_choices}]"
|
| 177 |
+
prompt.append(" ")
|
| 178 |
+
prompt.append(choices, "prompt.choices")
|
| 179 |
+
|
| 180 |
+
if (
|
| 181 |
+
default != ...
|
| 182 |
+
and self.show_default
|
| 183 |
+
and isinstance(default, (str, self.response_type))
|
| 184 |
+
):
|
| 185 |
+
prompt.append(" ")
|
| 186 |
+
_default = self.render_default(default)
|
| 187 |
+
prompt.append(_default)
|
| 188 |
+
|
| 189 |
+
prompt.append(self.prompt_suffix)
|
| 190 |
+
|
| 191 |
+
return prompt
|
| 192 |
+
|
| 193 |
+
@classmethod
|
| 194 |
+
def get_input(
|
| 195 |
+
cls,
|
| 196 |
+
console: Console,
|
| 197 |
+
prompt: TextType,
|
| 198 |
+
password: bool,
|
| 199 |
+
stream: Optional[TextIO] = None,
|
| 200 |
+
) -> str:
|
| 201 |
+
"""Get input from user.
|
| 202 |
+
|
| 203 |
+
Args:
|
| 204 |
+
console (Console): Console instance.
|
| 205 |
+
prompt (TextType): Prompt text.
|
| 206 |
+
password (bool): Enable password entry.
|
| 207 |
+
|
| 208 |
+
Returns:
|
| 209 |
+
str: String from user.
|
| 210 |
+
"""
|
| 211 |
+
return console.input(prompt, password=password, stream=stream)
|
| 212 |
+
|
| 213 |
+
def check_choice(self, value: str) -> bool:
|
| 214 |
+
"""Check value is in the list of valid choices.
|
| 215 |
+
|
| 216 |
+
Args:
|
| 217 |
+
value (str): Value entered by user.
|
| 218 |
+
|
| 219 |
+
Returns:
|
| 220 |
+
bool: True if choice was valid, otherwise False.
|
| 221 |
+
"""
|
| 222 |
+
assert self.choices is not None
|
| 223 |
+
if self.case_sensitive:
|
| 224 |
+
return value.strip() in self.choices
|
| 225 |
+
return value.strip().lower() in [choice.lower() for choice in self.choices]
|
| 226 |
+
|
| 227 |
+
def process_response(self, value: str) -> PromptType:
|
| 228 |
+
"""Process response from user, convert to prompt type.
|
| 229 |
+
|
| 230 |
+
Args:
|
| 231 |
+
value (str): String typed by user.
|
| 232 |
+
|
| 233 |
+
Raises:
|
| 234 |
+
InvalidResponse: If ``value`` is invalid.
|
| 235 |
+
|
| 236 |
+
Returns:
|
| 237 |
+
PromptType: The value to be returned from ask method.
|
| 238 |
+
"""
|
| 239 |
+
value = value.strip()
|
| 240 |
+
try:
|
| 241 |
+
return_value: PromptType = self.response_type(value)
|
| 242 |
+
except ValueError:
|
| 243 |
+
raise InvalidResponse(self.validate_error_message)
|
| 244 |
+
|
| 245 |
+
if self.choices is not None:
|
| 246 |
+
if not self.check_choice(value):
|
| 247 |
+
raise InvalidResponse(self.illegal_choice_message)
|
| 248 |
+
|
| 249 |
+
if not self.case_sensitive:
|
| 250 |
+
# return the original choice, not the lower case version
|
| 251 |
+
return_value = self.response_type(
|
| 252 |
+
self.choices[
|
| 253 |
+
[choice.lower() for choice in self.choices].index(value.lower())
|
| 254 |
+
]
|
| 255 |
+
)
|
| 256 |
+
return return_value
|
| 257 |
+
|
| 258 |
+
def on_validate_error(self, value: str, error: InvalidResponse) -> None:
|
| 259 |
+
"""Called to handle validation error.
|
| 260 |
+
|
| 261 |
+
Args:
|
| 262 |
+
value (str): String entered by user.
|
| 263 |
+
error (InvalidResponse): Exception instance the initiated the error.
|
| 264 |
+
"""
|
| 265 |
+
self.console.print(error)
|
| 266 |
+
|
| 267 |
+
def pre_prompt(self) -> None:
|
| 268 |
+
"""Hook to display something before the prompt."""
|
| 269 |
+
|
| 270 |
+
@overload
|
| 271 |
+
def __call__(self, *, stream: Optional[TextIO] = None) -> PromptType:
|
| 272 |
+
...
|
| 273 |
+
|
| 274 |
+
@overload
|
| 275 |
+
def __call__(
|
| 276 |
+
self, *, default: DefaultType, stream: Optional[TextIO] = None
|
| 277 |
+
) -> Union[PromptType, DefaultType]:
|
| 278 |
+
...
|
| 279 |
+
|
| 280 |
+
def __call__(self, *, default: Any = ..., stream: Optional[TextIO] = None) -> Any:
|
| 281 |
+
"""Run the prompt loop.
|
| 282 |
+
|
| 283 |
+
Args:
|
| 284 |
+
default (Any, optional): Optional default value.
|
| 285 |
+
|
| 286 |
+
Returns:
|
| 287 |
+
PromptType: Processed value.
|
| 288 |
+
"""
|
| 289 |
+
while True:
|
| 290 |
+
self.pre_prompt()
|
| 291 |
+
prompt = self.make_prompt(default)
|
| 292 |
+
value = self.get_input(self.console, prompt, self.password, stream=stream)
|
| 293 |
+
if value == "" and default != ...:
|
| 294 |
+
return default
|
| 295 |
+
try:
|
| 296 |
+
return_value = self.process_response(value)
|
| 297 |
+
except InvalidResponse as error:
|
| 298 |
+
self.on_validate_error(value, error)
|
| 299 |
+
continue
|
| 300 |
+
else:
|
| 301 |
+
return return_value
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
class Prompt(PromptBase[str]):
|
| 305 |
+
"""A prompt that returns a str.
|
| 306 |
+
|
| 307 |
+
Example:
|
| 308 |
+
>>> name = Prompt.ask("Enter your name")
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
"""
|
| 312 |
+
|
| 313 |
+
response_type = str
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
class IntPrompt(PromptBase[int]):
|
| 317 |
+
"""A prompt that returns an integer.
|
| 318 |
+
|
| 319 |
+
Example:
|
| 320 |
+
>>> burrito_count = IntPrompt.ask("How many burritos do you want to order")
|
| 321 |
+
|
| 322 |
+
"""
|
| 323 |
+
|
| 324 |
+
response_type = int
|
| 325 |
+
validate_error_message = "[prompt.invalid]Please enter a valid integer number"
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
class FloatPrompt(PromptBase[float]):
|
| 329 |
+
"""A prompt that returns a float.
|
| 330 |
+
|
| 331 |
+
Example:
|
| 332 |
+
>>> temperature = FloatPrompt.ask("Enter desired temperature")
|
| 333 |
+
|
| 334 |
+
"""
|
| 335 |
+
|
| 336 |
+
response_type = float
|
| 337 |
+
validate_error_message = "[prompt.invalid]Please enter a number"
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
class Confirm(PromptBase[bool]):
|
| 341 |
+
"""A yes / no confirmation prompt.
|
| 342 |
+
|
| 343 |
+
Example:
|
| 344 |
+
>>> if Confirm.ask("Continue"):
|
| 345 |
+
run_job()
|
| 346 |
+
|
| 347 |
+
"""
|
| 348 |
+
|
| 349 |
+
response_type = bool
|
| 350 |
+
validate_error_message = "[prompt.invalid]Please enter Y or N"
|
| 351 |
+
choices: List[str] = ["y", "n"]
|
| 352 |
+
|
| 353 |
+
def render_default(self, default: DefaultType) -> Text:
|
| 354 |
+
"""Render the default as (y) or (n) rather than True/False."""
|
| 355 |
+
yes, no = self.choices
|
| 356 |
+
return Text(f"({yes})" if default else f"({no})", style="prompt.default")
|
| 357 |
+
|
| 358 |
+
def process_response(self, value: str) -> bool:
|
| 359 |
+
"""Convert choices to a bool."""
|
| 360 |
+
value = value.strip().lower()
|
| 361 |
+
if value not in self.choices:
|
| 362 |
+
raise InvalidResponse(self.validate_error_message)
|
| 363 |
+
return value == self.choices[0]
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
if __name__ == "__main__": # pragma: no cover
|
| 367 |
+
from pip._vendor.rich import print
|
| 368 |
+
|
| 369 |
+
if Confirm.ask("Run [i]prompt[/i] tests?", default=True):
|
| 370 |
+
while True:
|
| 371 |
+
result = IntPrompt.ask(
|
| 372 |
+
":rocket: Enter a number between [b]1[/b] and [b]10[/b]", default=5
|
| 373 |
+
)
|
| 374 |
+
if result >= 1 and result <= 10:
|
| 375 |
+
break
|
| 376 |
+
print(":pile_of_poo: [prompt.invalid]Number must be between 1 and 10")
|
| 377 |
+
print(f"number={result}")
|
| 378 |
+
|
| 379 |
+
while True:
|
| 380 |
+
password = Prompt.ask(
|
| 381 |
+
"Please enter a password [cyan](must be at least 5 characters)",
|
| 382 |
+
password=True,
|
| 383 |
+
)
|
| 384 |
+
if len(password) >= 5:
|
| 385 |
+
break
|
| 386 |
+
print("[prompt.invalid]password too short")
|
| 387 |
+
print(f"password={password!r}")
|
| 388 |
+
|
| 389 |
+
fruit = Prompt.ask("Enter a fruit", choices=["apple", "orange", "pear"])
|
| 390 |
+
print(f"fruit={fruit!r}")
|
| 391 |
+
|
| 392 |
+
doggie = Prompt.ask(
|
| 393 |
+
"What's the best Dog? (Case INSENSITIVE)",
|
| 394 |
+
choices=["Border Terrier", "Collie", "Labradoodle"],
|
| 395 |
+
case_sensitive=False,
|
| 396 |
+
)
|
| 397 |
+
print(f"doggie={doggie!r}")
|
| 398 |
+
|
| 399 |
+
else:
|
| 400 |
+
print("[b]OK :loudly_crying_face:")
|
env/lib/python3.13/site-packages/pip/_vendor/rich/protocol.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, cast, Set, TYPE_CHECKING
|
| 2 |
+
from inspect import isclass
|
| 3 |
+
|
| 4 |
+
if TYPE_CHECKING:
|
| 5 |
+
from pip._vendor.rich.console import RenderableType
|
| 6 |
+
|
| 7 |
+
_GIBBERISH = """aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf"""
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def is_renderable(check_object: Any) -> bool:
|
| 11 |
+
"""Check if an object may be rendered by Rich."""
|
| 12 |
+
return (
|
| 13 |
+
isinstance(check_object, str)
|
| 14 |
+
or hasattr(check_object, "__rich__")
|
| 15 |
+
or hasattr(check_object, "__rich_console__")
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def rich_cast(renderable: object) -> "RenderableType":
|
| 20 |
+
"""Cast an object to a renderable by calling __rich__ if present.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
renderable (object): A potentially renderable object
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
object: The result of recursively calling __rich__.
|
| 27 |
+
"""
|
| 28 |
+
from pip._vendor.rich.console import RenderableType
|
| 29 |
+
|
| 30 |
+
rich_visited_set: Set[type] = set() # Prevent potential infinite loop
|
| 31 |
+
while hasattr(renderable, "__rich__") and not isclass(renderable):
|
| 32 |
+
# Detect object which claim to have all the attributes
|
| 33 |
+
if hasattr(renderable, _GIBBERISH):
|
| 34 |
+
return repr(renderable)
|
| 35 |
+
cast_method = getattr(renderable, "__rich__")
|
| 36 |
+
renderable = cast_method()
|
| 37 |
+
renderable_type = type(renderable)
|
| 38 |
+
if renderable_type in rich_visited_set:
|
| 39 |
+
break
|
| 40 |
+
rich_visited_set.add(renderable_type)
|
| 41 |
+
|
| 42 |
+
return cast(RenderableType, renderable)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/region.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import NamedTuple
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class Region(NamedTuple):
|
| 5 |
+
"""Defines a rectangular region of the screen."""
|
| 6 |
+
|
| 7 |
+
x: int
|
| 8 |
+
y: int
|
| 9 |
+
width: int
|
| 10 |
+
height: int
|
env/lib/python3.13/site-packages/pip/_vendor/rich/repr.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
+
TypeVar,
|
| 12 |
+
Union,
|
| 13 |
+
overload,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
T = TypeVar("T")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Result = Iterable[Union[Any, Tuple[Any], Tuple[str, Any], Tuple[str, Any, Any]]]
|
| 20 |
+
RichReprResult = Result
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class ReprError(Exception):
|
| 24 |
+
"""An error occurred when attempting to build a repr."""
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@overload
|
| 28 |
+
def auto(cls: Optional[Type[T]]) -> Type[T]:
|
| 29 |
+
...
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@overload
|
| 33 |
+
def auto(*, angular: bool = False) -> Callable[[Type[T]], Type[T]]:
|
| 34 |
+
...
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def auto(
|
| 38 |
+
cls: Optional[Type[T]] = None, *, angular: Optional[bool] = None
|
| 39 |
+
) -> Union[Type[T], Callable[[Type[T]], Type[T]]]:
|
| 40 |
+
"""Class decorator to create __repr__ from __rich_repr__"""
|
| 41 |
+
|
| 42 |
+
def do_replace(cls: Type[T], angular: Optional[bool] = None) -> Type[T]:
|
| 43 |
+
def auto_repr(self: T) -> str:
|
| 44 |
+
"""Create repr string from __rich_repr__"""
|
| 45 |
+
repr_str: List[str] = []
|
| 46 |
+
append = repr_str.append
|
| 47 |
+
|
| 48 |
+
angular: bool = getattr(self.__rich_repr__, "angular", False) # type: ignore[attr-defined]
|
| 49 |
+
for arg in self.__rich_repr__(): # type: ignore[attr-defined]
|
| 50 |
+
if isinstance(arg, tuple):
|
| 51 |
+
if len(arg) == 1:
|
| 52 |
+
append(repr(arg[0]))
|
| 53 |
+
else:
|
| 54 |
+
key, value, *default = arg
|
| 55 |
+
if key is None:
|
| 56 |
+
append(repr(value))
|
| 57 |
+
else:
|
| 58 |
+
if default and default[0] == value:
|
| 59 |
+
continue
|
| 60 |
+
append(f"{key}={value!r}")
|
| 61 |
+
else:
|
| 62 |
+
append(repr(arg))
|
| 63 |
+
if angular:
|
| 64 |
+
return f"<{self.__class__.__name__} {' '.join(repr_str)}>"
|
| 65 |
+
else:
|
| 66 |
+
return f"{self.__class__.__name__}({', '.join(repr_str)})"
|
| 67 |
+
|
| 68 |
+
def auto_rich_repr(self: Type[T]) -> Result:
|
| 69 |
+
"""Auto generate __rich_rep__ from signature of __init__"""
|
| 70 |
+
try:
|
| 71 |
+
signature = inspect.signature(self.__init__)
|
| 72 |
+
for name, param in signature.parameters.items():
|
| 73 |
+
if param.kind == param.POSITIONAL_ONLY:
|
| 74 |
+
yield getattr(self, name)
|
| 75 |
+
elif param.kind in (
|
| 76 |
+
param.POSITIONAL_OR_KEYWORD,
|
| 77 |
+
param.KEYWORD_ONLY,
|
| 78 |
+
):
|
| 79 |
+
if param.default is param.empty:
|
| 80 |
+
yield getattr(self, param.name)
|
| 81 |
+
else:
|
| 82 |
+
yield param.name, getattr(self, param.name), param.default
|
| 83 |
+
except Exception as error:
|
| 84 |
+
raise ReprError(
|
| 85 |
+
f"Failed to auto generate __rich_repr__; {error}"
|
| 86 |
+
) from None
|
| 87 |
+
|
| 88 |
+
if not hasattr(cls, "__rich_repr__"):
|
| 89 |
+
auto_rich_repr.__doc__ = "Build a rich repr"
|
| 90 |
+
cls.__rich_repr__ = auto_rich_repr # type: ignore[attr-defined]
|
| 91 |
+
|
| 92 |
+
auto_repr.__doc__ = "Return repr(self)"
|
| 93 |
+
cls.__repr__ = auto_repr # type: ignore[assignment]
|
| 94 |
+
if angular is not None:
|
| 95 |
+
cls.__rich_repr__.angular = angular # type: ignore[attr-defined]
|
| 96 |
+
return cls
|
| 97 |
+
|
| 98 |
+
if cls is None:
|
| 99 |
+
return partial(do_replace, angular=angular)
|
| 100 |
+
else:
|
| 101 |
+
return do_replace(cls, angular=angular)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
@overload
|
| 105 |
+
def rich_repr(cls: Optional[Type[T]]) -> Type[T]:
|
| 106 |
+
...
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@overload
|
| 110 |
+
def rich_repr(*, angular: bool = False) -> Callable[[Type[T]], Type[T]]:
|
| 111 |
+
...
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def rich_repr(
|
| 115 |
+
cls: Optional[Type[T]] = None, *, angular: bool = False
|
| 116 |
+
) -> Union[Type[T], Callable[[Type[T]], Type[T]]]:
|
| 117 |
+
if cls is None:
|
| 118 |
+
return auto(angular=angular)
|
| 119 |
+
else:
|
| 120 |
+
return auto(cls)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
|
| 125 |
+
@auto
|
| 126 |
+
class Foo:
|
| 127 |
+
def __rich_repr__(self) -> Result:
|
| 128 |
+
yield "foo"
|
| 129 |
+
yield "bar", {"shopping": ["eggs", "ham", "pineapple"]}
|
| 130 |
+
yield "buy", "hand sanitizer"
|
| 131 |
+
|
| 132 |
+
foo = Foo()
|
| 133 |
+
from pip._vendor.rich.console import Console
|
| 134 |
+
|
| 135 |
+
console = Console()
|
| 136 |
+
|
| 137 |
+
console.rule("Standard repr")
|
| 138 |
+
console.print(foo)
|
| 139 |
+
|
| 140 |
+
console.print(foo, width=60)
|
| 141 |
+
console.print(foo, width=30)
|
| 142 |
+
|
| 143 |
+
console.rule("Angular repr")
|
| 144 |
+
Foo.__rich_repr__.angular = True # type: ignore[attr-defined]
|
| 145 |
+
|
| 146 |
+
console.print(foo)
|
| 147 |
+
|
| 148 |
+
console.print(foo, width=60)
|
| 149 |
+
console.print(foo, width=30)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/rule.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Union
|
| 2 |
+
|
| 3 |
+
from .align import AlignMethod
|
| 4 |
+
from .cells import cell_len, set_cell_size
|
| 5 |
+
from .console import Console, ConsoleOptions, RenderResult
|
| 6 |
+
from .jupyter import JupyterMixin
|
| 7 |
+
from .measure import Measurement
|
| 8 |
+
from .style import Style
|
| 9 |
+
from .text import Text
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Rule(JupyterMixin):
|
| 13 |
+
"""A console renderable to draw a horizontal rule (line).
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
title (Union[str, Text], optional): Text to render in the rule. Defaults to "".
|
| 17 |
+
characters (str, optional): Character(s) used to draw the line. Defaults to "─".
|
| 18 |
+
style (StyleType, optional): Style of Rule. Defaults to "rule.line".
|
| 19 |
+
end (str, optional): Character at end of Rule. defaults to "\\\\n"
|
| 20 |
+
align (str, optional): How to align the title, one of "left", "center", or "right". Defaults to "center".
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
title: Union[str, Text] = "",
|
| 26 |
+
*,
|
| 27 |
+
characters: str = "─",
|
| 28 |
+
style: Union[str, Style] = "rule.line",
|
| 29 |
+
end: str = "\n",
|
| 30 |
+
align: AlignMethod = "center",
|
| 31 |
+
) -> None:
|
| 32 |
+
if cell_len(characters) < 1:
|
| 33 |
+
raise ValueError(
|
| 34 |
+
"'characters' argument must have a cell width of at least 1"
|
| 35 |
+
)
|
| 36 |
+
if align not in ("left", "center", "right"):
|
| 37 |
+
raise ValueError(
|
| 38 |
+
f'invalid value for align, expected "left", "center", "right" (not {align!r})'
|
| 39 |
+
)
|
| 40 |
+
self.title = title
|
| 41 |
+
self.characters = characters
|
| 42 |
+
self.style = style
|
| 43 |
+
self.end = end
|
| 44 |
+
self.align = align
|
| 45 |
+
|
| 46 |
+
def __repr__(self) -> str:
|
| 47 |
+
return f"Rule({self.title!r}, {self.characters!r})"
|
| 48 |
+
|
| 49 |
+
def __rich_console__(
|
| 50 |
+
self, console: Console, options: ConsoleOptions
|
| 51 |
+
) -> RenderResult:
|
| 52 |
+
width = options.max_width
|
| 53 |
+
|
| 54 |
+
characters = (
|
| 55 |
+
"-"
|
| 56 |
+
if (options.ascii_only and not self.characters.isascii())
|
| 57 |
+
else self.characters
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
chars_len = cell_len(characters)
|
| 61 |
+
if not self.title:
|
| 62 |
+
yield self._rule_line(chars_len, width)
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
if isinstance(self.title, Text):
|
| 66 |
+
title_text = self.title
|
| 67 |
+
else:
|
| 68 |
+
title_text = console.render_str(self.title, style="rule.text")
|
| 69 |
+
|
| 70 |
+
title_text.plain = title_text.plain.replace("\n", " ")
|
| 71 |
+
title_text.expand_tabs()
|
| 72 |
+
|
| 73 |
+
required_space = 4 if self.align == "center" else 2
|
| 74 |
+
truncate_width = max(0, width - required_space)
|
| 75 |
+
if not truncate_width:
|
| 76 |
+
yield self._rule_line(chars_len, width)
|
| 77 |
+
return
|
| 78 |
+
|
| 79 |
+
rule_text = Text(end=self.end)
|
| 80 |
+
if self.align == "center":
|
| 81 |
+
title_text.truncate(truncate_width, overflow="ellipsis")
|
| 82 |
+
side_width = (width - cell_len(title_text.plain)) // 2
|
| 83 |
+
left = Text(characters * (side_width // chars_len + 1))
|
| 84 |
+
left.truncate(side_width - 1)
|
| 85 |
+
right_length = width - cell_len(left.plain) - cell_len(title_text.plain)
|
| 86 |
+
right = Text(characters * (side_width // chars_len + 1))
|
| 87 |
+
right.truncate(right_length)
|
| 88 |
+
rule_text.append(left.plain + " ", self.style)
|
| 89 |
+
rule_text.append(title_text)
|
| 90 |
+
rule_text.append(" " + right.plain, self.style)
|
| 91 |
+
elif self.align == "left":
|
| 92 |
+
title_text.truncate(truncate_width, overflow="ellipsis")
|
| 93 |
+
rule_text.append(title_text)
|
| 94 |
+
rule_text.append(" ")
|
| 95 |
+
rule_text.append(characters * (width - rule_text.cell_len), self.style)
|
| 96 |
+
elif self.align == "right":
|
| 97 |
+
title_text.truncate(truncate_width, overflow="ellipsis")
|
| 98 |
+
rule_text.append(characters * (width - title_text.cell_len - 1), self.style)
|
| 99 |
+
rule_text.append(" ")
|
| 100 |
+
rule_text.append(title_text)
|
| 101 |
+
|
| 102 |
+
rule_text.plain = set_cell_size(rule_text.plain, width)
|
| 103 |
+
yield rule_text
|
| 104 |
+
|
| 105 |
+
def _rule_line(self, chars_len: int, width: int) -> Text:
|
| 106 |
+
rule_text = Text(self.characters * ((width // chars_len) + 1), self.style)
|
| 107 |
+
rule_text.truncate(width)
|
| 108 |
+
rule_text.plain = set_cell_size(rule_text.plain, width)
|
| 109 |
+
return rule_text
|
| 110 |
+
|
| 111 |
+
def __rich_measure__(
|
| 112 |
+
self, console: Console, options: ConsoleOptions
|
| 113 |
+
) -> Measurement:
|
| 114 |
+
return Measurement(1, 1)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__": # pragma: no cover
|
| 118 |
+
import sys
|
| 119 |
+
|
| 120 |
+
from pip._vendor.rich.console import Console
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
text = sys.argv[1]
|
| 124 |
+
except IndexError:
|
| 125 |
+
text = "Hello, World"
|
| 126 |
+
console = Console()
|
| 127 |
+
console.print(Rule(title=text))
|
| 128 |
+
|
| 129 |
+
console = Console()
|
| 130 |
+
console.print(Rule("foo"), width=4)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/scope.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Mapping
|
| 2 |
+
from typing import TYPE_CHECKING, Any, Optional, Tuple
|
| 3 |
+
|
| 4 |
+
from .highlighter import ReprHighlighter
|
| 5 |
+
from .panel import Panel
|
| 6 |
+
from .pretty import Pretty
|
| 7 |
+
from .table import Table
|
| 8 |
+
from .text import Text, TextType
|
| 9 |
+
|
| 10 |
+
if TYPE_CHECKING:
|
| 11 |
+
from .console import ConsoleRenderable
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def render_scope(
|
| 15 |
+
scope: "Mapping[str, Any]",
|
| 16 |
+
*,
|
| 17 |
+
title: Optional[TextType] = None,
|
| 18 |
+
sort_keys: bool = True,
|
| 19 |
+
indent_guides: bool = False,
|
| 20 |
+
max_length: Optional[int] = None,
|
| 21 |
+
max_string: Optional[int] = None,
|
| 22 |
+
) -> "ConsoleRenderable":
|
| 23 |
+
"""Render python variables in a given scope.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
scope (Mapping): A mapping containing variable names and values.
|
| 27 |
+
title (str, optional): Optional title. Defaults to None.
|
| 28 |
+
sort_keys (bool, optional): Enable sorting of items. Defaults to True.
|
| 29 |
+
indent_guides (bool, optional): Enable indentation guides. Defaults to False.
|
| 30 |
+
max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
|
| 31 |
+
Defaults to None.
|
| 32 |
+
max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
ConsoleRenderable: A renderable object.
|
| 36 |
+
"""
|
| 37 |
+
highlighter = ReprHighlighter()
|
| 38 |
+
items_table = Table.grid(padding=(0, 1), expand=False)
|
| 39 |
+
items_table.add_column(justify="right")
|
| 40 |
+
|
| 41 |
+
def sort_items(item: Tuple[str, Any]) -> Tuple[bool, str]:
|
| 42 |
+
"""Sort special variables first, then alphabetically."""
|
| 43 |
+
key, _ = item
|
| 44 |
+
return (not key.startswith("__"), key.lower())
|
| 45 |
+
|
| 46 |
+
items = sorted(scope.items(), key=sort_items) if sort_keys else scope.items()
|
| 47 |
+
for key, value in items:
|
| 48 |
+
key_text = Text.assemble(
|
| 49 |
+
(key, "scope.key.special" if key.startswith("__") else "scope.key"),
|
| 50 |
+
(" =", "scope.equals"),
|
| 51 |
+
)
|
| 52 |
+
items_table.add_row(
|
| 53 |
+
key_text,
|
| 54 |
+
Pretty(
|
| 55 |
+
value,
|
| 56 |
+
highlighter=highlighter,
|
| 57 |
+
indent_guides=indent_guides,
|
| 58 |
+
max_length=max_length,
|
| 59 |
+
max_string=max_string,
|
| 60 |
+
),
|
| 61 |
+
)
|
| 62 |
+
return Panel.fit(
|
| 63 |
+
items_table,
|
| 64 |
+
title=title,
|
| 65 |
+
border_style="scope.border",
|
| 66 |
+
padding=(0, 1),
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__": # pragma: no cover
|
| 71 |
+
from pip._vendor.rich import print
|
| 72 |
+
|
| 73 |
+
print()
|
| 74 |
+
|
| 75 |
+
def test(foo: float, bar: float) -> None:
|
| 76 |
+
list_of_things = [1, 2, 3, None, 4, True, False, "Hello World"]
|
| 77 |
+
dict_of_things = {
|
| 78 |
+
"version": "1.1",
|
| 79 |
+
"method": "confirmFruitPurchase",
|
| 80 |
+
"params": [["apple", "orange", "mangoes", "pomelo"], 1.123],
|
| 81 |
+
"id": "194521489",
|
| 82 |
+
}
|
| 83 |
+
print(render_scope(locals(), title="[i]locals", sort_keys=False))
|
| 84 |
+
|
| 85 |
+
test(20.3423, 3.1427)
|
| 86 |
+
print()
|
env/lib/python3.13/site-packages/pip/_vendor/rich/screen.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional, TYPE_CHECKING
|
| 2 |
+
|
| 3 |
+
from .segment import Segment
|
| 4 |
+
from .style import StyleType
|
| 5 |
+
from ._loop import loop_last
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
if TYPE_CHECKING:
|
| 9 |
+
from .console import (
|
| 10 |
+
Console,
|
| 11 |
+
ConsoleOptions,
|
| 12 |
+
RenderResult,
|
| 13 |
+
RenderableType,
|
| 14 |
+
Group,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class Screen:
|
| 19 |
+
"""A renderable that fills the terminal screen and crops excess.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
renderable (RenderableType): Child renderable.
|
| 23 |
+
style (StyleType, optional): Optional background style. Defaults to None.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
renderable: "RenderableType"
|
| 27 |
+
|
| 28 |
+
def __init__(
|
| 29 |
+
self,
|
| 30 |
+
*renderables: "RenderableType",
|
| 31 |
+
style: Optional[StyleType] = None,
|
| 32 |
+
application_mode: bool = False,
|
| 33 |
+
) -> None:
|
| 34 |
+
from pip._vendor.rich.console import Group
|
| 35 |
+
|
| 36 |
+
self.renderable = Group(*renderables)
|
| 37 |
+
self.style = style
|
| 38 |
+
self.application_mode = application_mode
|
| 39 |
+
|
| 40 |
+
def __rich_console__(
|
| 41 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 42 |
+
) -> "RenderResult":
|
| 43 |
+
width, height = options.size
|
| 44 |
+
style = console.get_style(self.style) if self.style else None
|
| 45 |
+
render_options = options.update(width=width, height=height)
|
| 46 |
+
lines = console.render_lines(
|
| 47 |
+
self.renderable or "", render_options, style=style, pad=True
|
| 48 |
+
)
|
| 49 |
+
lines = Segment.set_shape(lines, width, height, style=style)
|
| 50 |
+
new_line = Segment("\n\r") if self.application_mode else Segment.line()
|
| 51 |
+
for last, line in loop_last(lines):
|
| 52 |
+
yield from line
|
| 53 |
+
if not last:
|
| 54 |
+
yield new_line
|
env/lib/python3.13/site-packages/pip/_vendor/rich/segment.py
ADDED
|
@@ -0,0 +1,752 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from enum import IntEnum
|
| 2 |
+
from functools import lru_cache
|
| 3 |
+
from itertools import filterfalse
|
| 4 |
+
from logging import getLogger
|
| 5 |
+
from operator import attrgetter
|
| 6 |
+
from typing import (
|
| 7 |
+
TYPE_CHECKING,
|
| 8 |
+
Dict,
|
| 9 |
+
Iterable,
|
| 10 |
+
List,
|
| 11 |
+
NamedTuple,
|
| 12 |
+
Optional,
|
| 13 |
+
Sequence,
|
| 14 |
+
Tuple,
|
| 15 |
+
Type,
|
| 16 |
+
Union,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
from .cells import (
|
| 20 |
+
_is_single_cell_widths,
|
| 21 |
+
cached_cell_len,
|
| 22 |
+
cell_len,
|
| 23 |
+
get_character_cell_size,
|
| 24 |
+
set_cell_size,
|
| 25 |
+
)
|
| 26 |
+
from .repr import Result, rich_repr
|
| 27 |
+
from .style import Style
|
| 28 |
+
|
| 29 |
+
if TYPE_CHECKING:
|
| 30 |
+
from .console import Console, ConsoleOptions, RenderResult
|
| 31 |
+
|
| 32 |
+
log = getLogger("rich")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class ControlType(IntEnum):
|
| 36 |
+
"""Non-printable control codes which typically translate to ANSI codes."""
|
| 37 |
+
|
| 38 |
+
BELL = 1
|
| 39 |
+
CARRIAGE_RETURN = 2
|
| 40 |
+
HOME = 3
|
| 41 |
+
CLEAR = 4
|
| 42 |
+
SHOW_CURSOR = 5
|
| 43 |
+
HIDE_CURSOR = 6
|
| 44 |
+
ENABLE_ALT_SCREEN = 7
|
| 45 |
+
DISABLE_ALT_SCREEN = 8
|
| 46 |
+
CURSOR_UP = 9
|
| 47 |
+
CURSOR_DOWN = 10
|
| 48 |
+
CURSOR_FORWARD = 11
|
| 49 |
+
CURSOR_BACKWARD = 12
|
| 50 |
+
CURSOR_MOVE_TO_COLUMN = 13
|
| 51 |
+
CURSOR_MOVE_TO = 14
|
| 52 |
+
ERASE_IN_LINE = 15
|
| 53 |
+
SET_WINDOW_TITLE = 16
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
ControlCode = Union[
|
| 57 |
+
Tuple[ControlType],
|
| 58 |
+
Tuple[ControlType, Union[int, str]],
|
| 59 |
+
Tuple[ControlType, int, int],
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@rich_repr()
|
| 64 |
+
class Segment(NamedTuple):
|
| 65 |
+
"""A piece of text with associated style. Segments are produced by the Console render process and
|
| 66 |
+
are ultimately converted in to strings to be written to the terminal.
|
| 67 |
+
|
| 68 |
+
Args:
|
| 69 |
+
text (str): A piece of text.
|
| 70 |
+
style (:class:`~rich.style.Style`, optional): An optional style to apply to the text.
|
| 71 |
+
control (Tuple[ControlCode], optional): Optional sequence of control codes.
|
| 72 |
+
|
| 73 |
+
Attributes:
|
| 74 |
+
cell_length (int): The cell length of this Segment.
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
text: str
|
| 78 |
+
style: Optional[Style] = None
|
| 79 |
+
control: Optional[Sequence[ControlCode]] = None
|
| 80 |
+
|
| 81 |
+
@property
|
| 82 |
+
def cell_length(self) -> int:
|
| 83 |
+
"""The number of terminal cells required to display self.text.
|
| 84 |
+
|
| 85 |
+
Returns:
|
| 86 |
+
int: A number of cells.
|
| 87 |
+
"""
|
| 88 |
+
text, _style, control = self
|
| 89 |
+
return 0 if control else cell_len(text)
|
| 90 |
+
|
| 91 |
+
def __rich_repr__(self) -> Result:
|
| 92 |
+
yield self.text
|
| 93 |
+
if self.control is None:
|
| 94 |
+
if self.style is not None:
|
| 95 |
+
yield self.style
|
| 96 |
+
else:
|
| 97 |
+
yield self.style
|
| 98 |
+
yield self.control
|
| 99 |
+
|
| 100 |
+
def __bool__(self) -> bool:
|
| 101 |
+
"""Check if the segment contains text."""
|
| 102 |
+
return bool(self.text)
|
| 103 |
+
|
| 104 |
+
@property
|
| 105 |
+
def is_control(self) -> bool:
|
| 106 |
+
"""Check if the segment contains control codes."""
|
| 107 |
+
return self.control is not None
|
| 108 |
+
|
| 109 |
+
@classmethod
|
| 110 |
+
@lru_cache(1024 * 16)
|
| 111 |
+
def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment"]:
|
| 112 |
+
"""Split a segment in to two at a given cell position.
|
| 113 |
+
|
| 114 |
+
Note that splitting a double-width character, may result in that character turning
|
| 115 |
+
into two spaces.
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
segment (Segment): A segment to split.
|
| 119 |
+
cut (int): A cell position to cut on.
|
| 120 |
+
|
| 121 |
+
Returns:
|
| 122 |
+
A tuple of two segments.
|
| 123 |
+
"""
|
| 124 |
+
text, style, control = segment
|
| 125 |
+
_Segment = Segment
|
| 126 |
+
cell_length = segment.cell_length
|
| 127 |
+
if cut >= cell_length:
|
| 128 |
+
return segment, _Segment("", style, control)
|
| 129 |
+
|
| 130 |
+
cell_size = get_character_cell_size
|
| 131 |
+
|
| 132 |
+
pos = int((cut / cell_length) * len(text))
|
| 133 |
+
|
| 134 |
+
while True:
|
| 135 |
+
before = text[:pos]
|
| 136 |
+
cell_pos = cell_len(before)
|
| 137 |
+
out_by = cell_pos - cut
|
| 138 |
+
if not out_by:
|
| 139 |
+
return (
|
| 140 |
+
_Segment(before, style, control),
|
| 141 |
+
_Segment(text[pos:], style, control),
|
| 142 |
+
)
|
| 143 |
+
if out_by == -1 and cell_size(text[pos]) == 2:
|
| 144 |
+
return (
|
| 145 |
+
_Segment(text[:pos] + " ", style, control),
|
| 146 |
+
_Segment(" " + text[pos + 1 :], style, control),
|
| 147 |
+
)
|
| 148 |
+
if out_by == +1 and cell_size(text[pos - 1]) == 2:
|
| 149 |
+
return (
|
| 150 |
+
_Segment(text[: pos - 1] + " ", style, control),
|
| 151 |
+
_Segment(" " + text[pos:], style, control),
|
| 152 |
+
)
|
| 153 |
+
if cell_pos < cut:
|
| 154 |
+
pos += 1
|
| 155 |
+
else:
|
| 156 |
+
pos -= 1
|
| 157 |
+
|
| 158 |
+
def split_cells(self, cut: int) -> Tuple["Segment", "Segment"]:
|
| 159 |
+
"""Split segment in to two segments at the specified column.
|
| 160 |
+
|
| 161 |
+
If the cut point falls in the middle of a 2-cell wide character then it is replaced
|
| 162 |
+
by two spaces, to preserve the display width of the parent segment.
|
| 163 |
+
|
| 164 |
+
Args:
|
| 165 |
+
cut (int): Offset within the segment to cut.
|
| 166 |
+
|
| 167 |
+
Returns:
|
| 168 |
+
Tuple[Segment, Segment]: Two segments.
|
| 169 |
+
"""
|
| 170 |
+
text, style, control = self
|
| 171 |
+
assert cut >= 0
|
| 172 |
+
|
| 173 |
+
if _is_single_cell_widths(text):
|
| 174 |
+
# Fast path with all 1 cell characters
|
| 175 |
+
if cut >= len(text):
|
| 176 |
+
return self, Segment("", style, control)
|
| 177 |
+
return (
|
| 178 |
+
Segment(text[:cut], style, control),
|
| 179 |
+
Segment(text[cut:], style, control),
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
return self._split_cells(self, cut)
|
| 183 |
+
|
| 184 |
+
@classmethod
|
| 185 |
+
def line(cls) -> "Segment":
|
| 186 |
+
"""Make a new line segment."""
|
| 187 |
+
return cls("\n")
|
| 188 |
+
|
| 189 |
+
@classmethod
|
| 190 |
+
def apply_style(
|
| 191 |
+
cls,
|
| 192 |
+
segments: Iterable["Segment"],
|
| 193 |
+
style: Optional[Style] = None,
|
| 194 |
+
post_style: Optional[Style] = None,
|
| 195 |
+
) -> Iterable["Segment"]:
|
| 196 |
+
"""Apply style(s) to an iterable of segments.
|
| 197 |
+
|
| 198 |
+
Returns an iterable of segments where the style is replaced by ``style + segment.style + post_style``.
|
| 199 |
+
|
| 200 |
+
Args:
|
| 201 |
+
segments (Iterable[Segment]): Segments to process.
|
| 202 |
+
style (Style, optional): Base style. Defaults to None.
|
| 203 |
+
post_style (Style, optional): Style to apply on top of segment style. Defaults to None.
|
| 204 |
+
|
| 205 |
+
Returns:
|
| 206 |
+
Iterable[Segments]: A new iterable of segments (possibly the same iterable).
|
| 207 |
+
"""
|
| 208 |
+
result_segments = segments
|
| 209 |
+
if style:
|
| 210 |
+
apply = style.__add__
|
| 211 |
+
result_segments = (
|
| 212 |
+
cls(text, None if control else apply(_style), control)
|
| 213 |
+
for text, _style, control in result_segments
|
| 214 |
+
)
|
| 215 |
+
if post_style:
|
| 216 |
+
result_segments = (
|
| 217 |
+
cls(
|
| 218 |
+
text,
|
| 219 |
+
(
|
| 220 |
+
None
|
| 221 |
+
if control
|
| 222 |
+
else (_style + post_style if _style else post_style)
|
| 223 |
+
),
|
| 224 |
+
control,
|
| 225 |
+
)
|
| 226 |
+
for text, _style, control in result_segments
|
| 227 |
+
)
|
| 228 |
+
return result_segments
|
| 229 |
+
|
| 230 |
+
@classmethod
|
| 231 |
+
def filter_control(
|
| 232 |
+
cls, segments: Iterable["Segment"], is_control: bool = False
|
| 233 |
+
) -> Iterable["Segment"]:
|
| 234 |
+
"""Filter segments by ``is_control`` attribute.
|
| 235 |
+
|
| 236 |
+
Args:
|
| 237 |
+
segments (Iterable[Segment]): An iterable of Segment instances.
|
| 238 |
+
is_control (bool, optional): is_control flag to match in search.
|
| 239 |
+
|
| 240 |
+
Returns:
|
| 241 |
+
Iterable[Segment]: And iterable of Segment instances.
|
| 242 |
+
|
| 243 |
+
"""
|
| 244 |
+
if is_control:
|
| 245 |
+
return filter(attrgetter("control"), segments)
|
| 246 |
+
else:
|
| 247 |
+
return filterfalse(attrgetter("control"), segments)
|
| 248 |
+
|
| 249 |
+
@classmethod
|
| 250 |
+
def split_lines(cls, segments: Iterable["Segment"]) -> Iterable[List["Segment"]]:
|
| 251 |
+
"""Split a sequence of segments in to a list of lines.
|
| 252 |
+
|
| 253 |
+
Args:
|
| 254 |
+
segments (Iterable[Segment]): Segments potentially containing line feeds.
|
| 255 |
+
|
| 256 |
+
Yields:
|
| 257 |
+
Iterable[List[Segment]]: Iterable of segment lists, one per line.
|
| 258 |
+
"""
|
| 259 |
+
line: List[Segment] = []
|
| 260 |
+
append = line.append
|
| 261 |
+
|
| 262 |
+
for segment in segments:
|
| 263 |
+
if "\n" in segment.text and not segment.control:
|
| 264 |
+
text, style, _ = segment
|
| 265 |
+
while text:
|
| 266 |
+
_text, new_line, text = text.partition("\n")
|
| 267 |
+
if _text:
|
| 268 |
+
append(cls(_text, style))
|
| 269 |
+
if new_line:
|
| 270 |
+
yield line
|
| 271 |
+
line = []
|
| 272 |
+
append = line.append
|
| 273 |
+
else:
|
| 274 |
+
append(segment)
|
| 275 |
+
if line:
|
| 276 |
+
yield line
|
| 277 |
+
|
| 278 |
+
@classmethod
|
| 279 |
+
def split_and_crop_lines(
|
| 280 |
+
cls,
|
| 281 |
+
segments: Iterable["Segment"],
|
| 282 |
+
length: int,
|
| 283 |
+
style: Optional[Style] = None,
|
| 284 |
+
pad: bool = True,
|
| 285 |
+
include_new_lines: bool = True,
|
| 286 |
+
) -> Iterable[List["Segment"]]:
|
| 287 |
+
"""Split segments in to lines, and crop lines greater than a given length.
|
| 288 |
+
|
| 289 |
+
Args:
|
| 290 |
+
segments (Iterable[Segment]): An iterable of segments, probably
|
| 291 |
+
generated from console.render.
|
| 292 |
+
length (int): Desired line length.
|
| 293 |
+
style (Style, optional): Style to use for any padding.
|
| 294 |
+
pad (bool): Enable padding of lines that are less than `length`.
|
| 295 |
+
|
| 296 |
+
Returns:
|
| 297 |
+
Iterable[List[Segment]]: An iterable of lines of segments.
|
| 298 |
+
"""
|
| 299 |
+
line: List[Segment] = []
|
| 300 |
+
append = line.append
|
| 301 |
+
|
| 302 |
+
adjust_line_length = cls.adjust_line_length
|
| 303 |
+
new_line_segment = cls("\n")
|
| 304 |
+
|
| 305 |
+
for segment in segments:
|
| 306 |
+
if "\n" in segment.text and not segment.control:
|
| 307 |
+
text, segment_style, _ = segment
|
| 308 |
+
while text:
|
| 309 |
+
_text, new_line, text = text.partition("\n")
|
| 310 |
+
if _text:
|
| 311 |
+
append(cls(_text, segment_style))
|
| 312 |
+
if new_line:
|
| 313 |
+
cropped_line = adjust_line_length(
|
| 314 |
+
line, length, style=style, pad=pad
|
| 315 |
+
)
|
| 316 |
+
if include_new_lines:
|
| 317 |
+
cropped_line.append(new_line_segment)
|
| 318 |
+
yield cropped_line
|
| 319 |
+
line.clear()
|
| 320 |
+
else:
|
| 321 |
+
append(segment)
|
| 322 |
+
if line:
|
| 323 |
+
yield adjust_line_length(line, length, style=style, pad=pad)
|
| 324 |
+
|
| 325 |
+
@classmethod
|
| 326 |
+
def adjust_line_length(
|
| 327 |
+
cls,
|
| 328 |
+
line: List["Segment"],
|
| 329 |
+
length: int,
|
| 330 |
+
style: Optional[Style] = None,
|
| 331 |
+
pad: bool = True,
|
| 332 |
+
) -> List["Segment"]:
|
| 333 |
+
"""Adjust a line to a given width (cropping or padding as required).
|
| 334 |
+
|
| 335 |
+
Args:
|
| 336 |
+
segments (Iterable[Segment]): A list of segments in a single line.
|
| 337 |
+
length (int): The desired width of the line.
|
| 338 |
+
style (Style, optional): The style of padding if used (space on the end). Defaults to None.
|
| 339 |
+
pad (bool, optional): Pad lines with spaces if they are shorter than `length`. Defaults to True.
|
| 340 |
+
|
| 341 |
+
Returns:
|
| 342 |
+
List[Segment]: A line of segments with the desired length.
|
| 343 |
+
"""
|
| 344 |
+
line_length = sum(segment.cell_length for segment in line)
|
| 345 |
+
new_line: List[Segment]
|
| 346 |
+
|
| 347 |
+
if line_length < length:
|
| 348 |
+
if pad:
|
| 349 |
+
new_line = line + [cls(" " * (length - line_length), style)]
|
| 350 |
+
else:
|
| 351 |
+
new_line = line[:]
|
| 352 |
+
elif line_length > length:
|
| 353 |
+
new_line = []
|
| 354 |
+
append = new_line.append
|
| 355 |
+
line_length = 0
|
| 356 |
+
for segment in line:
|
| 357 |
+
segment_length = segment.cell_length
|
| 358 |
+
if line_length + segment_length < length or segment.control:
|
| 359 |
+
append(segment)
|
| 360 |
+
line_length += segment_length
|
| 361 |
+
else:
|
| 362 |
+
text, segment_style, _ = segment
|
| 363 |
+
text = set_cell_size(text, length - line_length)
|
| 364 |
+
append(cls(text, segment_style))
|
| 365 |
+
break
|
| 366 |
+
else:
|
| 367 |
+
new_line = line[:]
|
| 368 |
+
return new_line
|
| 369 |
+
|
| 370 |
+
@classmethod
|
| 371 |
+
def get_line_length(cls, line: List["Segment"]) -> int:
|
| 372 |
+
"""Get the length of list of segments.
|
| 373 |
+
|
| 374 |
+
Args:
|
| 375 |
+
line (List[Segment]): A line encoded as a list of Segments (assumes no '\\\\n' characters),
|
| 376 |
+
|
| 377 |
+
Returns:
|
| 378 |
+
int: The length of the line.
|
| 379 |
+
"""
|
| 380 |
+
_cell_len = cell_len
|
| 381 |
+
return sum(_cell_len(text) for text, style, control in line if not control)
|
| 382 |
+
|
| 383 |
+
@classmethod
|
| 384 |
+
def get_shape(cls, lines: List[List["Segment"]]) -> Tuple[int, int]:
|
| 385 |
+
"""Get the shape (enclosing rectangle) of a list of lines.
|
| 386 |
+
|
| 387 |
+
Args:
|
| 388 |
+
lines (List[List[Segment]]): A list of lines (no '\\\\n' characters).
|
| 389 |
+
|
| 390 |
+
Returns:
|
| 391 |
+
Tuple[int, int]: Width and height in characters.
|
| 392 |
+
"""
|
| 393 |
+
get_line_length = cls.get_line_length
|
| 394 |
+
max_width = max(get_line_length(line) for line in lines) if lines else 0
|
| 395 |
+
return (max_width, len(lines))
|
| 396 |
+
|
| 397 |
+
@classmethod
|
| 398 |
+
def set_shape(
|
| 399 |
+
cls,
|
| 400 |
+
lines: List[List["Segment"]],
|
| 401 |
+
width: int,
|
| 402 |
+
height: Optional[int] = None,
|
| 403 |
+
style: Optional[Style] = None,
|
| 404 |
+
new_lines: bool = False,
|
| 405 |
+
) -> List[List["Segment"]]:
|
| 406 |
+
"""Set the shape of a list of lines (enclosing rectangle).
|
| 407 |
+
|
| 408 |
+
Args:
|
| 409 |
+
lines (List[List[Segment]]): A list of lines.
|
| 410 |
+
width (int): Desired width.
|
| 411 |
+
height (int, optional): Desired height or None for no change.
|
| 412 |
+
style (Style, optional): Style of any padding added.
|
| 413 |
+
new_lines (bool, optional): Padded lines should include "\n". Defaults to False.
|
| 414 |
+
|
| 415 |
+
Returns:
|
| 416 |
+
List[List[Segment]]: New list of lines.
|
| 417 |
+
"""
|
| 418 |
+
_height = height or len(lines)
|
| 419 |
+
|
| 420 |
+
blank = (
|
| 421 |
+
[cls(" " * width + "\n", style)] if new_lines else [cls(" " * width, style)]
|
| 422 |
+
)
|
| 423 |
+
|
| 424 |
+
adjust_line_length = cls.adjust_line_length
|
| 425 |
+
shaped_lines = lines[:_height]
|
| 426 |
+
shaped_lines[:] = [
|
| 427 |
+
adjust_line_length(line, width, style=style) for line in lines
|
| 428 |
+
]
|
| 429 |
+
if len(shaped_lines) < _height:
|
| 430 |
+
shaped_lines.extend([blank] * (_height - len(shaped_lines)))
|
| 431 |
+
return shaped_lines
|
| 432 |
+
|
| 433 |
+
@classmethod
|
| 434 |
+
def align_top(
|
| 435 |
+
cls: Type["Segment"],
|
| 436 |
+
lines: List[List["Segment"]],
|
| 437 |
+
width: int,
|
| 438 |
+
height: int,
|
| 439 |
+
style: Style,
|
| 440 |
+
new_lines: bool = False,
|
| 441 |
+
) -> List[List["Segment"]]:
|
| 442 |
+
"""Aligns lines to top (adds extra lines to bottom as required).
|
| 443 |
+
|
| 444 |
+
Args:
|
| 445 |
+
lines (List[List[Segment]]): A list of lines.
|
| 446 |
+
width (int): Desired width.
|
| 447 |
+
height (int, optional): Desired height or None for no change.
|
| 448 |
+
style (Style): Style of any padding added.
|
| 449 |
+
new_lines (bool, optional): Padded lines should include "\n". Defaults to False.
|
| 450 |
+
|
| 451 |
+
Returns:
|
| 452 |
+
List[List[Segment]]: New list of lines.
|
| 453 |
+
"""
|
| 454 |
+
extra_lines = height - len(lines)
|
| 455 |
+
if not extra_lines:
|
| 456 |
+
return lines[:]
|
| 457 |
+
lines = lines[:height]
|
| 458 |
+
blank = cls(" " * width + "\n", style) if new_lines else cls(" " * width, style)
|
| 459 |
+
lines = lines + [[blank]] * extra_lines
|
| 460 |
+
return lines
|
| 461 |
+
|
| 462 |
+
@classmethod
|
| 463 |
+
def align_bottom(
|
| 464 |
+
cls: Type["Segment"],
|
| 465 |
+
lines: List[List["Segment"]],
|
| 466 |
+
width: int,
|
| 467 |
+
height: int,
|
| 468 |
+
style: Style,
|
| 469 |
+
new_lines: bool = False,
|
| 470 |
+
) -> List[List["Segment"]]:
|
| 471 |
+
"""Aligns render to bottom (adds extra lines above as required).
|
| 472 |
+
|
| 473 |
+
Args:
|
| 474 |
+
lines (List[List[Segment]]): A list of lines.
|
| 475 |
+
width (int): Desired width.
|
| 476 |
+
height (int, optional): Desired height or None for no change.
|
| 477 |
+
style (Style): Style of any padding added. Defaults to None.
|
| 478 |
+
new_lines (bool, optional): Padded lines should include "\n". Defaults to False.
|
| 479 |
+
|
| 480 |
+
Returns:
|
| 481 |
+
List[List[Segment]]: New list of lines.
|
| 482 |
+
"""
|
| 483 |
+
extra_lines = height - len(lines)
|
| 484 |
+
if not extra_lines:
|
| 485 |
+
return lines[:]
|
| 486 |
+
lines = lines[:height]
|
| 487 |
+
blank = cls(" " * width + "\n", style) if new_lines else cls(" " * width, style)
|
| 488 |
+
lines = [[blank]] * extra_lines + lines
|
| 489 |
+
return lines
|
| 490 |
+
|
| 491 |
+
@classmethod
|
| 492 |
+
def align_middle(
|
| 493 |
+
cls: Type["Segment"],
|
| 494 |
+
lines: List[List["Segment"]],
|
| 495 |
+
width: int,
|
| 496 |
+
height: int,
|
| 497 |
+
style: Style,
|
| 498 |
+
new_lines: bool = False,
|
| 499 |
+
) -> List[List["Segment"]]:
|
| 500 |
+
"""Aligns lines to middle (adds extra lines to above and below as required).
|
| 501 |
+
|
| 502 |
+
Args:
|
| 503 |
+
lines (List[List[Segment]]): A list of lines.
|
| 504 |
+
width (int): Desired width.
|
| 505 |
+
height (int, optional): Desired height or None for no change.
|
| 506 |
+
style (Style): Style of any padding added.
|
| 507 |
+
new_lines (bool, optional): Padded lines should include "\n". Defaults to False.
|
| 508 |
+
|
| 509 |
+
Returns:
|
| 510 |
+
List[List[Segment]]: New list of lines.
|
| 511 |
+
"""
|
| 512 |
+
extra_lines = height - len(lines)
|
| 513 |
+
if not extra_lines:
|
| 514 |
+
return lines[:]
|
| 515 |
+
lines = lines[:height]
|
| 516 |
+
blank = cls(" " * width + "\n", style) if new_lines else cls(" " * width, style)
|
| 517 |
+
top_lines = extra_lines // 2
|
| 518 |
+
bottom_lines = extra_lines - top_lines
|
| 519 |
+
lines = [[blank]] * top_lines + lines + [[blank]] * bottom_lines
|
| 520 |
+
return lines
|
| 521 |
+
|
| 522 |
+
@classmethod
|
| 523 |
+
def simplify(cls, segments: Iterable["Segment"]) -> Iterable["Segment"]:
|
| 524 |
+
"""Simplify an iterable of segments by combining contiguous segments with the same style.
|
| 525 |
+
|
| 526 |
+
Args:
|
| 527 |
+
segments (Iterable[Segment]): An iterable of segments.
|
| 528 |
+
|
| 529 |
+
Returns:
|
| 530 |
+
Iterable[Segment]: A possibly smaller iterable of segments that will render the same way.
|
| 531 |
+
"""
|
| 532 |
+
iter_segments = iter(segments)
|
| 533 |
+
try:
|
| 534 |
+
last_segment = next(iter_segments)
|
| 535 |
+
except StopIteration:
|
| 536 |
+
return
|
| 537 |
+
|
| 538 |
+
_Segment = Segment
|
| 539 |
+
for segment in iter_segments:
|
| 540 |
+
if last_segment.style == segment.style and not segment.control:
|
| 541 |
+
last_segment = _Segment(
|
| 542 |
+
last_segment.text + segment.text, last_segment.style
|
| 543 |
+
)
|
| 544 |
+
else:
|
| 545 |
+
yield last_segment
|
| 546 |
+
last_segment = segment
|
| 547 |
+
yield last_segment
|
| 548 |
+
|
| 549 |
+
@classmethod
|
| 550 |
+
def strip_links(cls, segments: Iterable["Segment"]) -> Iterable["Segment"]:
|
| 551 |
+
"""Remove all links from an iterable of styles.
|
| 552 |
+
|
| 553 |
+
Args:
|
| 554 |
+
segments (Iterable[Segment]): An iterable segments.
|
| 555 |
+
|
| 556 |
+
Yields:
|
| 557 |
+
Segment: Segments with link removed.
|
| 558 |
+
"""
|
| 559 |
+
for segment in segments:
|
| 560 |
+
if segment.control or segment.style is None:
|
| 561 |
+
yield segment
|
| 562 |
+
else:
|
| 563 |
+
text, style, _control = segment
|
| 564 |
+
yield cls(text, style.update_link(None) if style else None)
|
| 565 |
+
|
| 566 |
+
@classmethod
|
| 567 |
+
def strip_styles(cls, segments: Iterable["Segment"]) -> Iterable["Segment"]:
|
| 568 |
+
"""Remove all styles from an iterable of segments.
|
| 569 |
+
|
| 570 |
+
Args:
|
| 571 |
+
segments (Iterable[Segment]): An iterable segments.
|
| 572 |
+
|
| 573 |
+
Yields:
|
| 574 |
+
Segment: Segments with styles replace with None
|
| 575 |
+
"""
|
| 576 |
+
for text, _style, control in segments:
|
| 577 |
+
yield cls(text, None, control)
|
| 578 |
+
|
| 579 |
+
@classmethod
|
| 580 |
+
def remove_color(cls, segments: Iterable["Segment"]) -> Iterable["Segment"]:
|
| 581 |
+
"""Remove all color from an iterable of segments.
|
| 582 |
+
|
| 583 |
+
Args:
|
| 584 |
+
segments (Iterable[Segment]): An iterable segments.
|
| 585 |
+
|
| 586 |
+
Yields:
|
| 587 |
+
Segment: Segments with colorless style.
|
| 588 |
+
"""
|
| 589 |
+
|
| 590 |
+
cache: Dict[Style, Style] = {}
|
| 591 |
+
for text, style, control in segments:
|
| 592 |
+
if style:
|
| 593 |
+
colorless_style = cache.get(style)
|
| 594 |
+
if colorless_style is None:
|
| 595 |
+
colorless_style = style.without_color
|
| 596 |
+
cache[style] = colorless_style
|
| 597 |
+
yield cls(text, colorless_style, control)
|
| 598 |
+
else:
|
| 599 |
+
yield cls(text, None, control)
|
| 600 |
+
|
| 601 |
+
@classmethod
|
| 602 |
+
def divide(
|
| 603 |
+
cls, segments: Iterable["Segment"], cuts: Iterable[int]
|
| 604 |
+
) -> Iterable[List["Segment"]]:
|
| 605 |
+
"""Divides an iterable of segments in to portions.
|
| 606 |
+
|
| 607 |
+
Args:
|
| 608 |
+
cuts (Iterable[int]): Cell positions where to divide.
|
| 609 |
+
|
| 610 |
+
Yields:
|
| 611 |
+
[Iterable[List[Segment]]]: An iterable of Segments in List.
|
| 612 |
+
"""
|
| 613 |
+
split_segments: List["Segment"] = []
|
| 614 |
+
add_segment = split_segments.append
|
| 615 |
+
|
| 616 |
+
iter_cuts = iter(cuts)
|
| 617 |
+
|
| 618 |
+
while True:
|
| 619 |
+
cut = next(iter_cuts, -1)
|
| 620 |
+
if cut == -1:
|
| 621 |
+
return
|
| 622 |
+
if cut != 0:
|
| 623 |
+
break
|
| 624 |
+
yield []
|
| 625 |
+
pos = 0
|
| 626 |
+
|
| 627 |
+
segments_clear = split_segments.clear
|
| 628 |
+
segments_copy = split_segments.copy
|
| 629 |
+
|
| 630 |
+
_cell_len = cached_cell_len
|
| 631 |
+
for segment in segments:
|
| 632 |
+
text, _style, control = segment
|
| 633 |
+
while text:
|
| 634 |
+
end_pos = pos if control else pos + _cell_len(text)
|
| 635 |
+
if end_pos < cut:
|
| 636 |
+
add_segment(segment)
|
| 637 |
+
pos = end_pos
|
| 638 |
+
break
|
| 639 |
+
|
| 640 |
+
if end_pos == cut:
|
| 641 |
+
add_segment(segment)
|
| 642 |
+
yield segments_copy()
|
| 643 |
+
segments_clear()
|
| 644 |
+
pos = end_pos
|
| 645 |
+
|
| 646 |
+
cut = next(iter_cuts, -1)
|
| 647 |
+
if cut == -1:
|
| 648 |
+
if split_segments:
|
| 649 |
+
yield segments_copy()
|
| 650 |
+
return
|
| 651 |
+
|
| 652 |
+
break
|
| 653 |
+
|
| 654 |
+
else:
|
| 655 |
+
before, segment = segment.split_cells(cut - pos)
|
| 656 |
+
text, _style, control = segment
|
| 657 |
+
add_segment(before)
|
| 658 |
+
yield segments_copy()
|
| 659 |
+
segments_clear()
|
| 660 |
+
pos = cut
|
| 661 |
+
|
| 662 |
+
cut = next(iter_cuts, -1)
|
| 663 |
+
if cut == -1:
|
| 664 |
+
if split_segments:
|
| 665 |
+
yield segments_copy()
|
| 666 |
+
return
|
| 667 |
+
|
| 668 |
+
yield segments_copy()
|
| 669 |
+
|
| 670 |
+
|
| 671 |
+
class Segments:
|
| 672 |
+
"""A simple renderable to render an iterable of segments. This class may be useful if
|
| 673 |
+
you want to print segments outside of a __rich_console__ method.
|
| 674 |
+
|
| 675 |
+
Args:
|
| 676 |
+
segments (Iterable[Segment]): An iterable of segments.
|
| 677 |
+
new_lines (bool, optional): Add new lines between segments. Defaults to False.
|
| 678 |
+
"""
|
| 679 |
+
|
| 680 |
+
def __init__(self, segments: Iterable[Segment], new_lines: bool = False) -> None:
|
| 681 |
+
self.segments = list(segments)
|
| 682 |
+
self.new_lines = new_lines
|
| 683 |
+
|
| 684 |
+
def __rich_console__(
|
| 685 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 686 |
+
) -> "RenderResult":
|
| 687 |
+
if self.new_lines:
|
| 688 |
+
line = Segment.line()
|
| 689 |
+
for segment in self.segments:
|
| 690 |
+
yield segment
|
| 691 |
+
yield line
|
| 692 |
+
else:
|
| 693 |
+
yield from self.segments
|
| 694 |
+
|
| 695 |
+
|
| 696 |
+
class SegmentLines:
|
| 697 |
+
def __init__(self, lines: Iterable[List[Segment]], new_lines: bool = False) -> None:
|
| 698 |
+
"""A simple renderable containing a number of lines of segments. May be used as an intermediate
|
| 699 |
+
in rendering process.
|
| 700 |
+
|
| 701 |
+
Args:
|
| 702 |
+
lines (Iterable[List[Segment]]): Lists of segments forming lines.
|
| 703 |
+
new_lines (bool, optional): Insert new lines after each line. Defaults to False.
|
| 704 |
+
"""
|
| 705 |
+
self.lines = list(lines)
|
| 706 |
+
self.new_lines = new_lines
|
| 707 |
+
|
| 708 |
+
def __rich_console__(
|
| 709 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 710 |
+
) -> "RenderResult":
|
| 711 |
+
if self.new_lines:
|
| 712 |
+
new_line = Segment.line()
|
| 713 |
+
for line in self.lines:
|
| 714 |
+
yield from line
|
| 715 |
+
yield new_line
|
| 716 |
+
else:
|
| 717 |
+
for line in self.lines:
|
| 718 |
+
yield from line
|
| 719 |
+
|
| 720 |
+
|
| 721 |
+
if __name__ == "__main__": # pragma: no cover
|
| 722 |
+
from pip._vendor.rich.console import Console
|
| 723 |
+
from pip._vendor.rich.syntax import Syntax
|
| 724 |
+
from pip._vendor.rich.text import Text
|
| 725 |
+
|
| 726 |
+
code = """from rich.console import Console
|
| 727 |
+
console = Console()
|
| 728 |
+
text = Text.from_markup("Hello, [bold magenta]World[/]!")
|
| 729 |
+
console.print(text)"""
|
| 730 |
+
|
| 731 |
+
text = Text.from_markup("Hello, [bold magenta]World[/]!")
|
| 732 |
+
|
| 733 |
+
console = Console()
|
| 734 |
+
|
| 735 |
+
console.rule("rich.Segment")
|
| 736 |
+
console.print(
|
| 737 |
+
"A Segment is the last step in the Rich render process before generating text with ANSI codes."
|
| 738 |
+
)
|
| 739 |
+
console.print("\nConsider the following code:\n")
|
| 740 |
+
console.print(Syntax(code, "python", line_numbers=True))
|
| 741 |
+
console.print()
|
| 742 |
+
console.print(
|
| 743 |
+
"When you call [b]print()[/b], Rich [i]renders[/i] the object in to the following:\n"
|
| 744 |
+
)
|
| 745 |
+
fragments = list(console.render(text))
|
| 746 |
+
console.print(fragments)
|
| 747 |
+
console.print()
|
| 748 |
+
console.print("The Segments are then processed to produce the following output:\n")
|
| 749 |
+
console.print(text)
|
| 750 |
+
console.print(
|
| 751 |
+
"\nYou will only need to know this if you are implementing your own Rich renderables."
|
| 752 |
+
)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/status.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from types import TracebackType
|
| 2 |
+
from typing import Optional, Type
|
| 3 |
+
|
| 4 |
+
from .console import Console, RenderableType
|
| 5 |
+
from .jupyter import JupyterMixin
|
| 6 |
+
from .live import Live
|
| 7 |
+
from .spinner import Spinner
|
| 8 |
+
from .style import StyleType
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Status(JupyterMixin):
|
| 12 |
+
"""Displays a status indicator with a 'spinner' animation.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
status (RenderableType): A status renderable (str or Text typically).
|
| 16 |
+
console (Console, optional): Console instance to use, or None for global console. Defaults to None.
|
| 17 |
+
spinner (str, optional): Name of spinner animation (see python -m rich.spinner). Defaults to "dots".
|
| 18 |
+
spinner_style (StyleType, optional): Style of spinner. Defaults to "status.spinner".
|
| 19 |
+
speed (float, optional): Speed factor for spinner animation. Defaults to 1.0.
|
| 20 |
+
refresh_per_second (float, optional): Number of refreshes per second. Defaults to 12.5.
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
status: RenderableType,
|
| 26 |
+
*,
|
| 27 |
+
console: Optional[Console] = None,
|
| 28 |
+
spinner: str = "dots",
|
| 29 |
+
spinner_style: StyleType = "status.spinner",
|
| 30 |
+
speed: float = 1.0,
|
| 31 |
+
refresh_per_second: float = 12.5,
|
| 32 |
+
):
|
| 33 |
+
self.status = status
|
| 34 |
+
self.spinner_style = spinner_style
|
| 35 |
+
self.speed = speed
|
| 36 |
+
self._spinner = Spinner(spinner, text=status, style=spinner_style, speed=speed)
|
| 37 |
+
self._live = Live(
|
| 38 |
+
self.renderable,
|
| 39 |
+
console=console,
|
| 40 |
+
refresh_per_second=refresh_per_second,
|
| 41 |
+
transient=True,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
@property
|
| 45 |
+
def renderable(self) -> Spinner:
|
| 46 |
+
return self._spinner
|
| 47 |
+
|
| 48 |
+
@property
|
| 49 |
+
def console(self) -> "Console":
|
| 50 |
+
"""Get the Console used by the Status objects."""
|
| 51 |
+
return self._live.console
|
| 52 |
+
|
| 53 |
+
def update(
|
| 54 |
+
self,
|
| 55 |
+
status: Optional[RenderableType] = None,
|
| 56 |
+
*,
|
| 57 |
+
spinner: Optional[str] = None,
|
| 58 |
+
spinner_style: Optional[StyleType] = None,
|
| 59 |
+
speed: Optional[float] = None,
|
| 60 |
+
) -> None:
|
| 61 |
+
"""Update status.
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
status (Optional[RenderableType], optional): New status renderable or None for no change. Defaults to None.
|
| 65 |
+
spinner (Optional[str], optional): New spinner or None for no change. Defaults to None.
|
| 66 |
+
spinner_style (Optional[StyleType], optional): New spinner style or None for no change. Defaults to None.
|
| 67 |
+
speed (Optional[float], optional): Speed factor for spinner animation or None for no change. Defaults to None.
|
| 68 |
+
"""
|
| 69 |
+
if status is not None:
|
| 70 |
+
self.status = status
|
| 71 |
+
if spinner_style is not None:
|
| 72 |
+
self.spinner_style = spinner_style
|
| 73 |
+
if speed is not None:
|
| 74 |
+
self.speed = speed
|
| 75 |
+
if spinner is not None:
|
| 76 |
+
self._spinner = Spinner(
|
| 77 |
+
spinner, text=self.status, style=self.spinner_style, speed=self.speed
|
| 78 |
+
)
|
| 79 |
+
self._live.update(self.renderable, refresh=True)
|
| 80 |
+
else:
|
| 81 |
+
self._spinner.update(
|
| 82 |
+
text=self.status, style=self.spinner_style, speed=self.speed
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
def start(self) -> None:
|
| 86 |
+
"""Start the status animation."""
|
| 87 |
+
self._live.start()
|
| 88 |
+
|
| 89 |
+
def stop(self) -> None:
|
| 90 |
+
"""Stop the spinner animation."""
|
| 91 |
+
self._live.stop()
|
| 92 |
+
|
| 93 |
+
def __rich__(self) -> RenderableType:
|
| 94 |
+
return self.renderable
|
| 95 |
+
|
| 96 |
+
def __enter__(self) -> "Status":
|
| 97 |
+
self.start()
|
| 98 |
+
return self
|
| 99 |
+
|
| 100 |
+
def __exit__(
|
| 101 |
+
self,
|
| 102 |
+
exc_type: Optional[Type[BaseException]],
|
| 103 |
+
exc_val: Optional[BaseException],
|
| 104 |
+
exc_tb: Optional[TracebackType],
|
| 105 |
+
) -> None:
|
| 106 |
+
self.stop()
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__": # pragma: no cover
|
| 110 |
+
from time import sleep
|
| 111 |
+
|
| 112 |
+
from .console import Console
|
| 113 |
+
|
| 114 |
+
console = Console()
|
| 115 |
+
with console.status("[magenta]Covid detector booting up") as status:
|
| 116 |
+
sleep(3)
|
| 117 |
+
console.log("Importing advanced AI")
|
| 118 |
+
sleep(3)
|
| 119 |
+
console.log("Advanced Covid AI Ready")
|
| 120 |
+
sleep(3)
|
| 121 |
+
status.update(status="[bold blue] Scanning for Covid", spinner="earth")
|
| 122 |
+
sleep(3)
|
| 123 |
+
console.log("Found 10,000,000,000 copies of Covid32.exe")
|
| 124 |
+
sleep(3)
|
| 125 |
+
status.update(
|
| 126 |
+
status="[bold red]Moving Covid32.exe to Trash",
|
| 127 |
+
spinner="bouncingBall",
|
| 128 |
+
spinner_style="yellow",
|
| 129 |
+
)
|
| 130 |
+
sleep(5)
|
| 131 |
+
console.print("[bold green]Covid deleted successfully")
|
env/lib/python3.13/site-packages/pip/_vendor/rich/styled.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TYPE_CHECKING
|
| 2 |
+
|
| 3 |
+
from .measure import Measurement
|
| 4 |
+
from .segment import Segment
|
| 5 |
+
from .style import StyleType
|
| 6 |
+
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
from .console import Console, ConsoleOptions, RenderResult, RenderableType
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Styled:
|
| 12 |
+
"""Apply a style to a renderable.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
renderable (RenderableType): Any renderable.
|
| 16 |
+
style (StyleType): A style to apply across the entire renderable.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
def __init__(self, renderable: "RenderableType", style: "StyleType") -> None:
|
| 20 |
+
self.renderable = renderable
|
| 21 |
+
self.style = style
|
| 22 |
+
|
| 23 |
+
def __rich_console__(
|
| 24 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 25 |
+
) -> "RenderResult":
|
| 26 |
+
style = console.get_style(self.style)
|
| 27 |
+
rendered_segments = console.render(self.renderable, options)
|
| 28 |
+
segments = Segment.apply_style(rendered_segments, style)
|
| 29 |
+
return segments
|
| 30 |
+
|
| 31 |
+
def __rich_measure__(
|
| 32 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 33 |
+
) -> Measurement:
|
| 34 |
+
return Measurement.get(console, options, self.renderable)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__": # pragma: no cover
|
| 38 |
+
from pip._vendor.rich import print
|
| 39 |
+
from pip._vendor.rich.panel import Panel
|
| 40 |
+
|
| 41 |
+
panel = Styled(Panel("hello"), "on blue")
|
| 42 |
+
print(panel)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/table.py
ADDED
|
@@ -0,0 +1,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|
| 6 |
+
List,
|
| 7 |
+
NamedTuple,
|
| 8 |
+
Optional,
|
| 9 |
+
Sequence,
|
| 10 |
+
Tuple,
|
| 11 |
+
Union,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
from . import box, errors
|
| 15 |
+
from ._loop import loop_first_last, loop_last
|
| 16 |
+
from ._pick import pick_bool
|
| 17 |
+
from ._ratio import ratio_distribute, ratio_reduce
|
| 18 |
+
from .align import VerticalAlignMethod
|
| 19 |
+
from .jupyter import JupyterMixin
|
| 20 |
+
from .measure import Measurement
|
| 21 |
+
from .padding import Padding, PaddingDimensions
|
| 22 |
+
from .protocol import is_renderable
|
| 23 |
+
from .segment import Segment
|
| 24 |
+
from .style import Style, StyleType
|
| 25 |
+
from .text import Text, TextType
|
| 26 |
+
|
| 27 |
+
if TYPE_CHECKING:
|
| 28 |
+
from .console import (
|
| 29 |
+
Console,
|
| 30 |
+
ConsoleOptions,
|
| 31 |
+
JustifyMethod,
|
| 32 |
+
OverflowMethod,
|
| 33 |
+
RenderableType,
|
| 34 |
+
RenderResult,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@dataclass
|
| 39 |
+
class Column:
|
| 40 |
+
"""Defines a column within a ~Table.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
title (Union[str, Text], optional): The title of the table rendered at the top. Defaults to None.
|
| 44 |
+
caption (Union[str, Text], optional): The table caption rendered below. Defaults to None.
|
| 45 |
+
width (int, optional): The width in characters of the table, or ``None`` to automatically fit. Defaults to None.
|
| 46 |
+
min_width (Optional[int], optional): The minimum width of the table, or ``None`` for no minimum. Defaults to None.
|
| 47 |
+
box (box.Box, optional): One of the constants in box.py used to draw the edges (see :ref:`appendix_box`), or ``None`` for no box lines. Defaults to box.HEAVY_HEAD.
|
| 48 |
+
safe_box (Optional[bool], optional): Disable box characters that don't display on windows legacy terminal with *raster* fonts. Defaults to True.
|
| 49 |
+
padding (PaddingDimensions, optional): Padding for cells (top, right, bottom, left). Defaults to (0, 1).
|
| 50 |
+
collapse_padding (bool, optional): Enable collapsing of padding around cells. Defaults to False.
|
| 51 |
+
pad_edge (bool, optional): Enable padding of edge cells. Defaults to True.
|
| 52 |
+
expand (bool, optional): Expand the table to fit the available space if ``True``, otherwise the table width will be auto-calculated. Defaults to False.
|
| 53 |
+
show_header (bool, optional): Show a header row. Defaults to True.
|
| 54 |
+
show_footer (bool, optional): Show a footer row. Defaults to False.
|
| 55 |
+
show_edge (bool, optional): Draw a box around the outside of the table. Defaults to True.
|
| 56 |
+
show_lines (bool, optional): Draw lines between every row. Defaults to False.
|
| 57 |
+
leading (int, optional): Number of blank lines between rows (precludes ``show_lines``). Defaults to 0.
|
| 58 |
+
style (Union[str, Style], optional): Default style for the table. Defaults to "none".
|
| 59 |
+
row_styles (List[Union, str], optional): Optional list of row styles, if more than one style is given then the styles will alternate. Defaults to None.
|
| 60 |
+
header_style (Union[str, Style], optional): Style of the header. Defaults to "table.header".
|
| 61 |
+
footer_style (Union[str, Style], optional): Style of the footer. Defaults to "table.footer".
|
| 62 |
+
border_style (Union[str, Style], optional): Style of the border. Defaults to None.
|
| 63 |
+
title_style (Union[str, Style], optional): Style of the title. Defaults to None.
|
| 64 |
+
caption_style (Union[str, Style], optional): Style of the caption. Defaults to None.
|
| 65 |
+
title_justify (str, optional): Justify method for title. Defaults to "center".
|
| 66 |
+
caption_justify (str, optional): Justify method for caption. Defaults to "center".
|
| 67 |
+
highlight (bool, optional): Highlight cell contents (if str). Defaults to False.
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
header: "RenderableType" = ""
|
| 71 |
+
"""RenderableType: Renderable for the header (typically a string)"""
|
| 72 |
+
|
| 73 |
+
footer: "RenderableType" = ""
|
| 74 |
+
"""RenderableType: Renderable for the footer (typically a string)"""
|
| 75 |
+
|
| 76 |
+
header_style: StyleType = ""
|
| 77 |
+
"""StyleType: The style of the header."""
|
| 78 |
+
|
| 79 |
+
footer_style: StyleType = ""
|
| 80 |
+
"""StyleType: The style of the footer."""
|
| 81 |
+
|
| 82 |
+
style: StyleType = ""
|
| 83 |
+
"""StyleType: The style of the column."""
|
| 84 |
+
|
| 85 |
+
justify: "JustifyMethod" = "left"
|
| 86 |
+
"""str: How to justify text within the column ("left", "center", "right", or "full")"""
|
| 87 |
+
|
| 88 |
+
vertical: "VerticalAlignMethod" = "top"
|
| 89 |
+
"""str: How to vertically align content ("top", "middle", or "bottom")"""
|
| 90 |
+
|
| 91 |
+
overflow: "OverflowMethod" = "ellipsis"
|
| 92 |
+
"""str: Overflow method."""
|
| 93 |
+
|
| 94 |
+
width: Optional[int] = None
|
| 95 |
+
"""Optional[int]: Width of the column, or ``None`` (default) to auto calculate width."""
|
| 96 |
+
|
| 97 |
+
min_width: Optional[int] = None
|
| 98 |
+
"""Optional[int]: Minimum width of column, or ``None`` for no minimum. Defaults to None."""
|
| 99 |
+
|
| 100 |
+
max_width: Optional[int] = None
|
| 101 |
+
"""Optional[int]: Maximum width of column, or ``None`` for no maximum. Defaults to None."""
|
| 102 |
+
|
| 103 |
+
ratio: Optional[int] = None
|
| 104 |
+
"""Optional[int]: Ratio to use when calculating column width, or ``None`` (default) to adapt to column contents."""
|
| 105 |
+
|
| 106 |
+
no_wrap: bool = False
|
| 107 |
+
"""bool: Prevent wrapping of text within the column. Defaults to ``False``."""
|
| 108 |
+
|
| 109 |
+
highlight: bool = False
|
| 110 |
+
"""bool: Apply highlighter to column. Defaults to ``False``."""
|
| 111 |
+
|
| 112 |
+
_index: int = 0
|
| 113 |
+
"""Index of column."""
|
| 114 |
+
|
| 115 |
+
_cells: List["RenderableType"] = field(default_factory=list)
|
| 116 |
+
|
| 117 |
+
def copy(self) -> "Column":
|
| 118 |
+
"""Return a copy of this Column."""
|
| 119 |
+
return replace(self, _cells=[])
|
| 120 |
+
|
| 121 |
+
@property
|
| 122 |
+
def cells(self) -> Iterable["RenderableType"]:
|
| 123 |
+
"""Get all cells in the column, not including header."""
|
| 124 |
+
yield from self._cells
|
| 125 |
+
|
| 126 |
+
@property
|
| 127 |
+
def flexible(self) -> bool:
|
| 128 |
+
"""Check if this column is flexible."""
|
| 129 |
+
return self.ratio is not None
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
@dataclass
|
| 133 |
+
class Row:
|
| 134 |
+
"""Information regarding a row."""
|
| 135 |
+
|
| 136 |
+
style: Optional[StyleType] = None
|
| 137 |
+
"""Style to apply to row."""
|
| 138 |
+
|
| 139 |
+
end_section: bool = False
|
| 140 |
+
"""Indicated end of section, which will force a line beneath the row."""
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
class _Cell(NamedTuple):
|
| 144 |
+
"""A single cell in a table."""
|
| 145 |
+
|
| 146 |
+
style: StyleType
|
| 147 |
+
"""Style to apply to cell."""
|
| 148 |
+
renderable: "RenderableType"
|
| 149 |
+
"""Cell renderable."""
|
| 150 |
+
vertical: VerticalAlignMethod
|
| 151 |
+
"""Cell vertical alignment."""
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
class Table(JupyterMixin):
|
| 155 |
+
"""A console renderable to draw a table.
|
| 156 |
+
|
| 157 |
+
Args:
|
| 158 |
+
*headers (Union[Column, str]): Column headers, either as a string, or :class:`~rich.table.Column` instance.
|
| 159 |
+
title (Union[str, Text], optional): The title of the table rendered at the top. Defaults to None.
|
| 160 |
+
caption (Union[str, Text], optional): The table caption rendered below. Defaults to None.
|
| 161 |
+
width (int, optional): The width in characters of the table, or ``None`` to automatically fit. Defaults to None.
|
| 162 |
+
min_width (Optional[int], optional): The minimum width of the table, or ``None`` for no minimum. Defaults to None.
|
| 163 |
+
box (box.Box, optional): One of the constants in box.py used to draw the edges (see :ref:`appendix_box`), or ``None`` for no box lines. Defaults to box.HEAVY_HEAD.
|
| 164 |
+
safe_box (Optional[bool], optional): Disable box characters that don't display on windows legacy terminal with *raster* fonts. Defaults to True.
|
| 165 |
+
padding (PaddingDimensions, optional): Padding for cells (top, right, bottom, left). Defaults to (0, 1).
|
| 166 |
+
collapse_padding (bool, optional): Enable collapsing of padding around cells. Defaults to False.
|
| 167 |
+
pad_edge (bool, optional): Enable padding of edge cells. Defaults to True.
|
| 168 |
+
expand (bool, optional): Expand the table to fit the available space if ``True``, otherwise the table width will be auto-calculated. Defaults to False.
|
| 169 |
+
show_header (bool, optional): Show a header row. Defaults to True.
|
| 170 |
+
show_footer (bool, optional): Show a footer row. Defaults to False.
|
| 171 |
+
show_edge (bool, optional): Draw a box around the outside of the table. Defaults to True.
|
| 172 |
+
show_lines (bool, optional): Draw lines between every row. Defaults to False.
|
| 173 |
+
leading (int, optional): Number of blank lines between rows (precludes ``show_lines``). Defaults to 0.
|
| 174 |
+
style (Union[str, Style], optional): Default style for the table. Defaults to "none".
|
| 175 |
+
row_styles (List[Union, str], optional): Optional list of row styles, if more than one style is given then the styles will alternate. Defaults to None.
|
| 176 |
+
header_style (Union[str, Style], optional): Style of the header. Defaults to "table.header".
|
| 177 |
+
footer_style (Union[str, Style], optional): Style of the footer. Defaults to "table.footer".
|
| 178 |
+
border_style (Union[str, Style], optional): Style of the border. Defaults to None.
|
| 179 |
+
title_style (Union[str, Style], optional): Style of the title. Defaults to None.
|
| 180 |
+
caption_style (Union[str, Style], optional): Style of the caption. Defaults to None.
|
| 181 |
+
title_justify (str, optional): Justify method for title. Defaults to "center".
|
| 182 |
+
caption_justify (str, optional): Justify method for caption. Defaults to "center".
|
| 183 |
+
highlight (bool, optional): Highlight cell contents (if str). Defaults to False.
|
| 184 |
+
"""
|
| 185 |
+
|
| 186 |
+
columns: List[Column]
|
| 187 |
+
rows: List[Row]
|
| 188 |
+
|
| 189 |
+
def __init__(
|
| 190 |
+
self,
|
| 191 |
+
*headers: Union[Column, str],
|
| 192 |
+
title: Optional[TextType] = None,
|
| 193 |
+
caption: Optional[TextType] = None,
|
| 194 |
+
width: Optional[int] = None,
|
| 195 |
+
min_width: Optional[int] = None,
|
| 196 |
+
box: Optional[box.Box] = box.HEAVY_HEAD,
|
| 197 |
+
safe_box: Optional[bool] = None,
|
| 198 |
+
padding: PaddingDimensions = (0, 1),
|
| 199 |
+
collapse_padding: bool = False,
|
| 200 |
+
pad_edge: bool = True,
|
| 201 |
+
expand: bool = False,
|
| 202 |
+
show_header: bool = True,
|
| 203 |
+
show_footer: bool = False,
|
| 204 |
+
show_edge: bool = True,
|
| 205 |
+
show_lines: bool = False,
|
| 206 |
+
leading: int = 0,
|
| 207 |
+
style: StyleType = "none",
|
| 208 |
+
row_styles: Optional[Iterable[StyleType]] = None,
|
| 209 |
+
header_style: Optional[StyleType] = "table.header",
|
| 210 |
+
footer_style: Optional[StyleType] = "table.footer",
|
| 211 |
+
border_style: Optional[StyleType] = None,
|
| 212 |
+
title_style: Optional[StyleType] = None,
|
| 213 |
+
caption_style: Optional[StyleType] = None,
|
| 214 |
+
title_justify: "JustifyMethod" = "center",
|
| 215 |
+
caption_justify: "JustifyMethod" = "center",
|
| 216 |
+
highlight: bool = False,
|
| 217 |
+
) -> None:
|
| 218 |
+
self.columns: List[Column] = []
|
| 219 |
+
self.rows: List[Row] = []
|
| 220 |
+
self.title = title
|
| 221 |
+
self.caption = caption
|
| 222 |
+
self.width = width
|
| 223 |
+
self.min_width = min_width
|
| 224 |
+
self.box = box
|
| 225 |
+
self.safe_box = safe_box
|
| 226 |
+
self._padding = Padding.unpack(padding)
|
| 227 |
+
self.pad_edge = pad_edge
|
| 228 |
+
self._expand = expand
|
| 229 |
+
self.show_header = show_header
|
| 230 |
+
self.show_footer = show_footer
|
| 231 |
+
self.show_edge = show_edge
|
| 232 |
+
self.show_lines = show_lines
|
| 233 |
+
self.leading = leading
|
| 234 |
+
self.collapse_padding = collapse_padding
|
| 235 |
+
self.style = style
|
| 236 |
+
self.header_style = header_style or ""
|
| 237 |
+
self.footer_style = footer_style or ""
|
| 238 |
+
self.border_style = border_style
|
| 239 |
+
self.title_style = title_style
|
| 240 |
+
self.caption_style = caption_style
|
| 241 |
+
self.title_justify: "JustifyMethod" = title_justify
|
| 242 |
+
self.caption_justify: "JustifyMethod" = caption_justify
|
| 243 |
+
self.highlight = highlight
|
| 244 |
+
self.row_styles: Sequence[StyleType] = list(row_styles or [])
|
| 245 |
+
append_column = self.columns.append
|
| 246 |
+
for header in headers:
|
| 247 |
+
if isinstance(header, str):
|
| 248 |
+
self.add_column(header=header)
|
| 249 |
+
else:
|
| 250 |
+
header._index = len(self.columns)
|
| 251 |
+
append_column(header)
|
| 252 |
+
|
| 253 |
+
@classmethod
|
| 254 |
+
def grid(
|
| 255 |
+
cls,
|
| 256 |
+
*headers: Union[Column, str],
|
| 257 |
+
padding: PaddingDimensions = 0,
|
| 258 |
+
collapse_padding: bool = True,
|
| 259 |
+
pad_edge: bool = False,
|
| 260 |
+
expand: bool = False,
|
| 261 |
+
) -> "Table":
|
| 262 |
+
"""Get a table with no lines, headers, or footer.
|
| 263 |
+
|
| 264 |
+
Args:
|
| 265 |
+
*headers (Union[Column, str]): Column headers, either as a string, or :class:`~rich.table.Column` instance.
|
| 266 |
+
padding (PaddingDimensions, optional): Get padding around cells. Defaults to 0.
|
| 267 |
+
collapse_padding (bool, optional): Enable collapsing of padding around cells. Defaults to True.
|
| 268 |
+
pad_edge (bool, optional): Enable padding around edges of table. Defaults to False.
|
| 269 |
+
expand (bool, optional): Expand the table to fit the available space if ``True``, otherwise the table width will be auto-calculated. Defaults to False.
|
| 270 |
+
|
| 271 |
+
Returns:
|
| 272 |
+
Table: A table instance.
|
| 273 |
+
"""
|
| 274 |
+
return cls(
|
| 275 |
+
*headers,
|
| 276 |
+
box=None,
|
| 277 |
+
padding=padding,
|
| 278 |
+
collapse_padding=collapse_padding,
|
| 279 |
+
show_header=False,
|
| 280 |
+
show_footer=False,
|
| 281 |
+
show_edge=False,
|
| 282 |
+
pad_edge=pad_edge,
|
| 283 |
+
expand=expand,
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
@property
|
| 287 |
+
def expand(self) -> bool:
|
| 288 |
+
"""Setting a non-None self.width implies expand."""
|
| 289 |
+
return self._expand or self.width is not None
|
| 290 |
+
|
| 291 |
+
@expand.setter
|
| 292 |
+
def expand(self, expand: bool) -> None:
|
| 293 |
+
"""Set expand."""
|
| 294 |
+
self._expand = expand
|
| 295 |
+
|
| 296 |
+
@property
|
| 297 |
+
def _extra_width(self) -> int:
|
| 298 |
+
"""Get extra width to add to cell content."""
|
| 299 |
+
width = 0
|
| 300 |
+
if self.box and self.show_edge:
|
| 301 |
+
width += 2
|
| 302 |
+
if self.box:
|
| 303 |
+
width += len(self.columns) - 1
|
| 304 |
+
return width
|
| 305 |
+
|
| 306 |
+
@property
|
| 307 |
+
def row_count(self) -> int:
|
| 308 |
+
"""Get the current number of rows."""
|
| 309 |
+
return len(self.rows)
|
| 310 |
+
|
| 311 |
+
def get_row_style(self, console: "Console", index: int) -> StyleType:
|
| 312 |
+
"""Get the current row style."""
|
| 313 |
+
style = Style.null()
|
| 314 |
+
if self.row_styles:
|
| 315 |
+
style += console.get_style(self.row_styles[index % len(self.row_styles)])
|
| 316 |
+
row_style = self.rows[index].style
|
| 317 |
+
if row_style is not None:
|
| 318 |
+
style += console.get_style(row_style)
|
| 319 |
+
return style
|
| 320 |
+
|
| 321 |
+
def __rich_measure__(
|
| 322 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 323 |
+
) -> Measurement:
|
| 324 |
+
max_width = options.max_width
|
| 325 |
+
if self.width is not None:
|
| 326 |
+
max_width = self.width
|
| 327 |
+
if max_width < 0:
|
| 328 |
+
return Measurement(0, 0)
|
| 329 |
+
|
| 330 |
+
extra_width = self._extra_width
|
| 331 |
+
max_width = sum(
|
| 332 |
+
self._calculate_column_widths(
|
| 333 |
+
console, options.update_width(max_width - extra_width)
|
| 334 |
+
)
|
| 335 |
+
)
|
| 336 |
+
_measure_column = self._measure_column
|
| 337 |
+
|
| 338 |
+
measurements = [
|
| 339 |
+
_measure_column(console, options.update_width(max_width), column)
|
| 340 |
+
for column in self.columns
|
| 341 |
+
]
|
| 342 |
+
minimum_width = (
|
| 343 |
+
sum(measurement.minimum for measurement in measurements) + extra_width
|
| 344 |
+
)
|
| 345 |
+
maximum_width = (
|
| 346 |
+
sum(measurement.maximum for measurement in measurements) + extra_width
|
| 347 |
+
if (self.width is None)
|
| 348 |
+
else self.width
|
| 349 |
+
)
|
| 350 |
+
measurement = Measurement(minimum_width, maximum_width)
|
| 351 |
+
measurement = measurement.clamp(self.min_width)
|
| 352 |
+
return measurement
|
| 353 |
+
|
| 354 |
+
@property
|
| 355 |
+
def padding(self) -> Tuple[int, int, int, int]:
|
| 356 |
+
"""Get cell padding."""
|
| 357 |
+
return self._padding
|
| 358 |
+
|
| 359 |
+
@padding.setter
|
| 360 |
+
def padding(self, padding: PaddingDimensions) -> "Table":
|
| 361 |
+
"""Set cell padding."""
|
| 362 |
+
self._padding = Padding.unpack(padding)
|
| 363 |
+
return self
|
| 364 |
+
|
| 365 |
+
def add_column(
|
| 366 |
+
self,
|
| 367 |
+
header: "RenderableType" = "",
|
| 368 |
+
footer: "RenderableType" = "",
|
| 369 |
+
*,
|
| 370 |
+
header_style: Optional[StyleType] = None,
|
| 371 |
+
highlight: Optional[bool] = None,
|
| 372 |
+
footer_style: Optional[StyleType] = None,
|
| 373 |
+
style: Optional[StyleType] = None,
|
| 374 |
+
justify: "JustifyMethod" = "left",
|
| 375 |
+
vertical: "VerticalAlignMethod" = "top",
|
| 376 |
+
overflow: "OverflowMethod" = "ellipsis",
|
| 377 |
+
width: Optional[int] = None,
|
| 378 |
+
min_width: Optional[int] = None,
|
| 379 |
+
max_width: Optional[int] = None,
|
| 380 |
+
ratio: Optional[int] = None,
|
| 381 |
+
no_wrap: bool = False,
|
| 382 |
+
) -> None:
|
| 383 |
+
"""Add a column to the table.
|
| 384 |
+
|
| 385 |
+
Args:
|
| 386 |
+
header (RenderableType, optional): Text or renderable for the header.
|
| 387 |
+
Defaults to "".
|
| 388 |
+
footer (RenderableType, optional): Text or renderable for the footer.
|
| 389 |
+
Defaults to "".
|
| 390 |
+
header_style (Union[str, Style], optional): Style for the header, or None for default. Defaults to None.
|
| 391 |
+
highlight (bool, optional): Whether to highlight the text. The default of None uses the value of the table (self) object.
|
| 392 |
+
footer_style (Union[str, Style], optional): Style for the footer, or None for default. Defaults to None.
|
| 393 |
+
style (Union[str, Style], optional): Style for the column cells, or None for default. Defaults to None.
|
| 394 |
+
justify (JustifyMethod, optional): Alignment for cells. Defaults to "left".
|
| 395 |
+
vertical (VerticalAlignMethod, optional): Vertical alignment, one of "top", "middle", or "bottom". Defaults to "top".
|
| 396 |
+
overflow (OverflowMethod): Overflow method: "crop", "fold", "ellipsis". Defaults to "ellipsis".
|
| 397 |
+
width (int, optional): Desired width of column in characters, or None to fit to contents. Defaults to None.
|
| 398 |
+
min_width (Optional[int], optional): Minimum width of column, or ``None`` for no minimum. Defaults to None.
|
| 399 |
+
max_width (Optional[int], optional): Maximum width of column, or ``None`` for no maximum. Defaults to None.
|
| 400 |
+
ratio (int, optional): Flexible ratio for the column (requires ``Table.expand`` or ``Table.width``). Defaults to None.
|
| 401 |
+
no_wrap (bool, optional): Set to ``True`` to disable wrapping of this column.
|
| 402 |
+
"""
|
| 403 |
+
|
| 404 |
+
column = Column(
|
| 405 |
+
_index=len(self.columns),
|
| 406 |
+
header=header,
|
| 407 |
+
footer=footer,
|
| 408 |
+
header_style=header_style or "",
|
| 409 |
+
highlight=highlight if highlight is not None else self.highlight,
|
| 410 |
+
footer_style=footer_style or "",
|
| 411 |
+
style=style or "",
|
| 412 |
+
justify=justify,
|
| 413 |
+
vertical=vertical,
|
| 414 |
+
overflow=overflow,
|
| 415 |
+
width=width,
|
| 416 |
+
min_width=min_width,
|
| 417 |
+
max_width=max_width,
|
| 418 |
+
ratio=ratio,
|
| 419 |
+
no_wrap=no_wrap,
|
| 420 |
+
)
|
| 421 |
+
self.columns.append(column)
|
| 422 |
+
|
| 423 |
+
def add_row(
|
| 424 |
+
self,
|
| 425 |
+
*renderables: Optional["RenderableType"],
|
| 426 |
+
style: Optional[StyleType] = None,
|
| 427 |
+
end_section: bool = False,
|
| 428 |
+
) -> None:
|
| 429 |
+
"""Add a row of renderables.
|
| 430 |
+
|
| 431 |
+
Args:
|
| 432 |
+
*renderables (None or renderable): Each cell in a row must be a renderable object (including str),
|
| 433 |
+
or ``None`` for a blank cell.
|
| 434 |
+
style (StyleType, optional): An optional style to apply to the entire row. Defaults to None.
|
| 435 |
+
end_section (bool, optional): End a section and draw a line. Defaults to False.
|
| 436 |
+
|
| 437 |
+
Raises:
|
| 438 |
+
errors.NotRenderableError: If you add something that can't be rendered.
|
| 439 |
+
"""
|
| 440 |
+
|
| 441 |
+
def add_cell(column: Column, renderable: "RenderableType") -> None:
|
| 442 |
+
column._cells.append(renderable)
|
| 443 |
+
|
| 444 |
+
cell_renderables: List[Optional["RenderableType"]] = list(renderables)
|
| 445 |
+
|
| 446 |
+
columns = self.columns
|
| 447 |
+
if len(cell_renderables) < len(columns):
|
| 448 |
+
cell_renderables = [
|
| 449 |
+
*cell_renderables,
|
| 450 |
+
*[None] * (len(columns) - len(cell_renderables)),
|
| 451 |
+
]
|
| 452 |
+
for index, renderable in enumerate(cell_renderables):
|
| 453 |
+
if index == len(columns):
|
| 454 |
+
column = Column(_index=index, highlight=self.highlight)
|
| 455 |
+
for _ in self.rows:
|
| 456 |
+
add_cell(column, Text(""))
|
| 457 |
+
self.columns.append(column)
|
| 458 |
+
else:
|
| 459 |
+
column = columns[index]
|
| 460 |
+
if renderable is None:
|
| 461 |
+
add_cell(column, "")
|
| 462 |
+
elif is_renderable(renderable):
|
| 463 |
+
add_cell(column, renderable)
|
| 464 |
+
else:
|
| 465 |
+
raise errors.NotRenderableError(
|
| 466 |
+
f"unable to render {type(renderable).__name__}; a string or other renderable object is required"
|
| 467 |
+
)
|
| 468 |
+
self.rows.append(Row(style=style, end_section=end_section))
|
| 469 |
+
|
| 470 |
+
def add_section(self) -> None:
|
| 471 |
+
"""Add a new section (draw a line after current row)."""
|
| 472 |
+
|
| 473 |
+
if self.rows:
|
| 474 |
+
self.rows[-1].end_section = True
|
| 475 |
+
|
| 476 |
+
def __rich_console__(
|
| 477 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 478 |
+
) -> "RenderResult":
|
| 479 |
+
if not self.columns:
|
| 480 |
+
yield Segment("\n")
|
| 481 |
+
return
|
| 482 |
+
|
| 483 |
+
max_width = options.max_width
|
| 484 |
+
if self.width is not None:
|
| 485 |
+
max_width = self.width
|
| 486 |
+
|
| 487 |
+
extra_width = self._extra_width
|
| 488 |
+
widths = self._calculate_column_widths(
|
| 489 |
+
console, options.update_width(max_width - extra_width)
|
| 490 |
+
)
|
| 491 |
+
table_width = sum(widths) + extra_width
|
| 492 |
+
|
| 493 |
+
render_options = options.update(
|
| 494 |
+
width=table_width, highlight=self.highlight, height=None
|
| 495 |
+
)
|
| 496 |
+
|
| 497 |
+
def render_annotation(
|
| 498 |
+
text: TextType, style: StyleType, justify: "JustifyMethod" = "center"
|
| 499 |
+
) -> "RenderResult":
|
| 500 |
+
render_text = (
|
| 501 |
+
console.render_str(text, style=style, highlight=False)
|
| 502 |
+
if isinstance(text, str)
|
| 503 |
+
else text
|
| 504 |
+
)
|
| 505 |
+
return console.render(
|
| 506 |
+
render_text, options=render_options.update(justify=justify)
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
+
if self.title:
|
| 510 |
+
yield from render_annotation(
|
| 511 |
+
self.title,
|
| 512 |
+
style=Style.pick_first(self.title_style, "table.title"),
|
| 513 |
+
justify=self.title_justify,
|
| 514 |
+
)
|
| 515 |
+
yield from self._render(console, render_options, widths)
|
| 516 |
+
if self.caption:
|
| 517 |
+
yield from render_annotation(
|
| 518 |
+
self.caption,
|
| 519 |
+
style=Style.pick_first(self.caption_style, "table.caption"),
|
| 520 |
+
justify=self.caption_justify,
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
def _calculate_column_widths(
|
| 524 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 525 |
+
) -> List[int]:
|
| 526 |
+
"""Calculate the widths of each column, including padding, not including borders."""
|
| 527 |
+
max_width = options.max_width
|
| 528 |
+
columns = self.columns
|
| 529 |
+
width_ranges = [
|
| 530 |
+
self._measure_column(console, options, column) for column in columns
|
| 531 |
+
]
|
| 532 |
+
widths = [_range.maximum or 1 for _range in width_ranges]
|
| 533 |
+
get_padding_width = self._get_padding_width
|
| 534 |
+
extra_width = self._extra_width
|
| 535 |
+
if self.expand:
|
| 536 |
+
ratios = [col.ratio or 0 for col in columns if col.flexible]
|
| 537 |
+
if any(ratios):
|
| 538 |
+
fixed_widths = [
|
| 539 |
+
0 if column.flexible else _range.maximum
|
| 540 |
+
for _range, column in zip(width_ranges, columns)
|
| 541 |
+
]
|
| 542 |
+
flex_minimum = [
|
| 543 |
+
(column.width or 1) + get_padding_width(column._index)
|
| 544 |
+
for column in columns
|
| 545 |
+
if column.flexible
|
| 546 |
+
]
|
| 547 |
+
flexible_width = max_width - sum(fixed_widths)
|
| 548 |
+
flex_widths = ratio_distribute(flexible_width, ratios, flex_minimum)
|
| 549 |
+
iter_flex_widths = iter(flex_widths)
|
| 550 |
+
for index, column in enumerate(columns):
|
| 551 |
+
if column.flexible:
|
| 552 |
+
widths[index] = fixed_widths[index] + next(iter_flex_widths)
|
| 553 |
+
table_width = sum(widths)
|
| 554 |
+
|
| 555 |
+
if table_width > max_width:
|
| 556 |
+
widths = self._collapse_widths(
|
| 557 |
+
widths,
|
| 558 |
+
[(column.width is None and not column.no_wrap) for column in columns],
|
| 559 |
+
max_width,
|
| 560 |
+
)
|
| 561 |
+
table_width = sum(widths)
|
| 562 |
+
# last resort, reduce columns evenly
|
| 563 |
+
if table_width > max_width:
|
| 564 |
+
excess_width = table_width - max_width
|
| 565 |
+
widths = ratio_reduce(excess_width, [1] * len(widths), widths, widths)
|
| 566 |
+
table_width = sum(widths)
|
| 567 |
+
|
| 568 |
+
width_ranges = [
|
| 569 |
+
self._measure_column(console, options.update_width(width), column)
|
| 570 |
+
for width, column in zip(widths, columns)
|
| 571 |
+
]
|
| 572 |
+
widths = [_range.maximum or 0 for _range in width_ranges]
|
| 573 |
+
|
| 574 |
+
if (table_width < max_width and self.expand) or (
|
| 575 |
+
self.min_width is not None and table_width < (self.min_width - extra_width)
|
| 576 |
+
):
|
| 577 |
+
_max_width = (
|
| 578 |
+
max_width
|
| 579 |
+
if self.min_width is None
|
| 580 |
+
else min(self.min_width - extra_width, max_width)
|
| 581 |
+
)
|
| 582 |
+
pad_widths = ratio_distribute(_max_width - table_width, widths)
|
| 583 |
+
widths = [_width + pad for _width, pad in zip(widths, pad_widths)]
|
| 584 |
+
|
| 585 |
+
return widths
|
| 586 |
+
|
| 587 |
+
@classmethod
|
| 588 |
+
def _collapse_widths(
|
| 589 |
+
cls, widths: List[int], wrapable: List[bool], max_width: int
|
| 590 |
+
) -> List[int]:
|
| 591 |
+
"""Reduce widths so that the total is under max_width.
|
| 592 |
+
|
| 593 |
+
Args:
|
| 594 |
+
widths (List[int]): List of widths.
|
| 595 |
+
wrapable (List[bool]): List of booleans that indicate if a column may shrink.
|
| 596 |
+
max_width (int): Maximum width to reduce to.
|
| 597 |
+
|
| 598 |
+
Returns:
|
| 599 |
+
List[int]: A new list of widths.
|
| 600 |
+
"""
|
| 601 |
+
total_width = sum(widths)
|
| 602 |
+
excess_width = total_width - max_width
|
| 603 |
+
if any(wrapable):
|
| 604 |
+
while total_width and excess_width > 0:
|
| 605 |
+
max_column = max(
|
| 606 |
+
width for width, allow_wrap in zip(widths, wrapable) if allow_wrap
|
| 607 |
+
)
|
| 608 |
+
second_max_column = max(
|
| 609 |
+
width if allow_wrap and width != max_column else 0
|
| 610 |
+
for width, allow_wrap in zip(widths, wrapable)
|
| 611 |
+
)
|
| 612 |
+
column_difference = max_column - second_max_column
|
| 613 |
+
ratios = [
|
| 614 |
+
(1 if (width == max_column and allow_wrap) else 0)
|
| 615 |
+
for width, allow_wrap in zip(widths, wrapable)
|
| 616 |
+
]
|
| 617 |
+
if not any(ratios) or not column_difference:
|
| 618 |
+
break
|
| 619 |
+
max_reduce = [min(excess_width, column_difference)] * len(widths)
|
| 620 |
+
widths = ratio_reduce(excess_width, ratios, max_reduce, widths)
|
| 621 |
+
|
| 622 |
+
total_width = sum(widths)
|
| 623 |
+
excess_width = total_width - max_width
|
| 624 |
+
return widths
|
| 625 |
+
|
| 626 |
+
def _get_cells(
|
| 627 |
+
self, console: "Console", column_index: int, column: Column
|
| 628 |
+
) -> Iterable[_Cell]:
|
| 629 |
+
"""Get all the cells with padding and optional header."""
|
| 630 |
+
|
| 631 |
+
collapse_padding = self.collapse_padding
|
| 632 |
+
pad_edge = self.pad_edge
|
| 633 |
+
padding = self.padding
|
| 634 |
+
any_padding = any(padding)
|
| 635 |
+
|
| 636 |
+
first_column = column_index == 0
|
| 637 |
+
last_column = column_index == len(self.columns) - 1
|
| 638 |
+
|
| 639 |
+
_padding_cache: Dict[Tuple[bool, bool], Tuple[int, int, int, int]] = {}
|
| 640 |
+
|
| 641 |
+
def get_padding(first_row: bool, last_row: bool) -> Tuple[int, int, int, int]:
|
| 642 |
+
cached = _padding_cache.get((first_row, last_row))
|
| 643 |
+
if cached:
|
| 644 |
+
return cached
|
| 645 |
+
top, right, bottom, left = padding
|
| 646 |
+
|
| 647 |
+
if collapse_padding:
|
| 648 |
+
if not first_column:
|
| 649 |
+
left = max(0, left - right)
|
| 650 |
+
if not last_row:
|
| 651 |
+
bottom = max(0, top - bottom)
|
| 652 |
+
|
| 653 |
+
if not pad_edge:
|
| 654 |
+
if first_column:
|
| 655 |
+
left = 0
|
| 656 |
+
if last_column:
|
| 657 |
+
right = 0
|
| 658 |
+
if first_row:
|
| 659 |
+
top = 0
|
| 660 |
+
if last_row:
|
| 661 |
+
bottom = 0
|
| 662 |
+
_padding = (top, right, bottom, left)
|
| 663 |
+
_padding_cache[(first_row, last_row)] = _padding
|
| 664 |
+
return _padding
|
| 665 |
+
|
| 666 |
+
raw_cells: List[Tuple[StyleType, "RenderableType"]] = []
|
| 667 |
+
_append = raw_cells.append
|
| 668 |
+
get_style = console.get_style
|
| 669 |
+
if self.show_header:
|
| 670 |
+
header_style = get_style(self.header_style or "") + get_style(
|
| 671 |
+
column.header_style
|
| 672 |
+
)
|
| 673 |
+
_append((header_style, column.header))
|
| 674 |
+
cell_style = get_style(column.style or "")
|
| 675 |
+
for cell in column.cells:
|
| 676 |
+
_append((cell_style, cell))
|
| 677 |
+
if self.show_footer:
|
| 678 |
+
footer_style = get_style(self.footer_style or "") + get_style(
|
| 679 |
+
column.footer_style
|
| 680 |
+
)
|
| 681 |
+
_append((footer_style, column.footer))
|
| 682 |
+
|
| 683 |
+
if any_padding:
|
| 684 |
+
_Padding = Padding
|
| 685 |
+
for first, last, (style, renderable) in loop_first_last(raw_cells):
|
| 686 |
+
yield _Cell(
|
| 687 |
+
style,
|
| 688 |
+
_Padding(renderable, get_padding(first, last)),
|
| 689 |
+
getattr(renderable, "vertical", None) or column.vertical,
|
| 690 |
+
)
|
| 691 |
+
else:
|
| 692 |
+
for style, renderable in raw_cells:
|
| 693 |
+
yield _Cell(
|
| 694 |
+
style,
|
| 695 |
+
renderable,
|
| 696 |
+
getattr(renderable, "vertical", None) or column.vertical,
|
| 697 |
+
)
|
| 698 |
+
|
| 699 |
+
def _get_padding_width(self, column_index: int) -> int:
|
| 700 |
+
"""Get extra width from padding."""
|
| 701 |
+
_, pad_right, _, pad_left = self.padding
|
| 702 |
+
if self.collapse_padding:
|
| 703 |
+
if column_index > 0:
|
| 704 |
+
pad_left = max(0, pad_left - pad_right)
|
| 705 |
+
return pad_left + pad_right
|
| 706 |
+
|
| 707 |
+
def _measure_column(
|
| 708 |
+
self,
|
| 709 |
+
console: "Console",
|
| 710 |
+
options: "ConsoleOptions",
|
| 711 |
+
column: Column,
|
| 712 |
+
) -> Measurement:
|
| 713 |
+
"""Get the minimum and maximum width of the column."""
|
| 714 |
+
|
| 715 |
+
max_width = options.max_width
|
| 716 |
+
if max_width < 1:
|
| 717 |
+
return Measurement(0, 0)
|
| 718 |
+
|
| 719 |
+
padding_width = self._get_padding_width(column._index)
|
| 720 |
+
|
| 721 |
+
if column.width is not None:
|
| 722 |
+
# Fixed width column
|
| 723 |
+
return Measurement(
|
| 724 |
+
column.width + padding_width, column.width + padding_width
|
| 725 |
+
).with_maximum(max_width)
|
| 726 |
+
# Flexible column, we need to measure contents
|
| 727 |
+
min_widths: List[int] = []
|
| 728 |
+
max_widths: List[int] = []
|
| 729 |
+
append_min = min_widths.append
|
| 730 |
+
append_max = max_widths.append
|
| 731 |
+
get_render_width = Measurement.get
|
| 732 |
+
for cell in self._get_cells(console, column._index, column):
|
| 733 |
+
_min, _max = get_render_width(console, options, cell.renderable)
|
| 734 |
+
append_min(_min)
|
| 735 |
+
append_max(_max)
|
| 736 |
+
|
| 737 |
+
measurement = Measurement(
|
| 738 |
+
max(min_widths) if min_widths else 1,
|
| 739 |
+
max(max_widths) if max_widths else max_width,
|
| 740 |
+
).with_maximum(max_width)
|
| 741 |
+
measurement = measurement.clamp(
|
| 742 |
+
None if column.min_width is None else column.min_width + padding_width,
|
| 743 |
+
None if column.max_width is None else column.max_width + padding_width,
|
| 744 |
+
)
|
| 745 |
+
return measurement
|
| 746 |
+
|
| 747 |
+
def _render(
|
| 748 |
+
self, console: "Console", options: "ConsoleOptions", widths: List[int]
|
| 749 |
+
) -> "RenderResult":
|
| 750 |
+
table_style = console.get_style(self.style or "")
|
| 751 |
+
|
| 752 |
+
border_style = table_style + console.get_style(self.border_style or "")
|
| 753 |
+
_column_cells = (
|
| 754 |
+
self._get_cells(console, column_index, column)
|
| 755 |
+
for column_index, column in enumerate(self.columns)
|
| 756 |
+
)
|
| 757 |
+
row_cells: List[Tuple[_Cell, ...]] = list(zip(*_column_cells))
|
| 758 |
+
_box = (
|
| 759 |
+
self.box.substitute(
|
| 760 |
+
options, safe=pick_bool(self.safe_box, console.safe_box)
|
| 761 |
+
)
|
| 762 |
+
if self.box
|
| 763 |
+
else None
|
| 764 |
+
)
|
| 765 |
+
_box = _box.get_plain_headed_box() if _box and not self.show_header else _box
|
| 766 |
+
|
| 767 |
+
new_line = Segment.line()
|
| 768 |
+
|
| 769 |
+
columns = self.columns
|
| 770 |
+
show_header = self.show_header
|
| 771 |
+
show_footer = self.show_footer
|
| 772 |
+
show_edge = self.show_edge
|
| 773 |
+
show_lines = self.show_lines
|
| 774 |
+
leading = self.leading
|
| 775 |
+
|
| 776 |
+
_Segment = Segment
|
| 777 |
+
if _box:
|
| 778 |
+
box_segments = [
|
| 779 |
+
(
|
| 780 |
+
_Segment(_box.head_left, border_style),
|
| 781 |
+
_Segment(_box.head_right, border_style),
|
| 782 |
+
_Segment(_box.head_vertical, border_style),
|
| 783 |
+
),
|
| 784 |
+
(
|
| 785 |
+
_Segment(_box.mid_left, border_style),
|
| 786 |
+
_Segment(_box.mid_right, border_style),
|
| 787 |
+
_Segment(_box.mid_vertical, border_style),
|
| 788 |
+
),
|
| 789 |
+
(
|
| 790 |
+
_Segment(_box.foot_left, border_style),
|
| 791 |
+
_Segment(_box.foot_right, border_style),
|
| 792 |
+
_Segment(_box.foot_vertical, border_style),
|
| 793 |
+
),
|
| 794 |
+
]
|
| 795 |
+
if show_edge:
|
| 796 |
+
yield _Segment(_box.get_top(widths), border_style)
|
| 797 |
+
yield new_line
|
| 798 |
+
else:
|
| 799 |
+
box_segments = []
|
| 800 |
+
|
| 801 |
+
get_row_style = self.get_row_style
|
| 802 |
+
get_style = console.get_style
|
| 803 |
+
|
| 804 |
+
for index, (first, last, row_cell) in enumerate(loop_first_last(row_cells)):
|
| 805 |
+
header_row = first and show_header
|
| 806 |
+
footer_row = last and show_footer
|
| 807 |
+
row = (
|
| 808 |
+
self.rows[index - show_header]
|
| 809 |
+
if (not header_row and not footer_row)
|
| 810 |
+
else None
|
| 811 |
+
)
|
| 812 |
+
max_height = 1
|
| 813 |
+
cells: List[List[List[Segment]]] = []
|
| 814 |
+
if header_row or footer_row:
|
| 815 |
+
row_style = Style.null()
|
| 816 |
+
else:
|
| 817 |
+
row_style = get_style(
|
| 818 |
+
get_row_style(console, index - 1 if show_header else index)
|
| 819 |
+
)
|
| 820 |
+
for width, cell, column in zip(widths, row_cell, columns):
|
| 821 |
+
render_options = options.update(
|
| 822 |
+
width=width,
|
| 823 |
+
justify=column.justify,
|
| 824 |
+
no_wrap=column.no_wrap,
|
| 825 |
+
overflow=column.overflow,
|
| 826 |
+
height=None,
|
| 827 |
+
highlight=column.highlight,
|
| 828 |
+
)
|
| 829 |
+
lines = console.render_lines(
|
| 830 |
+
cell.renderable,
|
| 831 |
+
render_options,
|
| 832 |
+
style=get_style(cell.style) + row_style,
|
| 833 |
+
)
|
| 834 |
+
max_height = max(max_height, len(lines))
|
| 835 |
+
cells.append(lines)
|
| 836 |
+
|
| 837 |
+
row_height = max(len(cell) for cell in cells)
|
| 838 |
+
|
| 839 |
+
def align_cell(
|
| 840 |
+
cell: List[List[Segment]],
|
| 841 |
+
vertical: "VerticalAlignMethod",
|
| 842 |
+
width: int,
|
| 843 |
+
style: Style,
|
| 844 |
+
) -> List[List[Segment]]:
|
| 845 |
+
if header_row:
|
| 846 |
+
vertical = "bottom"
|
| 847 |
+
elif footer_row:
|
| 848 |
+
vertical = "top"
|
| 849 |
+
|
| 850 |
+
if vertical == "top":
|
| 851 |
+
return _Segment.align_top(cell, width, row_height, style)
|
| 852 |
+
elif vertical == "middle":
|
| 853 |
+
return _Segment.align_middle(cell, width, row_height, style)
|
| 854 |
+
return _Segment.align_bottom(cell, width, row_height, style)
|
| 855 |
+
|
| 856 |
+
cells[:] = [
|
| 857 |
+
_Segment.set_shape(
|
| 858 |
+
align_cell(
|
| 859 |
+
cell,
|
| 860 |
+
_cell.vertical,
|
| 861 |
+
width,
|
| 862 |
+
get_style(_cell.style) + row_style,
|
| 863 |
+
),
|
| 864 |
+
width,
|
| 865 |
+
max_height,
|
| 866 |
+
)
|
| 867 |
+
for width, _cell, cell, column in zip(widths, row_cell, cells, columns)
|
| 868 |
+
]
|
| 869 |
+
|
| 870 |
+
if _box:
|
| 871 |
+
if last and show_footer:
|
| 872 |
+
yield _Segment(
|
| 873 |
+
_box.get_row(widths, "foot", edge=show_edge), border_style
|
| 874 |
+
)
|
| 875 |
+
yield new_line
|
| 876 |
+
left, right, _divider = box_segments[0 if first else (2 if last else 1)]
|
| 877 |
+
|
| 878 |
+
# If the column divider is whitespace also style it with the row background
|
| 879 |
+
divider = (
|
| 880 |
+
_divider
|
| 881 |
+
if _divider.text.strip()
|
| 882 |
+
else _Segment(
|
| 883 |
+
_divider.text, row_style.background_style + _divider.style
|
| 884 |
+
)
|
| 885 |
+
)
|
| 886 |
+
for line_no in range(max_height):
|
| 887 |
+
if show_edge:
|
| 888 |
+
yield left
|
| 889 |
+
for last_cell, rendered_cell in loop_last(cells):
|
| 890 |
+
yield from rendered_cell[line_no]
|
| 891 |
+
if not last_cell:
|
| 892 |
+
yield divider
|
| 893 |
+
if show_edge:
|
| 894 |
+
yield right
|
| 895 |
+
yield new_line
|
| 896 |
+
else:
|
| 897 |
+
for line_no in range(max_height):
|
| 898 |
+
for rendered_cell in cells:
|
| 899 |
+
yield from rendered_cell[line_no]
|
| 900 |
+
yield new_line
|
| 901 |
+
if _box and first and show_header:
|
| 902 |
+
yield _Segment(
|
| 903 |
+
_box.get_row(widths, "head", edge=show_edge), border_style
|
| 904 |
+
)
|
| 905 |
+
yield new_line
|
| 906 |
+
end_section = row and row.end_section
|
| 907 |
+
if _box and (show_lines or leading or end_section):
|
| 908 |
+
if (
|
| 909 |
+
not last
|
| 910 |
+
and not (show_footer and index >= len(row_cells) - 2)
|
| 911 |
+
and not (show_header and header_row)
|
| 912 |
+
):
|
| 913 |
+
if leading:
|
| 914 |
+
yield _Segment(
|
| 915 |
+
_box.get_row(widths, "mid", edge=show_edge) * leading,
|
| 916 |
+
border_style,
|
| 917 |
+
)
|
| 918 |
+
else:
|
| 919 |
+
yield _Segment(
|
| 920 |
+
_box.get_row(widths, "row", edge=show_edge), border_style
|
| 921 |
+
)
|
| 922 |
+
yield new_line
|
| 923 |
+
|
| 924 |
+
if _box and show_edge:
|
| 925 |
+
yield _Segment(_box.get_bottom(widths), border_style)
|
| 926 |
+
yield new_line
|
| 927 |
+
|
| 928 |
+
|
| 929 |
+
if __name__ == "__main__": # pragma: no cover
|
| 930 |
+
from pip._vendor.rich.console import Console
|
| 931 |
+
from pip._vendor.rich.highlighter import ReprHighlighter
|
| 932 |
+
|
| 933 |
+
from ._timer import timer
|
| 934 |
+
|
| 935 |
+
with timer("Table render"):
|
| 936 |
+
table = Table(
|
| 937 |
+
title="Star Wars Movies",
|
| 938 |
+
caption="Rich example table",
|
| 939 |
+
caption_justify="right",
|
| 940 |
+
)
|
| 941 |
+
|
| 942 |
+
table.add_column(
|
| 943 |
+
"Released", header_style="bright_cyan", style="cyan", no_wrap=True
|
| 944 |
+
)
|
| 945 |
+
table.add_column("Title", style="magenta")
|
| 946 |
+
table.add_column("Box Office", justify="right", style="green")
|
| 947 |
+
|
| 948 |
+
table.add_row(
|
| 949 |
+
"Dec 20, 2019",
|
| 950 |
+
"Star Wars: The Rise of Skywalker",
|
| 951 |
+
"$952,110,690",
|
| 952 |
+
)
|
| 953 |
+
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
|
| 954 |
+
table.add_row(
|
| 955 |
+
"Dec 15, 2017",
|
| 956 |
+
"Star Wars Ep. V111: The Last Jedi",
|
| 957 |
+
"$1,332,539,889",
|
| 958 |
+
style="on black",
|
| 959 |
+
end_section=True,
|
| 960 |
+
)
|
| 961 |
+
table.add_row(
|
| 962 |
+
"Dec 16, 2016",
|
| 963 |
+
"Rogue One: A Star Wars Story",
|
| 964 |
+
"$1,332,439,889",
|
| 965 |
+
)
|
| 966 |
+
|
| 967 |
+
def header(text: str) -> None:
|
| 968 |
+
console.print()
|
| 969 |
+
console.rule(highlight(text))
|
| 970 |
+
console.print()
|
| 971 |
+
|
| 972 |
+
console = Console()
|
| 973 |
+
highlight = ReprHighlighter()
|
| 974 |
+
header("Example Table")
|
| 975 |
+
console.print(table, justify="center")
|
| 976 |
+
|
| 977 |
+
table.expand = True
|
| 978 |
+
header("expand=True")
|
| 979 |
+
console.print(table)
|
| 980 |
+
|
| 981 |
+
table.width = 50
|
| 982 |
+
header("width=50")
|
| 983 |
+
|
| 984 |
+
console.print(table, justify="center")
|
| 985 |
+
|
| 986 |
+
table.width = None
|
| 987 |
+
table.expand = False
|
| 988 |
+
table.row_styles = ["dim", "none"]
|
| 989 |
+
header("row_styles=['dim', 'none']")
|
| 990 |
+
|
| 991 |
+
console.print(table, justify="center")
|
| 992 |
+
|
| 993 |
+
table.width = None
|
| 994 |
+
table.expand = False
|
| 995 |
+
table.row_styles = ["dim", "none"]
|
| 996 |
+
table.leading = 1
|
| 997 |
+
header("leading=1, row_styles=['dim', 'none']")
|
| 998 |
+
console.print(table, justify="center")
|
| 999 |
+
|
| 1000 |
+
table.width = None
|
| 1001 |
+
table.expand = False
|
| 1002 |
+
table.row_styles = ["dim", "none"]
|
| 1003 |
+
table.show_lines = True
|
| 1004 |
+
table.leading = 0
|
| 1005 |
+
header("show_lines=True, row_styles=['dim', 'none']")
|
| 1006 |
+
console.print(table, justify="center")
|
env/lib/python3.13/site-packages/pip/_vendor/rich/text.py
ADDED
|
@@ -0,0 +1,1361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from functools import partial, reduce
|
| 3 |
+
from math import gcd
|
| 4 |
+
from operator import itemgetter
|
| 5 |
+
from typing import (
|
| 6 |
+
TYPE_CHECKING,
|
| 7 |
+
Any,
|
| 8 |
+
Callable,
|
| 9 |
+
Dict,
|
| 10 |
+
Iterable,
|
| 11 |
+
List,
|
| 12 |
+
NamedTuple,
|
| 13 |
+
Optional,
|
| 14 |
+
Pattern,
|
| 15 |
+
Tuple,
|
| 16 |
+
Union,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
from ._loop import loop_last
|
| 20 |
+
from ._pick import pick_bool
|
| 21 |
+
from ._wrap import divide_line
|
| 22 |
+
from .align import AlignMethod
|
| 23 |
+
from .cells import cell_len, set_cell_size
|
| 24 |
+
from .containers import Lines
|
| 25 |
+
from .control import strip_control_codes
|
| 26 |
+
from .emoji import EmojiVariant
|
| 27 |
+
from .jupyter import JupyterMixin
|
| 28 |
+
from .measure import Measurement
|
| 29 |
+
from .segment import Segment
|
| 30 |
+
from .style import Style, StyleType
|
| 31 |
+
|
| 32 |
+
if TYPE_CHECKING: # pragma: no cover
|
| 33 |
+
from .console import Console, ConsoleOptions, JustifyMethod, OverflowMethod
|
| 34 |
+
|
| 35 |
+
DEFAULT_JUSTIFY: "JustifyMethod" = "default"
|
| 36 |
+
DEFAULT_OVERFLOW: "OverflowMethod" = "fold"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
_re_whitespace = re.compile(r"\s+$")
|
| 40 |
+
|
| 41 |
+
TextType = Union[str, "Text"]
|
| 42 |
+
"""A plain string or a :class:`Text` instance."""
|
| 43 |
+
|
| 44 |
+
GetStyleCallable = Callable[[str], Optional[StyleType]]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class Span(NamedTuple):
|
| 48 |
+
"""A marked up region in some text."""
|
| 49 |
+
|
| 50 |
+
start: int
|
| 51 |
+
"""Span start index."""
|
| 52 |
+
end: int
|
| 53 |
+
"""Span end index."""
|
| 54 |
+
style: Union[str, Style]
|
| 55 |
+
"""Style associated with the span."""
|
| 56 |
+
|
| 57 |
+
def __repr__(self) -> str:
|
| 58 |
+
return f"Span({self.start}, {self.end}, {self.style!r})"
|
| 59 |
+
|
| 60 |
+
def __bool__(self) -> bool:
|
| 61 |
+
return self.end > self.start
|
| 62 |
+
|
| 63 |
+
def split(self, offset: int) -> Tuple["Span", Optional["Span"]]:
|
| 64 |
+
"""Split a span in to 2 from a given offset."""
|
| 65 |
+
|
| 66 |
+
if offset < self.start:
|
| 67 |
+
return self, None
|
| 68 |
+
if offset >= self.end:
|
| 69 |
+
return self, None
|
| 70 |
+
|
| 71 |
+
start, end, style = self
|
| 72 |
+
span1 = Span(start, min(end, offset), style)
|
| 73 |
+
span2 = Span(span1.end, end, style)
|
| 74 |
+
return span1, span2
|
| 75 |
+
|
| 76 |
+
def move(self, offset: int) -> "Span":
|
| 77 |
+
"""Move start and end by a given offset.
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
offset (int): Number of characters to add to start and end.
|
| 81 |
+
|
| 82 |
+
Returns:
|
| 83 |
+
TextSpan: A new TextSpan with adjusted position.
|
| 84 |
+
"""
|
| 85 |
+
start, end, style = self
|
| 86 |
+
return Span(start + offset, end + offset, style)
|
| 87 |
+
|
| 88 |
+
def right_crop(self, offset: int) -> "Span":
|
| 89 |
+
"""Crop the span at the given offset.
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
offset (int): A value between start and end.
|
| 93 |
+
|
| 94 |
+
Returns:
|
| 95 |
+
Span: A new (possibly smaller) span.
|
| 96 |
+
"""
|
| 97 |
+
start, end, style = self
|
| 98 |
+
if offset >= end:
|
| 99 |
+
return self
|
| 100 |
+
return Span(start, min(offset, end), style)
|
| 101 |
+
|
| 102 |
+
def extend(self, cells: int) -> "Span":
|
| 103 |
+
"""Extend the span by the given number of cells.
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
cells (int): Additional space to add to end of span.
|
| 107 |
+
|
| 108 |
+
Returns:
|
| 109 |
+
Span: A span.
|
| 110 |
+
"""
|
| 111 |
+
if cells:
|
| 112 |
+
start, end, style = self
|
| 113 |
+
return Span(start, end + cells, style)
|
| 114 |
+
else:
|
| 115 |
+
return self
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class Text(JupyterMixin):
|
| 119 |
+
"""Text with color / style.
|
| 120 |
+
|
| 121 |
+
Args:
|
| 122 |
+
text (str, optional): Default unstyled text. Defaults to "".
|
| 123 |
+
style (Union[str, Style], optional): Base style for text. Defaults to "".
|
| 124 |
+
justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
|
| 125 |
+
overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
|
| 126 |
+
no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.
|
| 127 |
+
end (str, optional): Character to end text with. Defaults to "\\\\n".
|
| 128 |
+
tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None.
|
| 129 |
+
spans (List[Span], optional). A list of predefined style spans. Defaults to None.
|
| 130 |
+
"""
|
| 131 |
+
|
| 132 |
+
__slots__ = [
|
| 133 |
+
"_text",
|
| 134 |
+
"style",
|
| 135 |
+
"justify",
|
| 136 |
+
"overflow",
|
| 137 |
+
"no_wrap",
|
| 138 |
+
"end",
|
| 139 |
+
"tab_size",
|
| 140 |
+
"_spans",
|
| 141 |
+
"_length",
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
def __init__(
|
| 145 |
+
self,
|
| 146 |
+
text: str = "",
|
| 147 |
+
style: Union[str, Style] = "",
|
| 148 |
+
*,
|
| 149 |
+
justify: Optional["JustifyMethod"] = None,
|
| 150 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 151 |
+
no_wrap: Optional[bool] = None,
|
| 152 |
+
end: str = "\n",
|
| 153 |
+
tab_size: Optional[int] = None,
|
| 154 |
+
spans: Optional[List[Span]] = None,
|
| 155 |
+
) -> None:
|
| 156 |
+
sanitized_text = strip_control_codes(text)
|
| 157 |
+
self._text = [sanitized_text]
|
| 158 |
+
self.style = style
|
| 159 |
+
self.justify: Optional["JustifyMethod"] = justify
|
| 160 |
+
self.overflow: Optional["OverflowMethod"] = overflow
|
| 161 |
+
self.no_wrap = no_wrap
|
| 162 |
+
self.end = end
|
| 163 |
+
self.tab_size = tab_size
|
| 164 |
+
self._spans: List[Span] = spans or []
|
| 165 |
+
self._length: int = len(sanitized_text)
|
| 166 |
+
|
| 167 |
+
def __len__(self) -> int:
|
| 168 |
+
return self._length
|
| 169 |
+
|
| 170 |
+
def __bool__(self) -> bool:
|
| 171 |
+
return bool(self._length)
|
| 172 |
+
|
| 173 |
+
def __str__(self) -> str:
|
| 174 |
+
return self.plain
|
| 175 |
+
|
| 176 |
+
def __repr__(self) -> str:
|
| 177 |
+
return f"<text {self.plain!r} {self._spans!r} {self.style!r}>"
|
| 178 |
+
|
| 179 |
+
def __add__(self, other: Any) -> "Text":
|
| 180 |
+
if isinstance(other, (str, Text)):
|
| 181 |
+
result = self.copy()
|
| 182 |
+
result.append(other)
|
| 183 |
+
return result
|
| 184 |
+
return NotImplemented
|
| 185 |
+
|
| 186 |
+
def __eq__(self, other: object) -> bool:
|
| 187 |
+
if not isinstance(other, Text):
|
| 188 |
+
return NotImplemented
|
| 189 |
+
return self.plain == other.plain and self._spans == other._spans
|
| 190 |
+
|
| 191 |
+
def __contains__(self, other: object) -> bool:
|
| 192 |
+
if isinstance(other, str):
|
| 193 |
+
return other in self.plain
|
| 194 |
+
elif isinstance(other, Text):
|
| 195 |
+
return other.plain in self.plain
|
| 196 |
+
return False
|
| 197 |
+
|
| 198 |
+
def __getitem__(self, slice: Union[int, slice]) -> "Text":
|
| 199 |
+
def get_text_at(offset: int) -> "Text":
|
| 200 |
+
_Span = Span
|
| 201 |
+
text = Text(
|
| 202 |
+
self.plain[offset],
|
| 203 |
+
spans=[
|
| 204 |
+
_Span(0, 1, style)
|
| 205 |
+
for start, end, style in self._spans
|
| 206 |
+
if end > offset >= start
|
| 207 |
+
],
|
| 208 |
+
end="",
|
| 209 |
+
)
|
| 210 |
+
return text
|
| 211 |
+
|
| 212 |
+
if isinstance(slice, int):
|
| 213 |
+
return get_text_at(slice)
|
| 214 |
+
else:
|
| 215 |
+
start, stop, step = slice.indices(len(self.plain))
|
| 216 |
+
if step == 1:
|
| 217 |
+
lines = self.divide([start, stop])
|
| 218 |
+
return lines[1]
|
| 219 |
+
else:
|
| 220 |
+
# This would be a bit of work to implement efficiently
|
| 221 |
+
# For now, its not required
|
| 222 |
+
raise TypeError("slices with step!=1 are not supported")
|
| 223 |
+
|
| 224 |
+
@property
|
| 225 |
+
def cell_len(self) -> int:
|
| 226 |
+
"""Get the number of cells required to render this text."""
|
| 227 |
+
return cell_len(self.plain)
|
| 228 |
+
|
| 229 |
+
@property
|
| 230 |
+
def markup(self) -> str:
|
| 231 |
+
"""Get console markup to render this Text.
|
| 232 |
+
|
| 233 |
+
Returns:
|
| 234 |
+
str: A string potentially creating markup tags.
|
| 235 |
+
"""
|
| 236 |
+
from .markup import escape
|
| 237 |
+
|
| 238 |
+
output: List[str] = []
|
| 239 |
+
|
| 240 |
+
plain = self.plain
|
| 241 |
+
markup_spans = [
|
| 242 |
+
(0, False, self.style),
|
| 243 |
+
*((span.start, False, span.style) for span in self._spans),
|
| 244 |
+
*((span.end, True, span.style) for span in self._spans),
|
| 245 |
+
(len(plain), True, self.style),
|
| 246 |
+
]
|
| 247 |
+
markup_spans.sort(key=itemgetter(0, 1))
|
| 248 |
+
position = 0
|
| 249 |
+
append = output.append
|
| 250 |
+
for offset, closing, style in markup_spans:
|
| 251 |
+
if offset > position:
|
| 252 |
+
append(escape(plain[position:offset]))
|
| 253 |
+
position = offset
|
| 254 |
+
if style:
|
| 255 |
+
append(f"[/{style}]" if closing else f"[{style}]")
|
| 256 |
+
markup = "".join(output)
|
| 257 |
+
return markup
|
| 258 |
+
|
| 259 |
+
@classmethod
|
| 260 |
+
def from_markup(
|
| 261 |
+
cls,
|
| 262 |
+
text: str,
|
| 263 |
+
*,
|
| 264 |
+
style: Union[str, Style] = "",
|
| 265 |
+
emoji: bool = True,
|
| 266 |
+
emoji_variant: Optional[EmojiVariant] = None,
|
| 267 |
+
justify: Optional["JustifyMethod"] = None,
|
| 268 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 269 |
+
end: str = "\n",
|
| 270 |
+
) -> "Text":
|
| 271 |
+
"""Create Text instance from markup.
|
| 272 |
+
|
| 273 |
+
Args:
|
| 274 |
+
text (str): A string containing console markup.
|
| 275 |
+
style (Union[str, Style], optional): Base style for text. Defaults to "".
|
| 276 |
+
emoji (bool, optional): Also render emoji code. Defaults to True.
|
| 277 |
+
emoji_variant (str, optional): Optional emoji variant, either "text" or "emoji". Defaults to None.
|
| 278 |
+
justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
|
| 279 |
+
overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
|
| 280 |
+
end (str, optional): Character to end text with. Defaults to "\\\\n".
|
| 281 |
+
|
| 282 |
+
Returns:
|
| 283 |
+
Text: A Text instance with markup rendered.
|
| 284 |
+
"""
|
| 285 |
+
from .markup import render
|
| 286 |
+
|
| 287 |
+
rendered_text = render(text, style, emoji=emoji, emoji_variant=emoji_variant)
|
| 288 |
+
rendered_text.justify = justify
|
| 289 |
+
rendered_text.overflow = overflow
|
| 290 |
+
rendered_text.end = end
|
| 291 |
+
return rendered_text
|
| 292 |
+
|
| 293 |
+
@classmethod
|
| 294 |
+
def from_ansi(
|
| 295 |
+
cls,
|
| 296 |
+
text: str,
|
| 297 |
+
*,
|
| 298 |
+
style: Union[str, Style] = "",
|
| 299 |
+
justify: Optional["JustifyMethod"] = None,
|
| 300 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 301 |
+
no_wrap: Optional[bool] = None,
|
| 302 |
+
end: str = "\n",
|
| 303 |
+
tab_size: Optional[int] = 8,
|
| 304 |
+
) -> "Text":
|
| 305 |
+
"""Create a Text object from a string containing ANSI escape codes.
|
| 306 |
+
|
| 307 |
+
Args:
|
| 308 |
+
text (str): A string containing escape codes.
|
| 309 |
+
style (Union[str, Style], optional): Base style for text. Defaults to "".
|
| 310 |
+
justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
|
| 311 |
+
overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
|
| 312 |
+
no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.
|
| 313 |
+
end (str, optional): Character to end text with. Defaults to "\\\\n".
|
| 314 |
+
tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None.
|
| 315 |
+
"""
|
| 316 |
+
from .ansi import AnsiDecoder
|
| 317 |
+
|
| 318 |
+
joiner = Text(
|
| 319 |
+
"\n",
|
| 320 |
+
justify=justify,
|
| 321 |
+
overflow=overflow,
|
| 322 |
+
no_wrap=no_wrap,
|
| 323 |
+
end=end,
|
| 324 |
+
tab_size=tab_size,
|
| 325 |
+
style=style,
|
| 326 |
+
)
|
| 327 |
+
decoder = AnsiDecoder()
|
| 328 |
+
result = joiner.join(line for line in decoder.decode(text))
|
| 329 |
+
return result
|
| 330 |
+
|
| 331 |
+
@classmethod
|
| 332 |
+
def styled(
|
| 333 |
+
cls,
|
| 334 |
+
text: str,
|
| 335 |
+
style: StyleType = "",
|
| 336 |
+
*,
|
| 337 |
+
justify: Optional["JustifyMethod"] = None,
|
| 338 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 339 |
+
) -> "Text":
|
| 340 |
+
"""Construct a Text instance with a pre-applied styled. A style applied in this way won't be used
|
| 341 |
+
to pad the text when it is justified.
|
| 342 |
+
|
| 343 |
+
Args:
|
| 344 |
+
text (str): A string containing console markup.
|
| 345 |
+
style (Union[str, Style]): Style to apply to the text. Defaults to "".
|
| 346 |
+
justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
|
| 347 |
+
overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
|
| 348 |
+
|
| 349 |
+
Returns:
|
| 350 |
+
Text: A text instance with a style applied to the entire string.
|
| 351 |
+
"""
|
| 352 |
+
styled_text = cls(text, justify=justify, overflow=overflow)
|
| 353 |
+
styled_text.stylize(style)
|
| 354 |
+
return styled_text
|
| 355 |
+
|
| 356 |
+
@classmethod
|
| 357 |
+
def assemble(
|
| 358 |
+
cls,
|
| 359 |
+
*parts: Union[str, "Text", Tuple[str, StyleType]],
|
| 360 |
+
style: Union[str, Style] = "",
|
| 361 |
+
justify: Optional["JustifyMethod"] = None,
|
| 362 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 363 |
+
no_wrap: Optional[bool] = None,
|
| 364 |
+
end: str = "\n",
|
| 365 |
+
tab_size: int = 8,
|
| 366 |
+
meta: Optional[Dict[str, Any]] = None,
|
| 367 |
+
) -> "Text":
|
| 368 |
+
"""Construct a text instance by combining a sequence of strings with optional styles.
|
| 369 |
+
The positional arguments should be either strings, or a tuple of string + style.
|
| 370 |
+
|
| 371 |
+
Args:
|
| 372 |
+
style (Union[str, Style], optional): Base style for text. Defaults to "".
|
| 373 |
+
justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
|
| 374 |
+
overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
|
| 375 |
+
no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.
|
| 376 |
+
end (str, optional): Character to end text with. Defaults to "\\\\n".
|
| 377 |
+
tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None.
|
| 378 |
+
meta (Dict[str, Any], optional). Meta data to apply to text, or None for no meta data. Default to None
|
| 379 |
+
|
| 380 |
+
Returns:
|
| 381 |
+
Text: A new text instance.
|
| 382 |
+
"""
|
| 383 |
+
text = cls(
|
| 384 |
+
style=style,
|
| 385 |
+
justify=justify,
|
| 386 |
+
overflow=overflow,
|
| 387 |
+
no_wrap=no_wrap,
|
| 388 |
+
end=end,
|
| 389 |
+
tab_size=tab_size,
|
| 390 |
+
)
|
| 391 |
+
append = text.append
|
| 392 |
+
_Text = Text
|
| 393 |
+
for part in parts:
|
| 394 |
+
if isinstance(part, (_Text, str)):
|
| 395 |
+
append(part)
|
| 396 |
+
else:
|
| 397 |
+
append(*part)
|
| 398 |
+
if meta:
|
| 399 |
+
text.apply_meta(meta)
|
| 400 |
+
return text
|
| 401 |
+
|
| 402 |
+
@property
|
| 403 |
+
def plain(self) -> str:
|
| 404 |
+
"""Get the text as a single string."""
|
| 405 |
+
if len(self._text) != 1:
|
| 406 |
+
self._text[:] = ["".join(self._text)]
|
| 407 |
+
return self._text[0]
|
| 408 |
+
|
| 409 |
+
@plain.setter
|
| 410 |
+
def plain(self, new_text: str) -> None:
|
| 411 |
+
"""Set the text to a new value."""
|
| 412 |
+
if new_text != self.plain:
|
| 413 |
+
sanitized_text = strip_control_codes(new_text)
|
| 414 |
+
self._text[:] = [sanitized_text]
|
| 415 |
+
old_length = self._length
|
| 416 |
+
self._length = len(sanitized_text)
|
| 417 |
+
if old_length > self._length:
|
| 418 |
+
self._trim_spans()
|
| 419 |
+
|
| 420 |
+
@property
|
| 421 |
+
def spans(self) -> List[Span]:
|
| 422 |
+
"""Get a reference to the internal list of spans."""
|
| 423 |
+
return self._spans
|
| 424 |
+
|
| 425 |
+
@spans.setter
|
| 426 |
+
def spans(self, spans: List[Span]) -> None:
|
| 427 |
+
"""Set spans."""
|
| 428 |
+
self._spans = spans[:]
|
| 429 |
+
|
| 430 |
+
def blank_copy(self, plain: str = "") -> "Text":
|
| 431 |
+
"""Return a new Text instance with copied metadata (but not the string or spans)."""
|
| 432 |
+
copy_self = Text(
|
| 433 |
+
plain,
|
| 434 |
+
style=self.style,
|
| 435 |
+
justify=self.justify,
|
| 436 |
+
overflow=self.overflow,
|
| 437 |
+
no_wrap=self.no_wrap,
|
| 438 |
+
end=self.end,
|
| 439 |
+
tab_size=self.tab_size,
|
| 440 |
+
)
|
| 441 |
+
return copy_self
|
| 442 |
+
|
| 443 |
+
def copy(self) -> "Text":
|
| 444 |
+
"""Return a copy of this instance."""
|
| 445 |
+
copy_self = Text(
|
| 446 |
+
self.plain,
|
| 447 |
+
style=self.style,
|
| 448 |
+
justify=self.justify,
|
| 449 |
+
overflow=self.overflow,
|
| 450 |
+
no_wrap=self.no_wrap,
|
| 451 |
+
end=self.end,
|
| 452 |
+
tab_size=self.tab_size,
|
| 453 |
+
)
|
| 454 |
+
copy_self._spans[:] = self._spans
|
| 455 |
+
return copy_self
|
| 456 |
+
|
| 457 |
+
def stylize(
|
| 458 |
+
self,
|
| 459 |
+
style: Union[str, Style],
|
| 460 |
+
start: int = 0,
|
| 461 |
+
end: Optional[int] = None,
|
| 462 |
+
) -> None:
|
| 463 |
+
"""Apply a style to the text, or a portion of the text.
|
| 464 |
+
|
| 465 |
+
Args:
|
| 466 |
+
style (Union[str, Style]): Style instance or style definition to apply.
|
| 467 |
+
start (int): Start offset (negative indexing is supported). Defaults to 0.
|
| 468 |
+
end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None.
|
| 469 |
+
"""
|
| 470 |
+
if style:
|
| 471 |
+
length = len(self)
|
| 472 |
+
if start < 0:
|
| 473 |
+
start = length + start
|
| 474 |
+
if end is None:
|
| 475 |
+
end = length
|
| 476 |
+
if end < 0:
|
| 477 |
+
end = length + end
|
| 478 |
+
if start >= length or end <= start:
|
| 479 |
+
# Span not in text or not valid
|
| 480 |
+
return
|
| 481 |
+
self._spans.append(Span(start, min(length, end), style))
|
| 482 |
+
|
| 483 |
+
def stylize_before(
|
| 484 |
+
self,
|
| 485 |
+
style: Union[str, Style],
|
| 486 |
+
start: int = 0,
|
| 487 |
+
end: Optional[int] = None,
|
| 488 |
+
) -> None:
|
| 489 |
+
"""Apply a style to the text, or a portion of the text. Styles will be applied before other styles already present.
|
| 490 |
+
|
| 491 |
+
Args:
|
| 492 |
+
style (Union[str, Style]): Style instance or style definition to apply.
|
| 493 |
+
start (int): Start offset (negative indexing is supported). Defaults to 0.
|
| 494 |
+
end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None.
|
| 495 |
+
"""
|
| 496 |
+
if style:
|
| 497 |
+
length = len(self)
|
| 498 |
+
if start < 0:
|
| 499 |
+
start = length + start
|
| 500 |
+
if end is None:
|
| 501 |
+
end = length
|
| 502 |
+
if end < 0:
|
| 503 |
+
end = length + end
|
| 504 |
+
if start >= length or end <= start:
|
| 505 |
+
# Span not in text or not valid
|
| 506 |
+
return
|
| 507 |
+
self._spans.insert(0, Span(start, min(length, end), style))
|
| 508 |
+
|
| 509 |
+
def apply_meta(
|
| 510 |
+
self, meta: Dict[str, Any], start: int = 0, end: Optional[int] = None
|
| 511 |
+
) -> None:
|
| 512 |
+
"""Apply metadata to the text, or a portion of the text.
|
| 513 |
+
|
| 514 |
+
Args:
|
| 515 |
+
meta (Dict[str, Any]): A dict of meta information.
|
| 516 |
+
start (int): Start offset (negative indexing is supported). Defaults to 0.
|
| 517 |
+
end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None.
|
| 518 |
+
|
| 519 |
+
"""
|
| 520 |
+
style = Style.from_meta(meta)
|
| 521 |
+
self.stylize(style, start=start, end=end)
|
| 522 |
+
|
| 523 |
+
def on(self, meta: Optional[Dict[str, Any]] = None, **handlers: Any) -> "Text":
|
| 524 |
+
"""Apply event handlers (used by Textual project).
|
| 525 |
+
|
| 526 |
+
Example:
|
| 527 |
+
>>> from rich.text import Text
|
| 528 |
+
>>> text = Text("hello world")
|
| 529 |
+
>>> text.on(click="view.toggle('world')")
|
| 530 |
+
|
| 531 |
+
Args:
|
| 532 |
+
meta (Dict[str, Any]): Mapping of meta information.
|
| 533 |
+
**handlers: Keyword args are prefixed with "@" to defined handlers.
|
| 534 |
+
|
| 535 |
+
Returns:
|
| 536 |
+
Text: Self is returned to method may be chained.
|
| 537 |
+
"""
|
| 538 |
+
meta = {} if meta is None else meta
|
| 539 |
+
meta.update({f"@{key}": value for key, value in handlers.items()})
|
| 540 |
+
self.stylize(Style.from_meta(meta))
|
| 541 |
+
return self
|
| 542 |
+
|
| 543 |
+
def remove_suffix(self, suffix: str) -> None:
|
| 544 |
+
"""Remove a suffix if it exists.
|
| 545 |
+
|
| 546 |
+
Args:
|
| 547 |
+
suffix (str): Suffix to remove.
|
| 548 |
+
"""
|
| 549 |
+
if self.plain.endswith(suffix):
|
| 550 |
+
self.right_crop(len(suffix))
|
| 551 |
+
|
| 552 |
+
def get_style_at_offset(self, console: "Console", offset: int) -> Style:
|
| 553 |
+
"""Get the style of a character at give offset.
|
| 554 |
+
|
| 555 |
+
Args:
|
| 556 |
+
console (~Console): Console where text will be rendered.
|
| 557 |
+
offset (int): Offset in to text (negative indexing supported)
|
| 558 |
+
|
| 559 |
+
Returns:
|
| 560 |
+
Style: A Style instance.
|
| 561 |
+
"""
|
| 562 |
+
# TODO: This is a little inefficient, it is only used by full justify
|
| 563 |
+
if offset < 0:
|
| 564 |
+
offset = len(self) + offset
|
| 565 |
+
get_style = console.get_style
|
| 566 |
+
style = get_style(self.style).copy()
|
| 567 |
+
for start, end, span_style in self._spans:
|
| 568 |
+
if end > offset >= start:
|
| 569 |
+
style += get_style(span_style, default="")
|
| 570 |
+
return style
|
| 571 |
+
|
| 572 |
+
def extend_style(self, spaces: int) -> None:
|
| 573 |
+
"""Extend the Text given number of spaces where the spaces have the same style as the last character.
|
| 574 |
+
|
| 575 |
+
Args:
|
| 576 |
+
spaces (int): Number of spaces to add to the Text.
|
| 577 |
+
"""
|
| 578 |
+
if spaces <= 0:
|
| 579 |
+
return
|
| 580 |
+
spans = self.spans
|
| 581 |
+
new_spaces = " " * spaces
|
| 582 |
+
if spans:
|
| 583 |
+
end_offset = len(self)
|
| 584 |
+
self._spans[:] = [
|
| 585 |
+
span.extend(spaces) if span.end >= end_offset else span
|
| 586 |
+
for span in spans
|
| 587 |
+
]
|
| 588 |
+
self._text.append(new_spaces)
|
| 589 |
+
self._length += spaces
|
| 590 |
+
else:
|
| 591 |
+
self.plain += new_spaces
|
| 592 |
+
|
| 593 |
+
def highlight_regex(
|
| 594 |
+
self,
|
| 595 |
+
re_highlight: Union[Pattern[str], str],
|
| 596 |
+
style: Optional[Union[GetStyleCallable, StyleType]] = None,
|
| 597 |
+
*,
|
| 598 |
+
style_prefix: str = "",
|
| 599 |
+
) -> int:
|
| 600 |
+
"""Highlight text with a regular expression, where group names are
|
| 601 |
+
translated to styles.
|
| 602 |
+
|
| 603 |
+
Args:
|
| 604 |
+
re_highlight (Union[re.Pattern, str]): A regular expression object or string.
|
| 605 |
+
style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable
|
| 606 |
+
which accepts the matched text and returns a style. Defaults to None.
|
| 607 |
+
style_prefix (str, optional): Optional prefix to add to style group names.
|
| 608 |
+
|
| 609 |
+
Returns:
|
| 610 |
+
int: Number of regex matches
|
| 611 |
+
"""
|
| 612 |
+
count = 0
|
| 613 |
+
append_span = self._spans.append
|
| 614 |
+
_Span = Span
|
| 615 |
+
plain = self.plain
|
| 616 |
+
if isinstance(re_highlight, str):
|
| 617 |
+
re_highlight = re.compile(re_highlight)
|
| 618 |
+
for match in re_highlight.finditer(plain):
|
| 619 |
+
get_span = match.span
|
| 620 |
+
if style:
|
| 621 |
+
start, end = get_span()
|
| 622 |
+
match_style = style(plain[start:end]) if callable(style) else style
|
| 623 |
+
if match_style is not None and end > start:
|
| 624 |
+
append_span(_Span(start, end, match_style))
|
| 625 |
+
|
| 626 |
+
count += 1
|
| 627 |
+
for name in match.groupdict().keys():
|
| 628 |
+
start, end = get_span(name)
|
| 629 |
+
if start != -1 and end > start:
|
| 630 |
+
append_span(_Span(start, end, f"{style_prefix}{name}"))
|
| 631 |
+
return count
|
| 632 |
+
|
| 633 |
+
def highlight_words(
|
| 634 |
+
self,
|
| 635 |
+
words: Iterable[str],
|
| 636 |
+
style: Union[str, Style],
|
| 637 |
+
*,
|
| 638 |
+
case_sensitive: bool = True,
|
| 639 |
+
) -> int:
|
| 640 |
+
"""Highlight words with a style.
|
| 641 |
+
|
| 642 |
+
Args:
|
| 643 |
+
words (Iterable[str]): Words to highlight.
|
| 644 |
+
style (Union[str, Style]): Style to apply.
|
| 645 |
+
case_sensitive (bool, optional): Enable case sensitive matching. Defaults to True.
|
| 646 |
+
|
| 647 |
+
Returns:
|
| 648 |
+
int: Number of words highlighted.
|
| 649 |
+
"""
|
| 650 |
+
re_words = "|".join(re.escape(word) for word in words)
|
| 651 |
+
add_span = self._spans.append
|
| 652 |
+
count = 0
|
| 653 |
+
_Span = Span
|
| 654 |
+
for match in re.finditer(
|
| 655 |
+
re_words, self.plain, flags=0 if case_sensitive else re.IGNORECASE
|
| 656 |
+
):
|
| 657 |
+
start, end = match.span(0)
|
| 658 |
+
add_span(_Span(start, end, style))
|
| 659 |
+
count += 1
|
| 660 |
+
return count
|
| 661 |
+
|
| 662 |
+
def rstrip(self) -> None:
|
| 663 |
+
"""Strip whitespace from end of text."""
|
| 664 |
+
self.plain = self.plain.rstrip()
|
| 665 |
+
|
| 666 |
+
def rstrip_end(self, size: int) -> None:
|
| 667 |
+
"""Remove whitespace beyond a certain width at the end of the text.
|
| 668 |
+
|
| 669 |
+
Args:
|
| 670 |
+
size (int): The desired size of the text.
|
| 671 |
+
"""
|
| 672 |
+
text_length = len(self)
|
| 673 |
+
if text_length > size:
|
| 674 |
+
excess = text_length - size
|
| 675 |
+
whitespace_match = _re_whitespace.search(self.plain)
|
| 676 |
+
if whitespace_match is not None:
|
| 677 |
+
whitespace_count = len(whitespace_match.group(0))
|
| 678 |
+
self.right_crop(min(whitespace_count, excess))
|
| 679 |
+
|
| 680 |
+
def set_length(self, new_length: int) -> None:
|
| 681 |
+
"""Set new length of the text, clipping or padding is required."""
|
| 682 |
+
length = len(self)
|
| 683 |
+
if length != new_length:
|
| 684 |
+
if length < new_length:
|
| 685 |
+
self.pad_right(new_length - length)
|
| 686 |
+
else:
|
| 687 |
+
self.right_crop(length - new_length)
|
| 688 |
+
|
| 689 |
+
def __rich_console__(
|
| 690 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 691 |
+
) -> Iterable[Segment]:
|
| 692 |
+
tab_size: int = console.tab_size if self.tab_size is None else self.tab_size
|
| 693 |
+
justify = self.justify or options.justify or DEFAULT_JUSTIFY
|
| 694 |
+
|
| 695 |
+
overflow = self.overflow or options.overflow or DEFAULT_OVERFLOW
|
| 696 |
+
|
| 697 |
+
lines = self.wrap(
|
| 698 |
+
console,
|
| 699 |
+
options.max_width,
|
| 700 |
+
justify=justify,
|
| 701 |
+
overflow=overflow,
|
| 702 |
+
tab_size=tab_size or 8,
|
| 703 |
+
no_wrap=pick_bool(self.no_wrap, options.no_wrap, False),
|
| 704 |
+
)
|
| 705 |
+
all_lines = Text("\n").join(lines)
|
| 706 |
+
yield from all_lines.render(console, end=self.end)
|
| 707 |
+
|
| 708 |
+
def __rich_measure__(
|
| 709 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 710 |
+
) -> Measurement:
|
| 711 |
+
text = self.plain
|
| 712 |
+
lines = text.splitlines()
|
| 713 |
+
max_text_width = max(cell_len(line) for line in lines) if lines else 0
|
| 714 |
+
words = text.split()
|
| 715 |
+
min_text_width = (
|
| 716 |
+
max(cell_len(word) for word in words) if words else max_text_width
|
| 717 |
+
)
|
| 718 |
+
return Measurement(min_text_width, max_text_width)
|
| 719 |
+
|
| 720 |
+
def render(self, console: "Console", end: str = "") -> Iterable["Segment"]:
|
| 721 |
+
"""Render the text as Segments.
|
| 722 |
+
|
| 723 |
+
Args:
|
| 724 |
+
console (Console): Console instance.
|
| 725 |
+
end (Optional[str], optional): Optional end character.
|
| 726 |
+
|
| 727 |
+
Returns:
|
| 728 |
+
Iterable[Segment]: Result of render that may be written to the console.
|
| 729 |
+
"""
|
| 730 |
+
_Segment = Segment
|
| 731 |
+
text = self.plain
|
| 732 |
+
if not self._spans:
|
| 733 |
+
yield Segment(text)
|
| 734 |
+
if end:
|
| 735 |
+
yield _Segment(end)
|
| 736 |
+
return
|
| 737 |
+
get_style = partial(console.get_style, default=Style.null())
|
| 738 |
+
|
| 739 |
+
enumerated_spans = list(enumerate(self._spans, 1))
|
| 740 |
+
style_map = {index: get_style(span.style) for index, span in enumerated_spans}
|
| 741 |
+
style_map[0] = get_style(self.style)
|
| 742 |
+
|
| 743 |
+
spans = [
|
| 744 |
+
(0, False, 0),
|
| 745 |
+
*((span.start, False, index) for index, span in enumerated_spans),
|
| 746 |
+
*((span.end, True, index) for index, span in enumerated_spans),
|
| 747 |
+
(len(text), True, 0),
|
| 748 |
+
]
|
| 749 |
+
spans.sort(key=itemgetter(0, 1))
|
| 750 |
+
|
| 751 |
+
stack: List[int] = []
|
| 752 |
+
stack_append = stack.append
|
| 753 |
+
stack_pop = stack.remove
|
| 754 |
+
|
| 755 |
+
style_cache: Dict[Tuple[Style, ...], Style] = {}
|
| 756 |
+
style_cache_get = style_cache.get
|
| 757 |
+
combine = Style.combine
|
| 758 |
+
|
| 759 |
+
def get_current_style() -> Style:
|
| 760 |
+
"""Construct current style from stack."""
|
| 761 |
+
styles = tuple(style_map[_style_id] for _style_id in sorted(stack))
|
| 762 |
+
cached_style = style_cache_get(styles)
|
| 763 |
+
if cached_style is not None:
|
| 764 |
+
return cached_style
|
| 765 |
+
current_style = combine(styles)
|
| 766 |
+
style_cache[styles] = current_style
|
| 767 |
+
return current_style
|
| 768 |
+
|
| 769 |
+
for (offset, leaving, style_id), (next_offset, _, _) in zip(spans, spans[1:]):
|
| 770 |
+
if leaving:
|
| 771 |
+
stack_pop(style_id)
|
| 772 |
+
else:
|
| 773 |
+
stack_append(style_id)
|
| 774 |
+
if next_offset > offset:
|
| 775 |
+
yield _Segment(text[offset:next_offset], get_current_style())
|
| 776 |
+
if end:
|
| 777 |
+
yield _Segment(end)
|
| 778 |
+
|
| 779 |
+
def join(self, lines: Iterable["Text"]) -> "Text":
|
| 780 |
+
"""Join text together with this instance as the separator.
|
| 781 |
+
|
| 782 |
+
Args:
|
| 783 |
+
lines (Iterable[Text]): An iterable of Text instances to join.
|
| 784 |
+
|
| 785 |
+
Returns:
|
| 786 |
+
Text: A new text instance containing join text.
|
| 787 |
+
"""
|
| 788 |
+
|
| 789 |
+
new_text = self.blank_copy()
|
| 790 |
+
|
| 791 |
+
def iter_text() -> Iterable["Text"]:
|
| 792 |
+
if self.plain:
|
| 793 |
+
for last, line in loop_last(lines):
|
| 794 |
+
yield line
|
| 795 |
+
if not last:
|
| 796 |
+
yield self
|
| 797 |
+
else:
|
| 798 |
+
yield from lines
|
| 799 |
+
|
| 800 |
+
extend_text = new_text._text.extend
|
| 801 |
+
append_span = new_text._spans.append
|
| 802 |
+
extend_spans = new_text._spans.extend
|
| 803 |
+
offset = 0
|
| 804 |
+
_Span = Span
|
| 805 |
+
|
| 806 |
+
for text in iter_text():
|
| 807 |
+
extend_text(text._text)
|
| 808 |
+
if text.style:
|
| 809 |
+
append_span(_Span(offset, offset + len(text), text.style))
|
| 810 |
+
extend_spans(
|
| 811 |
+
_Span(offset + start, offset + end, style)
|
| 812 |
+
for start, end, style in text._spans
|
| 813 |
+
)
|
| 814 |
+
offset += len(text)
|
| 815 |
+
new_text._length = offset
|
| 816 |
+
return new_text
|
| 817 |
+
|
| 818 |
+
def expand_tabs(self, tab_size: Optional[int] = None) -> None:
|
| 819 |
+
"""Converts tabs to spaces.
|
| 820 |
+
|
| 821 |
+
Args:
|
| 822 |
+
tab_size (int, optional): Size of tabs. Defaults to 8.
|
| 823 |
+
|
| 824 |
+
"""
|
| 825 |
+
if "\t" not in self.plain:
|
| 826 |
+
return
|
| 827 |
+
if tab_size is None:
|
| 828 |
+
tab_size = self.tab_size
|
| 829 |
+
if tab_size is None:
|
| 830 |
+
tab_size = 8
|
| 831 |
+
|
| 832 |
+
new_text: List[Text] = []
|
| 833 |
+
append = new_text.append
|
| 834 |
+
|
| 835 |
+
for line in self.split("\n", include_separator=True):
|
| 836 |
+
if "\t" not in line.plain:
|
| 837 |
+
append(line)
|
| 838 |
+
else:
|
| 839 |
+
cell_position = 0
|
| 840 |
+
parts = line.split("\t", include_separator=True)
|
| 841 |
+
for part in parts:
|
| 842 |
+
if part.plain.endswith("\t"):
|
| 843 |
+
part._text[-1] = part._text[-1][:-1] + " "
|
| 844 |
+
cell_position += part.cell_len
|
| 845 |
+
tab_remainder = cell_position % tab_size
|
| 846 |
+
if tab_remainder:
|
| 847 |
+
spaces = tab_size - tab_remainder
|
| 848 |
+
part.extend_style(spaces)
|
| 849 |
+
cell_position += spaces
|
| 850 |
+
else:
|
| 851 |
+
cell_position += part.cell_len
|
| 852 |
+
append(part)
|
| 853 |
+
|
| 854 |
+
result = Text("").join(new_text)
|
| 855 |
+
|
| 856 |
+
self._text = [result.plain]
|
| 857 |
+
self._length = len(self.plain)
|
| 858 |
+
self._spans[:] = result._spans
|
| 859 |
+
|
| 860 |
+
def truncate(
|
| 861 |
+
self,
|
| 862 |
+
max_width: int,
|
| 863 |
+
*,
|
| 864 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 865 |
+
pad: bool = False,
|
| 866 |
+
) -> None:
|
| 867 |
+
"""Truncate text if it is longer that a given width.
|
| 868 |
+
|
| 869 |
+
Args:
|
| 870 |
+
max_width (int): Maximum number of characters in text.
|
| 871 |
+
overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow.
|
| 872 |
+
pad (bool, optional): Pad with spaces if the length is less than max_width. Defaults to False.
|
| 873 |
+
"""
|
| 874 |
+
_overflow = overflow or self.overflow or DEFAULT_OVERFLOW
|
| 875 |
+
if _overflow != "ignore":
|
| 876 |
+
length = cell_len(self.plain)
|
| 877 |
+
if length > max_width:
|
| 878 |
+
if _overflow == "ellipsis":
|
| 879 |
+
self.plain = set_cell_size(self.plain, max_width - 1) + "…"
|
| 880 |
+
else:
|
| 881 |
+
self.plain = set_cell_size(self.plain, max_width)
|
| 882 |
+
if pad and length < max_width:
|
| 883 |
+
spaces = max_width - length
|
| 884 |
+
self._text = [f"{self.plain}{' ' * spaces}"]
|
| 885 |
+
self._length = len(self.plain)
|
| 886 |
+
|
| 887 |
+
def _trim_spans(self) -> None:
|
| 888 |
+
"""Remove or modify any spans that are over the end of the text."""
|
| 889 |
+
max_offset = len(self.plain)
|
| 890 |
+
_Span = Span
|
| 891 |
+
self._spans[:] = [
|
| 892 |
+
(
|
| 893 |
+
span
|
| 894 |
+
if span.end < max_offset
|
| 895 |
+
else _Span(span.start, min(max_offset, span.end), span.style)
|
| 896 |
+
)
|
| 897 |
+
for span in self._spans
|
| 898 |
+
if span.start < max_offset
|
| 899 |
+
]
|
| 900 |
+
|
| 901 |
+
def pad(self, count: int, character: str = " ") -> None:
|
| 902 |
+
"""Pad left and right with a given number of characters.
|
| 903 |
+
|
| 904 |
+
Args:
|
| 905 |
+
count (int): Width of padding.
|
| 906 |
+
character (str): The character to pad with. Must be a string of length 1.
|
| 907 |
+
"""
|
| 908 |
+
assert len(character) == 1, "Character must be a string of length 1"
|
| 909 |
+
if count:
|
| 910 |
+
pad_characters = character * count
|
| 911 |
+
self.plain = f"{pad_characters}{self.plain}{pad_characters}"
|
| 912 |
+
_Span = Span
|
| 913 |
+
self._spans[:] = [
|
| 914 |
+
_Span(start + count, end + count, style)
|
| 915 |
+
for start, end, style in self._spans
|
| 916 |
+
]
|
| 917 |
+
|
| 918 |
+
def pad_left(self, count: int, character: str = " ") -> None:
|
| 919 |
+
"""Pad the left with a given character.
|
| 920 |
+
|
| 921 |
+
Args:
|
| 922 |
+
count (int): Number of characters to pad.
|
| 923 |
+
character (str, optional): Character to pad with. Defaults to " ".
|
| 924 |
+
"""
|
| 925 |
+
assert len(character) == 1, "Character must be a string of length 1"
|
| 926 |
+
if count:
|
| 927 |
+
self.plain = f"{character * count}{self.plain}"
|
| 928 |
+
_Span = Span
|
| 929 |
+
self._spans[:] = [
|
| 930 |
+
_Span(start + count, end + count, style)
|
| 931 |
+
for start, end, style in self._spans
|
| 932 |
+
]
|
| 933 |
+
|
| 934 |
+
def pad_right(self, count: int, character: str = " ") -> None:
|
| 935 |
+
"""Pad the right with a given character.
|
| 936 |
+
|
| 937 |
+
Args:
|
| 938 |
+
count (int): Number of characters to pad.
|
| 939 |
+
character (str, optional): Character to pad with. Defaults to " ".
|
| 940 |
+
"""
|
| 941 |
+
assert len(character) == 1, "Character must be a string of length 1"
|
| 942 |
+
if count:
|
| 943 |
+
self.plain = f"{self.plain}{character * count}"
|
| 944 |
+
|
| 945 |
+
def align(self, align: AlignMethod, width: int, character: str = " ") -> None:
|
| 946 |
+
"""Align text to a given width.
|
| 947 |
+
|
| 948 |
+
Args:
|
| 949 |
+
align (AlignMethod): One of "left", "center", or "right".
|
| 950 |
+
width (int): Desired width.
|
| 951 |
+
character (str, optional): Character to pad with. Defaults to " ".
|
| 952 |
+
"""
|
| 953 |
+
self.truncate(width)
|
| 954 |
+
excess_space = width - cell_len(self.plain)
|
| 955 |
+
if excess_space:
|
| 956 |
+
if align == "left":
|
| 957 |
+
self.pad_right(excess_space, character)
|
| 958 |
+
elif align == "center":
|
| 959 |
+
left = excess_space // 2
|
| 960 |
+
self.pad_left(left, character)
|
| 961 |
+
self.pad_right(excess_space - left, character)
|
| 962 |
+
else:
|
| 963 |
+
self.pad_left(excess_space, character)
|
| 964 |
+
|
| 965 |
+
def append(
|
| 966 |
+
self, text: Union["Text", str], style: Optional[Union[str, "Style"]] = None
|
| 967 |
+
) -> "Text":
|
| 968 |
+
"""Add text with an optional style.
|
| 969 |
+
|
| 970 |
+
Args:
|
| 971 |
+
text (Union[Text, str]): A str or Text to append.
|
| 972 |
+
style (str, optional): A style name. Defaults to None.
|
| 973 |
+
|
| 974 |
+
Returns:
|
| 975 |
+
Text: Returns self for chaining.
|
| 976 |
+
"""
|
| 977 |
+
|
| 978 |
+
if not isinstance(text, (str, Text)):
|
| 979 |
+
raise TypeError("Only str or Text can be appended to Text")
|
| 980 |
+
|
| 981 |
+
if len(text):
|
| 982 |
+
if isinstance(text, str):
|
| 983 |
+
sanitized_text = strip_control_codes(text)
|
| 984 |
+
self._text.append(sanitized_text)
|
| 985 |
+
offset = len(self)
|
| 986 |
+
text_length = len(sanitized_text)
|
| 987 |
+
if style:
|
| 988 |
+
self._spans.append(Span(offset, offset + text_length, style))
|
| 989 |
+
self._length += text_length
|
| 990 |
+
elif isinstance(text, Text):
|
| 991 |
+
_Span = Span
|
| 992 |
+
if style is not None:
|
| 993 |
+
raise ValueError(
|
| 994 |
+
"style must not be set when appending Text instance"
|
| 995 |
+
)
|
| 996 |
+
text_length = self._length
|
| 997 |
+
if text.style:
|
| 998 |
+
self._spans.append(
|
| 999 |
+
_Span(text_length, text_length + len(text), text.style)
|
| 1000 |
+
)
|
| 1001 |
+
self._text.append(text.plain)
|
| 1002 |
+
self._spans.extend(
|
| 1003 |
+
_Span(start + text_length, end + text_length, style)
|
| 1004 |
+
for start, end, style in text._spans.copy()
|
| 1005 |
+
)
|
| 1006 |
+
self._length += len(text)
|
| 1007 |
+
return self
|
| 1008 |
+
|
| 1009 |
+
def append_text(self, text: "Text") -> "Text":
|
| 1010 |
+
"""Append another Text instance. This method is more performant that Text.append, but
|
| 1011 |
+
only works for Text.
|
| 1012 |
+
|
| 1013 |
+
Args:
|
| 1014 |
+
text (Text): The Text instance to append to this instance.
|
| 1015 |
+
|
| 1016 |
+
Returns:
|
| 1017 |
+
Text: Returns self for chaining.
|
| 1018 |
+
"""
|
| 1019 |
+
_Span = Span
|
| 1020 |
+
text_length = self._length
|
| 1021 |
+
if text.style:
|
| 1022 |
+
self._spans.append(_Span(text_length, text_length + len(text), text.style))
|
| 1023 |
+
self._text.append(text.plain)
|
| 1024 |
+
self._spans.extend(
|
| 1025 |
+
_Span(start + text_length, end + text_length, style)
|
| 1026 |
+
for start, end, style in text._spans.copy()
|
| 1027 |
+
)
|
| 1028 |
+
self._length += len(text)
|
| 1029 |
+
return self
|
| 1030 |
+
|
| 1031 |
+
def append_tokens(
|
| 1032 |
+
self, tokens: Iterable[Tuple[str, Optional[StyleType]]]
|
| 1033 |
+
) -> "Text":
|
| 1034 |
+
"""Append iterable of str and style. Style may be a Style instance or a str style definition.
|
| 1035 |
+
|
| 1036 |
+
Args:
|
| 1037 |
+
tokens (Iterable[Tuple[str, Optional[StyleType]]]): An iterable of tuples containing str content and style.
|
| 1038 |
+
|
| 1039 |
+
Returns:
|
| 1040 |
+
Text: Returns self for chaining.
|
| 1041 |
+
"""
|
| 1042 |
+
append_text = self._text.append
|
| 1043 |
+
append_span = self._spans.append
|
| 1044 |
+
_Span = Span
|
| 1045 |
+
offset = len(self)
|
| 1046 |
+
for content, style in tokens:
|
| 1047 |
+
content = strip_control_codes(content)
|
| 1048 |
+
append_text(content)
|
| 1049 |
+
if style:
|
| 1050 |
+
append_span(_Span(offset, offset + len(content), style))
|
| 1051 |
+
offset += len(content)
|
| 1052 |
+
self._length = offset
|
| 1053 |
+
return self
|
| 1054 |
+
|
| 1055 |
+
def copy_styles(self, text: "Text") -> None:
|
| 1056 |
+
"""Copy styles from another Text instance.
|
| 1057 |
+
|
| 1058 |
+
Args:
|
| 1059 |
+
text (Text): A Text instance to copy styles from, must be the same length.
|
| 1060 |
+
"""
|
| 1061 |
+
self._spans.extend(text._spans)
|
| 1062 |
+
|
| 1063 |
+
def split(
|
| 1064 |
+
self,
|
| 1065 |
+
separator: str = "\n",
|
| 1066 |
+
*,
|
| 1067 |
+
include_separator: bool = False,
|
| 1068 |
+
allow_blank: bool = False,
|
| 1069 |
+
) -> Lines:
|
| 1070 |
+
"""Split rich text in to lines, preserving styles.
|
| 1071 |
+
|
| 1072 |
+
Args:
|
| 1073 |
+
separator (str, optional): String to split on. Defaults to "\\\\n".
|
| 1074 |
+
include_separator (bool, optional): Include the separator in the lines. Defaults to False.
|
| 1075 |
+
allow_blank (bool, optional): Return a blank line if the text ends with a separator. Defaults to False.
|
| 1076 |
+
|
| 1077 |
+
Returns:
|
| 1078 |
+
List[RichText]: A list of rich text, one per line of the original.
|
| 1079 |
+
"""
|
| 1080 |
+
assert separator, "separator must not be empty"
|
| 1081 |
+
|
| 1082 |
+
text = self.plain
|
| 1083 |
+
if separator not in text:
|
| 1084 |
+
return Lines([self.copy()])
|
| 1085 |
+
|
| 1086 |
+
if include_separator:
|
| 1087 |
+
lines = self.divide(
|
| 1088 |
+
match.end() for match in re.finditer(re.escape(separator), text)
|
| 1089 |
+
)
|
| 1090 |
+
else:
|
| 1091 |
+
|
| 1092 |
+
def flatten_spans() -> Iterable[int]:
|
| 1093 |
+
for match in re.finditer(re.escape(separator), text):
|
| 1094 |
+
start, end = match.span()
|
| 1095 |
+
yield start
|
| 1096 |
+
yield end
|
| 1097 |
+
|
| 1098 |
+
lines = Lines(
|
| 1099 |
+
line for line in self.divide(flatten_spans()) if line.plain != separator
|
| 1100 |
+
)
|
| 1101 |
+
|
| 1102 |
+
if not allow_blank and text.endswith(separator):
|
| 1103 |
+
lines.pop()
|
| 1104 |
+
|
| 1105 |
+
return lines
|
| 1106 |
+
|
| 1107 |
+
def divide(self, offsets: Iterable[int]) -> Lines:
|
| 1108 |
+
"""Divide text in to a number of lines at given offsets.
|
| 1109 |
+
|
| 1110 |
+
Args:
|
| 1111 |
+
offsets (Iterable[int]): Offsets used to divide text.
|
| 1112 |
+
|
| 1113 |
+
Returns:
|
| 1114 |
+
Lines: New RichText instances between offsets.
|
| 1115 |
+
"""
|
| 1116 |
+
_offsets = list(offsets)
|
| 1117 |
+
|
| 1118 |
+
if not _offsets:
|
| 1119 |
+
return Lines([self.copy()])
|
| 1120 |
+
|
| 1121 |
+
text = self.plain
|
| 1122 |
+
text_length = len(text)
|
| 1123 |
+
divide_offsets = [0, *_offsets, text_length]
|
| 1124 |
+
line_ranges = list(zip(divide_offsets, divide_offsets[1:]))
|
| 1125 |
+
|
| 1126 |
+
style = self.style
|
| 1127 |
+
justify = self.justify
|
| 1128 |
+
overflow = self.overflow
|
| 1129 |
+
_Text = Text
|
| 1130 |
+
new_lines = Lines(
|
| 1131 |
+
_Text(
|
| 1132 |
+
text[start:end],
|
| 1133 |
+
style=style,
|
| 1134 |
+
justify=justify,
|
| 1135 |
+
overflow=overflow,
|
| 1136 |
+
)
|
| 1137 |
+
for start, end in line_ranges
|
| 1138 |
+
)
|
| 1139 |
+
if not self._spans:
|
| 1140 |
+
return new_lines
|
| 1141 |
+
|
| 1142 |
+
_line_appends = [line._spans.append for line in new_lines._lines]
|
| 1143 |
+
line_count = len(line_ranges)
|
| 1144 |
+
_Span = Span
|
| 1145 |
+
|
| 1146 |
+
for span_start, span_end, style in self._spans:
|
| 1147 |
+
lower_bound = 0
|
| 1148 |
+
upper_bound = line_count
|
| 1149 |
+
start_line_no = (lower_bound + upper_bound) // 2
|
| 1150 |
+
|
| 1151 |
+
while True:
|
| 1152 |
+
line_start, line_end = line_ranges[start_line_no]
|
| 1153 |
+
if span_start < line_start:
|
| 1154 |
+
upper_bound = start_line_no - 1
|
| 1155 |
+
elif span_start > line_end:
|
| 1156 |
+
lower_bound = start_line_no + 1
|
| 1157 |
+
else:
|
| 1158 |
+
break
|
| 1159 |
+
start_line_no = (lower_bound + upper_bound) // 2
|
| 1160 |
+
|
| 1161 |
+
if span_end < line_end:
|
| 1162 |
+
end_line_no = start_line_no
|
| 1163 |
+
else:
|
| 1164 |
+
end_line_no = lower_bound = start_line_no
|
| 1165 |
+
upper_bound = line_count
|
| 1166 |
+
|
| 1167 |
+
while True:
|
| 1168 |
+
line_start, line_end = line_ranges[end_line_no]
|
| 1169 |
+
if span_end < line_start:
|
| 1170 |
+
upper_bound = end_line_no - 1
|
| 1171 |
+
elif span_end > line_end:
|
| 1172 |
+
lower_bound = end_line_no + 1
|
| 1173 |
+
else:
|
| 1174 |
+
break
|
| 1175 |
+
end_line_no = (lower_bound + upper_bound) // 2
|
| 1176 |
+
|
| 1177 |
+
for line_no in range(start_line_no, end_line_no + 1):
|
| 1178 |
+
line_start, line_end = line_ranges[line_no]
|
| 1179 |
+
new_start = max(0, span_start - line_start)
|
| 1180 |
+
new_end = min(span_end - line_start, line_end - line_start)
|
| 1181 |
+
if new_end > new_start:
|
| 1182 |
+
_line_appends[line_no](_Span(new_start, new_end, style))
|
| 1183 |
+
|
| 1184 |
+
return new_lines
|
| 1185 |
+
|
| 1186 |
+
def right_crop(self, amount: int = 1) -> None:
|
| 1187 |
+
"""Remove a number of characters from the end of the text."""
|
| 1188 |
+
max_offset = len(self.plain) - amount
|
| 1189 |
+
_Span = Span
|
| 1190 |
+
self._spans[:] = [
|
| 1191 |
+
(
|
| 1192 |
+
span
|
| 1193 |
+
if span.end < max_offset
|
| 1194 |
+
else _Span(span.start, min(max_offset, span.end), span.style)
|
| 1195 |
+
)
|
| 1196 |
+
for span in self._spans
|
| 1197 |
+
if span.start < max_offset
|
| 1198 |
+
]
|
| 1199 |
+
self._text = [self.plain[:-amount]]
|
| 1200 |
+
self._length -= amount
|
| 1201 |
+
|
| 1202 |
+
def wrap(
|
| 1203 |
+
self,
|
| 1204 |
+
console: "Console",
|
| 1205 |
+
width: int,
|
| 1206 |
+
*,
|
| 1207 |
+
justify: Optional["JustifyMethod"] = None,
|
| 1208 |
+
overflow: Optional["OverflowMethod"] = None,
|
| 1209 |
+
tab_size: int = 8,
|
| 1210 |
+
no_wrap: Optional[bool] = None,
|
| 1211 |
+
) -> Lines:
|
| 1212 |
+
"""Word wrap the text.
|
| 1213 |
+
|
| 1214 |
+
Args:
|
| 1215 |
+
console (Console): Console instance.
|
| 1216 |
+
width (int): Number of cells available per line.
|
| 1217 |
+
justify (str, optional): Justify method: "default", "left", "center", "full", "right". Defaults to "default".
|
| 1218 |
+
overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None.
|
| 1219 |
+
tab_size (int, optional): Default tab size. Defaults to 8.
|
| 1220 |
+
no_wrap (bool, optional): Disable wrapping, Defaults to False.
|
| 1221 |
+
|
| 1222 |
+
Returns:
|
| 1223 |
+
Lines: Number of lines.
|
| 1224 |
+
"""
|
| 1225 |
+
wrap_justify = justify or self.justify or DEFAULT_JUSTIFY
|
| 1226 |
+
wrap_overflow = overflow or self.overflow or DEFAULT_OVERFLOW
|
| 1227 |
+
|
| 1228 |
+
no_wrap = pick_bool(no_wrap, self.no_wrap, False) or overflow == "ignore"
|
| 1229 |
+
|
| 1230 |
+
lines = Lines()
|
| 1231 |
+
for line in self.split(allow_blank=True):
|
| 1232 |
+
if "\t" in line:
|
| 1233 |
+
line.expand_tabs(tab_size)
|
| 1234 |
+
if no_wrap:
|
| 1235 |
+
new_lines = Lines([line])
|
| 1236 |
+
else:
|
| 1237 |
+
offsets = divide_line(str(line), width, fold=wrap_overflow == "fold")
|
| 1238 |
+
new_lines = line.divide(offsets)
|
| 1239 |
+
for line in new_lines:
|
| 1240 |
+
line.rstrip_end(width)
|
| 1241 |
+
if wrap_justify:
|
| 1242 |
+
new_lines.justify(
|
| 1243 |
+
console, width, justify=wrap_justify, overflow=wrap_overflow
|
| 1244 |
+
)
|
| 1245 |
+
for line in new_lines:
|
| 1246 |
+
line.truncate(width, overflow=wrap_overflow)
|
| 1247 |
+
lines.extend(new_lines)
|
| 1248 |
+
return lines
|
| 1249 |
+
|
| 1250 |
+
def fit(self, width: int) -> Lines:
|
| 1251 |
+
"""Fit the text in to given width by chopping in to lines.
|
| 1252 |
+
|
| 1253 |
+
Args:
|
| 1254 |
+
width (int): Maximum characters in a line.
|
| 1255 |
+
|
| 1256 |
+
Returns:
|
| 1257 |
+
Lines: Lines container.
|
| 1258 |
+
"""
|
| 1259 |
+
lines: Lines = Lines()
|
| 1260 |
+
append = lines.append
|
| 1261 |
+
for line in self.split():
|
| 1262 |
+
line.set_length(width)
|
| 1263 |
+
append(line)
|
| 1264 |
+
return lines
|
| 1265 |
+
|
| 1266 |
+
def detect_indentation(self) -> int:
|
| 1267 |
+
"""Auto-detect indentation of code.
|
| 1268 |
+
|
| 1269 |
+
Returns:
|
| 1270 |
+
int: Number of spaces used to indent code.
|
| 1271 |
+
"""
|
| 1272 |
+
|
| 1273 |
+
_indentations = {
|
| 1274 |
+
len(match.group(1))
|
| 1275 |
+
for match in re.finditer(r"^( *)(.*)$", self.plain, flags=re.MULTILINE)
|
| 1276 |
+
}
|
| 1277 |
+
|
| 1278 |
+
try:
|
| 1279 |
+
indentation = (
|
| 1280 |
+
reduce(gcd, [indent for indent in _indentations if not indent % 2]) or 1
|
| 1281 |
+
)
|
| 1282 |
+
except TypeError:
|
| 1283 |
+
indentation = 1
|
| 1284 |
+
|
| 1285 |
+
return indentation
|
| 1286 |
+
|
| 1287 |
+
def with_indent_guides(
|
| 1288 |
+
self,
|
| 1289 |
+
indent_size: Optional[int] = None,
|
| 1290 |
+
*,
|
| 1291 |
+
character: str = "│",
|
| 1292 |
+
style: StyleType = "dim green",
|
| 1293 |
+
) -> "Text":
|
| 1294 |
+
"""Adds indent guide lines to text.
|
| 1295 |
+
|
| 1296 |
+
Args:
|
| 1297 |
+
indent_size (Optional[int]): Size of indentation, or None to auto detect. Defaults to None.
|
| 1298 |
+
character (str, optional): Character to use for indentation. Defaults to "│".
|
| 1299 |
+
style (Union[Style, str], optional): Style of indent guides.
|
| 1300 |
+
|
| 1301 |
+
Returns:
|
| 1302 |
+
Text: New text with indentation guides.
|
| 1303 |
+
"""
|
| 1304 |
+
|
| 1305 |
+
_indent_size = self.detect_indentation() if indent_size is None else indent_size
|
| 1306 |
+
|
| 1307 |
+
text = self.copy()
|
| 1308 |
+
text.expand_tabs()
|
| 1309 |
+
indent_line = f"{character}{' ' * (_indent_size - 1)}"
|
| 1310 |
+
|
| 1311 |
+
re_indent = re.compile(r"^( *)(.*)$")
|
| 1312 |
+
new_lines: List[Text] = []
|
| 1313 |
+
add_line = new_lines.append
|
| 1314 |
+
blank_lines = 0
|
| 1315 |
+
for line in text.split(allow_blank=True):
|
| 1316 |
+
match = re_indent.match(line.plain)
|
| 1317 |
+
if not match or not match.group(2):
|
| 1318 |
+
blank_lines += 1
|
| 1319 |
+
continue
|
| 1320 |
+
indent = match.group(1)
|
| 1321 |
+
full_indents, remaining_space = divmod(len(indent), _indent_size)
|
| 1322 |
+
new_indent = f"{indent_line * full_indents}{' ' * remaining_space}"
|
| 1323 |
+
line.plain = new_indent + line.plain[len(new_indent) :]
|
| 1324 |
+
line.stylize(style, 0, len(new_indent))
|
| 1325 |
+
if blank_lines:
|
| 1326 |
+
new_lines.extend([Text(new_indent, style=style)] * blank_lines)
|
| 1327 |
+
blank_lines = 0
|
| 1328 |
+
add_line(line)
|
| 1329 |
+
if blank_lines:
|
| 1330 |
+
new_lines.extend([Text("", style=style)] * blank_lines)
|
| 1331 |
+
|
| 1332 |
+
new_text = text.blank_copy("\n").join(new_lines)
|
| 1333 |
+
return new_text
|
| 1334 |
+
|
| 1335 |
+
|
| 1336 |
+
if __name__ == "__main__": # pragma: no cover
|
| 1337 |
+
from pip._vendor.rich.console import Console
|
| 1338 |
+
|
| 1339 |
+
text = Text(
|
| 1340 |
+
"""\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"""
|
| 1341 |
+
)
|
| 1342 |
+
text.highlight_words(["Lorem"], "bold")
|
| 1343 |
+
text.highlight_words(["ipsum"], "italic")
|
| 1344 |
+
|
| 1345 |
+
console = Console()
|
| 1346 |
+
|
| 1347 |
+
console.rule("justify='left'")
|
| 1348 |
+
console.print(text, style="red")
|
| 1349 |
+
console.print()
|
| 1350 |
+
|
| 1351 |
+
console.rule("justify='center'")
|
| 1352 |
+
console.print(text, style="green", justify="center")
|
| 1353 |
+
console.print()
|
| 1354 |
+
|
| 1355 |
+
console.rule("justify='right'")
|
| 1356 |
+
console.print(text, style="blue", justify="right")
|
| 1357 |
+
console.print()
|
| 1358 |
+
|
| 1359 |
+
console.rule("justify='full'")
|
| 1360 |
+
console.print(text, style="magenta", justify="full")
|
| 1361 |
+
console.print()
|
env/lib/python3.13/site-packages/pip/_vendor/rich/themes.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .default_styles import DEFAULT_STYLES
|
| 2 |
+
from .theme import Theme
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
DEFAULT = Theme(DEFAULT_STYLES)
|
env/lib/python3.13/site-packages/pip/_vendor/rich/tree.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Iterator, List, Optional, Tuple
|
| 2 |
+
|
| 3 |
+
from ._loop import loop_first, loop_last
|
| 4 |
+
from .console import Console, ConsoleOptions, RenderableType, RenderResult
|
| 5 |
+
from .jupyter import JupyterMixin
|
| 6 |
+
from .measure import Measurement
|
| 7 |
+
from .segment import Segment
|
| 8 |
+
from .style import Style, StyleStack, StyleType
|
| 9 |
+
from .styled import Styled
|
| 10 |
+
|
| 11 |
+
GuideType = Tuple[str, str, str, str]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class Tree(JupyterMixin):
|
| 15 |
+
"""A renderable for a tree structure.
|
| 16 |
+
|
| 17 |
+
Attributes:
|
| 18 |
+
ASCII_GUIDES (GuideType): Guide lines used when Console.ascii_only is True.
|
| 19 |
+
TREE_GUIDES (List[GuideType, GuideType, GuideType]): Default guide lines.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
label (RenderableType): The renderable or str for the tree label.
|
| 23 |
+
style (StyleType, optional): Style of this tree. Defaults to "tree".
|
| 24 |
+
guide_style (StyleType, optional): Style of the guide lines. Defaults to "tree.line".
|
| 25 |
+
expanded (bool, optional): Also display children. Defaults to True.
|
| 26 |
+
highlight (bool, optional): Highlight renderable (if str). Defaults to False.
|
| 27 |
+
hide_root (bool, optional): Hide the root node. Defaults to False.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
ASCII_GUIDES = (" ", "| ", "+-- ", "`-- ")
|
| 31 |
+
TREE_GUIDES = [
|
| 32 |
+
(" ", "│ ", "├── ", "└── "),
|
| 33 |
+
(" ", "┃ ", "┣━━ ", "┗━━ "),
|
| 34 |
+
(" ", "║ ", "╠══ ", "╚══ "),
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
def __init__(
|
| 38 |
+
self,
|
| 39 |
+
label: RenderableType,
|
| 40 |
+
*,
|
| 41 |
+
style: StyleType = "tree",
|
| 42 |
+
guide_style: StyleType = "tree.line",
|
| 43 |
+
expanded: bool = True,
|
| 44 |
+
highlight: bool = False,
|
| 45 |
+
hide_root: bool = False,
|
| 46 |
+
) -> None:
|
| 47 |
+
self.label = label
|
| 48 |
+
self.style = style
|
| 49 |
+
self.guide_style = guide_style
|
| 50 |
+
self.children: List[Tree] = []
|
| 51 |
+
self.expanded = expanded
|
| 52 |
+
self.highlight = highlight
|
| 53 |
+
self.hide_root = hide_root
|
| 54 |
+
|
| 55 |
+
def add(
|
| 56 |
+
self,
|
| 57 |
+
label: RenderableType,
|
| 58 |
+
*,
|
| 59 |
+
style: Optional[StyleType] = None,
|
| 60 |
+
guide_style: Optional[StyleType] = None,
|
| 61 |
+
expanded: bool = True,
|
| 62 |
+
highlight: Optional[bool] = False,
|
| 63 |
+
) -> "Tree":
|
| 64 |
+
"""Add a child tree.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
label (RenderableType): The renderable or str for the tree label.
|
| 68 |
+
style (StyleType, optional): Style of this tree. Defaults to "tree".
|
| 69 |
+
guide_style (StyleType, optional): Style of the guide lines. Defaults to "tree.line".
|
| 70 |
+
expanded (bool, optional): Also display children. Defaults to True.
|
| 71 |
+
highlight (Optional[bool], optional): Highlight renderable (if str). Defaults to False.
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
Tree: A new child Tree, which may be further modified.
|
| 75 |
+
"""
|
| 76 |
+
node = Tree(
|
| 77 |
+
label,
|
| 78 |
+
style=self.style if style is None else style,
|
| 79 |
+
guide_style=self.guide_style if guide_style is None else guide_style,
|
| 80 |
+
expanded=expanded,
|
| 81 |
+
highlight=self.highlight if highlight is None else highlight,
|
| 82 |
+
)
|
| 83 |
+
self.children.append(node)
|
| 84 |
+
return node
|
| 85 |
+
|
| 86 |
+
def __rich_console__(
|
| 87 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 88 |
+
) -> "RenderResult":
|
| 89 |
+
stack: List[Iterator[Tuple[bool, Tree]]] = []
|
| 90 |
+
pop = stack.pop
|
| 91 |
+
push = stack.append
|
| 92 |
+
new_line = Segment.line()
|
| 93 |
+
|
| 94 |
+
get_style = console.get_style
|
| 95 |
+
null_style = Style.null()
|
| 96 |
+
guide_style = get_style(self.guide_style, default="") or null_style
|
| 97 |
+
SPACE, CONTINUE, FORK, END = range(4)
|
| 98 |
+
|
| 99 |
+
_Segment = Segment
|
| 100 |
+
|
| 101 |
+
def make_guide(index: int, style: Style) -> Segment:
|
| 102 |
+
"""Make a Segment for a level of the guide lines."""
|
| 103 |
+
if options.ascii_only:
|
| 104 |
+
line = self.ASCII_GUIDES[index]
|
| 105 |
+
else:
|
| 106 |
+
guide = 1 if style.bold else (2 if style.underline2 else 0)
|
| 107 |
+
line = self.TREE_GUIDES[0 if options.legacy_windows else guide][index]
|
| 108 |
+
return _Segment(line, style)
|
| 109 |
+
|
| 110 |
+
levels: List[Segment] = [make_guide(CONTINUE, guide_style)]
|
| 111 |
+
push(iter(loop_last([self])))
|
| 112 |
+
|
| 113 |
+
guide_style_stack = StyleStack(get_style(self.guide_style))
|
| 114 |
+
style_stack = StyleStack(get_style(self.style))
|
| 115 |
+
remove_guide_styles = Style(bold=False, underline2=False)
|
| 116 |
+
|
| 117 |
+
depth = 0
|
| 118 |
+
|
| 119 |
+
while stack:
|
| 120 |
+
stack_node = pop()
|
| 121 |
+
try:
|
| 122 |
+
last, node = next(stack_node)
|
| 123 |
+
except StopIteration:
|
| 124 |
+
levels.pop()
|
| 125 |
+
if levels:
|
| 126 |
+
guide_style = levels[-1].style or null_style
|
| 127 |
+
levels[-1] = make_guide(FORK, guide_style)
|
| 128 |
+
guide_style_stack.pop()
|
| 129 |
+
style_stack.pop()
|
| 130 |
+
continue
|
| 131 |
+
push(stack_node)
|
| 132 |
+
if last:
|
| 133 |
+
levels[-1] = make_guide(END, levels[-1].style or null_style)
|
| 134 |
+
|
| 135 |
+
guide_style = guide_style_stack.current + get_style(node.guide_style)
|
| 136 |
+
style = style_stack.current + get_style(node.style)
|
| 137 |
+
prefix = levels[(2 if self.hide_root else 1) :]
|
| 138 |
+
renderable_lines = console.render_lines(
|
| 139 |
+
Styled(node.label, style),
|
| 140 |
+
options.update(
|
| 141 |
+
width=options.max_width
|
| 142 |
+
- sum(level.cell_length for level in prefix),
|
| 143 |
+
highlight=self.highlight,
|
| 144 |
+
height=None,
|
| 145 |
+
),
|
| 146 |
+
pad=options.justify is not None,
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
if not (depth == 0 and self.hide_root):
|
| 150 |
+
for first, line in loop_first(renderable_lines):
|
| 151 |
+
if prefix:
|
| 152 |
+
yield from _Segment.apply_style(
|
| 153 |
+
prefix,
|
| 154 |
+
style.background_style,
|
| 155 |
+
post_style=remove_guide_styles,
|
| 156 |
+
)
|
| 157 |
+
yield from line
|
| 158 |
+
yield new_line
|
| 159 |
+
if first and prefix:
|
| 160 |
+
prefix[-1] = make_guide(
|
| 161 |
+
SPACE if last else CONTINUE, prefix[-1].style or null_style
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
if node.expanded and node.children:
|
| 165 |
+
levels[-1] = make_guide(
|
| 166 |
+
SPACE if last else CONTINUE, levels[-1].style or null_style
|
| 167 |
+
)
|
| 168 |
+
levels.append(
|
| 169 |
+
make_guide(END if len(node.children) == 1 else FORK, guide_style)
|
| 170 |
+
)
|
| 171 |
+
style_stack.push(get_style(node.style))
|
| 172 |
+
guide_style_stack.push(get_style(node.guide_style))
|
| 173 |
+
push(iter(loop_last(node.children)))
|
| 174 |
+
depth += 1
|
| 175 |
+
|
| 176 |
+
def __rich_measure__(
|
| 177 |
+
self, console: "Console", options: "ConsoleOptions"
|
| 178 |
+
) -> "Measurement":
|
| 179 |
+
stack: List[Iterator[Tree]] = [iter([self])]
|
| 180 |
+
pop = stack.pop
|
| 181 |
+
push = stack.append
|
| 182 |
+
minimum = 0
|
| 183 |
+
maximum = 0
|
| 184 |
+
measure = Measurement.get
|
| 185 |
+
level = 0
|
| 186 |
+
while stack:
|
| 187 |
+
iter_tree = pop()
|
| 188 |
+
try:
|
| 189 |
+
tree = next(iter_tree)
|
| 190 |
+
except StopIteration:
|
| 191 |
+
level -= 1
|
| 192 |
+
continue
|
| 193 |
+
push(iter_tree)
|
| 194 |
+
min_measure, max_measure = measure(console, options, tree.label)
|
| 195 |
+
indent = level * 4
|
| 196 |
+
minimum = max(min_measure + indent, minimum)
|
| 197 |
+
maximum = max(max_measure + indent, maximum)
|
| 198 |
+
if tree.expanded and tree.children:
|
| 199 |
+
push(iter(tree.children))
|
| 200 |
+
level += 1
|
| 201 |
+
return Measurement(minimum, maximum)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
if __name__ == "__main__": # pragma: no cover
|
| 205 |
+
from pip._vendor.rich.console import Group
|
| 206 |
+
from pip._vendor.rich.markdown import Markdown
|
| 207 |
+
from pip._vendor.rich.panel import Panel
|
| 208 |
+
from pip._vendor.rich.syntax import Syntax
|
| 209 |
+
from pip._vendor.rich.table import Table
|
| 210 |
+
|
| 211 |
+
table = Table(row_styles=["", "dim"])
|
| 212 |
+
|
| 213 |
+
table.add_column("Released", style="cyan", no_wrap=True)
|
| 214 |
+
table.add_column("Title", style="magenta")
|
| 215 |
+
table.add_column("Box Office", justify="right", style="green")
|
| 216 |
+
|
| 217 |
+
table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
|
| 218 |
+
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
|
| 219 |
+
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
|
| 220 |
+
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
|
| 221 |
+
|
| 222 |
+
code = """\
|
| 223 |
+
class Segment(NamedTuple):
|
| 224 |
+
text: str = ""
|
| 225 |
+
style: Optional[Style] = None
|
| 226 |
+
is_control: bool = False
|
| 227 |
+
"""
|
| 228 |
+
syntax = Syntax(code, "python", theme="monokai", line_numbers=True)
|
| 229 |
+
|
| 230 |
+
markdown = Markdown(
|
| 231 |
+
"""\
|
| 232 |
+
### example.md
|
| 233 |
+
> Hello, World!
|
| 234 |
+
>
|
| 235 |
+
> Markdown _all_ the things
|
| 236 |
+
"""
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
root = Tree("🌲 [b green]Rich Tree", highlight=True, hide_root=True)
|
| 240 |
+
|
| 241 |
+
node = root.add(":file_folder: Renderables", guide_style="red")
|
| 242 |
+
simple_node = node.add(":file_folder: [bold yellow]Atomic", guide_style="uu green")
|
| 243 |
+
simple_node.add(Group("📄 Syntax", syntax))
|
| 244 |
+
simple_node.add(Group("📄 Markdown", Panel(markdown, border_style="green")))
|
| 245 |
+
|
| 246 |
+
containers_node = node.add(
|
| 247 |
+
":file_folder: [bold magenta]Containers", guide_style="bold magenta"
|
| 248 |
+
)
|
| 249 |
+
containers_node.expanded = True
|
| 250 |
+
panel = Panel.fit("Just a panel", border_style="red")
|
| 251 |
+
containers_node.add(Group("📄 Panels", panel))
|
| 252 |
+
|
| 253 |
+
containers_node.add(Group("📄 [b magenta]Table", table))
|
| 254 |
+
|
| 255 |
+
console = Console()
|
| 256 |
+
|
| 257 |
+
console.print(root)
|