Skip to content

Commit

Permalink
Fix terminal sizing for non-tty terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Dec 12, 2022
1 parent 6cc808d commit a0b83b7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/cleo/_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from __future__ import annotations

import math
import shutil

from dataclasses import dataclass
from html.parser import HTMLParser
from typing import TYPE_CHECKING

from rapidfuzz.distance import Levenshtein


if TYPE_CHECKING:
import os


class TagStripper(HTMLParser):
def __init__(self) -> None:
super().__init__(convert_charrefs=False)
Expand Down Expand Up @@ -105,3 +111,14 @@ def format_time(secs: float) -> str:
(fmt for fmt in _TIME_FORMATS if secs < fmt.threshold), _TIME_FORMATS[-1]
)
return format.apply(secs)


def get_terminal_size(fallback: tuple[int, int] | None = None) -> os.terminal_size:
"""
Get terminal size. For non-tty terminals, it will return
fallback values for terminal size.
:param fallback: Optional fallback terminal size.
:return: Terminal size tuple.
"""
return shutil.get_terminal_size(fallback=fallback or (80, 24))
4 changes: 2 additions & 2 deletions src/cleo/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import os
import re
import shutil
import sys

from contextlib import suppress
from typing import TYPE_CHECKING
from typing import cast

from cleo._utils import get_terminal_size
from cleo.commands.completions_command import CompletionsCommand
from cleo.commands.help_command import HelpCommand
from cleo.commands.list_command import ListCommand
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(self, name: str = "console", version: str = "") -> None:
self._name = name
self._version = version
self._display_name: str | None = None
self._terminal = shutil.get_terminal_size()
self._terminal = get_terminal_size()
self._default_command = "list"
self._single_command = False
self._commands: dict[str, Command] = {}
Expand Down
4 changes: 2 additions & 2 deletions src/cleo/io/outputs/section_output.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import math
import shutil

from typing import TYPE_CHECKING
from typing import TextIO

from cleo._utils import get_terminal_size
from cleo.io.outputs.output import Verbosity
from cleo.io.outputs.stream_output import StreamOutput

Expand All @@ -31,7 +31,7 @@ def __init__(
self._lines = 0
sections.insert(0, self)
self._sections = sections
self._terminal = shutil.get_terminal_size()
self._terminal = get_terminal_size()

@property
def content(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions src/cleo/ui/progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import math
import re
import shutil
import time

from typing import TYPE_CHECKING
from typing import Match

from cleo._utils import format_time
from cleo._utils import get_terminal_size
from cleo.cursor import Cursor
from cleo.io.io import IO
from cleo.io.outputs.section_output import SectionOutput
Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(
io = io.error_output

self._io = io
self._terminal = shutil.get_terminal_size()
self._terminal = get_terminal_size()
self._max = 0
self._step_width: int = 1
self._set_max_steps(max)
Expand Down

0 comments on commit a0b83b7

Please sign in to comment.