Skip to content

Commit

Permalink
feat: require package.json for node_version
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrzaw committed Jan 7, 2023
1 parent 4b19aa4 commit 0f20578
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 9 additions & 2 deletions powerline_shell/segments/node_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import subprocess
from ..utils import ThreadedSegment
from ..utils import ThreadedSegment, find_upwards


class Segment(ThreadedSegment):
Expand All @@ -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)
7 changes: 7 additions & 0 deletions powerline_shell/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import os
import threading
from pathlib import Path

py3 = sys.version_info[0] == 3

Expand Down Expand Up @@ -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)

0 comments on commit 0f20578

Please sign in to comment.