Read filenames as bytes and general code cleanup
This commit is contained in:
parent
4569e97200
commit
f8dd68edc0
33
fedit.py
33
fedit.py
@ -26,7 +26,9 @@ E_NOFILESELECTED = 3
|
|||||||
FIND_CMD = shutil.which('fd')
|
FIND_CMD = shutil.which('fd')
|
||||||
FIND_OPTS = ['--hidden', '--print0', '--type', 'f', '--no-ignore-vcs']
|
FIND_OPTS = ['--hidden', '--print0', '--type', 'f', '--no-ignore-vcs']
|
||||||
FZF_CMD = shutil.which('fzf')
|
FZF_CMD = shutil.which('fzf')
|
||||||
FZF_OPTS = ['--read0', '--select-1', '--exit-0']
|
FZF_OPTS = ['--read0', '--select-1', '--exit-0', '--print0']
|
||||||
|
|
||||||
|
LOCALE = 'utf-8'
|
||||||
|
|
||||||
|
|
||||||
# ========== Functions ==========
|
# ========== Functions ==========
|
||||||
@ -55,6 +57,21 @@ def select_editor(editor_override=None):
|
|||||||
else:
|
else:
|
||||||
raise FileNotFoundError('An editor could not be resolved')
|
raise FileNotFoundError('An editor could not be resolved')
|
||||||
|
|
||||||
|
def gen_editor_cmd(filename):
|
||||||
|
"""Generate a command line to run for editing a file based on
|
||||||
|
permissions.
|
||||||
|
|
||||||
|
:param filename: name of file to edit
|
||||||
|
:type filename: str or path-like object
|
||||||
|
:returns: command to execute to edit file
|
||||||
|
:rtype: list
|
||||||
|
"""
|
||||||
|
# possible for a race condition to occur here
|
||||||
|
if os.access(filename, os.W_OK):
|
||||||
|
return [editor, filename]
|
||||||
|
else:
|
||||||
|
return ['sudo', '--edit', filename]
|
||||||
|
|
||||||
|
|
||||||
# ========== Main Script ==========
|
# ========== Main Script ==========
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
@ -93,14 +110,16 @@ if args.dir is not None:
|
|||||||
final_find_cmd.extend(extra_opts)
|
final_find_cmd.extend(extra_opts)
|
||||||
|
|
||||||
files = subprocess.run(final_find_cmd,
|
files = subprocess.run(final_find_cmd,
|
||||||
text=True,
|
|
||||||
capture_output=True)
|
capture_output=True)
|
||||||
filename = subprocess.run([FZF_CMD] + FZF_OPTS,
|
fzf_output = subprocess.run([FZF_CMD] + FZF_OPTS,
|
||||||
input=files.stdout,
|
input=files.stdout,
|
||||||
text=True,
|
stdout=subprocess.PIPE).stdout
|
||||||
stdout=subprocess.PIPE).stdout
|
|
||||||
|
# filename is null terminated
|
||||||
|
filename = fzf_output.decode(LOCALE).strip('\x00')
|
||||||
|
|
||||||
if filename is not None:
|
if filename is not None:
|
||||||
subprocess.run([editor, filename.strip('\n')])
|
cmd = gen_editor_cmd(filename.strip('\n'))
|
||||||
|
subprocess.run(cmd)
|
||||||
else:
|
else:
|
||||||
exit(E_NOFILESELECTED)
|
exit(E_NOFILESELECTED)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user