Skip to content

Commit

Permalink
fix: align line numbers and tweak dbg formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 22, 2021
1 parent adfc41f commit 715ee23
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
67 changes: 38 additions & 29 deletions beet/contrib/dbg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"DbgOptions",
"DbgRenderer",
"DbgExtension",
"get_padding",
]


Expand All @@ -23,26 +24,29 @@

class DbgOptions(BaseModel):
command: str = "tellraw @a {payload}"
preview_line_length: int = 45

preview_digit_width: int = 6
preview_line_limit: int = 45
preview_padding: int = 2

payload: List[TextComponent] = [
{
"text": "",
"hoverEvent": {
"action": "show_text",
"contents": [
"",
{"text": "Type: {{ mode | title }}\n", "color": "gray"},
{"text": "Path: {{ render_path }}\n\n", "color": "gray"},
{"text": "{{ render_path }} ", "color": "aqua"},
{"text": "({{ mode }})\n\n", "color": "dark_aqua"},
"{{ preview }}",
],
},
},
{"text": "[{{ project_id }}]: ", "color": "gray"},
{
"text": "< {{ name }} ",
"text": "{{ target }} for {{ name }} = ",
"color": "gold",
"extra": ["{{ accessor }}", " >"],
"extra": ["{{ accessor }}"],
},
]

Expand All @@ -51,6 +55,17 @@ def beet_default(ctx: Context):
ctx.template.env.add_extension(DbgExtension)


def get_padding(pixels: int) -> TextComponent:
"""Generate a sequence of bold and normal spaces matching the given number of pixels."""
if pixels < 12 and pixels not in [4, 5, 8, 9, 10]:
raise ValueError(f"Invalid number of pixels {pixels}.")

regular, bold = divmod(pixels, 4)
regular -= bold

return [{"text": " " * regular}, {"text": " " * bold, "bold": True}]


@dataclass
class DbgRenderer:
"""Class responsible for formatting the json text of a dbg statement."""
Expand All @@ -75,39 +90,33 @@ def render_preview(self, path: str, lineno: int) -> TextComponent:
function = self.ctx.data.functions[path]
lines = function.text.splitlines()

color = cycle(["#dddddd", "gray"])

preview_start = max(lineno - 1 - self.opts.preview_padding, 0)
preview = lines[preview_start : lineno + self.opts.preview_padding]

truncated_lines = [
line
if len(line) < self.opts.preview_line_length
else line[: self.opts.preview_line_length] + "..."
for line in preview
]

numbers = [
str(i + 1) for i in range(preview_start, preview_start + len(preview))
]
number_width = max(len(n) for n in numbers)

preview_lines = [
{
"text": "",
"extra": [
{
"text": n.rjust(number_width),
"color": "red" if n == str(lineno) else "dark_red",
},
{"text": " | ", "color": "dark_gray"},
{"text": f"{line}\n", "color": next(color)},
],
}
for n, line in zip(numbers, truncated_lines)
]
output: List[TextComponent] = [""]

for number, line, color in zip(numbers, preview, cycle(["#dddddd", "gray"])):
if len(line) > self.opts.preview_line_limit:
line = line[: self.opts.preview_line_limit - 4] + "..."

padding = number_width - len(number)
output.extend(get_padding(padding * self.opts.preview_digit_width + 4))
output.append(
{
"text": number,
"color": "red" if int(number) == lineno else "dark_red",
}
)

output.append({"text": " | ", "color": "dark_gray"})
output.append({"text": f"{line}\n", "color": color})

return {"text": "", "extra": preview_lines}
return {"text": "", "extra": output}

def render(self, mode: str, name: str, target: str, lineno: int) -> str:
"""Return the json text as a string."""
Expand Down
14 changes: 7 additions & 7 deletions examples/load_dbg/src/data/demo/functions/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# My cool function

say hello

#!set foo = generate_objective("foo")

scoreboard players set @p __foo__ 123
say 1
execute if score $size rx.temp matches 1 run data modify storage rx:io playerdb.player set from storage rx:global playerdb.players[{selected: 1b}]
execute if score $size rx.temp matches 1 run data remove storage rx:io playerdb.player.bits
say Lorem 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.
#!dbg score "@p", foo
say hello
execute if score $size rx.temp matches 1 run data modify storage rx:io playerdb.player set from storage rx:global playerdb.players[{selected: 1b}]
execute if score $size rx.temp matches 1 run data remove storage rx:io playerdb.player.bits
say 1
say 42
scoreboard players reset @p __foo__
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
scoreboard players set @p 5wyugd7r8mxoc 123
say 1
execute if score $size rx.temp matches 1 run data modify storage rx:io playerdb.player set from storage rx:global playerdb.players[{selected: 1b}]
execute if score $size rx.temp matches 1 run data remove storage rx:io playerdb.player.bits
tellraw @a [{"text": "", "hoverEvent": {"action": "show_text", "contents": ["", {"text": "Type: Score\n", "color": "gray"}, {"text": "Path: demo:foo\n\n", "color": "gray"}, "",{"text": "", "extra": [{"text": "", "extra": [{"text": "5", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "execute if score $size rx.temp matches 1 run ...\n", "color": "#dddddd"}]}, {"text": "", "extra": [{"text": "6", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "execute if score $size rx.temp matches 1 run ...\n", "color": "gray"}]}, {"text": "", "extra": [{"text": "7", "color": "red"}, {"text": " | ", "color": "dark_gray"}, {"text": "#!dbg score \"@p\", foo\n", "color": "#dddddd"}]}, {"text": "", "extra": [{"text": "8", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "say hello\n", "color": "gray"}]}, {"text": "", "extra": [{"text": "9", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "execute if score $size rx.temp matches 1 run ...\n", "color": "#dddddd"}]}]},""]}}, {"text": "[load_dbg]: ", "color": "gray"}, {"text": "< @p ", "color": "gold", "extra": ["",{"score": {"name": "@p", "objective": "5wyugd7r8mxoc"}},"", " >"]}]
# My cool function

say hello
execute if score $size rx.temp matches 1 run data modify storage rx:io playerdb.player set from storage rx:global playerdb.players[{selected: 1b}]
execute if score $size rx.temp matches 1 run data remove storage rx:io playerdb.player.bits
say 1

scoreboard players set @p 5wyugd7r8mxoc 123
say Lorem 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.
tellraw @a [{"text": "", "hoverEvent": {"action": "show_text", "contents": ["", {"text": "demo:foo ", "color": "aqua"}, {"text": "(score)\n\n", "color": "dark_aqua"}, "",{"text": "", "extra": ["", {"text": ""}, {"text": " ", "bold": true}, {"text": "7", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "scoreboard players set @p __foo__ 123\n", "color": "#dddddd"}, {"text": ""}, {"text": " ", "bold": true}, {"text": "8", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "say Lorem ipsum dolor sit amet, consectet...\n", "color": "gray"}, {"text": ""}, {"text": " ", "bold": true}, {"text": "9", "color": "red"}, {"text": " | ", "color": "dark_gray"}, {"text": "#!dbg score \"@p\", foo\n", "color": "#dddddd"}, {"text": " "}, {"text": "", "bold": true}, {"text": "10", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "say 42\n", "color": "gray"}, {"text": " "}, {"text": "", "bold": true}, {"text": "11", "color": "dark_red"}, {"text": " | ", "color": "dark_gray"}, {"text": "scoreboard players reset @p __foo__\n", "color": "#dddddd"}]},""]}}, {"text": "[load_dbg]: ", "color": "gray"}, {"text": "5wyugd7r8mxoc for @p = ", "color": "gold", "extra": ["",{"score": {"name": "@p", "objective": "5wyugd7r8mxoc"}},""]}]
say 42
scoreboard players reset @p 5wyugd7r8mxoc

0 comments on commit 715ee23

Please sign in to comment.