Skip to content

Commit

Permalink
Platform specific config dir and default editor
Browse files Browse the repository at this point in the history
  • Loading branch information
bulletmark committed Aug 21, 2023
1 parent a03ed5b commit 06a82a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ the following ways:
23. Contrary to what it's name implies, `vidir` actually respects your
`$EDITOR` variable and runs your preferred editor like `edir` does
but `edir` has been given a generic name to make this more apparent.
If `$EDITOR` is not set then `edir` uses a default editor
appropriate to your system.

24. `vidir` returns status code 0 if all files successful, or 1 if any
error. `edir` returns 0 if all files successful, 1 if some had
Expand Down Expand Up @@ -263,7 +265,6 @@ To upgrade:
$ pipx upgrade edir
```

Edir runs on pure Python. No 3rd party packages are required.
[Git](https://git-scm.com/) must be installed if you want to use the git
options. A trash program such as
[trash-cli](https://github.com/andreafrancia/trash-cli) package is
Expand All @@ -272,8 +273,8 @@ required if you want `-t/--trash` functionality.
### EDIR_EDITOR Environment Variable

`edir` selects your editor from the first environment value found of:
`$EDIR_EDITOR`, `$VISUAL`, `$EDITOR`, then falls back to "vi" if
none of these are set.
`$EDIR_EDITOR` or `$EDITOR`, then guesses a fallback default editor
appropriate to your system if neither of these are set.

You can also set `EDIR_EDITOR` explicitly to an editor + arguments
string if you want `edir` to call your editor with specific arguments.
Expand Down Expand Up @@ -370,9 +371,9 @@ options:
--suffix SUFFIX specify suffix for editor file, default=".sh"
-V, --version show edir version
Note you can set default starting options in ~/.config/edir-flags.conf. The
negation options (i.e. the --no-* options and their shortforms) allow you to
temporarily override your defaults.
Note you can set default starting options in $HOME/.config/edir-
flags.conf. The negation options (i.e. the --no-* options and their
shortforms) allow you to temporarily override your defaults.
```

## Embed in Ranger File Manager
Expand Down
22 changes: 14 additions & 8 deletions edir.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
from pathlib import Path
from typing import List, Optional, TextIO, Tuple

from platformdirs import user_config_path

# Some constants
PROG = Path(sys.argv[0]).stem
CNFFILE = Path(os.getenv('XDG_CONFIG_HOME', '~/.config'),
f'{PROG}-flags.conf')
CNFFILE = user_config_path() / f'{PROG}-flags.conf'
EDITOR = PROG.upper() + '_EDITOR'
SUFFIX = '.sh'

Expand All @@ -45,6 +46,13 @@
gitfiles = set()
counts = [0, 0]

EDITORS = {'Windows': 'notepad', 'Darwin': 'open -e', 'default': 'vim'}

def get_default_editor() -> str:
'Return default editor for this system'
from platform import system
return EDITORS.get(system(), EDITORS['default'])

def log(msg: str, error: Optional[str] = None) -> None:
'Output given message with appropriate color'
counts[bool(error)] += 1
Expand Down Expand Up @@ -294,9 +302,8 @@ def readfile(cls, fp: TextIO) -> None:

def editfile(filename: Path) -> None:
'Run the editor command'
# Use explicit editor or choose default
editor = os.getenv(EDITOR) or os.getenv('VISUAL') or \
os.getenv('EDITOR') or 'vi'
# Use explicit user defined editor or choose system default
editor = os.getenv(EDITOR) or os.getenv('EDITOR') or get_default_editor()
editcmd = shlex.split(editor) + [str(filename)]

# Run the editor ..
Expand Down Expand Up @@ -382,9 +389,8 @@ def main() -> int:

# Merge in default args from user config file. Then parse the
# command line.
cnffile = CNFFILE.expanduser()
if cnffile.exists():
with cnffile.open() as fp:
if CNFFILE.exists():
with CNFFILE.open() as fp:
lines = [re.sub(r'#.*$', '', line).strip() for line in fp]
cnflines = ' '.join(lines).strip()
else:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ classifiers = [
]
dynamic = ["version"]
dependencies = [
"platformdirs",
"importlib-metadata; python_version < '3.8'",
]

Expand Down

0 comments on commit 06a82a4

Please sign in to comment.