indent 0.1.8__py3-none-any.whl → 0.1.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of indent might be problematic. Click here for more details.

@@ -1,246 +0,0 @@
1
- import logging
2
- import os
3
- import select
4
- import sys
5
-
6
- from colour import Color
7
-
8
- from exponent.utils.colors import (
9
- adjust_color_for_contrast,
10
- blend_colors_srgb,
11
- color_distance,
12
- )
13
-
14
- logger = logging.getLogger(__name__)
15
-
16
-
17
- DEFAULT_TERM_PALETTE = [
18
- # Windows 10 Console default palette.
19
- # The least bad default of them all ;)
20
- # Used as a fallback when terminal doesn't report its palette,
21
- # which only happens on older or feature-poor terminals, i.e. very rarely.
22
- Color("#0c0c0c"),
23
- Color("#c50f1f"),
24
- Color("#13a10e"),
25
- Color("#c19c00"),
26
- Color("#0037da"),
27
- Color("#881798"),
28
- Color("#3a96dd"),
29
- Color("#cccccc"),
30
- ]
31
-
32
-
33
- # As the only popular "modern" terminal emulator, Terminal.app doesn't support true color
34
- # so disable true color output if we're running in it.
35
- TRUE_COLOR = os.getenv("TERM_PROGRAM") != "Apple_Terminal"
36
-
37
-
38
- class Theme:
39
- term_fg: Color
40
- term_bg: Color
41
- red: Color
42
- green: Color
43
- blue: Color
44
- exponent_green: Color
45
- hl_theme_name: str
46
- dimmed_text_fg: Color
47
- block_header_bg: Color
48
- block_body_bg: Color
49
- block_footer_fg: Color
50
- block_footer_bg: Color
51
- statusbar_default_fg: Color
52
- statusbar_autorun_all: Color
53
- statusbar_autorun_ro: Color
54
- statusbar_thinking_on: Color
55
- thinking_spinner_fg: Color
56
-
57
- def __init__(
58
- self, term_fg: Color, term_bg: Color, term_palette: list[Color | None]
59
- ):
60
- self.term_fg = term_bg
61
- self.term_bg = term_bg
62
- self.red = adjust_color_for_contrast(
63
- term_bg, term_palette[1] or DEFAULT_TERM_PALETTE[1]
64
- )
65
- self.green = adjust_color_for_contrast(
66
- term_bg, term_palette[2] or DEFAULT_TERM_PALETTE[2]
67
- )
68
- self.blue = adjust_color_for_contrast(
69
- term_bg, term_palette[4] or DEFAULT_TERM_PALETTE[4]
70
- )
71
- self.exponent_green = adjust_color_for_contrast(
72
- term_bg,
73
- Color("#03bd89"), # green used in Exponent logo
74
- )
75
- dark_mode = term_bg.luminance < 0.5
76
- self.hl_theme_name = "ansi_dark" if dark_mode else "ansi_light"
77
- direction = 1 if dark_mode else -1
78
- (h, s, l) = term_bg.hsl # noqa: E741
79
- block_target = Color(hsl=(h, s, min(1.0, l + 0.005 * direction)))
80
- self.block_header_bg = adjust_color_for_contrast(term_bg, block_target, 1.2)
81
- self.block_body_bg = adjust_color_for_contrast(term_bg, block_target, 1.1)
82
- self.block_footer_bg = adjust_color_for_contrast(term_bg, block_target, 1.05)
83
- self.block_footer_fg = blend_colors_srgb(term_fg, self.block_footer_bg, 0.4)
84
- self.dimmed_text_fg = adjust_color_for_contrast(
85
- term_bg, blend_colors_srgb(term_fg, term_bg, 0.5)
86
- )
87
- self.statusbar_default_fg = self.dimmed_text_fg
88
- self.statusbar_autorun_all = self.red
89
- self.statusbar_autorun_ro = self.green
90
- self.statusbar_thinking_on = self.blue
91
- self.thinking_spinner_fg = adjust_color_for_contrast(
92
- term_bg, Color("#968ce6")
93
- ) # nice purple
94
-
95
-
96
- def get_term_colors(
97
- use_default_colors: bool,
98
- ) -> tuple[Color, Color, list[Color | None]]:
99
- from exponent.commands.shell_commands import POSIX_TERMINAL, RawMode
100
-
101
- fg = DEFAULT_TERM_PALETTE[7]
102
- bg = DEFAULT_TERM_PALETTE[0]
103
- palette: list[Color | None] = [None, None, None, None, None, None, None, None]
104
-
105
- # Not supported on Windows or when stdin is not a TTY
106
- if use_default_colors or not POSIX_TERMINAL or not sys.stdin.isatty():
107
- return (fg, bg, palette)
108
-
109
- try:
110
- with RawMode(sys.stdin.fileno()):
111
- stdin_fd = sys.stdin.fileno()
112
- stdout_fd = sys.stdout.fileno()
113
-
114
- # Try to write the ANSI escape sequences
115
- if stdout_fd in select.select([], [stdout_fd], [], 1)[1]:
116
- os.write(
117
- stdout_fd,
118
- b"\x1b]10;?\x07\x1b]11;?\x07\x1b]4;0;?\x07\x1b]4;1;?\x07\x1b]4;2;?\x07\x1b]4;3;?\x07\x1b]4;4;?\x07\x1b]4;5;?\x07\x1b]4;6;?\x07\x1b]4;7;?\x07\x1b[c",
119
- )
120
- else:
121
- return (fg, bg, palette) # Can't write to stdout
122
-
123
- got_da_response = False
124
- timeout = 0.5 # Short timeout to avoid hanging
125
-
126
- # Read terminal responses until we get DA response or timeout
127
- while not got_da_response:
128
- if stdin_fd in select.select([stdin_fd], [], [], timeout)[0]:
129
- reply = os.read(stdin_fd, 1024)
130
- seqs = reply.split(b"\x1b")
131
-
132
- for seq in seqs:
133
- if seq.startswith(b"]10;rgb:"):
134
- [r, g, b] = seq[8:].decode().split("/")
135
- fg = Color(f"#{r[0:2]}{g[0:2]}{b[0:2]}")
136
- elif seq.startswith(b"]11;rgb:"):
137
- [r, g, b] = seq[8:].decode().split("/")
138
- bg = Color(f"#{r[0:2]}{g[0:2]}{b[0:2]}")
139
- elif seq.startswith(b"]4;"):
140
- text = seq.decode()
141
- idx = int(text[3])
142
- [r, g, b] = text[9:].split("/")
143
- palette[idx] = Color(f"#{r[0:2]}{g[0:2]}{b[0:2]}")
144
- elif seq.endswith(b"c"):
145
- got_da_response = True
146
- else:
147
- # Timeout, terminal didn't respond
148
- break
149
- except Exception:
150
- logger.debug("Error getting term colors", exc_info=True)
151
- # Any exception at all, just use default colors
152
- pass
153
-
154
- return (fg, bg, palette)
155
-
156
-
157
- def color_component_to_8bit_cube(value: int) -> tuple[int, int]:
158
- # Based on https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
159
-
160
- if value < 48:
161
- value = 0
162
- idx = 0
163
- elif value < 115:
164
- value = 0x5F
165
- idx = 1
166
- elif value < 155:
167
- value = 0x87
168
- idx = 2
169
- elif value < 195:
170
- value = 0xAF
171
- idx = 3
172
- elif value < 235:
173
- value = 0xD7
174
- idx = 4
175
- else:
176
- value = 0xFF
177
- idx = 5
178
-
179
- return (value, idx)
180
-
181
-
182
- def closest_8bit_color_index(c: Color) -> int:
183
- (quantized_red, red_idx) = color_component_to_8bit_cube(round(c.red * 255))
184
- (quantized_green, green_idx) = color_component_to_8bit_cube(round(c.green * 255))
185
- (quantized_blue, blue_idx) = color_component_to_8bit_cube(round(c.blue * 255))
186
- quantized = Color(
187
- rgb=(quantized_red / 255, quantized_green / 255, quantized_blue / 255)
188
- )
189
- quantized_error = color_distance(c, quantized)
190
-
191
- gray = Color(hsl=(c.hue, 0, c.luminance))
192
- gray_idx = round(max(gray.red * 255 - 8, 0) / 10)
193
- quantized_gray_value = 8 + 10 * gray_idx
194
- quantized_gray = Color(
195
- rgb=(
196
- quantized_gray_value / 255,
197
- quantized_gray_value / 255,
198
- quantized_gray_value / 255,
199
- )
200
- )
201
- quantized_gray_error = color_distance(c, quantized_gray)
202
-
203
- if quantized_error < quantized_gray_error:
204
- # 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
205
- idx = 16 + 36 * red_idx + 6 * green_idx + blue_idx
206
- else:
207
- idx = 232 + gray_idx
208
-
209
- return idx
210
-
211
-
212
- def fg_color_seq(c: int | Color) -> str:
213
- if isinstance(c, int):
214
- return f"\x1b[{30 + c}m"
215
- elif TRUE_COLOR:
216
- (r, g, b) = c.rgb
217
- r = round(r * 255)
218
- g = round(g * 255)
219
- b = round(b * 255)
220
- return f"\x1b[38;2;{r};{g};{b}m"
221
- else:
222
- idx = closest_8bit_color_index(c)
223
- return f"\x1b[38;5;{idx}m"
224
-
225
-
226
- def default_fg_color_seq() -> str:
227
- return "\x1b[39m"
228
-
229
-
230
- def bg_color_seq(c: int | Color) -> str:
231
- if isinstance(c, int):
232
- return f"\x1b[{40 + c}m"
233
- elif TRUE_COLOR:
234
- (r, g, b) = c.rgb
235
- r = round(r * 255)
236
- g = round(g * 255)
237
- b = round(b * 255)
238
- return f"\x1b[48;2;{r};{g};{b}m"
239
- else:
240
- idx = closest_8bit_color_index(c)
241
- return f"\x1b[48;5;{idx}m"
242
-
243
-
244
- def get_theme(use_default_colors: bool) -> Theme:
245
- (fg, bg, palette) = get_term_colors(use_default_colors)
246
- return Theme(fg, bg, palette)