Environment (xonsh.environ)

Environment for the xonsh shell.

class xonsh.environ.PromptFormatter(*args, **kwargs)[source]

Base class that implements utilities for PromptFormatters.

You should read the docs for this class to learn how to write your own Prompt Formatter. However, you most likely want to inherit from DefaultPromptFormatter if you are writing a custom formatter.

Custom Prompt Formatters will need to implement their own __init__() that adds entries to several dicts depending on the needs of the variables they add. The Formatter may also implement methods to implement those variables (although very simple variables can be written with pre-existing functions.)

Example:

def __init__(self, *args, **kwargs):
    super(PromptFormatter, self).__init__(*args, **kwargs)

    # Add a variable that's just a static string.  Useful for constants
    # that you can't remember:
    self['bell'] = ''

    # To add a variable that only needs to be computed once add
    # the function to be called to self.  This can be used for any
    # variable that won't change while this shell instance is run.  It is
    # especially useful if the variable takes a long time to compute and
    # is infrequently used by users:
    self['login_time'] = self.login_time

    # To add a variable that needs to be computed every time the prompt is
    # printed simply add a function to self and then add the variable name
    # to the self._run_every set.
    self['time24'] = functools.partial(time.strftime, '%H:%M:%S')
    self._run_every.add('time24')

def login_time(self):
    who_out = subprocess.check_output(['who', '-m'])
    return ' '.join(who_out.decode().split()[2:4])
class xonsh.environ.DefaultPromptFormatter(*args, **kwargs)[source]

This class implements the default prompt format variables.

The following variables are recognized:

User:

Name of current user

Hostname:

Name of host computer

Cwd:

Current working directory

Curr_branch:

Name of current git branch (preceded by a space), if any

(QUALIFIER_)COLORNAME:
 

Inserts an ANSI color code

  • COLORNAME can be any of:

    BLACK, RED, GREEN, YELLOW, BLUE, PURPLE, CYAN, WHITE

  • QUALIFIER is optional and can be any of:

    BOLD, UNDERLINE, BACKGROUND, INTENSE, BOLD_INTENSE, BACKGROUND_INTENSE

NO_COLOR:

Resets any previously used color codes

user()[source]
cwd(full_path=True)[source]
curr_branch(cwd=None)[source]

Gets the branch for a current working directory. Returns None if the cwd is not a repository. This currently only works for git, bust should be extended in the future.

xonsh.environ.format_prompt(template='{BOLD_GREEN}{user}@{hostname}{BOLD_BLUE} {cwd}{BOLD_RED}{curr_branch} {BOLD_BLUE}${NO_COLOR} ')[source]

Formats a xonsh prompt template string.

See the DefaultPromptFormatter documentation for keyword arguments recognized in the template string.[

xonsh.environ.multiline_prompt()[source]

Returns the filler text for the prompt in multiline scenarios.

xonsh.environ.bash_env()[source]

Attempts to compute the bash envinronment variables.

xonsh.environ.xonshrc_context(rcfile=None, execer=None)[source]

Attempts to read in xonshrc file, and return the contents.

xonsh.environ.default_env(env=None)[source]

Constructs a default xonsh environment.

This Page