Bläddra i källkod

[F] Implement os detection in termenv

https://github.com/hykilpikonna/hyfetch/issues/16
Hykilpikonna 2 år sedan
förälder
incheckning
1bccaad8fd
2 ändrade filer med 16 tillägg och 13 borttagningar
  1. 3 5
      hyfetch/main.py
  2. 13 8
      hyfetch/termenv.py

+ 3 - 5
hyfetch/main.py

@@ -9,6 +9,7 @@ from itertools import permutations
 from math import ceil
 from math import ceil
 from typing import Iterable
 from typing import Iterable
 
 
+from . import termenv
 from .color_scale import Scale
 from .color_scale import Scale
 from .color_util import printc, clear_screen
 from .color_util import printc, clear_screen
 from .constants import *
 from .constants import *
@@ -86,11 +87,8 @@ def create_config() -> Config:
     :return: Config object (automatically stored)
     :return: Config object (automatically stored)
     """
     """
     # Detect terminal environment (doesn't work on Windows)
     # Detect terminal environment (doesn't work on Windows)
-    det_bg, det_ansi = None, None
-    if platform.system() != 'Windows':
-        from . import termenv
-        det_bg = termenv.get_background_color()
-        det_ansi = termenv.detect_ansi_mode()
+    det_bg = termenv.get_background_color()
+    det_ansi = termenv.detect_ansi_mode()
 
 
     asc = get_distro_ascii()
     asc = get_distro_ascii()
     asc_width, asc_lines = ascii_size(asc)
     asc_width, asc_lines = ascii_size(asc)

+ 13 - 8
hyfetch/termenv.py

@@ -2,11 +2,7 @@ from __future__ import annotations
 
 
 import os
 import os
 import platform
 import platform
-import signal
 import sys
 import sys
-import termios
-import tty
-from select import select
 
 
 from .color_util import RGB, AnsiMode
 from .color_util import RGB, AnsiMode
 
 
@@ -91,6 +87,11 @@ def detect_ansi_mode() -> AnsiMode | None:
 
 
 
 
 def unix_read_osc(seq: int) -> str:
 def unix_read_osc(seq: int) -> str:
+    import termios
+    import tty
+    import signal
+    from select import select
+
     # screen/tmux can't support OSC, because they can be connected to multiple
     # screen/tmux can't support OSC, because they can be connected to multiple
     # terminals concurrently.
     # terminals concurrently.
     term = os.environ.get('TERM')
     term = os.environ.get('TERM')
@@ -155,10 +156,14 @@ def unix_read_osc(seq: int) -> str:
 
 
 
 
 def get_background_color() -> RGB | None:
 def get_background_color() -> RGB | None:
-    try:
-        osc = unix_read_osc(11).lstrip("rgb:")
-        return RGB.from_hex(''.join([v[:2] for v in osc.split('/')]))
-    except Exception:
+    system = platform.system().lower()
+    if system.startswith("linux") or system.startswith("darwin"):
+        try:
+            osc = unix_read_osc(11).lstrip("rgb:")
+            return RGB.from_hex(''.join([v[:2] for v in osc.split('/')]))
+        except Exception:
+            return None
+    if system.startswith("windows"):
         return None
         return None