Skip to content

Commit 6489f0a

Browse files
committed
binary: fix boolean environment variable parsing
1 parent 967be08 commit 6489f0a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

binary.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,35 @@
1818
from simple_websocket_server import WebSocket
1919
from simple_websocket_server import WebSocketServer
2020

21-
BUILD_VERSION: str = "v0.5.0"
21+
BUILD_VERSION: str = "v0.5.1"
2222

2323
WINDOWS: bool = os.name == "nt"
2424
LOCALHOST: str = "127.0.0.1" if WINDOWS else "localhost"
25-
SUPER_QUIET: bool = bool(os.environ.get("NVIM_GHOST_SUPER_QUIET", False))
26-
SERVER_PORT: str = os.environ.get("GHOSTTEXT_SERVER_PORT", "4001")
27-
FOCUSED_NVIM_ADDRESS = os.environ.get("NVIM_LISTEN_ADDRESS", None)
2825
LOGGING_ENABLED: bool = False
29-
VERBOSE_LOGGING: bool = bool(os.environ.get("NVIM_GHOST_VERBOSE_LOGGING"))
30-
if os.environ.get("NVIM_GHOST_LOGGING_ENABLED") is not None:
31-
if os.environ.get("NVIM_GHOST_LOGGING_ENABLED").isdigit():
32-
LOGGING_ENABLED = bool(int(os.environ.get("NVIM_GHOST_LOGGING_ENABLED")))
33-
else:
34-
sys.exit("Invalid value of $NVIM_GHOST_LOGGING_ENABLED")
3526

3627

28+
def envbool(envvar: str, default: str = False) -> bool:
29+
val = os.environ.get(envvar)
30+
if val is None:
31+
return False
32+
if val.isdigit():
33+
val = int(val)
34+
if val in (0, 1):
35+
return bool(val)
36+
raise ValueError(
37+
f"The environment variable '{envvar}', if set, should be set to either 0 or 1"
38+
)
39+
40+
41+
AUTOEXIT: bool = envbool("NVIM_GHOST_AUTO_EXIT")
42+
SUPER_QUIET: bool = envbool("NVIM_GHOST_SUPER_QUIET")
43+
LOGGING_ENABLED: bool = envbool("NVIM_GHOST_LOGGING_ENABLED")
44+
VERBOSE_LOGGING: bool = envbool("NVIM_GHOST_VERBOSE_LOGGING")
45+
46+
FOCUSED_NVIM_ADDRESS = os.environ.get("NVIM_LISTEN_ADDRESS", None)
47+
NVIM_ADDRESSES = [FOCUSED_NVIM_ADDRESS] if FOCUSED_NVIM_ADDRESS is not None else []
48+
49+
SERVER_PORT: str = os.environ.get("GHOSTTEXT_SERVER_PORT", "4001")
3750
if not SERVER_PORT.isdigit():
3851
if FOCUSED_NVIM_ADDRESS is not None:
3952
with pynvim.attach("socket", path=FOCUSED_NVIM_ADDRESS) as nvim_handle:
@@ -45,9 +58,6 @@
4558
sys.exit("Port must be a number")
4659
GHOST_PORT: int = int(SERVER_PORT)
4760

48-
AUTOEXIT: bool = bool(os.environ.get("NVIM_GHOST_AUTO_EXIT"))
49-
NVIM_ADDRESSES = [FOCUSED_NVIM_ADDRESS] if FOCUSED_NVIM_ADDRESS is not None else []
50-
5161
# chdir to folder containing binary
5262
# otherwise the logs are generated whereever the server was started from (i.e curdir)
5363
# which..... isn't good. You'd have stdout.log and stderr.log files everywhere!

binary_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.5.0
1+
v0.5.1

0 commit comments

Comments
 (0)