Skip to content

Commit

Permalink
Change: Use pontos terminal (#292)
Browse files Browse the repository at this point in the history
* Make pontos a direct dependency of autohooks

We are going to use parts of pontos directly within autohooks to reduce
code duplication.

* Change: Use pontos for Terminal output

Remove duplicated code between pontos and autohooks by dropping the
autohooks terminal code and use pontos as a direct dependency instead.
  • Loading branch information
bjoernricks committed May 2, 2022
1 parent d5f2c9b commit 622a8ab
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 207 deletions.
40 changes: 1 addition & 39 deletions autohooks/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
import colorful as cf

from autohooks.terminal import Terminal

__term = None # pylint: disable=invalid-name
from pontos.terminal import bold_info, error, fail, info, ok, out, warning

__all__ = [
'error',
Expand All @@ -31,36 +26,3 @@
'out',
'warning',
]


def ok(message: str) -> None:
__term.ok(message)


def fail(message: str) -> None:
__term.fail(message)


def error(message: str) -> None:
__term.error(message)


def warning(message: str) -> None:
__term.warning(message)


def info(message: str) -> None:
__term.info(message)


def bold_info(message: str) -> None:
__term.bold_info(message)


def out(message: str):
__term.print(message)


def _set_terminal(term: Terminal):
global __term # pylint: disable=global-statement, invalid-name
__term = term
5 changes: 2 additions & 3 deletions autohooks/precommit/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import importlib
import inspect
import sys

from contextlib import contextmanager
from types import ModuleType
from typing import Generator

from contextlib import contextmanager
from pontos.terminal import _set_terminal

from autohooks.api import _set_terminal
from autohooks.config import load_config_from_pyproject_toml
from autohooks.hooks import PreCommitHook
from autohooks.settings import Mode
Expand Down
99 changes: 2 additions & 97 deletions autohooks/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,101 +16,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


from contextlib import contextmanager
from enum import Enum
from shutil import get_terminal_size
from typing import Callable, Generator
from pontos.terminal.terminal import Signs, Terminal

import colorful as cf

TERMINAL_SIZE_FALLBACK = (80, 24) # use a small standard size as fallback


class Signs(Enum):
FAIL = '\N{HEAVY MULTIPLICATION X}'
ERROR = '\N{MULTIPLICATION SIGN}'
WARNING = '\N{WARNING SIGN}'
OK = '\N{CHECK MARK}'
INFO = '\N{INFORMATION SOURCE}'
NONE = ' '

def __str__(self):
return f'{self.value}'


STATUS_LEN = 2


class Terminal:
def __init__(self):
self._indent = 0

@staticmethod
def get_width() -> int:
"""
Get the width of the terminal window
"""
width, _ = get_terminal_size(TERMINAL_SIZE_FALLBACK)
return width

def _print_status(
self,
message: str,
status: Signs,
color: Callable,
style: Callable,
) -> None:
first_line = ''
output = ''
width = self.get_width()
if status == Signs.NONE:
first_line += ' '
else:
first_line += f'{color(status)} '
if self._indent > 0:
first_line += ' ' * self._indent
usable_width = width - STATUS_LEN - self._indent
while usable_width < len(message):
part_line = ' ' * (self._indent + STATUS_LEN)
part = message[:usable_width]
message = message[usable_width:]
output += f'{part_line}{part}\n'
output += f'{first_line}{message}'
print(style(output))

@contextmanager
def indent(self, indentation: int = 4) -> Generator:
current_indent = self._indent
self.add_indent(indentation)

yield self

self._indent = current_indent

def add_indent(self, indentation: int = 4) -> None:
self._indent += indentation

def reset_indent(self) -> None:
self._indent = 0

def print(self, *messages: str, style: Callable = cf.reset) -> None:
message = ''.join(messages)
self._print_status(message, Signs.NONE, cf.white, style)

def ok(self, message: str, style: Callable = cf.reset) -> None:
self._print_status(message, Signs.OK, cf.green, style)

def fail(self, message: str, style: Callable = cf.reset) -> None:
self._print_status(message, Signs.FAIL, cf.red, style)

def error(self, message: str, style: Callable = cf.reset) -> None:
self._print_status(message, Signs.ERROR, cf.red, style)

def warning(self, message: str, style: Callable = cf.reset) -> None:
self._print_status(message, Signs.WARNING, cf.yellow, style)

def info(self, message: str, style: Callable = cf.reset) -> None:
self._print_status(message, Signs.INFO, cf.cyan, style)

def bold_info(self, message: str, style: Callable = cf.bold) -> None:
self._print_status(message, Signs.INFO, cf.cyan, style)
__all__ = ("Terminal", "Signs")
Loading

0 comments on commit 622a8ab

Please sign in to comment.