Skip to content

Commit 98bb970

Browse files
committed
feat: require package.json for node_version
1 parent 4b19aa4 commit 98bb970

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

powerline_shell/segments/node_version.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import subprocess
2-
from ..utils import ThreadedSegment
2+
from ..utils import ThreadedSegment, find_upwards
33

44

55
class Segment(ThreadedSegment):
@@ -12,7 +12,14 @@ def run(self):
1212

1313
def add_to_powerline(self):
1414
self.join()
15+
16+
require_package = self.powerline.segment_conf("node_version", "require_package", False)
17+
chars = int(self.powerline.segment_conf("node_version", "chars", 10))
18+
has_node = find_upwards("package.json") != None
19+
20+
if require_package and not has_node:
21+
return
1522
if not self.version:
1623
return
1724
# FIXME no hard-coded colors
18-
self.powerline.append("node " + self.version, 15, 18)
25+
self.powerline.append(" node " + self.version[:chars], 15, 18)

powerline_shell/utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import os
33
import threading
4+
from pathlib import Path
45

56
py3 = sys.version_info[0] == 3
67

@@ -150,3 +151,9 @@ def get_git_subprocess_env():
150151
# Otherwise we may be unable to parse the output.
151152
return get_subprocess_env(LANG="C")
152153

154+
155+
def find_upwards(filename: str, cwd: Path = Path.cwd()) -> Path | None:
156+
if cwd == Path(cwd.root) or cwd == cwd.parent:
157+
return None
158+
fullpath = cwd / filename
159+
return fullpath if fullpath.exists() else find_upwards(filename, cwd.parent)

0 commit comments

Comments
 (0)