Commit 0943662e authored by Tanguy Le Carrour's avatar Tanguy Le Carrour
Browse files

Use `rich` to print to the console.

parent 9834602a
This diff is collapsed.
......@@ -14,6 +14,7 @@ splito = "splito.entry_points.cli:audio_editor"
python = "^3.8"
pydub = "^0.24.1"
click = "^7.1.2"
rich = "^9.6.1"
[tool.poetry.dev-dependencies]
mypy = "^0.790"
......
......@@ -12,7 +12,7 @@ cli = typer.Typer()
@cli.callback()
def callback():
def callback() -> None:
# Empty call back to trick Typer into adding a single sub-command.
pass
......@@ -38,8 +38,7 @@ def extract(
"""
controller = controllers.ExtractSubTracks(track, intervals)
presenter = presenters.ExtractSubTracks()
presenter = presenters.ExtractSubTracks(output=sys.stdout)
interactor = ExtractSubTracksInteractor(presenter, ctx.obj["tracks"])
controller.call(interactor)
typer.echo(presenter.to_string())
sys.exit(presenter.exit_code())
from typing import List
from typing import IO
from rich.console import Console
from splito.use_cases.extract_sub_tracks import ExtractSubTracksPresenter
class ExtractSubTracks(ExtractSubTracksPresenter):
def __init__(self) -> None:
self.__exit_code = 1
self.__sub_tracks: List[str] = []
self.__error = ""
def __init__(self, output: IO[str]) -> None:
self.__exit_code = 0
self.__console = Console(file=output)
def not_an_audio_track(self, a_path: str) -> None:
self.__exit_code = 1
self.__error = f"Not an audio track: {a_path}."
self.__console.print(f"[bold red]Not an audio track: `{a_path}`.[/bold red]")
def wrong_interval(self, a_start: str, an_end: str) -> None:
self.__error = f"[WARN] Not a proper interval: {a_start}-{an_end}."
self.__exit_code = 1
self.__console.print(
f"[bold yellow]Not a proper interval: {a_start}-{an_end}.[/bold yellow]"
)
def sub_track_extracted(self, a_path: str) -> None:
self.__exit_code = 0
self.__sub_tracks.append(a_path)
self.__console.print(
f"Sub-track extracted to [bold cyan]`{a_path}`[/bold cyan]."
)
def exit_code(self) -> int:
return self.__exit_code
def to_string(self) -> str:
result = ""
if self.__error:
result += f"[ERR] {self.__error}\n"
if self.__sub_tracks:
result += "Sub tracks extracted:\n"
for sub_track in self.__sub_tracks:
result += f" - {sub_track}\n"
else:
result += "No track extracted!"
return result
from mamba import describe, it # type: ignore
import io
from mamba import describe, it, before # type: ignore
from robber import expect # type: ignore
from splito.interfaces.to_terminal import ExtractSubTracks
......@@ -6,27 +8,31 @@ from splito.interfaces.to_terminal import ExtractSubTracks
with describe("Presenters 'to terminal'"):
with before.each as self:
self.output = io.StringIO()
with describe(ExtractSubTracks):
with it("handles extracting sub-tracks"):
presenter = ExtractSubTracks()
presenter = ExtractSubTracks(self.output)
presenter.sub_track_extracted("a_path/a_file-1.mp3")
expect(presenter.exit_code()).to.eq(0)
expect(presenter.to_string()).to.contain("a_path/a_file-1.mp3")
expect(self.output.getvalue()).to.contain("a_path/a_file-1.mp3")
with it("handles wrong track"):
presenter = ExtractSubTracks()
presenter = ExtractSubTracks(self.output)
presenter.not_an_audio_track("a_path/a_file-1.mp3")
expect(presenter.exit_code()).to.eq(1)
expect(presenter.to_string()).to.contain("a_path/a_file-1.mp3")
expect(self.output.getvalue()).to.contain("a_path/a_file-1.mp3")
with it("handles wrong intervals"):
presenter = ExtractSubTracks()
presenter = ExtractSubTracks(self.output)
presenter.wrong_interval("1", "0")
expect(presenter.to_string()).to.contain("WARN")
expect(presenter.exit_code()).to.eq(1)
expect(self.output.getvalue()).to.contain("1-0")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment