add clipboard functionality
This commit is contained in:
parent
69b89af118
commit
64ff921a36
@ -16,4 +16,5 @@ This project is currently a work-in-progress. Only the fish shell is supported a
|
|||||||
- Installation & Generalization
|
- Installation & Generalization
|
||||||
- Command Memory/Env Support
|
- Command Memory/Env Support
|
||||||
- Double wh does an la, not ls
|
- Double wh does an la, not ls
|
||||||
- Clipboard functionality
|
- directory history for ls'ing back
|
||||||
|
- improve Git detection
|
||||||
|
107
do_what.py
107
do_what.py
@ -3,6 +3,7 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import configparser
|
import configparser
|
||||||
import magic
|
import magic
|
||||||
|
import json
|
||||||
|
|
||||||
mime = magic.Magic(mime=True)
|
mime = magic.Magic(mime=True)
|
||||||
|
|
||||||
@ -60,7 +61,12 @@ 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):
|
||||||
pass
|
pass
|
||||||
return i + 1
|
try:
|
||||||
|
i
|
||||||
|
except NameError:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return i + 1
|
||||||
|
|
||||||
# bootstrap the config
|
# bootstrap the config
|
||||||
ENV_HOME=os.environ['HOME']
|
ENV_HOME=os.environ['HOME']
|
||||||
@ -68,7 +74,7 @@ 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
|
# TODO add else default configs
|
||||||
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']
|
||||||
@ -112,6 +118,97 @@ for arg in sys.argv:
|
|||||||
break
|
break
|
||||||
argiter += 1
|
argiter += 1
|
||||||
|
|
||||||
|
# absolutize the path for python
|
||||||
|
if ( (active and len(sys.argv) >= 3) or (not active and len(sys.argv) >= 2) ):
|
||||||
|
filearg = sys.argv[1]
|
||||||
|
path = sys.argv[1]
|
||||||
|
if ( path[0] != "/" and path[0] != "~" and path[0] != "." ):
|
||||||
|
path = os.getcwd()+"/"+path
|
||||||
|
elif ( path[0] == "." and len(path) > 1 and ( path[:2] == "./" ) ):
|
||||||
|
path = os.getcwd()+path[1:]
|
||||||
|
elif ( path[0] == "." and len(path) > 1 and ( path[:2] == ".." ) ):
|
||||||
|
path = os.getcwd()+"/"+path
|
||||||
|
elif ( path[0] == "." and len(path) == 1 ):
|
||||||
|
path = os.getcwd()
|
||||||
|
|
||||||
|
# CLIpboard (ha.)
|
||||||
|
if ( active and len(sys.argv) > argiter+1 and sys.argv[argiter+1] == 'c' ):
|
||||||
|
subprocess.call(['rm', '-rf', ENV_HOME+'/.config/do_what/clipboard'])
|
||||||
|
subprocess.call(['touch', ENV_HOME+'/.config/do_what/clipboard'])
|
||||||
|
if ( filearg == '-' ):
|
||||||
|
path = os.getcwd()
|
||||||
|
if ( os.path.isdir(path) or os.path.isfile(path) ):
|
||||||
|
with open(ENV_HOME+"/.config/do_what/clipboard", "w") as cb:
|
||||||
|
cbdata = { 'action': 'copy', 'path': path }
|
||||||
|
json.dump(cbdata, cb)
|
||||||
|
print("echo Copied: "+path)
|
||||||
|
exit()
|
||||||
|
else:
|
||||||
|
print("echo Unknown file/directory.")
|
||||||
|
exit()
|
||||||
|
elif ( active and len(sys.argv) > argiter+1 and sys.argv[argiter+1] == 'x' ):
|
||||||
|
subprocess.call(['rm', '-rf', ENV_HOME+'/.config/do_what/clipboard'])
|
||||||
|
subprocess.call(['touch', ENV_HOME+'/.config/do_what/clipboard'])
|
||||||
|
if ( filearg == '-' ):
|
||||||
|
path = os.getcwd()
|
||||||
|
if ( os.path.isdir(path) or os.path.isfile(path) ):
|
||||||
|
with open(ENV_HOME+"/.config/do_what/clipboard", "w") as cb:
|
||||||
|
cbdata = { 'action': 'cut', 'path': path }
|
||||||
|
json.dump(cbdata, cb)
|
||||||
|
print("echo Cut: "+path)
|
||||||
|
exit()
|
||||||
|
else:
|
||||||
|
print("echo Unknown file/directory.")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
elif ( active and len(sys.argv) > argiter+1 and sys.argv[argiter+1] == 'p' ):
|
||||||
|
if ( os.path.isfile(ENV_HOME+"/.config/do_what/clipboard") ):
|
||||||
|
with open(ENV_HOME+"/.config/do_what/clipboard", "r") as cb:
|
||||||
|
try:
|
||||||
|
cbdata = json.load(cb)
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
print("echo Invalid clipboard file.")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
print("echo \""+str(cbdata)+"\"")
|
||||||
|
|
||||||
|
if ( cbdata['action'] == 'copy' ):
|
||||||
|
file_op = 'cp'
|
||||||
|
elif ( cbdata['action'] == 'cut' ):
|
||||||
|
file_op = 'mv'
|
||||||
|
|
||||||
|
if ( os.path.isdir( cbdata['path'] ) and filearg == '-' ):
|
||||||
|
print("echo Pasted directory: "+cbdata['path'])
|
||||||
|
print(file_op+" -r "+cbdata['path']+" .")
|
||||||
|
elif ( os.path.isdir( cbdata['path'] ) and os.path.isdir( path ) ):
|
||||||
|
print("echo Pasted directory "+cbdata['path']+" in "+path)
|
||||||
|
print(file_op+" -r "+cbdata['path']+" "+path)
|
||||||
|
elif ( os.path.isdir( cbdata['path'] ) and os.path.isfile( path ) ):
|
||||||
|
print("echo Cannot paste directory into regular file.")
|
||||||
|
print("echo \"("+cbdata['path']+" -> "+path+")\"")
|
||||||
|
elif ( os.path.isfile( cbdata['path'] ) and filearg == '-' ):
|
||||||
|
print("echo Pasted file: "+cbdata['path'])
|
||||||
|
print(file_op+" "+cbdata['path']+" .")
|
||||||
|
elif ( os.path.isfile( cbdata['path'] ) and os.path.isdir( path ) ):
|
||||||
|
print("echo Pasted file "+cbdata['path']+" in directory "+path)
|
||||||
|
print(file_op+" "+cbdata['path']+" "+path)
|
||||||
|
elif ( os.path.isfile( cbdata['path'] ) and os.path.isfile( path ) ):
|
||||||
|
print("echo Pasted file "+cbdata['path']+" over existing file "+path)
|
||||||
|
print(file_op+" "+cbdata['path']+" "+path)
|
||||||
|
elif ( os.path.isfile( cbdata['path'] ) and filearg != '-' ):
|
||||||
|
print("echo Pasted file "+cbdata['path']+" as new file "+path)
|
||||||
|
print(file_op+" "+cbdata['path']+" "+path)
|
||||||
|
elif ( os.path.isdir( cbdata['path'] ) and filearg != '-' ):
|
||||||
|
print("echo Pasted directory "+cbdata['path']+" as new directory "+path)
|
||||||
|
print(file_op+" "+cbdata['path']+" "+path)
|
||||||
|
else:
|
||||||
|
print("echo Invalid clipboard. Use \\\'c\\\' directive to copy a file or directory.")
|
||||||
|
else:
|
||||||
|
print("echo Empty clipboard. Use \\\'c\\\' directive to copy a file or directory.")
|
||||||
|
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# File Operations
|
||||||
if ( (not active and len(sys.argv) == 1) or (active and len(sys.argv) == 2) ):
|
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 ):
|
if ( not active ):
|
||||||
@ -123,12 +220,6 @@ if ( (not active and len(sys.argv) == 1) or (active and len(sys.argv) == 2) ):
|
|||||||
elif ( active ):
|
elif ( active ):
|
||||||
print(print_dir)
|
print(print_dir)
|
||||||
elif ( (not active and len(sys.argv) == 2) or (active and len(sys.argv) == 3) ):
|
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
|
|
||||||
|
|
||||||
# if directory, list its contents
|
# if directory, list its contents
|
||||||
if ( os.path.isdir(path) and not active ):
|
if ( os.path.isdir(path) and not active ):
|
||||||
if ( os.path.isdir(path+"/.git") ):
|
if ( os.path.isdir(path+"/.git") ):
|
||||||
|
Loading…
Reference in New Issue
Block a user