active mode

This commit is contained in:
glmdev 2018-11-28 21:25:53 -06:00
parent d94f945a6a
commit b1bd51d66f
2 changed files with 53 additions and 14 deletions

View File

@ -19,4 +19,4 @@ This project is currently a work-in-progress. Only the fish shell is supported a
- Git Repo detection for ls - Git Repo detection for ls
- Clipboard functionality - Clipboard functionality
- sudo vim - sudo vim
- Active Mode - Binary File MIME Types

View File

@ -6,17 +6,23 @@ import configparser
# supported values: fish, bash, zsh # supported values: fish, bash, zsh
shell="fish" shell="fish"
# Defaults
print_file="cat" print_file="cat"
pretty_print_file="less -R" pretty_print_file="less -R"
edit_file="vim" edit_file="vim"
list_directory="ls --color=auto" list_directory="ls --color=auto"
change_dir="cd"
print_dir="pwd"
help_command="man"
# general function for setting a shell's environment variable
def set_runtime_var(name, value, options=""): def set_runtime_var(name, value, options=""):
if ( shell == "fish" ): if ( shell == "fish" ):
print("set "+name+" "+options+" \""+value+"\"") print("set "+name+" "+options+" \""+value+"\"")
elif ( shell == "bash" or shell == "zsh" ): elif ( shell == "bash" or shell == "zsh" ):
print("export "+name+"="+"\""+value+"\"") print("export "+name+"="+"\""+value+"\"")
# checks if a file is a binary file or a plaintext file
def is_binary_file(filepathname): def is_binary_file(filepathname):
textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x7f)) + bytearray(range(0x80, 0x100)) textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x7f)) + bytearray(range(0x80, 0x100))
is_binary_string = lambda bytes: bool(bytes.translate(None, textchars)) is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
@ -26,10 +32,12 @@ def is_binary_file(filepathname):
else: else:
return False return False
# check if a given program is in the path environment using which
def env_has(program): def env_has(program):
whichcall = subprocess.Popen(['which', program], stdout=subprocess.PIPE) whichcall = subprocess.Popen(['which', program], stdout=subprocess.PIPE)
return whichcall.returncode return whichcall.returncode
# get the number of lines in a file
def file_len(fname): def file_len(fname):
with open(fname) as f: with open(fname) as f:
for i, l in enumerate(f): for i, l in enumerate(f):
@ -42,65 +50,96 @@ subprocess.call(['mkdir', '-p', ENV_HOME+'/.config/do_what'])
if ( os.path.isfile(ENV_HOME+'/.config/do_what/what.config') ): if ( os.path.isfile(ENV_HOME+'/.config/do_what/what.config') ):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(ENV_HOME+'/.config/do_what/what.config') config.read(ENV_HOME+'/.config/do_what/what.config')
# TODO add else default configs & config flush
if ( 'DEFAULT' in config ): if ( 'DEFAULT' in config ):
if ( 'print_file' in config['DEFAULT'] ): if ( 'print_file' in config['DEFAULT'] ):
print_file = config['DEFAULT']['print_file'] print_file = config['DEFAULT']['print_file']
if ( 'pretty_print_file' in config['DEFAULT'] ): if ( 'pretty_print_file' in config['DEFAULT'] ):
pretty_print_file = config['DEFAULT']['pretty_print_file'] pretty_print_file = config['DEFAULT']['pretty_print_file']
if ( 'edit_file' in config['DEFAULT'] ): if ( 'edit_file' in config['DEFAULT'] ):
pretty_print_file = config['DEFAULT']['edit_file'] edit_file = config['DEFAULT']['edit_file']
if ( 'list_directory' in config['DEFAULT'] ): if ( 'list_directory' in config['DEFAULT'] ):
list_directory = config['DEFAULT']['list_directory'] list_directory = config['DEFAULT']['list_directory']
if ( 'help_command' in config['DEFAULT'] ):
help_command = config['DEFAULT']['help_command']
else: else:
config = configparser.ConfigParser() config = configparser.ConfigParser()
config['DEFAULT'] = { config['DEFAULT'] = {
'list_directory': list_directory, 'list_directory': list_directory,
'edit_file': edit_file, 'edit_file': edit_file,
'pretty_print_file': pretty_print_file, 'pretty_print_file': pretty_print_file,
'print_file': print_file 'print_file': print_file,
'help_command': help_command,
} }
print("echo Default config loaded.") print("echo Default config loaded.")
with open(ENV_HOME+'/.config/do_what/what.config', 'w') as configfile: with open(ENV_HOME+'/.config/do_what/what.config', 'w') as configfile:
config.write(configfile) config.write(configfile)
configfile.close() configfile.close()
# check if active mode is enabled
argiter = 0
active = False
for arg in sys.argv:
if ( str(arg) == '-' ):
active = True
break
argiter += 1
if ( len(sys.argv) == 1 ): if ( (not active and len(sys.argv) == 1) or (active and len(sys.argv) == 2) ):
# list the contents of the current directory # list the contents of the current directory
if ( not active ):
print(list_directory) print(list_directory)
elif ( len(sys.argv) == 2 ): # print the working directory (active)
elif ( active ):
print(print_dir)
elif ( (not active and len(sys.argv) == 2) or (active and len(sys.argv) == 3) ):
# absolutize the path for Python # absolutize the path for Python
filearg = sys.argv[1] filearg = sys.argv[1]
path = sys.argv[1] path = sys.argv[1]
if ( path[0] != "/" and path[0] != "~" and path[0] != "." ): if ( path[0] != "/" and path[0] != "~" and path[0] != "." ):
path = os.getcwd()+"/"+path path = os.getcwd()+"/"+path
# check if path is a file or directory # if directory, list its contents
if ( os.path.isdir(path) ): if ( os.path.isdir(path) and not active ):
print(list_directory+" "+path) print(list_directory+" "+path)
# if directory, change into it (active)
elif ( os.path.isfile(path) ): elif ( os.path.isdir(path) and active ):
print(change_dir+" "+path)
# if file, display its contents
elif ( os.path.isfile(path) and not active ):
# check if file is binary # check if file is binary
if ( is_binary_file(path) ): if ( is_binary_file(path) ):
print("echo Binary file.") print("echo Binary file.")
else: else:
trows, tcolumns = os.popen('stty size', 'r').read().split() trows, tcolumns = os.popen('stty size', 'r').read().split()
# if file is taller than the terminal, pipe it to a pretty-print display
if ( int(trows) - 5 < int(file_len(path)) ): if ( int(trows) - 5 < int(file_len(path)) ):
set_runtime_var('LESSOPEN', '| /usr/share/source-highlight/src-hilite-lesspipe.sh %s') set_runtime_var('LESSOPEN', '| /usr/share/source-highlight/src-hilite-lesspipe.sh %s')
set_runtime_var('LESS', ' -R ') set_runtime_var('LESS', ' -R ')
print(pretty_print_file+" "+path) print(pretty_print_file+" "+path)
# if file is shorter than the terminal, print its contents
else: else:
print(print_file+" "+path) print(print_file+" "+path)
# if file, open in editor (active)
elif ( os.path.isfile(path) and active ):
if ( is_binary_file(path) ):
print("echo Binary file.")
else:
print(edit_file+" "+path)
else: else:
# check if file is a path program, then list using which # check if file is a path program, then list using which/man on active
whichcall = subprocess.Popen(['which', filearg], stdout=subprocess.PIPE) whichcall = subprocess.Popen(['which', filearg], stdout=subprocess.PIPE)
result = whichcall.communicate()[0] result = whichcall.communicate()[0]
statuscode = whichcall.returncode statuscode = whichcall.returncode
if ( int(statuscode) == 0 ): # print the location of the program
print("echo "+filearg+": "+result.decode('utf-8')) if ( int(statuscode) == 0 and not active ):
print("echo "+result.decode('utf-8'))
# display the help page for the program (active)
elif ( int(statuscode) == 0 and active ):
print(help_command+" "+filearg)
else: else:
print("echo Unknown file.") print("echo Unknown file/directory.")