add handlers for Binary files

master
glmdev 6 years ago
parent ba8d4473d0
commit 69b89af118

@ -17,4 +17,3 @@ This project is currently a work-in-progress. Only the fish shell is supported a
- Command Memory/Env Support - Command Memory/Env Support
- Double wh does an la, not ls - Double wh does an la, not ls
- Clipboard functionality - Clipboard functionality
- Binary File MIME Types

@ -2,6 +2,9 @@ import sys
import os import os
import subprocess import subprocess
import configparser import configparser
import magic
mime = magic.Magic(mime=True)
# supported values: fish, bash, zsh # supported values: fish, bash, zsh
shell="fish" shell="fish"
@ -16,6 +19,7 @@ change_dir="cd"
print_dir="pwd" print_dir="pwd"
help_command="man" help_command="man"
path_locator="which" path_locator="which"
open_file="xdg-open"
# general function for setting a shell's environment variable # general function for setting a shell's environment variable
def set_runtime_var(name, value, options=""): def set_runtime_var(name, value, options=""):
@ -80,6 +84,8 @@ if ( os.path.isfile(ENV_HOME+'/.config/do_what/what.config') ):
path_locator = config['DEFAULT']['path_locator'] path_locator = config['DEFAULT']['path_locator']
if ( 'list_git_directory' in config['DEFAULT'] ): if ( 'list_git_directory' in config['DEFAULT'] ):
list_git_directory = config['DEFAULT']['list_git_directory'] list_git_directory = config['DEFAULT']['list_git_directory']
if ( 'open_file' in config['DEFAULT'] ):
open_file = config['DEFAULT']['open_file']
else: else:
config = configparser.ConfigParser() config = configparser.ConfigParser()
config['DEFAULT'] = { config['DEFAULT'] = {
@ -90,6 +96,7 @@ else:
'help_command': help_command, 'help_command': help_command,
'path_locator': path_locator, 'path_locator': path_locator,
'list_git_directory': list_git_directory, 'list_git_directory': list_git_directory,
'open_file': open_file
} }
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:
@ -135,7 +142,8 @@ elif ( (not active and len(sys.argv) == 2) or (active and len(sys.argv) == 3) ):
elif ( os.path.isfile(path) and not active ): 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.") filemime = mime.from_file(path)
print("echo Binary file: "+filemime)
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 file is taller than the terminal, pipe it to a pretty-print display
@ -149,7 +157,7 @@ elif ( (not active and len(sys.argv) == 2) or (active and len(sys.argv) == 3) ):
# if file, open in editor (active) # if file, open in editor (active)
elif ( os.path.isfile(path) and active ): elif ( os.path.isfile(path) and active ):
if ( is_binary_file(path) ): if ( is_binary_file(path) ):
print("echo Binary file.") print(open_file+" "+path)
else: else:
if ( os.access(path, os.W_OK) ): if ( os.access(path, os.W_OK) ):
print(edit_file+" "+path) print(edit_file+" "+path)

@ -1,2 +1,2 @@
source-highlight source-highlight
pip python-magic

Loading…
Cancel
Save