mirror of
https://github.com/powerline/powerline.git
synced 2025-07-26 23:35:04 +02:00
Move readlines function to powerline.lib.shell
This commit is contained in:
parent
7f5c4968c1
commit
1498fc714c
@ -1,9 +1,15 @@
|
|||||||
# vim:fileencoding=utf-8:noet
|
# vim:fileencoding=utf-8:noet
|
||||||
|
|
||||||
|
from __future__ import absolute_import, unicode_literals, division, print_function
|
||||||
|
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
from locale import getlocale, getdefaultlocale, LC_MESSAGES
|
from locale import getlocale, getdefaultlocale, LC_MESSAGES
|
||||||
|
|
||||||
|
|
||||||
|
def _get_shell_encoding():
|
||||||
|
return getlocale(LC_MESSAGES)[1] or getdefaultlocale()[1] or 'utf-8'
|
||||||
|
|
||||||
|
|
||||||
def run_cmd(pl, cmd, stdin=None):
|
def run_cmd(pl, cmd, stdin=None):
|
||||||
try:
|
try:
|
||||||
p = Popen(cmd, stdout=PIPE, stdin=PIPE)
|
p = Popen(cmd, stdout=PIPE, stdin=PIPE)
|
||||||
@ -12,11 +18,26 @@ def run_cmd(pl, cmd, stdin=None):
|
|||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
stdout, err = p.communicate(stdin)
|
stdout, err = p.communicate(stdin)
|
||||||
encoding = getlocale(LC_MESSAGES)[1] or getdefaultlocale()[1] or 'utf-8'
|
stdout = stdout.decode(_get_shell_encoding())
|
||||||
stdout = stdout.decode(encoding)
|
|
||||||
return stdout.strip()
|
return stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
def asrun(pl, ascript):
|
def asrun(pl, ascript):
|
||||||
'''Run the given AppleScript and return the standard output and error.'''
|
'''Run the given AppleScript and return the standard output and error.'''
|
||||||
return run_cmd(pl, ['osascript', '-'], ascript)
|
return run_cmd(pl, ['osascript', '-'], ascript)
|
||||||
|
|
||||||
|
|
||||||
|
def readlines(cmd, cwd):
|
||||||
|
'''Run command and read its output, line by line
|
||||||
|
|
||||||
|
:param list cmd:
|
||||||
|
Command which will be run.
|
||||||
|
:param str cwd:
|
||||||
|
Working directory of the command which will be run.
|
||||||
|
'''
|
||||||
|
p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd)
|
||||||
|
encoding = _get_shell_encoding()
|
||||||
|
p.stderr.close()
|
||||||
|
with p.stdout:
|
||||||
|
for line in p.stdout:
|
||||||
|
yield line[:-1].decode(encoding)
|
||||||
|
@ -7,6 +7,7 @@ import sys
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from powerline.lib.vcs import get_branch_name as _get_branch_name, get_file_status
|
from powerline.lib.vcs import get_branch_name as _get_branch_name, get_file_status
|
||||||
|
from powerline.lib.shell import readlines
|
||||||
|
|
||||||
|
|
||||||
_ref_pat = re.compile(br'ref:\s*refs/heads/(.+)')
|
_ref_pat = re.compile(br'ref:\s*refs/heads/(.+)')
|
||||||
@ -144,15 +145,6 @@ try:
|
|||||||
def branch(self):
|
def branch(self):
|
||||||
return get_branch_name(self.directory)
|
return get_branch_name(self.directory)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from subprocess import Popen, PIPE
|
|
||||||
|
|
||||||
def readlines(cmd, cwd):
|
|
||||||
p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd)
|
|
||||||
p.stderr.close()
|
|
||||||
with p.stdout:
|
|
||||||
for line in p.stdout:
|
|
||||||
yield line[:-1].decode('utf-8')
|
|
||||||
|
|
||||||
class Repository(object):
|
class Repository(object):
|
||||||
__slots__ = ('directory', 'ignore_event')
|
__slots__ = ('directory', 'ignore_event')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user