Skip to content

Commit

Permalink
Update ehh.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lennardv2 committed Oct 20, 2021
1 parent 08b0093 commit 728c166
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions ehh.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@
commands = []
commandsJsonFile = os.environ['HOME'] + '/ehh.json'

if (len(sys.argv) > 1 and sys.argv[1] == "--source"):
if (len(sys.argv) > 2):
commandsJsonFile = sys.argv[2]
del sys.argv[1]
del sys.argv[1]


if(os.path.isfile(commandsJsonFile)):
with open(commandsJsonFile) as f:
commands = json.load(f)


def trunc(data, max, min = 0):
return (data[:max] + (data[max:] and '…')).ljust(min)
Expand Down Expand Up @@ -53,7 +60,8 @@ def groupCommands(cmds):

@click.group()
@click.version_option(__version__)
def main():
@click.option('--source', help="Path to the ehh.json source", default=None)
def main(source = None):
"""
Simple CLI for remembering commands
"""
Expand All @@ -67,7 +75,7 @@ def echoCommand(command, index):
def echoCommandBig(command, index):
click.echo("Id: " + str(index))
click.echo("Command: " + Fore.MAGENTA + command['command'] + Fore.RESET)
click.echo("Description: " + command['description'])
click.echo("Description: " + Fore.LIGHTBLUE_EX + command['description'] + Fore.RESET)
click.echo("Group: " + command['group'])
click.echo("Alias: " + command['alias'])

Expand All @@ -79,7 +87,13 @@ def echoGroup(group):

click.echo(" ")

def execCommand(command):
def execCommand(match):
command = match['command']

click.echo("Running: " + Fore.MAGENTA + command + Fore.RESET)
click.echo("Description: " + Fore.LIGHTBLUE_EX + match['description'] + Fore.RESET)
click.echo("")

commandVars = re.findall(r"\(:(.+?)\)",command)
# Remove dups
commandVars = list(dict.fromkeys(commandVars))
Expand All @@ -88,8 +102,6 @@ def execCommand(command):
answer = click.prompt(var)
command = command.replace("(:" + var + ")", answer)

click.echo("Running: " + Fore.MAGENTA + command + Fore.RESET)

os.system(command)

@main.command()
Expand Down Expand Up @@ -141,7 +153,7 @@ def run(query, confirmation):
answer = click.confirm('Run ' + Fore.MAGENTA + match['command'] + Fore.RESET, default=True)

if (confirmation == False or answer):
execCommand(match['command'])
execCommand(match)
return

ls.callback(query)
Expand Down Expand Up @@ -206,7 +218,7 @@ def help():
run.callback(sys.argv[1], False)
exit()
else:
if mainArg in ["--version", "--help"]:
if mainArg in ["--version", "--help", "--source"]:
args = sys.argv[2:]
main()
elif mainArg in ["add", "help", "get", "ls", "rm", "run"]:
Expand Down

0 comments on commit 728c166

Please sign in to comment.