Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to require package.json for node_version segment #539

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)