Browse Source

add handlers for Binary files

glmdev 4 months ago
parent
commit
69b89af118
3 changed files with 11 additions and 4 deletions
  1. 0
    1
      README.md
  2. 10
    2
      do_what.py
  3. 1
    1
      requires.txt

+ 0
- 1
README.md View File

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

+ 10
- 2
do_what.py View File

@@ -2,6 +2,9 @@ import sys
2 2
 import os
3 3
 import subprocess
4 4
 import configparser
5
+import magic
6
+
7
+mime = magic.Magic(mime=True)
5 8
 
6 9
 # supported values: fish, bash, zsh
7 10
 shell="fish"
@@ -16,6 +19,7 @@ change_dir="cd"
16 19
 print_dir="pwd"
17 20
 help_command="man"
18 21
 path_locator="which"
22
+open_file="xdg-open"
19 23
 
20 24
 # general function for setting a shell's environment variable
21 25
 def set_runtime_var(name, value, options=""):
@@ -80,6 +84,8 @@ if ( os.path.isfile(ENV_HOME+'/.config/do_what/what.config') ):
80 84
 			path_locator = config['DEFAULT']['path_locator']
81 85
 		if ( 'list_git_directory' in config['DEFAULT'] ):
82 86
 			list_git_directory = config['DEFAULT']['list_git_directory']
87
+		if ( 'open_file' in config['DEFAULT'] ):
88
+			open_file = config['DEFAULT']['open_file']
83 89
 else:
84 90
 	config = configparser.ConfigParser()
85 91
 	config['DEFAULT'] = {
@@ -90,6 +96,7 @@ else:
90 96
 		'help_command': help_command,
91 97
 		'path_locator': path_locator,
92 98
 		'list_git_directory': list_git_directory,
99
+		'open_file': open_file
93 100
 	}
94 101
 	print("echo Default config loaded.")
95 102
 	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) ):
135 142
 	elif ( os.path.isfile(path) and not active ):
136 143
 		# check if file is binary
137 144
 		if ( is_binary_file(path) ):
138
-			print("echo Binary file.")
145
+			filemime = mime.from_file(path)
146
+			print("echo Binary file: "+filemime)
139 147
 		else:
140 148
 			trows, tcolumns = os.popen('stty size', 'r').read().split()
141 149
 			# 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) ):
149 157
 	# if file, open in editor (active)
150 158
 	elif ( os.path.isfile(path) and active ):
151 159
 		if ( is_binary_file(path) ):
152
-			print("echo Binary file.")
160
+			print(open_file+" "+path)
153 161
 		else:
154 162
 			if ( os.access(path, os.W_OK) ):
155 163
 				print(edit_file+" "+path)

+ 1
- 1
requires.txt View File

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

Loading…
Cancel
Save