From 0f20578bfd9f6b71930ba75a6d05729b2b1d9859 Mon Sep 17 00:00:00 2001 From: Max Zawisa Date: Fri, 6 Jan 2023 23:06:44 -0500 Subject: [PATCH] feat: require package.json for node_version --- powerline_shell/segments/node_version.py | 11 +++++++++-- powerline_shell/utils.py | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/powerline_shell/segments/node_version.py b/powerline_shell/segments/node_version.py index aa195928..8f8dc4af 100644 --- a/powerline_shell/segments/node_version.py +++ b/powerline_shell/segments/node_version.py @@ -1,5 +1,5 @@ import subprocess -from ..utils import ThreadedSegment +from ..utils import ThreadedSegment, find_upwards class Segment(ThreadedSegment): @@ -12,7 +12,14 @@ def run(self): def add_to_powerline(self): self.join() + + require_package = self.powerline.segment_conf("node_version", "require_package", False) + chars = int(self.powerline.segment_conf("node_version", "chars", 10)) + has_package = find_upwards("package.json") != None + + if require_package and not has_package: + return if not self.version: return # FIXME no hard-coded colors - self.powerline.append("node " + self.version, 15, 18) + self.powerline.append(" node " + self.version[:chars], 15, 18) diff --git a/powerline_shell/utils.py b/powerline_shell/utils.py index 76c45d03..505e4389 100644 --- a/powerline_shell/utils.py +++ b/powerline_shell/utils.py @@ -1,6 +1,7 @@ import sys import os import threading +from pathlib import Path py3 = sys.version_info[0] == 3 @@ -150,3 +151,9 @@ def get_git_subprocess_env(): # Otherwise we may be unable to parse the output. return get_subprocess_env(LANG="C") + +def find_upwards(filename: str, cwd: Path = Path.cwd()) -> Path | None: + if cwd == Path(cwd.root) or cwd == cwd.parent: + return None + fullpath = cwd / filename + return fullpath if fullpath.exists() else find_upwards(filename, cwd.parent)