From 401d09992cc5cc61ef5461a4e873d1039f0b1529 Mon Sep 17 00:00:00 2001 From: glmdev Date: Wed, 28 Nov 2018 14:27:01 -0600 Subject: [PATCH] config support --- README.md | 3 ++- do_what.py | 36 +++++++++++++++++++++++++++++++++++- what.config | 5 +++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 what.config diff --git a/README.md b/README.md index b22cfff..7942a0d 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,6 @@ This project is currently a work-in-progress. Only the fish shell is supported a - Command Memory/Env Support - Double wh does an la, not ls - Git Repo detection for ls - - Dynamic handler selection (e.g. exa vs ls) - Clipboard functionality + - sudo vim + - Active Mode diff --git a/do_what.py b/do_what.py index fdb4944..e209689 100644 --- a/do_what.py +++ b/do_what.py @@ -1,9 +1,10 @@ import sys import os import subprocess +import configparser # supported values: fish, bash -shell="bash" +shell="fish" print_file="cat" pretty_print_file="less -R" @@ -25,12 +26,45 @@ def is_binary_file(filepathname): else: return False +def env_has(program): + whichcall = subprocess.Popen(['which', program], stdout=subprocess.PIPE) + return whichcall.returncode + def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 +# bootstrap the config +ENV_HOME=os.environ['HOME'] +subprocess.call(['mkdir', '-p', ENV_HOME+'/.config']) +if ( os.path.isfile(ENV_HOME+'/.config/what.config') ): + config = configparser.ConfigParser() + config.read(ENV_HOME+'/.config/what.config') + if ( 'DEFAULT' in config ): + if ( 'print_file' in config['DEFAULT'] ): + print_file = config['DEFAULT']['print_file'] + if ( 'pretty_print_file' in config['DEFAULT'] ): + pretty_print_file = config['DEFAULT']['pretty_print_file'] + if ( 'edit_file' in config['DEFAULT'] ): + pretty_print_file = config['DEFAULT']['edit_file'] + if ( 'list_directory' in config['DEFAULT'] ): + list_directory = config['DEFAULT']['list_directory'] +else: + config = configparser.ConfigParser() + config['DEFAULT'] = { + 'list_directory': list_directory, + 'edit_file': edit_file, + 'pretty_print_file': pretty_print_file, + 'print_file': print_file + } + print("echo Default config loaded.") + with open(ENV_HOME+'/.config/what.config', 'w') as configfile: + config.write(configfile) + configfile.close() + + if ( len(sys.argv) == 1 ): # list the contents of the current directory print(list_directory) diff --git a/what.config b/what.config new file mode 100644 index 0000000..dcef7e5 --- /dev/null +++ b/what.config @@ -0,0 +1,5 @@ +[DEFAULT] +print_file = cat +pretty_print_file = less -R +edit_file = vim +list_directory = ls --color=auto