[PR] #99 from redalpa333/master

Improved ensure_git_bash
This commit is contained in:
Azalea 2023-03-11 15:36:54 -05:00 committed by GitHub
commit dcff08340e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 55 deletions

View file

@ -7,7 +7,6 @@ import random
import traceback import traceback
from itertools import permutations from itertools import permutations
from math import ceil from math import ceil
from typing import Iterable
from . import termenv, neofetch_util from . import termenv, neofetch_util
from .color_scale import Scale from .color_scale import Scale
@ -33,51 +32,6 @@ def check_config(path) -> Config:
return create_config() return create_config()
def literal_input(prompt: str, options: Iterable[str], default: str, show_ops: bool = True) -> str:
"""
Ask the user to provide an input among a list of options
:param prompt: Input prompt
:param options: Options
:param default: Default option
:param show_ops: Show options
:return: Selection
"""
options = list(options)
lows = [o.lower() for o in options]
if show_ops:
op_text = '|'.join([f'&l&n{o}&r' if o == default else o for o in options])
printc(f'{prompt} ({op_text})')
else:
printc(f'{prompt} (default: {default})')
def find_selection(sel: str):
if not sel:
return None
# Find exact match
if sel in lows:
return options[lows.index(sel)]
# Find starting abbreviation
for i, op in enumerate(lows):
if op.startswith(sel):
return options[i]
return None
selection = input('> ').lower() or default
while not find_selection(selection):
print(f'Invalid selection! {selection} is not one of {"|".join(options)}')
selection = input('> ').lower() or default
print()
return find_selection(selection)
def create_config() -> Config: def create_config() -> Config:
""" """
Create config interactively Create config interactively

View file

@ -12,6 +12,7 @@ from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from subprocess import check_output from subprocess import check_output
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from typing import Iterable
import pkg_resources import pkg_resources
@ -24,6 +25,48 @@ from .types import BackendLiteral, ColorAlignMode
RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}') RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}')
def literal_input(prompt: str, options: Iterable[str], default: str, show_ops: bool = True) -> str:
"""
Ask the user to provide an input among a list of options
:param prompt: Input prompt
:param options: Options
:param default: Default option
:param show_ops: Show options
:return: Selection
"""
options = list(options)
lows = [o.lower() for o in options]
if show_ops:
op_text = '|'.join([f'&l&n{o}&r' if o == default else o for o in options])
printc(f'{prompt} ({op_text})')
else:
printc(f'{prompt} (default: {default})')
def find_selection(sel: str):
if not sel:
return None
# Find exact match
if sel in lows:
return options[lows.index(sel)]
# Find starting abbreviation
for i, op in enumerate(lows):
if op.startswith(sel):
return options[i]
return None
selection = input('> ').lower() or default
while not find_selection(selection):
print(f'Invalid selection! {selection} is not one of {"|".join(options)}')
selection = input('> ').lower() or default
print()
return find_selection(selection)
def term_size() -> tuple[int, int]: def term_size() -> tuple[int, int]:
""" """
@ -185,7 +228,7 @@ def ensure_git_bash() -> Path:
return def_path return def_path
# Detect third-party git.exe in path # Detect third-party git.exe in path
git_exe = shutil.which("git.exe") or shutil.which("git") git_exe = shutil.which("bash") or shutil.which("git.exe") or shutil.which("git")
if git_exe is not None: if git_exe is not None:
pth = Path(git_exe).parent pth = Path(git_exe).parent
if (pth / r'bash.exe').is_file(): if (pth / r'bash.exe').is_file():
@ -207,14 +250,17 @@ def ensure_git_bash() -> Path:
# No installation found, download a portable installation # No installation found, download a portable installation
print('Git installation not found. Git is required to use HyFetch/neofetch on Windows') print('Git installation not found. Git is required to use HyFetch/neofetch on Windows')
print('Downloading a minimal portable package for Git...') if literal_input('Would you like to install a minimal package for Git? (if no is selected colors almost certianly won\'t work)', ['yes', 'no'], 'yes', False) == 'yes':
from urllib.request import urlretrieve print('Downloading a minimal portable package for Git...')
urlretrieve(MINGIT_URL, pkg_path) from urllib.request import urlretrieve
print('Download finished! Extracting...') urlretrieve(MINGIT_URL, pkg_path)
with zipfile.ZipFile(pkg_path, 'r') as zip_ref: print('Download finished! Extracting...')
zip_ref.extractall(path) with zipfile.ZipFile(pkg_path, 'r') as zip_ref:
print('Done!') zip_ref.extractall(path)
return path / r'bin\bash.exe' print('Done!')
return path / r'bin\bash.exe'
else:
sys.exit()
def check_windows_cmd(): def check_windows_cmd():