Last active
November 5, 2019 12:51
-
-
Save shanehh/685dc6e4f8e5e73fd3940c3e26523de2 to your computer and use it in GitHub Desktop.
letsco
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
assume today is 2019-11-5. | |
now, we want to code. | |
> mkdir ~/Documents/py3/2019-11-05-big-deal | |
> code ~/Documents/py3/2019-11-05-big-deal | |
oh! it's boring, even though you can use !$ | |
so, this is the script, it is equal to all of above steps: | |
> letsco big-deal ~/Documents/py3 | |
the arguments requires dirname and path | |
""" | |
import os | |
colors = dict( | |
red='\033[91m', | |
green='\033[32m', | |
yellow='\033[33m', | |
blue='\033[34m', | |
voilet='\033[35m', | |
beige='\033[36m', | |
) | |
def colorful(text, color): | |
text = str(text) | |
END = '\033[0m' | |
BOLD = '\033[1m' | |
return colors.get(color, '') + BOLD + text + END | |
def red(text): | |
return colorful(text, 'red') | |
def cmd_args(): | |
import sys | |
if len(sys.argv) < 3: | |
sys.exit( | |
f"Error: expected a {red('Folder-name')} and {red('Path')} as arguments") | |
return sys.argv[1:] | |
def yes_or_no(prompt): | |
try: | |
anser = input(f'{prompt} ([y]/n)? ').strip() | |
if anser is '': | |
anser = 'y' | |
return anser | |
except (KeyboardInterrupt, EOFError): | |
# Catch multiple exceptions in one line (except block) | |
# 漂亮的退场 | |
import sys | |
sys.exit('\nExiting and nothing happened.') | |
def if_you_do(path): | |
a = yes_or_no( | |
f'{colorful(path, "beige")} will be created! Are you sure?') | |
while True: | |
if a not in ['y', 'n']: | |
a = yes_or_no('There is nothing to do. Type agin') | |
else: | |
break | |
return a | |
def exec(cmd: str): | |
os.system(cmd) | |
def fill2(int) -> str: | |
return str(int).zfill(2) | |
def now_time() -> str: | |
import time | |
t = time.localtime() | |
y = t.tm_year | |
m = fill2(t.tm_mon) | |
d = fill2(t.tm_mday) | |
return f'{y}-{m}-{d}' | |
def main(): | |
args = cmd_args() | |
# print(args) | |
folder = now_time() + '-' + args[0] | |
path = os.path.join(args[1], folder) | |
# print(path) | |
anser = if_you_do(path) | |
if anser is 'y': | |
# mkdir and using vscode to open it | |
os.mkdir(path) | |
cmd = 'code ' + path | |
exec(cmd) | |
else: | |
# goodbye | |
pass | |
if __name__ == "__main__": | |
main() | |
# print(now_time()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment