Skip to content

Commit

Permalink
Rework paging to take terminal height into account
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Jan 21, 2025
1 parent bfcf623 commit ecc8033
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
54 changes: 34 additions & 20 deletions toot/cli/timelines_v2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from functools import wraps
from typing import Iterable, Optional, Tuple
import shutil
from typing import Iterable, List, Optional, Tuple
from urllib.parse import quote

import click
Expand All @@ -12,11 +13,12 @@
from toot.entities import (
Account,
Status,
from_dict_list,
from_response,
from_response_list,
from_responses_batched,
)
from toot.output import get_continue, print_timeline
from toot.output import get_continue, get_max_width, get_terminal_height, get_terminal_width, print_timeline, status_lines
from toot.utils import drop_empty_values, str_bool_nullable


Expand Down Expand Up @@ -46,17 +48,14 @@ def common_timeline_options(func):
@click.option(
"-p",
"--pager",
help="Page the results, optionally define how many results to show per page",
type=int,
callback=validate_positive,
is_flag=False,
flag_value=10,
help="Page the results",
is_flag=True,
)
@click.option(
"-c",
"--clear",
"--clear/--no-clear",
help="Clear the screen before printing. If paged, clear before each page.",
is_flag=True,
default=True,
)
@wraps(func)
def wrapper(*args, **kwargs):
Expand Down Expand Up @@ -92,7 +91,7 @@ def account(
max_id: Optional[str],
since_id: Optional[str],
limit: Optional[int],
pager: Optional[int],
pager: bool,
clear: bool,
json: bool,
):
Expand Down Expand Up @@ -120,7 +119,7 @@ def home(
max_id: Optional[str],
since_id: Optional[str],
limit: Optional[int],
pager: Optional[int],
pager: bool,
clear: bool,
json: bool,
):
Expand Down Expand Up @@ -148,7 +147,7 @@ def link(
max_id: Optional[str],
since_id: Optional[str],
limit: Optional[int],
pager: Optional[int],
pager: bool,
clear: bool,
json: bool,
):
Expand All @@ -170,19 +169,19 @@ def link(
_show_timeline(ctx, path, params, json, pager, clear, limit)


@timelines.command()
@timelines.command("list")
@click.argument("list_name_or_id")
@common_timeline_options
@json_option
@pass_context
def list(
def list_cmd(
ctx: Context,
list_name_or_id: str,
min_id: Optional[str],
max_id: Optional[str],
since_id: Optional[str],
limit: Optional[int],
pager: Optional[int],
pager: bool,
clear: bool,
json: bool,
):
Expand Down Expand Up @@ -230,7 +229,7 @@ def public(
max_id: Optional[str],
since_id: Optional[str],
limit: Optional[int],
pager: Optional[int],
pager: bool,
clear: bool,
local: Optional[bool],
remote: Optional[bool],
Expand Down Expand Up @@ -303,7 +302,7 @@ def tag(
max_id: Optional[str],
since_id: Optional[str],
limit: Optional[int],
pager: Optional[int],
pager: bool,
clear: bool,
local: Optional[bool],
remote: Optional[bool],
Expand Down Expand Up @@ -381,18 +380,33 @@ def _print_single(response: Response, clear: bool, limit: Optional[int]):
dim=True,
)
else:
click.echo("No statuses found containing the given tag")
click.echo("No statuses found")


def _print_paged(responses: Iterable[Response], page_size: int, clear: bool):
width = get_max_width()
height = get_terminal_height()
separator = "─" * width

def _page_generator():
batch_lines: List[str] = []
for response in responses:
statuses = from_dict_list(Status, response.json())
for status in statuses:
lines = [separator] + list(status_lines(status))
if len(batch_lines) + len(lines) > height - 2 and batch_lines:
yield "\n".join(batch_lines) + "\n" + separator
batch_lines = []
batch_lines.extend(lines)

first = True
printed_any = False
for page in from_responses_batched(responses, Status, page_size):
for page in _page_generator():
if not first and not get_continue():
break
if clear:
click.clear()
print_timeline(page)
click.echo(page)
first = False
printed_any = True

Expand Down
4 changes: 4 additions & 0 deletions toot/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def get_terminal_width() -> int:
return shutil.get_terminal_size().columns


def get_terminal_height() -> int:
return shutil.get_terminal_size().lines


def get_width() -> int:
return min(get_terminal_width(), get_max_width())

Expand Down

0 comments on commit ecc8033

Please sign in to comment.