sync: в процессе разработки TUI

This commit is contained in:
ElectronixTM
2025-03-31 22:03:49 +03:00
parent 942f3c73fd
commit 13f244d118

View File

@ -2,9 +2,11 @@ from dpdebugger import Debugger
from dataclasses import dataclass
from rich import print as rprint
from rich.layout import Layout
from rich.live import Live
from rich.prompt import Prompt
from rich.panel import Panel
from rich.text import Text
from rich.console import Console
import math
@dataclass
class SourceLines:
@ -27,19 +29,24 @@ def _get_source_lines(
Собирает выборку из строк исходного кода так, чтобы она
подходила по количесту строчек
"""
# номер строки, с которого начнется отрисовка (НЕ ИНДЕКС)
start = max(srcline - max_height//2, 1)
# сколько строк, находится над "серединной"
upper_length = srcline - start
# номер строки, на которой закругляемся
end = min(
srcline + (max_height - upper_length - 1),
len(lines)
)
if srcline > len(lines):
raise IndexError(f"Cant take line number {srcline} from passed lines, "
"index is out of range")
start_bound = srcline - math.floor(max_height//2)
end_bound = srcline + math.ceil(max_height//2)
lines_len = len(lines)
if start_bound < 1:
end_bound += abs(start_bound) + 1
if end_bound > lines_len:
start_bound -= end_bound - lines_len
start_bound = max(start_bound, 1)
end_bound = min(end_bound, lines_len)
return SourceLines(
active=srcline,
begining_number=start,
lines=lines[start-1:end].copy()
begining_number=start_bound,
lines=list(lines[start_bound-1:end_bound])
)
def _source_lines_to_rich(srclines: SourceLines) -> RichedSourceLines:
@ -53,21 +60,50 @@ def _source_lines_to_rich(srclines: SourceLines) -> RichedSourceLines:
active_line.stylize('bold red')
return riched_lines
def _join_riched_lines(rl: RichedSourceLines) -> Text:
return Text("\n").join(rl.lines)
def _generate_source_lines(
debugger: Debugger,
srcfile: str,
size: int
) -> Panel:
sl = _get_source_lines(
lines=srcfile.split('\n')[:-1],
max_height=size,
srcline=debugger.get_current_source_line_number()
)
rl = _source_lines_to_rich(sl)
full = _join_riched_lines(rl)
return Panel(full)
def main(debugger: Debugger, srcfile: str):
SRC_WIN_HEIGHT = 10
layout = Layout()
layout.split_column(
Layout(name="source"),
Layout(
_generate_source_lines(debugger, srcfile, SRC_WIN_HEIGHT),
size=SRC_WIN_HEIGHT,
name="source"),
Layout(name="terminal")
)
layout["source"].update(_generate_source_lines(
debugger=debugger,
srcfile=srcfile,
size=SRC_WIN_HEIGHT
))
print()
rprint(layout)
if __name__ == "__main__":
console = Console()
with open("test.dasm", 'r') as f:
lines = f.read().split('\n')
srclines = _get_source_lines(20, lines, 7)
lines = f.read().split('\n')[:-1]
srclines = _get_source_lines(22, lines, 7)
riched = _source_lines_to_rich(srclines)
print(f"focus: {srclines.active}; start: {srclines.begining_number}")
print("_"*20)
console.print(*riched.lines, sep="\n")
# rprint(*riched.lines, sep="\n")
rprint(_join_riched_lines(riched))