Skip to content

Commit 2386adc

Browse files
committed
feat: Add coordinate conversion helper function
1 parent 07b097d commit 2386adc

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

easymotion.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ def restore_terminal():
216216
sys.stdout.flush()
217217

218218

219+
def to_terminal_coords(y: int, x: int) -> tuple[int, int]:
220+
"""
221+
Convert 0-based coordinate system to terminal's 1-based coordinate system
222+
"""
223+
return y + 1, x + 1
224+
219225
def getch():
220226
"""Get a single character from terminal"""
221227
fd = sys.stdin.fileno()
@@ -349,20 +355,20 @@ def draw_all_panes(panes, max_x, padding_cache, terminal_height):
349355
if padding_size not in padding_cache:
350356
padding_cache[padding_size] = ' ' * padding_size
351357
line = line + padding_cache[padding_size]
352-
sys.stdout.write(
353-
f'{ESC}[{pane.start_y + y + 1};{pane.start_x + 1}H{line[:pane.width]}')
358+
term_y, term_x = to_terminal_coords(pane.start_y + y, pane.start_x)
359+
sys.stdout.write(f'{ESC}[{term_y};{term_x}H{line[:pane.width]}')
354360

355361
# draw vertical borders
356362
if pane.start_x + pane.width < max_x:
357363
for y in range(pane.start_y, pane.start_y + visible_height):
358-
sys.stdout.write(
359-
f'{ESC}[{y + 1};{pane.start_x + pane.width}H{DIM}{VERTICAL_BORDER}{RESET}')
364+
term_y, term_x = to_terminal_coords(y, pane.start_x + pane.width)
365+
sys.stdout.write(f'{ESC}[{term_y};{term_x}H{DIM}{VERTICAL_BORDER}{RESET}')
360366

361367
# draw horizontal borders for non-last pane
362368
end_y = pane.start_y + visible_height
363369
if end_y < terminal_height and pane != sorted_panes[-1]:
364-
sys.stdout.write(f'{ESC}[{
365-
end_y + 1};{pane.start_x + 1}H{DIM}{HORIZONTAL_BORDER * pane.width}{RESET}')
370+
term_y, term_x = to_terminal_coords(end_y, pane.start_x)
371+
sys.stdout.write(f'{ESC}[{term_y};{term_x}H{DIM}{HORIZONTAL_BORDER * pane.width}{RESET}')
366372

367373
sys.stdout.flush()
368374

@@ -396,12 +402,12 @@ def draw_all_hints(panes, terminal_height):
396402
y = pane.start_y + line_num
397403
x = pane.start_x + col
398404
if (y < min(pane.start_y + pane.height, terminal_height) and x + get_char_width(char) <= pane.start_x + pane.width):
399-
sys.stdout.write(
400-
f'{ESC}[{y + 1};{x + 1}H{RED_FG}{hint[0]}{RESET}')
405+
term_y, term_x = to_terminal_coords(y, x)
406+
sys.stdout.write(f'{ESC}[{term_y};{term_x}H{RED_FG}{hint[0]}{RESET}')
401407
char_width = get_char_width(char)
402408
if x + char_width < pane.start_x + pane.width:
403-
sys.stdout.write(
404-
f'{ESC}[{y + 1};{x + char_width + 1}H{GREEN_FG}{hint[1]}{RESET}')
409+
term_y, term_x = to_terminal_coords(y, x + char_width)
410+
sys.stdout.write(f'{ESC}[{term_y};{term_x}H{GREEN_FG}{hint[1]}{RESET}')
405411
sys.stdout.flush()
406412

407413

0 commit comments

Comments
 (0)