api docs for prompt

This commit is contained in:
BlahGeek 2016-09-04 19:59:48 +08:00
parent f8c345bd70
commit e1008e8202
7 changed files with 49 additions and 11 deletions

View file

@ -32,6 +32,7 @@ For those of you who want the gritty details.
history history
completer completer
completers/index completers/index
prompt/index
shell shell
base_shell base_shell
readline_shell readline_shell

View file

@ -0,0 +1,11 @@
.. _xonsh_prompt_gitstatus:
***********************************************
Git status prompt (``xonsh.prompt.gitstatus``)
***********************************************
.. automodule:: xonsh.prompt.gitstatus
:members:
:undoc-members:
:inherited-members:

12
docs/api/prompt/index.rst Normal file
View file

@ -0,0 +1,12 @@
.. _api_prompt:
=================
Prompt API
=================
Functions in ``FORMATTER_DICT`` for prompt.
.. toctree::
:maxdepth: 1
gitstatus
vc_branch

View file

@ -0,0 +1,11 @@
.. _xonsh_prompt_vc_branch:
*******************************************************************
Version control branch info prompt (``xonsh.prompt.vc_branch``)
*******************************************************************
.. automodule:: xonsh.prompt.vc_branch
:members:
:undoc-members:
:inherited-members:

View file

@ -16,7 +16,7 @@ from xonsh.prompt.env import (env_name, vte_new_tab_cwd)
from xonsh.prompt.vc_branch import ( from xonsh.prompt.vc_branch import (
current_branch, branch_color, branch_bg_color current_branch, branch_color, branch_bg_color
) )
from xonsh.prompt.gitstatus import _gitstatus_prompt from xonsh.prompt.gitstatus import gitstatus_prompt
FORMATTER_DICT = xl.LazyObject(lambda: dict( FORMATTER_DICT = xl.LazyObject(lambda: dict(
@ -33,5 +33,5 @@ FORMATTER_DICT = xl.LazyObject(lambda: dict(
current_job=_current_job, current_job=_current_job,
env_name=env_name, env_name=env_name,
vte_new_tab_cwd=vte_new_tab_cwd, vte_new_tab_cwd=vte_new_tab_cwd,
gitstatus=_gitstatus_prompt, gitstatus=gitstatus_prompt,
), globals(), 'FORMATTER_DICT') ), globals(), 'FORMATTER_DICT')

View file

@ -1,5 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""informative git status for prompt""" """informative git status for prompt"""
import os import os
@ -19,8 +18,8 @@ def _check_output(*args, **kwargs):
@xl.lazyobject @xl.lazyobject
def DEFS(): def _DEFS():
_DEFS = { DEFS = {
'HASH': ':', 'HASH': ':',
'BRANCH': '{CYAN}', 'BRANCH': '{CYAN}',
'OPERATION': '{CYAN}', 'OPERATION': '{CYAN}',
@ -33,11 +32,11 @@ def DEFS():
'AHEAD': '↑·', 'AHEAD': '↑·',
'BEHIND': '↓·', 'BEHIND': '↓·',
} }
return _DEFS return DEFS
def _get_def(key): def _get_def(key):
return builtins.__xonsh_env__.get('XONSH_GITSTATUS_' + key) or DEFS[key] return builtins.__xonsh_env__.get('XONSH_GITSTATUS_' + key) or _DEFS[key]
def _get_tag_or_hash(): def _get_tag_or_hash():
@ -69,7 +68,10 @@ def _gitoperation(gitdir):
if os.path.exists(os.path.join(gitdir, f[0]))] if os.path.exists(os.path.join(gitdir, f[0]))]
def _gitstatus(): def gitstatus():
"""Return (branch name, number of ahead commit, number of behind commit,
untracked number, changed number, conflicts number,
staged number, stashed number, operation)"""
status = _check_output(['git', 'status', '--porcelain', '--branch']) status = _check_output(['git', 'status', '--porcelain', '--branch'])
branch = '' branch = ''
num_ahead, num_behind = 0, 0 num_ahead, num_behind = 0, 0
@ -113,11 +115,12 @@ def _gitstatus():
operations) operations)
def _gitstatus_prompt(): def gitstatus_prompt():
"""Return str `[BRANCH|OPERATOR|numbers]`"""
try: try:
(branch, num_ahead, num_behind, (branch, num_ahead, num_behind,
untracked, changed, conflicts, staged, stashed, untracked, changed, conflicts, staged, stashed,
operations) = _gitstatus() operations) = gitstatus()
except subprocess.SubprocessError: except subprocess.SubprocessError:
return '' return ''

View file

@ -12,7 +12,7 @@ from xonsh.lazyasd import LazyObject
from xonsh.platform import HAS_PYGMENTS from xonsh.platform import HAS_PYGMENTS
from xonsh.tools import DefaultNotGiven, print_color, normabspath, to_bool from xonsh.tools import DefaultNotGiven, print_color, normabspath, to_bool
from xonsh.inspectors import find_file, getouterframes from xonsh.inspectors import find_file, getouterframes
from xonsh.environ import _replace_home from xonsh.prompt.cwd import _replace_home
from xonsh.lazyimps import pygments, pyghooks from xonsh.lazyimps import pygments, pyghooks