Skip to content

Instantly share code, notes, and snippets.

@mrsipan
Last active November 3, 2023 23:42
Show Gist options
  • Save mrsipan/65d1a775e77b4f6bef1b22bac0dbbb97 to your computer and use it in GitHub Desktop.
Save mrsipan/65d1a775e77b4f6bef1b22bac0dbbb97 to your computer and use it in GitHub Desktop.
from pprint import pprint as pp
#import bs4
import datetime
#import inscripts
#import lxml
import os
#import pexpect
import random
import re
# import requests
import string
import sys
from prompt_toolkit.keys import Keys
from prompt_toolkit.application.current import get_app
from prompt_toolkit.filters import (
vi_insert_mode,
Condition,
EmacsInsertMode,
ViInsertMode,
vi_mode,
)
from prompt_toolkit.key_binding.bindings import named_commands
import xonsh.jobs
xontribs = ["abbrevs", "kitty", "mpl", "prompt_vi_mode", "sh", "z"]
for xontrib_name in xontribs:
xpip install @(f"xontrib-{xontrib_name}")
# xontrib load autojump
# xontrib load avox
# xontrib load docker_tabcomplete
# xontrib load prompt_ret_code
# xontrib load vox
# xontrib load vox_tabcomplete
xontrib load abbrevs
#xontrib load fzf-widgets
# xontrib load jedi
xontrib load kitty
xontrib load mpl
xontrib load prompt_vi_mode
# xontrib load schedule
xontrib load z
$XONTRIB_SH_USEFIRST = True
$XONTRIB_SH_SHELLS = ['dash', 'bash', 'sh']
xontrib load sh
$AUTO_CD = True
$AUTO_SUGGEST = True
$CASE_SENSITIVE_COMPLETIONS = False
$DYLD_LIBRARY_PATH = $HOME + '/opt/pylocal/lib'
$EDITOR = $VISUAL = 'vim'
$HOME = os.path.expanduser('~')
# $JAVA_HOME = $HOME + '/opt/jdk-11.0.11+9/Contents/Home/'
# $JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home"
$SHELL_TYPE = 'prompt_toolkit'
$VI_MODE = True
$PAGER = 'less'
$LESS = '-R'
$PYTHONWARNINGS = 'ignore'
$XONSH_COLOR_STYLE = 'paraiso-dark'
if 'INSIDE_EMACS' in ${...}:
$VI_MODE = False
$XONSH_COLOR_STYLE = 'paraiso-dark'
if 'TERM_PROGRAM' in ${...} and $TERM_PROGRAM == 'vscode':
$XONSH_COLOR_STYLE = 'solarized-light'
$XONSH_DEBUG = 0
$XONSH_SHOW_TRACEBACK = True
$XONSH_TRACE_SUBPROC = False
$XONSH_HISTORY_SIZE = 100000
$PROMPT = '\n{CYAN}{hostname}{RESET}{cwd}\n> '
# $NPM_PACKAGES = '/.npm-packages'
$NPM_PACKAGES = p'~/.npm-packages'.expanduser().as_posix()
$NODE_PATH = p'~/.npm-packages/lib/node_modules'.expanduser().as_posix()
$GOPATH = p'~/opt/go-path'.expanduser().as_posix()
$GOROOT = p'~/opt/go-root'.expanduser().as_posix()
$GOBIN = p'~/opt/go-root/bin'.expanduser().as_posix()
# $GREP_COLORS = '38;5;192'
$GREP_COLORS = '38;5;21'
$JAVA_HOME = '/Users/bsan0009/opt/microsoft-jvm/jdk-17.0.1+12/Contents/Home'
$SPARK_HOME = '/Users/bsan0009/opt/spark-3.2.0-bin-hadoop3.2'
$PATH[0:0] = [
# p'~/opt/jdk-11.0.11+9/Contents/Home/bin'.as_posix(),
p'~/opt/microsoft-jvm/jdk-17.0.1+12/Contents/Home/bin'.expanduser().as_posix(),
p'~/opt/apache-maven-3.8.1/bin'.expanduser().as_posix(),
p'/usr/local/opt/coreutils/libexec/gnubin'.as_posix(),
p'/usr/local/opt/coreutils/libexec/gnubin'.as_posix(),
p'~/opt/blink/bin'.expanduser().as_posix(),
p'~/opt/go-root/bin'.expanduser().as_posix(),
p'~/opt/go-path/bin'.expanduser().as_posix(),
p'~/opt/pylocal/bin'.expanduser().as_posix(),
p'~/opt/vimlocal/bin'.expanduser().as_posix(),
p'~/opt/vimlocal/bin'.expanduser().as_posix(),
p'~/opt/tmuxlocal/bin'.expanduser().as_posix(),
p'/usr/local/bin'.as_posix(),
p'/usr/local/sbin'.as_posix(),
p'~/opt/go-root/bin'.expanduser().as_posix(),
p'~/.cargo/bin'.expanduser().as_posix(),
p'~/bin'.expanduser().as_posix(),
p'~/opt/bin'.expanduser().as_posix(),
p'~/.npm-packages/bin'.expanduser().as_posix(),
p'/usr/local/bin'.as_posix(),
p'~/.local/bin'.expanduser().as_posix(),
$GOBIN,
]
abbrevs['k'] = 'kubectl'
abbrevs['g'] = 'git'
abbrevs['emacsd'] = 'emacs -nw --daemon=main'
# abbrevs['ls'] = 'ls --color=auto -1'
abbrevs['j'] = 'jupyter lab'
abbrevs['h'] = 'http'
abbrevs['r'] = 'ranger'
abbrevs['v'] = 'vim'
abbrevs['cl'] = 'clear'
abbrevs['b'] = 'bat'
abbrevs['s'] = "salambria r'''* '''"
aliases['ls'] = 'ls --color=auto -1'
# aliases['emacs'] = '$SHELL="/usr/local/bin/dash" and /usr/local/bin/emacs -nw'
def _emacs():
# with ${...}.swap(SHELL='/usr/local/bin/dash'):
# /usr/local/bin/emacs -nw
/usr/local/bin/emacs -nw
# Better to return the rc of emacs
return 0
aliases['emacs'] = _emacs
aliases['ec'] = 'emacsclient -t -s ~/.emacs.d/server/server'
aliases['ew'] = 'emacsclient -c -s ~/.emacs.d/server/server -F \'((font . "-ADBE-Source Code Pro-normal-normal-normal-*-18-*-*-*-m-0-iso10646-1"))\''
# aliases['bat'] = 'bat --style=plain --pager=never --theme DarkNeon'
aliases['bat'] = 'bat --style=plain --theme DarkNeon'
# aliases['clojure'] = "$HOME/opt/jdk-11.0.11+9/Contents/Home/bin/java -Djavax.net.ssl.trustStore=/Users/bsan0009/opt/jdk-11.0.11+9/Contents/Home/lib/security/cacerts -Djavax.net.ssl.trustStorePassword=changeit -jar $HOME/tmp/clojure/clojure.jar"
aliases['clojure'] = "java -jar $HOME/tmp/clojure/clojure.jar"
aliases['aria2c'] = 'aria2c --seed-time=15'
aliases['ps'] = 'ps aux' if sys.platform == 'darwin' else 'ps auxf'
aliases['groovysh'] = "bash -c '. $HOME/.sdkman/bin/sdkman-init.sh && groovysh'"
aliases['groovy'] = "bash -c '. $HOME/.sdkman/bin/sdkman-init.sh && groovy'"
aliases['vr'] = "vim -c 'RangerChooser'"
# aliases['java'] = "$HOME/opt/jdk-11.0.11+9/Contents/Home/bin/java -Djavax.net.ssl.trustStore=/Users/bsan0009/opt/jdk-11.0.11+9/Contents/Home//lib/security/cacerts -Djavax.net.ssl.trustStorePassword=changeit"
# aliases['java'] = "/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home/bin/java"
$DOTNET_CLI_TELEMETRY_OPTOUT = '1'
aliases['dotnet'] = '/usr/local/share/dotnet/dotnet'
aliases['grep'] = 'ggrep --color=always'
# aliases['docker'] = 'podman'
aliases['icat'] = 'kitty +kitten icat'
import enum
class Resolution(enum.Enum):
dayly = 1
hourly = 2
once_a_minute = 3
def terminal_size():
import fcntl, termios, struct
th, tw, hp, wp = struct.unpack(
'HHHH',
fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))
)
return th, tw
def _capture(resolution=Resolution.dayly):
root = '/1drive/OneDrive/mrsipan/org/capture'
if resolution == Resolution.dayly:
file_name = datetime.datetime.now().strftime('%Y%m%d-%a.org')
elif resolution == Resolution.once_a_minute:
file_name = datetime.datetime.now().strftime('%Y%m%d-%a-%H-%M.org')
return f'{$HOME}/{root}/{file_name}'
aliases['dn'] = 'vim @(_capture())'
aliases['mn'] = 'vim @(_capture(Resolution.once_a_minute))'
aliases['yn'] = 'vim $HOME/1drive/OneDrive/mrsipan/org/@(datetime.date.today().year).org'
if sys.platform == 'darwin':
$DYLD_LIBRARY_PATH[0:0] = [
p'~/opt/python/lib'.as_posix(),
]
# aliases['diffr'] = 'diffr --colors refine-added:none:background:0x33,0x99,0x33:bold --colors added:none:background:0x33,0x55,0x33 --colors refine-removed:none:background:0x99,0x33,0x33:bold --colors removed:none:background:0x55,0x33,0x33'
if sys.platform == 'darwin':
aliases['sed'] = '/usr/local/bin/gsed'
# Coloured man page support
# using 'less' env vars (format is '\E[<brightness>;<colour>m')
$LESS_TERMCAP_mb = "\033[01;31m" # begin blinking
$LESS_TERMCAP_md = "\033[01;31m" # begin bold
$LESS_TERMCAP_me = "\033[0m" # end mode
$LESS_TERMCAP_so = "\033[01;44;36m" # begin standout-mode (bottom of screen)
$LESS_TERMCAP_se = "\033[0m" # end standout-mode
$LESS_TERMCAP_us = "\033[00;36m" # begin underline
$LESS_TERMCAP_ue = "\033[0m" # end underline
# @events.on_ptk_create
# def key_bindings(bindings, **kw):
# @bindings.add('c-_')
# def _(ev):
# if VimInsertMode:
# ev.current_buffer.insert_text('hi')
aliases['genpw'] = lambda x: ''.join(random.sample(
string.ascii_letters + string.punctuation + string.digits, 12
))
# aliases['ssh'] = "kitty +kitten ssh"
@events.on_ptk_create
def custom_keybinding(bindings, **kw):
@Condition
def suggestion_available() -> bool:
app = get_app()
return (
app.current_buffer.suggestion is not None
and len(app.current_buffer.suggestion.text) > 0
and app.current_buffer.document.is_cursor_at_the_end
)
@bindings.add('c-f', filter=suggestion_available & vi_insert_mode)
def _fill_partially_by_word(ev) -> None:
'''Fill partial suggestion
'''
buf = ev.current_buffer
suggestion = buf.suggestion
if suggestion:
t = re.split(r"(\S+\s+|[./]+)", suggestion.text)
buf.insert_text(next(x for x in t if x))
@bindings.add('c-z', filter=vi_mode)
def _call_fg(ev) -> None:
'''Call `fg` command
'''
n_of_tasks = len(xonsh.jobs.tasks)
if n_of_tasks == 1:
fg
elif n_of_tasks > 1:
jobs
else:
print('No jobs', end='\r')
# Registering our own `unix-word-rubout`. Check the regex
@named_commands.register("unix-word-rubout")
def unix_word_rubout(event, WORD: bool = False) -> None:
"""
Kill the word behind point, using whitespace as a word boundary.
Usually bound to ControlW.
"""
buf = event.current_buffer
po = buf.document.find_start_of_previous_word(
# count=event.arg, WORD=WORD, pattern=re.compile(r'(\S+\s+|[./]+)')
count=event.arg, WORD=WORD, pattern=re.compile(r'(\s+|[./]+)')
)
if po is None:
# Nothing found? delete until the start of the document. (The
# input starts with whitespace and no words were found before the
# cursor.)
po = -buf.cursor_position
if po:
deleted = buf.delete_before_cursor(count=-po)
# If the previous key press was also Control-W, concatenate deleted
# text.
if event.is_repeat:
deleted += event.app.clipboard.get_data().text
event.app.clipboard.set_text(deleted)
else:
# Nothing to delete. Bell.
event.app.output.bell()
$LIBRARY_PATH = [
'/usr/local/lib/gcc/current',
'/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/lib/'
]
#$fzf_history_binding = "c-r"
#$fzf_ssh_binding = "c-s"
# $fzf_file_binding = "c-@"
# $fzf_dir_binding = "c-t"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment