Skip to content
Back to top
View this page
Edit this page

# xonsh.prompt.base

Base prompt, provides PROMPT_FIELDS and prompt related functions

class xonsh.prompt.base.BasePromptField(**kwargs)
  Parameters:
    kwargs

      attributes of the class will be set from this

  classmethod wrap(**kwargs) → tp.Callable[..., FieldType]

    decorator to set the updator

  update(ctx: PromptFields) → None

    will be called from PromptFields getter for each new prompt

  property name: str

    will be set during load.

    Notes

    fields with names such as `gitstatus.branch` mean they are defined in a module named `gitstatus` and are most likely a subfield used by `gitstatus`

  updator: tp.Callable[[FieldType, PromptFields], None] | None = None

    this is a callable that needs to update the value or any of the attribute of the field

  value = ''

    This field should hold the bare value of the field without any color/format strings

class xonsh.prompt.base.MultiPromptField(*fragments: str, **kwargs)

  Facilitate combining other PromptFields

  Parameters:
    kwargs

      attributes of the class will be set from this

  classmethod wrap(**kwargs) → tp.Callable[..., FieldType]

    decorator to set the updator

  get_frags(env)
  update(ctx: PromptFields)

    will be called from PromptFields getter for each new prompt

  fragments: tuple[str, ...] = ()

    name of the fields or the literals to combine. If the framgment name startswith `.` then they are resolved to include the name of this field.

  property name: str

    will be set during load.

    Notes

    fields with names such as `gitstatus.branch` mean they are defined in a module named `gitstatus` and are most likely a subfield used by `gitstatus`

  separator = ''

    in case this combines values from other prompt fields

  updator = None

    this is a callable that needs to update the value or any of the attribute of the field

  value = ''

    This field should hold the bare value of the field without any color/format strings

class xonsh.prompt.base.ParsedTokens(tokens, template)

  Create new instance of ParsedTokens(tokens, template)

  count(value, /)

    Return number of occurrences of value.

  index(value, start=0, stop=sys.maxsize, /)

    Return first index of value.

    Raises ValueError if the value is not present.

  process() → str

    Wrapper that gets formatter-function from environment and returns final prompt.

  update(idx: int, val: str | None, spec: str | None, conv: str | None) → None

    Update tokens list in-place

  template: str | Callable

    Alias for field number 1

  tokens: list[_ParsedToken]

    Alias for field number 0

class xonsh.prompt.base.PromptField(**kwargs)

  Any new non-private attributes set by the sub-classes are considered a way to configure the format

  Parameters:
    kwargs

      attributes of the class will be set from this

  classmethod wrap(**kwargs) → tp.Callable[..., FieldType]

    decorator to set the updator

  update(ctx: PromptFields) → None

    will be called from PromptFields getter for each new prompt

  property name: str

    will be set during load.

    Notes

    fields with names such as `gitstatus.branch` mean they are defined in a module named `gitstatus` and are most likely a subfield used by `gitstatus`

  prefix = ''
  suffix = ''
  updator = None

    this is a callable that needs to update the value or any of the attribute of the field

  value = ''

    This field should hold the bare value of the field without any color/format strings

class xonsh.prompt.base.PromptFields(xsh: XonshSession, init=True)

  Mapping of functions available for prompt-display.

  clear() → None. Remove all items from D.
  get(k[, d]) → D[k] if k in D, else d. d defaults to None.
  get_fields(module)

    Find and load all instances of PromptField from the given module.

    Each module is expected to have a single prompt-field with the same name as the module

  items() → a set-like object providing a view on D's items
  keys() → a set-like object providing a view on D's keys
  load_initial()
  needs_calling(name) → bool

    check if we can offload the work

  pick(key: str | FieldType) → str | FieldType | None

    Get the value of the prompt-field

    Notes

    If it is callable, then the result of the callable is returned. If it is a PromptField then it is updated

  pick_val(key)

    wrap .pick() method to get .value attribute in case of PromptField

  pop(k[, d]) → v, remove specified key and return the corresponding

    value. If key is not found, d is returned if given, otherwise KeyError is raised.

  popitem() → (k, v), remove and return some (key, value) pair

    as a 2-tuple; but raise KeyError if D is empty.

  reset()

    the results are cached and need to be reset between prompts

  reset_key(key)

    remove a single key from the cache (if it exists)

  setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
  update([E, ]**F) → None. Update D from mapping/iterable E and F.
    If E present and has a .keys() method, does:

      for k in E.keys(): D[k] = E[k]

    If E present and lacks .keys() method, does:

      for (k, v) in E: D[k] = v

    In either case, this is followed by:

      for k, v in F.items(): D[k] = v

  values() → an object providing a view on D's values

class xonsh.prompt.base.PromptFormatter

  Class that holds all the related prompt formatting methods, uses the `PROMPT_FIELDS` envvar (no color formatting).

xonsh.prompt.base.default_prompt()

  Creates a new instance of the default prompt.

xonsh.prompt.base.is_template_string(template, PROMPT_FIELDS=None)

  Returns whether or not the string is a valid template.

xonsh.prompt.base.multiline_prompt(curr='')

  Returns the filler text for the prompt in multiline scenarios.

xonsh.prompt.base.prompt_tokens_formatter_default(container: ParsedTokens) → str

  > Join the tokens

  Parameters:
    container: ParsedTokens

      parsed tokens holder

  Returns:
    str

      process the tokens and finally return the prompt string