From 206fa8d7263dde235b5c59230803c644df887982 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 20 Aug 2022 20:10:53 -0400 Subject: [PATCH 1/5] [F] Fix chromeOS (again!) https://github.com/dylanaraps/neofetch/issues/1949 https://github.com/dylanaraps/neofetch/issues/1653 https://github.com/dylanaraps/neofetch/issues/1384 --- neofetch | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/neofetch b/neofetch index c5deb210..51f51736 100755 --- a/neofetch +++ b/neofetch @@ -1130,14 +1130,8 @@ get_distro() { elif [[ -d /system/app/ && -d /system/priv-app ]]; then distro="Android $(getprop ro.build.version.release)" - - # Chrome OS doesn't conform to the /etc/*-release standard. - # While the file is a series of variables they can't be sourced - # by the shell since the values aren't quoted. - elif [[ -f /etc/lsb-release && $(< /etc/lsb-release) == *CHROMEOS* ]]; then - distro='Chrome OS' - elif [[ -f /etc/vzlinux-release ]]; then + elif [[ -f /etc/vzlinux-release ]]; then distro='VzLinux' elif type -p guix >/dev/null; then @@ -1200,14 +1194,19 @@ get_distro() { *) distro+=" on Windows $windows_version" ;; esac + # Chrome OS doesn't conform to the /etc/*-release standard. + # While the file is a series of variables they can't be sourced + # by the shell since the values aren't quoted. + elif [[ -f /etc/lsb-release && $(< /etc/lsb-release) == *CHROMEOS* ]]; then + distro='Chrome OS' + elif [[ $(< /proc/version) == *chrome-bot* || -f /dev/cros_ec ]]; then [[ $distro != *Chrome* ]] && case $distro_shorthand in - on) distro+=" [Chrome OS]" ;; + on) distro="Chrome OS $distro" ;; tiny) distro="Chrome OS" ;; - *) distro+=" on Chrome OS" ;; + *) distro="Chrome OS $distro" ;; esac - distro=${distro## on } fi distro=$(trim_quotes "$distro") From 6911087eaba323dd54d369f2d50374fff9d60838 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 20 Aug 2022 20:46:56 -0400 Subject: [PATCH 2/5] [+] Add --ask-exit option --- hyfetch/main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hyfetch/main.py b/hyfetch/main.py index ecfaf3cc..25e216b3 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -336,6 +336,7 @@ def run(): parser.add_argument('--debug', action='store_true', help=f'Debug mode') parser.add_argument('--test-distro', help=f'Test for a specific distro') parser.add_argument('--test-print', action='store_true', help=f'Test print distro ascii art only') + parser.add_argument('--ask-exit', action='store_true', help=f'Ask before exitting') args = parser.parse_args() @@ -385,3 +386,6 @@ def run(): # Run run_neofetch(preset, config.color_align) + + if args.ask_exit: + input('Press any key to exit...') From 3124ec0b3ed4293efb3fab76ebba3eb6db6afd2b Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 20 Aug 2022 20:47:45 -0400 Subject: [PATCH 3/5] [+] Ensure git bash is installed on Windows --- hyfetch/constants.py | 2 ++ hyfetch/main.py | 3 +++ hyfetch/neofetch_util.py | 42 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/hyfetch/constants.py b/hyfetch/constants.py index 076831ae..b6ff702c 100644 --- a/hyfetch/constants.py +++ b/hyfetch/constants.py @@ -46,3 +46,5 @@ class GlobalConfig: GLOBAL_CFG = GlobalConfig(color_mode='8bit', override_distro=None, debug=False, is_light=False) + +MINGIT_URL = 'https://github.com/git-for-windows/git/releases/download/v2.37.2.windows.2/MinGit-2.37.2.2-busybox-32-bit.zip' diff --git a/hyfetch/main.py b/hyfetch/main.py index 25e216b3..cfa6b07a 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -344,6 +344,9 @@ def run(): print(f'Version is {VERSION}') return + # Ensure git bash for windows + ensure_git_bash() + # Test distro ascii art if args.test_distro: print(f'Setting distro to {args.test_distro}') diff --git a/hyfetch/neofetch_util.py b/hyfetch/neofetch_util.py index 92d2e52e..4974399b 100644 --- a/hyfetch/neofetch_util.py +++ b/hyfetch/neofetch_util.py @@ -6,16 +6,19 @@ import platform import re import shlex import subprocess +import zipfile from dataclasses import dataclass from pathlib import Path from subprocess import check_output from tempfile import TemporaryDirectory +from urllib.request import urlretrieve import pkg_resources +import psutil from typing_extensions import Literal from hyfetch.color_util import color -from .constants import GLOBAL_CFG +from .constants import GLOBAL_CFG, MINGIT_URL from .presets import ColorProfile from .serializer import from_dict @@ -142,6 +145,40 @@ def get_command_path() -> str: return pkg_resources.resource_filename(__name__, 'scripts/neowofetch') +def ensure_git_bash() -> Path: + """ + Ensure git bash installation for windows + + :returns git bash path + """ + if platform.system() == 'Windows': + # Find installation in default path + def_path = Path(r'C:\Program Files\Git\bin\bash.exe') + if def_path.is_file(): + return def_path + + # Find installation in PATH (C:\Program Files\Git\cmd should be in path) + pth = (os.environ.get('PATH') or '').lower().split(';') + pth = [p for p in pth if p.endswith(r'\git\cmd')] + if pth: + return Path(pth[0]).parent / r'bin\bash.exe' + + # Previously downloaded portable installation + path = Path(__file__).parent / 'min_git' + pkg_path = path / 'package.zip' + if path.is_dir(): + return path / r'bin\bash.exe' + + # No installation found, download a portable installation + print('Git installation not found. Git is required to use HyFetch/neofetch on Windows') + print('Downloading a minimal portable package for Git...') + urlretrieve(MINGIT_URL, pkg_path) + print('Download finished! Extracting...') + with zipfile.ZipFile(pkg_path, 'r') as zip_ref: + zip_ref.extractall(path) + print('Done!') + return path / r'bin\bash.exe' + def run_command(args: str, pipe: bool = False) -> str | None: """ Run neofetch command @@ -153,7 +190,7 @@ def run_command(args: str, pipe: bool = False) -> str | None: cmd = get_command_path().replace("\\", "/").replace("C:/", "/c/") args = args.replace('\\', '/').replace('C:/', '/c/') - full_cmd = ['C:\\Program Files\\Git\\bin\\bash.exe', '-c', f'{cmd} {args}'] + full_cmd = [ensure_git_bash(), '-c', f'{cmd} {args}'] # print(full_cmd) if pipe: @@ -162,7 +199,6 @@ def run_command(args: str, pipe: bool = False) -> str | None: subprocess.run(full_cmd) - def get_distro_ascii(distro: str | None = None) -> str: """ Get the distro ascii of the current distro. Or if distro is specified, get the specific distro's From b100e35bc7e3d05e0fad1e7133e12574fa4f9ba2 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 20 Aug 2022 20:48:02 -0400 Subject: [PATCH 4/5] [+] Enable running through python -m hyfetch --- hyfetch/__main__.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 hyfetch/__main__.py diff --git a/hyfetch/__main__.py b/hyfetch/__main__.py new file mode 100644 index 00000000..debf90a4 --- /dev/null +++ b/hyfetch/__main__.py @@ -0,0 +1,4 @@ +from hyfetch import main + +if __name__ == '__main__': + main.run() From 7001d873b9252361189f81a12629c9fcbffee3f2 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 20 Aug 2022 20:52:21 -0400 Subject: [PATCH 5/5] [+] Run MinTTY from cmd.exe --- hyfetch/main.py | 1 + hyfetch/neofetch_util.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/hyfetch/main.py b/hyfetch/main.py index cfa6b07a..b3e04fcf 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -346,6 +346,7 @@ def run(): # Ensure git bash for windows ensure_git_bash() + check_windows_cmd() # Test distro ascii art if args.test_distro: diff --git a/hyfetch/neofetch_util.py b/hyfetch/neofetch_util.py index 4974399b..6a6269c0 100644 --- a/hyfetch/neofetch_util.py +++ b/hyfetch/neofetch_util.py @@ -179,6 +179,19 @@ def ensure_git_bash() -> Path: print('Done!') return path / r'bin\bash.exe' + +def check_windows_cmd(): + """ + Check if this script is running under cmd.exe. If so, launch an external window with git bash + since cmd doesn't support RGB colors. + """ + if psutil.Process(os.getppid()).name().lower().strip() == 'cmd.exe': + print("cmd.exe doesn't support RGB colors, restarting in MinTTY...") + cmd = f'"{ensure_git_bash().parent.parent / "usr/bin/mintty.exe"}" -s 110,40 -e python -m hyfetch --ask-exit' + os.system(cmd) + exit() + + def run_command(args: str, pipe: bool = False) -> str | None: """ Run neofetch command