diff --git a/src/lightning_app/CHANGELOG.md b/src/lightning_app/CHANGELOG.md index e7ca605b4c2b5..aaaf916db6015 100644 --- a/src/lightning_app/CHANGELOG.md +++ b/src/lightning_app/CHANGELOG.md @@ -65,6 +65,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Fixed an issue where custom property setters were not being used `LightningWork` class. ([#14259](https://github.com/Lightning-AI/lightning/pull/14259)) +- Fixed an issue where some terminals would display broken icons in the PL app CLI. ([#14226](https://github.com/Lightning-AI/lightning/issues/14226)) + ## [0.6.0] - 2022-09-08 diff --git a/src/lightning_app/cli/cmd_pl_init.py b/src/lightning_app/cli/cmd_pl_init.py index 06ec80d5cdb90..dda492c9c9e08 100644 --- a/src/lightning_app/cli/cmd_pl_init.py +++ b/src/lightning_app/cli/cmd_pl_init.py @@ -2,6 +2,7 @@ import re import shutil import subprocess +import sys import tarfile import urllib.request from pathlib import Path @@ -149,6 +150,8 @@ def print_pretty_report( text_pathname.append(f" {padding} {help_text}", "blue") icon = "📂 " if path.is_dir() else "📄 " + icon = icon if _can_encode_icon(icon) else "" + tree.add(Text(icon) + text_pathname) print("\n") @@ -156,3 +159,12 @@ def print_pretty_report( print(tree) print("\nRun it:\n") print(Panel(f"[red]lightning run app {directory.relative_to(Path.cwd()) / 'app.py'}")) + + +def _can_encode_icon(icon: str) -> bool: + """Helper function to check whether an icon can be encoded.""" + try: + icon.encode(sys.stdout.encoding) + return True + except UnicodeEncodeError: + return False diff --git a/tests/tests_app/cli/test_cmd_pl_init.py b/tests/tests_app/cli/test_cmd_pl_init.py index 1d5548b051323..2e479ee6e9eb3 100644 --- a/tests/tests_app/cli/test_cmd_pl_init.py +++ b/tests/tests_app/cli/test_cmd_pl_init.py @@ -1,11 +1,13 @@ import os +import sys from unittest import mock +from unittest.mock import Mock import pytest from click.testing import CliRunner from lightning_app.cli import lightning_cli -from lightning_app.cli.cmd_pl_init import download_frontend, pl_app +from lightning_app.cli.cmd_pl_init import _can_encode_icon, download_frontend, pl_app def test_pl_app_input_paths_do_not_exist(tmp_path): @@ -84,6 +86,19 @@ def test_pl_app_download_frontend(tmp_path): assert "static" in contents +def test_pl_app_encode_icon(monkeypatch): + stdout_mock = Mock(wraps=sys.stdout) + monkeypatch.setattr(sys, "stdout", stdout_mock) + + stdout_mock.encoding = "utf-8" + assert _can_encode_icon("📂") + assert _can_encode_icon("📄") + + stdout_mock.encoding = "ascii" + assert not _can_encode_icon("📂") + assert not _can_encode_icon("📄") + + @pytest.mark.parametrize( "cwd, source_dir, script_path", [