Merge pull request #340 from b-ryan/fix-windows-bash-cwd

Fix handling of CWD on Bash for Windows
This commit is contained in:
Buck Ryan 2017-12-20 12:45:40 -05:00 committed by GitHub
commit cde72c25af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 15 deletions

View file

@ -10,18 +10,32 @@ from .utils import warn, py3
import re
def _current_dir():
"""Returns the full current working directory as the user would have used
in their shell (ie. without following symbolic links).
With the introduction of Bash for Windows, we can't use the PWD environment
variable very easily. `os.sep` for windows is `\` but the PWD variable will
use `/`. So just always use the `os` functions for dealing with paths. This
also is fine because the use of PWD below is done to avoid following
symlinks, which Windows doesn't have.
For non-Windows systems, prefer the PWD environment variable. Python's
`os.getcwd` function follows symbolic links, which is undesirable."""
if os.name == "nt":
return os.getcwd()
return os.getenv("PWD") or os.getcwd()
def get_valid_cwd():
""" We check if the current working directory is valid or not. Typically
happens when you checkout a different branch on git that doesn't have
this directory.
We return the original cwd because the shell still considers that to be
the working directory, so returning our guess will confuse people
"""
# Prefer the PWD environment variable. Python's os.getcwd function follows
# symbolic links, which is undesirable. But if PWD is not set then fall
# back to this func
"""Determine and check the current working directory for validity.
Typically, an directory arises when you checkout a different branch on git
that doesn't have this directory. When an invalid directory is found, a
warning is printed to the screen, but the directory is still returned
as-is, since this is what the shell considers to be the cwd."""
try:
cwd = os.getenv('PWD') or os.getcwd()
cwd = _current_dir()
except:
warn("Your current directory is invalid. If you open a ticket at " +
"https://github.com/milkbikis/powerline-shell/issues/new " +
@ -36,7 +50,7 @@ def get_valid_cwd():
up = os.sep.join(parts)
if cwd != up:
warn("Your current directory is invalid. Lowest valid directory: "
+ up)
+ up)
return cwd

View file

@ -53,7 +53,7 @@ def get_fg_bg(powerline, name, is_last_dir):
def add_cwd_segment(powerline):
cwd = powerline.cwd or os.getenv('PWD')
cwd = powerline.cwd
if not py3:
cwd = cwd.decode("utf-8")
cwd = replace_home_dir(cwd)

View file

@ -5,8 +5,7 @@ from ..utils import BasicSegment
class Segment(BasicSegment):
def add_to_powerline(self):
powerline = self.powerline
cwd = powerline.cwd or os.getenv('PWD')
if not os.access(cwd, os.W_OK):
if not os.access(powerline.cwd, os.W_OK):
powerline.append(' %s ' % powerline.lock,
powerline.theme.READONLY_FG,
powerline.theme.READONLY_BG)

View file

@ -17,6 +17,6 @@ class Segment(BasicSegment):
set_title = '\033]0;%s@%s: %s\007' % (
os.getenv('USER'),
socket.gethostname().split('.')[0],
powerline.cwd or os.getenv('PWD'),
powerline.cwd,
)
powerline.append(set_title, None, None, '')