2019-02-12 12:24:10 -08:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""
|
|
|
|
Fuzzy-find a file and edit it.
|
|
|
|
|
|
|
|
Dependencies
|
|
|
|
============
|
|
|
|
* fd
|
|
|
|
* fzf
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
2020-06-07 15:24:36 -07:00
|
|
|
from sys import platform
|
|
|
|
|
2019-02-12 12:24:10 -08:00
|
|
|
# ========== Constants ==========
|
2019-03-20 14:32:29 -07:00
|
|
|
# ----- Paths -----
|
2019-03-06 23:34:48 -08:00
|
|
|
BOOT_DIR = "/boot"
|
|
|
|
ETC_DIR = "/etc"
|
2019-02-12 12:24:10 -08:00
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
# ----- Exit Codes -----
|
2019-04-17 18:55:51 -07:00
|
|
|
E_INTERRUPT = 1
|
2019-02-12 12:24:10 -08:00
|
|
|
E_NOEDITORFOUND = 2
|
|
|
|
E_NOFILESELECTED = 3
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
# ----- Commands -----
|
2020-06-07 15:24:36 -07:00
|
|
|
|
|
|
|
# Options: show hidden files, null terminator, files only
|
|
|
|
# Optional arguments: show vcs files, show every file
|
|
|
|
FIND_CMD = shutil.which("fd")
|
2019-03-20 14:32:29 -07:00
|
|
|
FIND_OPTS = ["--hidden", "--print0", "--type", "f"]
|
|
|
|
EXTRA_FIND_OPTS = {"no_ignore_vcs": "--no-ignore", "no_ignore": "--no-ignore-vcs"}
|
|
|
|
|
2020-06-07 15:24:36 -07:00
|
|
|
# Options: null terminator, ignore case, print names matching all non-option arguments
|
|
|
|
LOCATE_CMD = shutil.which("locate")
|
2019-02-12 12:51:26 -08:00
|
|
|
|
2020-06-07 15:24:36 -07:00
|
|
|
# Options: read null terminator, auto-select if one option, exit if no options, print null terminator
|
|
|
|
FZF_CMD = shutil.which("fzf")
|
2019-03-20 14:32:29 -07:00
|
|
|
FZF_OPTS = ["--read0", "--select-1", "--exit-0", "--print0"]
|
|
|
|
|
2020-06-07 15:24:36 -07:00
|
|
|
# Platform-specific options
|
|
|
|
# macOS doesn't support GNU-style long options
|
|
|
|
if platform == "linux":
|
|
|
|
LOCATE_OPTS = ["--all", "--ignore-case", "--null"]
|
|
|
|
elif platform == "darwin":
|
|
|
|
LOCATE_OPTS = ["-0", "-i"]
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
# ----- Misc. -----
|
2019-03-06 23:34:48 -08:00
|
|
|
LOCALE = "utf-8"
|
2019-02-12 12:24:10 -08:00
|
|
|
|
|
|
|
|
|
|
|
# ========== Functions ==========
|
2019-03-20 14:32:29 -07:00
|
|
|
def select_editor(override=None):
|
2019-02-12 12:24:10 -08:00
|
|
|
"""Return a possible canonical path to an editor.
|
|
|
|
Select an editor from one of:
|
|
|
|
* -e, --editor
|
|
|
|
* $EDITOR
|
|
|
|
* Default of vim
|
|
|
|
|
|
|
|
In this order
|
|
|
|
|
|
|
|
If an editor cannot be resolved, then an Error is raised instead.
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
:param override: argument to override an editor
|
2019-02-12 12:24:10 -08:00
|
|
|
:returns: path to one of these editors
|
|
|
|
:rtype: str
|
|
|
|
:raises: FileNotFoundError if an editor could not be resolved
|
|
|
|
"""
|
2019-02-19 16:48:34 -08:00
|
|
|
editor = None
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
if override is not None:
|
|
|
|
editor = shutil.which(override)
|
2019-03-06 23:34:48 -08:00
|
|
|
elif "EDITOR" in os.environ:
|
|
|
|
editor = shutil.which(os.environ.get("EDITOR"))
|
|
|
|
elif shutil.which("vim") is not None:
|
|
|
|
editor = shutil.which("vim")
|
2019-02-19 16:48:34 -08:00
|
|
|
|
|
|
|
if editor is None:
|
2019-03-06 23:34:48 -08:00
|
|
|
raise FileNotFoundError("An editor could not be resolved")
|
2019-02-12 12:24:10 -08:00
|
|
|
|
2019-02-20 02:24:02 -08:00
|
|
|
return editor
|
|
|
|
|
2019-02-13 17:26:48 -08:00
|
|
|
|
2019-02-12 12:51:26 -08:00
|
|
|
def gen_editor_cmd(filename):
|
|
|
|
"""Generate a command line to run for editing a file based on
|
|
|
|
permissions.
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
This command does not pass extra options to the editor, hence
|
|
|
|
there are no arguments to pass for options.
|
|
|
|
|
2019-02-12 12:51:26 -08:00
|
|
|
:param filename: name of file to edit
|
|
|
|
:type filename: str or path-like object
|
|
|
|
:returns: command to execute to edit file
|
|
|
|
:rtype: list
|
|
|
|
"""
|
2019-03-20 14:32:29 -07:00
|
|
|
# Possible for a race condition to occur here
|
|
|
|
# What happens if the file or its metadata changes?
|
2019-02-12 12:51:26 -08:00
|
|
|
if os.access(filename, os.W_OK):
|
|
|
|
return [editor, filename]
|
|
|
|
else:
|
2019-03-06 23:34:48 -08:00
|
|
|
return ["sudo", "--edit", filename]
|
2019-02-12 12:51:26 -08:00
|
|
|
|
2019-02-12 12:24:10 -08:00
|
|
|
|
2019-02-13 17:26:48 -08:00
|
|
|
def run_fzf(files):
|
|
|
|
"""Run fzf on a stream of searched files for the user to select.
|
|
|
|
|
|
|
|
:param files: stream of null-terminated files to read
|
|
|
|
:type files: bytes stream (stdout of a completed process)
|
|
|
|
:returns: selected file
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2019-03-06 23:34:48 -08:00
|
|
|
selected_file = subprocess.run(
|
2019-03-20 14:32:29 -07:00
|
|
|
[FZF_CMD, *FZF_OPTS], input=files, stdout=subprocess.PIPE
|
2019-03-06 23:34:48 -08:00
|
|
|
).stdout
|
2019-02-13 17:26:48 -08:00
|
|
|
|
2019-03-06 23:34:48 -08:00
|
|
|
return selected_file.decode(LOCALE).strip("\x00")
|
2019-02-13 17:26:48 -08:00
|
|
|
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
def find_files(opts, directory=None):
|
2019-02-13 17:26:48 -08:00
|
|
|
"""Use a find-based program to locate files, then pass to fzf.
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
:param opts: options to pass to the find program
|
|
|
|
:type opts: list of str
|
2019-02-13 17:26:48 -08:00
|
|
|
:param directory: directory to search for files
|
|
|
|
:type directory: str
|
|
|
|
:returns: path of user-selected file
|
|
|
|
:rtype: bytes
|
|
|
|
"""
|
2019-03-20 14:32:29 -07:00
|
|
|
cmd = [FIND_CMD, *opts]
|
|
|
|
|
2019-02-13 17:26:48 -08:00
|
|
|
if directory is not None:
|
2019-03-06 23:34:48 -08:00
|
|
|
cmd.extend(["--", ".", directory])
|
2019-02-13 17:26:48 -08:00
|
|
|
|
|
|
|
return subprocess.run(cmd, capture_output=True).stdout
|
|
|
|
|
|
|
|
|
|
|
|
def locate_files(patterns):
|
|
|
|
"""Use a locate-based program to locate files, then pass to fzf.
|
|
|
|
|
|
|
|
:param patterns: patterns to pass to locate
|
|
|
|
:type patterns: list
|
|
|
|
:returns: path of user-selected file
|
|
|
|
:rtype: bytes
|
|
|
|
"""
|
|
|
|
cmd = [LOCATE_CMD] + LOCATE_OPTS
|
|
|
|
cmd.extend(patterns)
|
|
|
|
|
|
|
|
return subprocess.run(cmd, capture_output=True).stdout
|
|
|
|
|
|
|
|
|
2019-02-12 12:24:10 -08:00
|
|
|
# ========== Main Script ==========
|
2019-03-20 14:32:29 -07:00
|
|
|
if __name__ == "__main__":
|
2020-06-07 15:24:36 -07:00
|
|
|
# This script doesn't support Windows
|
|
|
|
if platform == "windows":
|
|
|
|
sys.exit(E_INTERRUPT)
|
|
|
|
|
2019-03-20 14:32:29 -07:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
"-b",
|
|
|
|
"--boot",
|
|
|
|
action="store_const",
|
|
|
|
const=BOOT_DIR,
|
|
|
|
dest="dir",
|
|
|
|
help="edit a file in /boot",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d", "--dir", dest="dir", type=str, help="edit a file in a given directory"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-E",
|
|
|
|
"--etc",
|
|
|
|
action="store_const",
|
|
|
|
const=ETC_DIR,
|
|
|
|
dest="dir",
|
|
|
|
help="edit a file in /etc",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-I",
|
|
|
|
"--no-ignore",
|
|
|
|
action="append_const",
|
|
|
|
const="--no-ignore",
|
|
|
|
dest="extra_find_opts",
|
|
|
|
help="do not respect .(git|fd)ignore files",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-i",
|
|
|
|
"--no-ignore-vcs",
|
|
|
|
action="append_const",
|
|
|
|
const="--no-ignore-vcs",
|
|
|
|
dest="extra_find_opts",
|
|
|
|
help="do not respect .gitignore files",
|
|
|
|
)
|
|
|
|
parser.add_argument("-e", "--editor", help="use a given editor")
|
|
|
|
parser.add_argument(
|
|
|
|
"patterns", type=str, nargs="*", help="patterns to pass to locate"
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
user_opts = [] if args.extra_find_opts is None else args.extra_find_opts
|
|
|
|
user_opts.extend(FIND_OPTS)
|
|
|
|
|
|
|
|
editor = ""
|
|
|
|
|
|
|
|
try:
|
|
|
|
editor = select_editor(args.editor)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
print(e)
|
|
|
|
exit(E_NOEDITORFOUND)
|
|
|
|
|
|
|
|
# If patterns were passed, use locate
|
|
|
|
# Otherwise check for -d and use fd
|
|
|
|
files = (
|
|
|
|
find_files(user_opts, args.dir)
|
|
|
|
if not args.patterns
|
|
|
|
else locate_files(args.patterns)
|
|
|
|
)
|
|
|
|
|
2019-04-17 18:55:51 -07:00
|
|
|
try:
|
|
|
|
selected_file = run_fzf(files)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
exit(E_INTERRUPT)
|
2019-03-20 14:32:29 -07:00
|
|
|
|
|
|
|
if selected_file != "":
|
|
|
|
cmd = gen_editor_cmd(selected_file)
|
|
|
|
subprocess.run(cmd)
|
|
|
|
else:
|
|
|
|
exit(E_NOFILESELECTED)
|