Skip to content

Commit

Permalink
feat: enhance UI and navigation
Browse files Browse the repository at this point in the history
- Added HEIGHT import from badger2040 for dynamic UI scaling.
- Changed NAVIGATION format from string to list for easier management.
- Introduced truncatestring function to truncate text dynamically based on width.
- Updated draw_view to handle columns and truncate text if it exceeds column width.
- Enhanced navigation bar display by iterating NAVIGATION list.
- Removed "failures_only" feature for cleaner navigation and simplified data fetch logic.
- Improved line formatting in process_workflow_runs by storing parts in a list.
- Cleaned up deprecated "failures_only" code for better readability.
  • Loading branch information
dawidrylko committed Jul 10, 2024
1 parent 59e63eb commit 61c1ef0
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions badger_os/examples/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import badger_os
import badger2040
import time
from badger2040 import WIDTH
from badger2040 import WIDTH, HEIGHT
import urequests as requests
from GITHUB_CONFIG import TOKEN, REPOSITORIES

REQUEST_PER_PAGE = 5
NAVIGATION = "(A) prev | (B) only failures | (C) next | (UP / DOWN) change repo"
NAVIGATION = ["(A) prev", "(B) refresh", "(C) next", "(UP / DOWN) change repo"]

HEADERS = {
"User-Agent": "Badger2040-Actions-Display/0.1",
Expand All @@ -20,12 +20,21 @@
"current_page": 1,
"current_sub_page": 0, # 0-4 because REQUEST_PER_PAGE = 5
"current_repo": 0,
"failures_only": False,
}

badger_os.state_load("actions", state)


def truncatestring(text, text_size, width):
while True:
length = badger.measure_text(text, text_size)
if length > 0 and length > width:
text = text[:-1]
else:
text += ""
return text


def draw_view(repo_owner_name, pagination, lines):
# Clear to white
badger.set_pen(15)
Expand All @@ -41,8 +50,19 @@ def draw_view(repo_owner_name, pagination, lines):
)

badger.set_pen(0)
cols = [40, 120, 30, 100]
for i, line in enumerate(lines):
badger.text(line, 5, 16 + (15 // 2) + (i * 15), WIDTH, 1)
for j, col in enumerate(line):
if badger.measure_text(col, 0.5) > cols[j]:
col = truncatestring(col, 0.5, cols[j])
badger.text(col, sum(cols[:j]) + 5, 30 + (i * 15), WIDTH, 1)

badger.set_pen(0)
badger.rectangle(0, HEIGHT - 16, WIDTH, 16)
badger.set_pen(15)
nav = " | ".join(NAVIGATION)
badger.text(nav, 3, HEIGHT - 12, WIDTH, 1)

badger.update()


Expand All @@ -61,11 +81,8 @@ def process_workflow_runs(j):
run_number = f"#{item.get('run_number', 'None')}"
run_started_at = item.get("run_started_at", "None")

if len(name) > 20:
name = name[:17] + "..."

formatted_line = " | ".join([conclusion, name, run_number, run_started_at])
lines.append(formatted_line)
parts = [conclusion, name, run_number, run_started_at]
lines.append(parts)

return lines

Expand All @@ -76,8 +93,6 @@ def get_data(lines=[]):
"per_page": 1,
"page": state["current_page"] + state["current_sub_page"],
}
if state["failures_only"]:
PARAMS["status"] = "failure"
repo = REPOSITORIES[state["current_repo"]]
# Documentation: https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository
url = f"https://api.github.com/repos/{repo['owner']}/{repo['repo']}/actions/runs?{build_params(PARAMS)}"
Expand Down Expand Up @@ -108,7 +123,6 @@ def get_data(lines=[]):
time.sleep(1)
get_data(lines)
else:
lines.extend(["", NAVIGATION])
draw_view(
f"{repo['owner']}/{repo['repo']}",
f"Page {state['current_page'] // REQUEST_PER_PAGE + 1}/{total_pages}",
Expand All @@ -120,13 +134,6 @@ def get_data(lines=[]):
gc.collect()


def toggle_failures():
state["failures_only"] = not state["failures_only"]
state["current_page"] = 1
state["current_sub_page"] = 0
badger_os.state_save("actions", state)


def update_page(increment):
state["current_page"] = max(1, state["current_page"] + increment * REQUEST_PER_PAGE)
state["current_sub_page"] = 0
Expand Down Expand Up @@ -163,7 +170,6 @@ def update_repo(increment):
update_page(-1)
changed = True
if badger.pressed(badger2040.BUTTON_B):
toggle_failures()
changed = True
if badger.pressed(badger2040.BUTTON_C):
update_page(1)
Expand Down

0 comments on commit 61c1ef0

Please sign in to comment.