rc.xsh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. $VI_MODE = True
  2. $AUTO_PUSHD = True
  3. $XONSH_AUTOPAIR = True
  4. # tab selection: do not execute cmd when pressing enter
  5. $COMPLETIONS_CONFIRM = True
  6. xontrib load vox z
  7. def _last_exit_status():
  8. try:
  9. exit_status = __xonsh_history__.rtns[-1]
  10. return exit_status if exit_status != 0 else None
  11. except IndexError:
  12. return None
  13. $PROMPT_FIELDS['last_exit_status'] = _last_exit_status
  14. $XONSH_STDERR_PREFIX = '{RED}'
  15. $XONSH_STDERR_POSTFIX = '{NO_COLOR}'
  16. $DYNAMIC_CWD_WIDTH = '30%'
  17. $DYNAMIC_CWD_ELISION_CHAR = '…'
  18. $PROMPT = ''.join([
  19. '{RED}{last_exit_status:[{}] }',
  20. '{BOLD_GREEN}{user}@{hostname} ',
  21. '{YELLOW}{cwd} ',
  22. '{BLUE}{prompt_end} ',
  23. '{NO_COLOR}',
  24. ])
  25. $RIGHT_PROMPT = '{gitstatus}{env_name: {}}'
  26. import datetime as dt
  27. import os
  28. import re
  29. import shutil
  30. $EDITOR = 'vim'
  31. # required by pinentry-tty when using gpg command:
  32. $GPG_TTY = $(tty)
  33. if shutil.which('gpgconf'):
  34. # required by scute
  35. $GPG_AGENT_INFO = $(gpgconf --list-dir agent-socket).rstrip() + ':0:1'
  36. if not 'SSH_CLIENT' in ${...}:
  37. # in gnupg 2.1.13 the location of agents socket changed
  38. $SSH_AUTH_SOCK = $(gpgconf --list-dir agent-ssh-socket).rstrip()
  39. # wrapper for termite required when launching termite from ranger:
  40. $TERMCMD = os.path.join(os.path.dirname(__file__), 'ranger-termite-termcmd')
  41. def dpkg_listfiles(pkg_name):
  42. assert isinstance(pkg_name, str)
  43. paths = $(dpkg --listfiles @(pkg_name)).split('\n')[:-1]
  44. assert len(paths) > 0, 'pkg {!r} not installed'.format(pkg_name)
  45. return paths
  46. def dpkg_search(path_search_pattern):
  47. assert isinstance(path_search_pattern, str)
  48. return re.findall(
  49. '^(\S+): (.*)$\n',
  50. $(dpkg --search @(path_search_pattern)),
  51. flags=re.MULTILINE,
  52. )
  53. def dpkg_welse(cmd):
  54. pkg_name, cmd_path = dpkg_which(cmd)
  55. return dpkg_listfiles(pkg_name)
  56. def dpkg_which(cmd):
  57. cmd_path = shutil.which(cmd)
  58. assert cmd_path, 'cmd {!r} not found'.format(cmd)
  59. matches = dpkg_search(cmd_path)
  60. assert len(matches) != 0, '{!r} not installed via dpkg'.format(cmd_path)
  61. assert len(matches) == 1
  62. return matches[0]
  63. def locate(*patterns, match_all=True, ignore_case=True):
  64. params = []
  65. if match_all:
  66. params.insert(0, '--all')
  67. if ignore_case:
  68. params.insert(0, '--ignore-case')
  69. return $(locate @(params) -- @(patterns)).split('\n')[:-1]
  70. def timestamp_iso():
  71. # if called without tz argument astimezone() assumes
  72. # the system local timezone for the target timezone
  73. return dt.datetime.now().astimezone().strftime('%Y%m%dT%H%M%S%z')
  74. aliases['dpkg-welse'] = lambda args: '\n'.join(dpkg_welse(args[0]))
  75. aliases['dpkg-which'] = lambda args: '\t'.join(dpkg_which(args[0]))
  76. aliases['g'] = ['git']
  77. aliases['ll'] = ['ls', '-l', '--all', '--indicator-style=slash',
  78. '--human-readable', '--time-style=long-iso', '--color=auto']
  79. # vim: filetype=python