Skip to content

Commit

Permalink
add visual display
Browse files Browse the repository at this point in the history
```
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌───────┐
│         | │         | │         | │       |
│         | │         | │         | │       |
│         | │         | │         | │       |
│ version | │ version | │ version | │ blink |
│         | │         | │         | │       |
│         | │         | │         | │       |
│         | │         | │         | │       |
│         | │         | │         | │       |
└─────────┘ └─────────┘ └─────────┘ └───────┘
┌───────────────────────────────────────────┐
│ Kernel                                    |
└───────────────────────────────────────────┘
```
  • Loading branch information
bradjc committed Feb 17, 2024
1 parent 5d75416 commit 9ded71c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
85 changes: 85 additions & 0 deletions tockloader/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ def app_bracket(width, left, right):

self.out += out

def show_board_visual(self, apps):
def horizontal_add(existing, new):
existing_lines = existing.split("\n")
new_lines = existing.split("\n")
out = ""
for i, l in enumerate(existing_lines):
out += "{} {}\n".format(l, new_lines[i])
return out

out = ""
for i, app in enumerate(apps):
name = app.get_name()
box_width = len(name) + 4
box = helpers.text_in_box(name, box_width, 10)
if i == 0:
out = box
else:
out = horizontal_add(out, box)

self.out = out

def list_attributes(self, attributes):
if self.show_headers:
self.out += "Attributes:\n"
Expand All @@ -156,6 +177,70 @@ def kernel_attributes(self, kern_attrs):
self.out += kern_attrs.info()


class VisualDisplay(Display):
"""
Format output as an ASCII art string.
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌───────┐
│ | │ | │ | │ |
│ | │ | │ | │ |
│ | │ | │ | │ |
│ version | │ version | │ version | │ blink |
│ | │ | │ | │ |
│ | │ | │ | │ |
│ | │ | │ | │ |
│ | │ | │ | │ |
└─────────┘ └─────────┘ └─────────┘ └───────┘
┌───────────────────────────────────────────┐
│ Kernel |
└───────────────────────────────────────────┘
"""

def __init__(self):
self.out = ""

def list_apps(self, apps, verbose, quiet):
def horizontal_add(existing, new):
existing_lines = existing.split("\n")
new_lines = new.split("\n")
out = ""
for i, l in enumerate(existing_lines):
if len(l) > 0:
out += "{} {}\n".format(l, new_lines[i])
return out[:-1]

out = ""
for i, app in enumerate(apps):
name = app.get_name()
box_width = len(name) + 4
box = helpers.text_in_box(name, box_width, 10) + "\n"
if i == 0:
out = box
else:
out = horizontal_add(out, box)

self.out += out

def list_attributes(self, attributes):
pass

def bootloader_version(self, version):
pass

def kernel_attributes(self, kern_attrs):
width = self._width()
out = "\n"
out += helpers.text_in_box("Tock Kernel", width)
self.out += out

def _width(self):
width = 0
for l in self.out.split("\n"):
if len(l) > width:
width = len(l)
return width


class JSONDisplay(Display):
"""
Format output as JSON.
Expand Down
11 changes: 10 additions & 1 deletion tockloader/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def plural(value):
return "s"


def text_in_box(string, box_width):
def text_in_box(string, box_width, box_height=3):
"""
Return a string like:
```
Expand All @@ -189,8 +189,17 @@ def text_in_box(string, box_width):
truncated_str = (
(string[: string_len - 3] + "...") if len(string) > string_len else string
)

vertical_padding = max(box_height - 3, 0)
vertical_padding_top = vertical_padding // 2
vertical_padding_bottom = vertical_padding - vertical_padding_top

out = "┌{}┐\n".format("─" * (box_width - 2))
for i in range(0, vertical_padding_top):
out += "│ {} |\n".format(" " * string_len)
out += "│ {} |\n".format(truncated_str.ljust(string_len))
for i in range(0, vertical_padding_bottom):
out += "│ {} |\n".format(" " * string_len)
out += "└{}┘".format("─" * (box_width - 2))
return out

Expand Down
2 changes: 1 addition & 1 deletion tockloader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ def main():
parent_format.add_argument(
"--output-format",
help="Address where apps are located",
choices=["terminal", "json"],
choices=["terminal", "visual", "json"],
default="terminal",
)

Expand Down
2 changes: 2 additions & 0 deletions tockloader/tockloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ def info(self):
with self._start_communication_with_board():
if self.args.output_format == "json":
displayer = display.JSONDisplay()
elif self.args.output_format == "visual":
displayer = display.VisualDisplay()
else:
displayer = display.HumanReadableDisplay(show_headers=True)

Expand Down

0 comments on commit 9ded71c

Please sign in to comment.