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
- Clipboard functionality
- sudo vim
- Active Mode
- Binary File MIME Types

View File

@ -6,17 +6,23 @@ import configparser
# supported values: fish, bash, zsh
shell="fish"
# Defaults
print_file="cat"
pretty_print_file="less -R"
edit_file="vim"
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=""):
if ( shell == "fish" ):
print("set "+name+" "+options+" \""+value+"\"")
elif ( shell == "bash" or shell == "zsh" ):
print("export "+name+"="+"\""+value+"\"")
# checks if a file is a binary file or a plaintext file
def is_binary_file(filepathname):
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))
@ -26,10 +32,12 @@ def is_binary_file(filepathname):
else:
return False
# check if a given program is in the path environment using which
def env_has(program):
whichcall = subprocess.Popen(['which', program], stdout=subprocess.PIPE)
return whichcall.returncode
# get the number of lines in a file
def file_len(fname):
with open(fname) as 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') ):
config = configparser.ConfigParser()
config.read(ENV_HOME+'/.config/do_what/what.config')
# TODO add else default configs & config flush
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']
edit_file = config['DEFAULT']['edit_file']
if ( 'list_directory' in config['DEFAULT'] ):
list_directory = config['DEFAULT']['list_directory']
if ( 'help_command' in config['DEFAULT'] ):
help_command = config['DEFAULT']['help_command']
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_file': print_file,
'help_command': help_command,
}
print("echo Default config loaded.")
with open(ENV_HOME+'/.config/do_what/what.config', 'w') as configfile:
config.write(configfile)
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
print(list_directory)
elif ( len(sys.argv) == 2 ):
if ( not active ):
print(list_directory)
# 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
filearg = sys.argv[1]
path = sys.argv[1]
if ( path[0] != "/" and path[0] != "~" and path[0] != "." ):
path = os.getcwd()+"/"+path
# check if path is a file or directory
if ( os.path.isdir(path) ):
# if directory, list its contents
if ( os.path.isdir(path) and not active ):
print(list_directory+" "+path)
elif ( os.path.isfile(path) ):
# if directory, change into it (active)
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
if ( is_binary_file(path) ):
print("echo Binary file.")
else:
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)) ):
set_runtime_var('LESSOPEN', '| /usr/share/source-highlight/src-hilite-lesspipe.sh %s')
set_runtime_var('LESS', ' -R ')
print(pretty_print_file+" "+path)
# if file is shorter than the terminal, print its contents
else:
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:
# 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)
result = whichcall.communicate()[0]
statuscode = whichcall.returncode
if ( int(statuscode) == 0 ):
print("echo "+filearg+": "+result.decode('utf-8'))
# print the location of the program
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:
print("echo Unknown file.")
print("echo Unknown file/directory.")