-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
rc_awesome.xsh
301 lines (235 loc) · 13.4 KB
/
rc_awesome.xsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""
Awesome snippets of code to make your awesome xonsh RC.
Source: https://github.com/anki-code/xontrib-rc-awesome
If you like the idea click ⭐ on the repo and stay tuned.
"""
# ------------------------------------------------------------------------------
# Temporary fixes of known issues
# ------------------------------------------------------------------------------
# https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1696
__import__('warnings').filterwarnings('ignore', 'There is no current event loop', DeprecationWarning, 'prompt_toolkit.eventloop.utils')
# ------------------------------------------------------------------------------
# Imports
# It's a good practice to keep xonsh session cleano and add _ alias for import
# ------------------------------------------------------------------------------
import shutil as _shutil
import time as _time
# ------------------------------------------------------------------------------
# Cross platform
# ------------------------------------------------------------------------------
if __xonsh__.env.get('XONTRIB_RC_AWESOME_SHELL_TYPE_CHECK', True) and $SHELL_TYPE not in ['prompt_toolkit', 'none', 'best']:
printx("{YELLOW}xontrib-rc-awesome: We recommend to use prompt_toolkit shell by installing `xonsh[full]` package.{RESET}")
# First of all replace `$` to `@` in the prompt to not to be confused with another shell.
# It will be good to read
# - https://github.com/anki-code/xonsh-cheatsheet#three-most-frequent-things-that-newcomers-missed
# - https://github.com/xonsh/xonsh/issues/4152#issue-823993141
$PROMPT_FIELDS['prompt_end'] = '@'
# Add xontrib-cmd-durations to right prompt
$RIGHT_PROMPT = '{long_cmd_duration}'
# The SQLite history backend saves command immediately
# unlike JSON backend that save the commands at the end of the session.
$XONSH_HISTORY_BACKEND = 'sqlite'
# What commands are saved to the history list. By default all commands are saved.
# * The option ‘ignoredups’ will not save the command if it matches the previous command.
# * The option `erasedups` will remove all previous commands that matches and updates the command frequency.
# The minus of `erasedups` is that the history of every session becomes unrepeatable
# because it will have a lack of the command you repeat in another session.
# Docs: https://xonsh.github.io/envvars.html#histcontrol
$HISTCONTROL = 'ignoredups'
# Set regex to avoid saving unwanted commands
# Do not write the command to the history if it was ended by `###`
$XONSH_HISTORY_IGNORE_REGEX = '.*(\\#\\#\\#\\s*)$'
# Remove front dot in multiline input to make the code copy-pastable.
$MULTILINE_PROMPT = ' '
# Enable mouse support in the prompt_toolkit shell.
# This allows clicking for positioning the cursor or selecting a completion.
# In some terminals however, this disables the ability to scroll back through the history of the terminal.
# To scroll on macOS in iTerm2 press Option key and scroll on touchpad.
$MOUSE_SUPPORT = True
# cd-ing shortcuts.
aliases['-'] = 'cd -'
aliases['..'] = 'cd ..'
aliases['....'] = 'cd ../..'
# Avoid typing cd just directory path.
# Docs: https://xonsh.github.io/envvars.html#auto-cd
$AUTO_CD = True
#
# Xontribs - https://github.com/topics/xontrib
#
# Note! Because of xonsh read ~/.xonshrc on every start and can be executed from any virtual environment
# with the different set of installed packages it's a highly recommended approach to check
# the list of the xontribs before loading to avoid errors.
#
# Read: https://github.com/anki-code/xonsh-cheatsheet/blob/main/README.md#install-xonsh-with-package-and-environment-management-system
# Vote to simplify loading: https://github.com/xonsh/xonsh/issues/5023
#
# TODO: reduce this code after merging https://github.com/xonsh/xonsh/pull/5028
#
from xonsh.xontribs import get_xontribs
_xontribs_installed = set(get_xontribs().keys())
_xontribs_to_load = (
'whole_word_jumping', # Jumping across whole words (non-whitespace) with Ctrl+Left/Right and Alt+Left/Right on Linux or Option+Left/Right on macOS.
'back2dir', # Back to the latest used directory when starting xonsh shell. URL: https://github.com/anki-code/xontrib-back2dir
'prompt_bar', # The bar prompt for xonsh shell with customizable sections. URL: https://github.com/anki-code/xontrib-prompt-bar
'pipeliner', # Let your pipe lines flow thru the Python code. URL: https://github.com/anki-code/xontrib-pipeliner
'cmd_done', # Show long running commands durations in prompt with option to send notification when terminal is not focused. URL: https://github.com/jnoortheen/xontrib-cmd-durations
'jedi', # Jedi - an awesome autocompletion, static analysis and refactoring library for Python. URL: https://github.com/xonsh/xontrib-jedi
)
xontrib load @(_xontribs_installed.intersection(_xontribs_to_load))
# ------------------------------------------------------------------------------
# Platform specific
# ------------------------------------------------------------------------------
from xonsh.platform import ON_LINUX, ON_DARWIN #, ON_WINDOWS, ON_WSL, ON_CYGWIN, ON_MSYS, ON_POSIX, ON_FREEBSD, ON_DRAGONFLY, ON_NETBSD, ON_OPENBSD
if ON_LINUX or ON_DARWIN:
# Globbing files with “*” or “**” will also match dotfiles, or those ‘hidden’ files whose names begin with a literal ‘.’.
# Note! This affects also on rsync and other tools.
$DOTGLOB = True
# Don't clear the screen after quitting a manual page.
$MANPAGER = "less -X"
$LESS = "--ignore-case --quit-if-one-screen --quit-on-intr FRXQ"
# Flag for automatically pushing directories onto the directory stack i.e. `dirs -p` (https://xon.sh/aliases.html#dirs).
# It's for `mc` alias (below).
$AUTO_PUSHD = True
# Add default bin paths
for p in [p'/home/linuxbrew/.linuxbrew/bin', fp'/home/{$USER}/.local/bin', p'/opt/homebrew/opt/coreutils/libexec/gnubin']:
if p.exists():
$PATH.append(str(p))
# List all files: sorted, with colors, directories will be first (Midnight Commander style).
aliases['ll'] = "$LC_COLLATE='C' ls --group-directories-first -lAh --color @($args)"
# Run Midnight Commander where left and right panel will be the current and the previous directory.
# Required: $AUTO_PUSHD = True
if _shutil.which('mc'):
aliases['mc'] = "mc @($PWD if not $args else $args) @($OLDPWD if not $args else $PWD)"
# Make directory and cd into it.
# Example: md /tmp/my/awesome/dir/will/be/here
aliases['md'] = 'mkdir -p $arg0 && cd $arg0'
# Using rsync instead of cp to get the progress and speed of copying.
if _shutil.which('rsync'):
aliases['cp'] = 'rsync --progress --recursive --archive'
# Grepping string occurrences recursively starting from current directory.
# Example: cd ~/git/xonsh && greps environ
aliases['greps'] = 'grep -ri'
# Copy output to current clipboard using xclip. This snippet could be improved and packed into the xontrib. Start from https://github.com/xonsh/xontrib-template
# Example: echo hello | clp
if _shutil.which('pbcopy'): # DARWIN
aliases['clp'] = 'pbcopy'
elif _shutil.which('xclip'): # LINUX
aliases['clp'] = 'xclip -sel clip'
elif _shutil.which('clip.exe'): # WINDOWS
aliases['clp'] = 'clip.exe'
# SSH: Suppress "Connection close" message.
aliases['ssh'] = 'ssh -o LogLevel=QUIET'
# Run http server in the current directory.
aliases['http-here'] = 'python3 -m http.server'
if _shutil.which('screen'):
# `screen-run` alias to run command in screen session
def _screen_run(args):
from datetime import datetime as _datetime
screen_name = "s" + _datetime.now().strftime("%S%M%H") # This screen name is more unique to run `screen -r <name>`
screen_cmd = " ".join(args)
print('Start session', screen_name, ':', screen_cmd)
screen -S @(screen_name) xonsh -c @(screen_cmd + '; echo Done; exec xonsh')
screen -ls
aliases['screen-run'] = _screen_run
del _screen_run
# Universal pm aliases. This snippet could be improved and packed into the xontrib. Start from https://github.com/xonsh/xontrib-template
if _shutil.which('pacman'):
# Aliases from https://devhints.io/pacman
aliases['pm'] = 'sudo pacman'
aliases['pm-install'] = 'sudo pacman -Sy'
aliases['pm-uninstall'] = 'sudo pacman -Rsc'
aliases['pm-search'] = 'sudo pacman -Ss'
aliases['pm-upgrade-everything'] = 'sudo pacman -Syu'
aliases['pm-package-info'] = 'sudo pacman -Qii'
aliases['pm-package-unneeded-list'] = 'sudo pacman -Qdt'
aliases['pm-package-unneeded-uninstall'] = 'sudo pacman -Rns @($(pacman -Qdtq).splitlines())'
elif _shutil.which('apt'):
aliases['pm'] = 'sudo apt'
aliases['pm-install'] = 'sudo apt install'
aliases['pm-uninstall'] = 'sudo apt uninstall'
aliases['pm-search'] = 'sudo apt search'
#
# Xontribs - https://github.com/topics/xontrib
#
# Note! Because of xonsh read ~/.xonshrc on every start and can be executed from any virtual environment
# with the different set of installed packages it's a highly recommended approach to check
# the list of the xontribs before loading to avoid errors.
#
# Read: https://github.com/anki-code/xonsh-cheatsheet/blob/main/README.md#install-xonsh-with-package-and-environment-management-system
# Vote to simplify loading: https://github.com/xonsh/xonsh/issues/5023
#
_xontribs_to_load = (
'sh', # Paste and run commands from bash, zsh, fish, tcsh in xonsh shell. URL: https://github.com/anki-code/xontrib-sh
#'output_search', # Get words from the previous command output for the next command. URL: https://github.com/tokenizer/xontrib-output-search
)
xontrib load @(_xontribs_installed.intersection(_xontribs_to_load))
if False: # Example of how to check the operating system
if ON_LINUX and 'apt_tabcomplete' in _xontribs_installed and shutil.which('lsb_release'):
if 'Ubuntu' in $(lsb_release --id --release --short).strip():
xontrib load apt_tabcomplete
# History search alias
# You can use it ordinarily: `history-search "cd /"`
# Or as a macro call: `history-search! cd /`
aliases['history-search'] = """sqlite3 $XONSH_HISTORY_FILE @("SELECT inp FROM xonsh_history WHERE inp LIKE '%" + $arg0 + "%' AND inp NOT LIKE 'history-%' ORDER BY tsb DESC LIMIT 10");"""
#
# The command to pull history from other SQLite history backend sessions.
#
try:
$XONSH_HISTORY_SQLITE_PULL_TIME = __xonsh__.history[0].ts[0]
except:
$XONSH_HISTORY_SQLITE_PULL_TIME = _time.time()
def _history_pull():
if $XONSH_HISTORY_BACKEND != 'sqlite':
printx('{RED}To pull history use SQLite history backend.{RESET}')
return -1
if not _shutil.which('sqlite3'):
printx('{RED}Install sqlite3.{RESET}')
return -1
sessionid = __xonsh__.history.info()['sessionid']
sql_records = f"SELECT inp FROM xonsh_history WHERE tsb > '{$XONSH_HISTORY_SQLITE_PULL_TIME}' AND sessionid != '{sessionid}' ORDER BY tsb"
rows = $(sqlite3 $XONSH_HISTORY_FILE @(sql_records)).splitlines()
i = 0
for r in rows:
__xonsh__.shell.shell.prompter.history.append_string(r)
print(r)
i += 1
$XONSH_HISTORY_SQLITE_PULL_TIME = _time.time()
if i > 0:
printx(f'{{GREEN}}Added {i} records!{{RESET}}')
else:
printx(f'{{YELLOW}}No new records found.{{RESET}}')
aliases['history-pull'] = _history_pull
del _history_pull
#
# Binding the hotkeys - https://xon.sh/tutorial_ptk.html
# List of keys - https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/src/prompt_toolkit/keys.py
#
from prompt_toolkit.keys import Keys
@events.on_ptk_create
def custom_keybindings(bindings, **kw):
# Press F1 and get the list of files
@bindings.add(Keys.F1)
def run_ls(event):
ls -l
event.cli.renderer.erase()
# Press F3 to insert the grep command
@bindings.add(Keys.F3)
def say_hi(event):
event.current_buffer.insert_text(' | grep -i ')
#
# Comma separated thousands in output
# Input: 1000+10000
# Output: 11,000
#
import xonsh.pretty
xonsh.pretty.for_type(type(1), lambda int, printer, cycle: printer.text(f'{int:,}'))
xonsh.pretty.for_type(type(1.0), lambda float, printer, cycle: printer.text(f'{float:,}'))
# ------------------------------------------------------------------------------
# Final
# ------------------------------------------------------------------------------
# For the experienced users
# Suppress line "xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True"
# in case of exceptions or wrong command.
$XONSH_SHOW_TRACEBACK = False
# Suppress line "Did you mean one of the following?"
$SUGGEST_COMMANDS = False