Skip to content

Commit

Permalink
mud init is fixed. Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasursadikov committed Sep 7, 2024
1 parent 6b750b7 commit 7e4b36d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
2 changes: 1 addition & 1 deletion commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def _get_branch_status(path: str) -> str:
branch_color = Commands._get_branch_color(branch_path[0])
branch = f'{branch_color}{icon}{RESET}{utils.GLYPHS["space"]}{branch_path[0]}{RESET}/{BOLD}{("/".join(branch_path[1:]))}'
else:
branch = f'{TEXT["cyan"]}{utils.GLYPHS["branch"]}{RESET}{utils.GLYPHS["space"]}{branch_stdout}'
branch = f'{CYAN}{utils.GLYPHS["branch"]}{RESET}{utils.GLYPHS["space"]}{branch_stdout}'
return branch

@staticmethod
Expand Down
1 change: 0 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
import sys

import utils
import settings
Expand Down
34 changes: 13 additions & 21 deletions mud.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ def run(self) -> None:
return
# Prints version
elif sys.argv[1] in COMMANDS['version']:
utils.print_version()
return
elif sys.argv[1] in COMMANDS['configure']:
self.configure()
utils.version()
return
# Checks for available updates
elif sys.argv[1] in COMMANDS['update']:
utils.check_updates(True)
return

# Runs configuration wizard
elif sys.argv[1] in COMMANDS['configure']:
utils.configure()
return
current_directory = os.getcwd()
self.config = config.Config()

if len(sys.argv) > 1 and sys.argv[1] in [cmd for group in COMMANDS.values() for cmd in group]:
args = self.parser.parse_args()
if args.command in COMMANDS['init']:
self.init(args)
return
# Discovers repositories in current directory
if sys.argv[1] in COMMANDS['init']:
self.init(self.parser.parse_args())
return

self.config.find()
self._filter_repos()
Expand Down Expand Up @@ -158,10 +158,13 @@ def run(self) -> None:
else:
self.cmd_runner.run_ordered(self.repos.keys(), sys.argv)


def init(self, args) -> None:
self.config.data = {}
index = 0
directories = [d for d in os.listdir('.') if os.path.isdir(d) and os.path.isdir(os.path.join(d, '.git'))]
print(directories)
print(os.getcwd())
for directory in directories:
if directory in self.config.paths():
continue
Expand All @@ -174,17 +177,6 @@ def init(self, args) -> None:
return
self.config.save(utils.CONFIG_FILE_NAME)

def configure(self):
utils.settings.config['mud']['run_async'] = str(utils.ask('Do you want to run commands simultaneously for multiple repositories?'))
utils.settings.config['mud']['run_table'] = str(utils.ask('Do you want to see command execution progress in table view? This will limit output content.'))
utils.settings.config['mud']['auto_fetch'] = str(utils.ask(f'Do you want to automatically run {BOLD}\'git fetch\'{RESET} whenever you run commands such as {BOLD}\'mud info\'{RESET}?'))
utils.settings.config['mud']['ask_updates'] = str(utils.ask(f'Do you want to get information about latest updates?'))
utils.settings.config['mud']['nerd_fonts'] = str(utils.ask(f'Do you want to use {BOLD}nerd-fonts{RESET}?'))
utils.settings.config['mud']['simplify_branches'] = str(utils.ask(f'Do you want to simplify branches? (ex. {BOLD}feature/name{RESET} -> {BOLD}f/name{RESET}'))
utils.settings.save()
print('Your settings are updated!')
pass

def add(self, args) -> None:
self.config.add_label(args.path, args.label)
self.config.save(utils.CONFIG_FILE_NAME)
Expand Down
53 changes: 32 additions & 21 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import random
import subprocess
import sys

from settings import *
from styles import *
Expand Down Expand Up @@ -66,12 +66,28 @@ def set_up():
sys.exit()


def version() -> None:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], text=True).splitlines()[0]
m = random.choice(TEXT[3:])
u = random.choice(TEXT[3:])
d = random.choice(TEXT[3:])
t = random.choice(TEXT[3:])
v = random.choice(TEXT[3:])
print(fr'''
{m} __ __{u} __ __{d} _____
{m}/\ '-./ \{u}/\ \/\ \{d}/\ __-. {BOLD}{t}Multi-directory runner{RESET} [{v}{hash}{RESET}]
{m}\ \ \-./\ \{u} \ \_\ \{d} \ \/\ \ {RESET}Jasur Sadikov
{m} \ \_\ \ \_\{u} \_____\{d} \____- {RESET}https://github.com/jasursadikov/mud
{m} \/_/ \/_/{u}\/_____/{d}\/____/ {RESET}Type 'mud --help' for help
''')


def check_updates(explicit: bool = False) -> bool:
target_directory = os.curdir
target_directory = os.getcwd()
os.chdir(os.path.dirname(os.path.abspath(__file__)))

subprocess.run(['git', 'fetch'], check=True)

result = subprocess.run(['git', 'status', '-uno'], capture_output=True, text=True)

if 'Your branch is behind' in result.stdout:
Expand Down Expand Up @@ -106,6 +122,18 @@ def check_updates(explicit: bool = False) -> bool:
return False


def configure():
settings.config['mud']['run_async'] = str(ask('Do you want to run commands simultaneously for multiple repositories?'))
settings.config['mud']['run_table'] = str(ask('Do you want to see command execution progress in table view? This will limit output content.'))
settings.config['mud']['auto_fetch'] = str(ask(f'Do you want to automatically run {BOLD}\'git fetch\'{RESET} whenever you run commands such as {BOLD}\'mud info\'{RESET}?'))
settings.config['mud']['ask_updates'] = str(ask(f'Do you want to get information about latest updates?'))
settings.config['mud']['nerd_fonts'] = str(ask(f'Do you want to use {BOLD}nerd-fonts{RESET}?'))
settings.config['mud']['simplify_branches'] = str(ask(f'Do you want to simplify branches? (ex. {BOLD}feature/name{RESET} -> {BOLD}f/name{RESET}'))
settings.save()
print('Your settings are updated!')
pass


def ask(text: str) -> bool:
print(f"{text} [Y/n] ", end='', flush=True)
if sys.platform.startswith('win'):
Expand All @@ -127,21 +155,4 @@ def ask(text: str) -> bool:

def print_error(text: str, code: int = 255) -> None:
print(f'{RED}Error:{RESET} {text}')
sys.exit(code)


def print_version() -> None:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], text=True).splitlines()[0]
m = random.choice(TEXT[3:])
u = random.choice(TEXT[3:])
d = random.choice(TEXT[3:])
t = random.choice(TEXT[3:])
v = random.choice(TEXT[3:])
print(fr'''
{m} __ __{u} __ __{d} _____
{m}/\ '-./ \{u}/\ \/\ \{d}/\ __-. {BOLD}{t}Multi-directory runner{RESET} [{v}{hash}{RESET}]
{m}\ \ \-./\ \{u} \ \_\ \{d} \ \/\ \ {RESET}Jasur Sadikov
{m} \ \_\ \ \_\{u} \_____\{d} \____- {RESET}https://github.com/jasursadikov/mud
{m} \/_/ \/_/{u}\/_____/{d}\/____/ {RESET}Type 'mud --help' for help
''')
sys.exit(code)

0 comments on commit 7e4b36d

Please sign in to comment.