Skip to content

Commit 17135da

Browse files
ChrisJr404dbaty
authored andcommitted
Honor PSQL_EDITOR when opening the external editor
1 parent 5d5e082 commit 17135da

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Contributors:
152152
* Shayan Golshani (shgol)
153153
* Tommi Kyntölä (kynde)
154154
* Diego
155+
* Chris (ChrisJr404)
155156

156157
Creator:
157158
--------

‎changelog.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Bug fixes:
1010

1111
Features:
1212
---------
13+
* Honor the ``PSQL_EDITOR`` environment variable when opening the external
14+
editor (``\\e``, ``\\ev``, ``\\ef``, ``\\ne``), matching psql's precedence of
15+
``PSQL_EDITOR``, then ``EDITOR``, then ``VISUAL`` ([issue 1398](https://github.com/dbcli/pgcli/issues/1398)).
1316
* Add ``\\ne <name>`` to edit a named query in the external editor. Loads the
1417
named query's SQL into ``$EDITOR``; on save it is written back to the
1518
``[named queries]`` section, creating it if it does not exist. Complements

‎pgcli/main.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ def notify_callback(notify: Notify):
146146
)
147147

148148

149+
def get_editor():
150+
"""Pick the external editor for ``\\e``/``\\ev``/``\\ef``/``\\ne``.
151+
152+
Mirrors psql, which checks ``PSQL_EDITOR`` first, then ``EDITOR``, then
153+
``VISUAL``. Returning ``None`` when none are set lets click fall back to
154+
its platform default, so the behaviour is unchanged for anyone who wasn't
155+
setting ``PSQL_EDITOR``.
156+
"""
157+
return os.environ.get("PSQL_EDITOR") or os.environ.get("EDITOR") or os.environ.get("VISUAL") or None
158+
159+
149160
class PGCli:
150161
default_prompt = "\\u@\\h:\\d> "
151162
max_len_prompt = 30
@@ -331,7 +342,7 @@ def edit_named_query(self, pattern, **_):
331342
return [(None, None, None, "Usage: \\ne <name>")]
332343

333344
existing = NamedQueries.instance.get(name)
334-
sql, message = special.open_external_editor(sql=existing or "")
345+
sql, message = special.open_external_editor(sql=existing or "", editor=get_editor())
335346
if message:
336347
return [(None, None, None, message)]
337348

@@ -832,7 +843,7 @@ def handle_editor_command(self, text):
832843
query = self.pgexecute.view_definition(spec)
833844
elif editor_command == "\\ef":
834845
query = self.pgexecute.function_definition(spec)
835-
sql, message = special.open_external_editor(filename, sql=query)
846+
sql, message = special.open_external_editor(filename, sql=query, editor=get_editor())
836847
if message:
837848
# Something went wrong. Raise an exception and bail.
838849
raise RuntimeError(message)

‎tests/test_main.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
obfuscate_process_password,
1717
duration_in_words,
1818
format_output,
19+
get_editor,
1920
notify_callback,
2021
PGCli,
2122
OutputSettings,
@@ -681,3 +682,22 @@ def test_edit_named_query():
681682
# Missing name -> usage message.
682683
out = cli.edit_named_query("")
683684
assert "Usage" in out[0][3]
685+
686+
687+
def test_get_editor_precedence():
688+
"""PSQL_EDITOR wins over EDITOR/VISUAL, like psql; None when nothing is set."""
689+
env = {"PSQL_EDITOR": "psqled", "EDITOR": "myedit", "VISUAL": "myvisual"}
690+
with mock.patch.dict(os.environ, env, clear=False):
691+
assert get_editor() == "psqled"
692+
693+
# PSQL_EDITOR unset -> fall back to EDITOR.
694+
with mock.patch.dict(os.environ, {"EDITOR": "myedit", "VISUAL": "myvisual"}, clear=True):
695+
assert get_editor() == "myedit"
696+
697+
# Only VISUAL set.
698+
with mock.patch.dict(os.environ, {"VISUAL": "myvisual"}, clear=True):
699+
assert get_editor() == "myvisual"
700+
701+
# Nothing set -> None, so click uses its platform default.
702+
with mock.patch.dict(os.environ, {}, clear=True):
703+
assert get_editor() is None

0 commit comments

Comments
 (0)