Skip to content

Instantly share code, notes, and snippets.

View jamescherti's full-sized avatar

James Cherti jamescherti

View GitHub Profile
@jamescherti
jamescherti / bash_retry.sh
Last active December 25, 2022 22:08
Shell: Retry a command until it succeeds.
#!/usr/bin/env bash
# Description: Retry a command until it succeeds.
# Author: James Cherti
# License: MIT
# URL: https://gist.github.com/jamescherti/995add1dcbf3080dfa25cf372287cca9
echo "[RUN] $*" >&2
while true; do
if "$@"; then
break
@jamescherti
jamescherti / python_sha512_of_file.py
Last active December 25, 2022 22:08
A Python method that returns the SHA512 hash of a file.
#!/usr/bin/env python
# License: MIT
# URL: https://gist.github.com/jamescherti/47e0cd74505147911b483cee11e256b1
import hashlib
def sha512sum(filename):
"""Return the SHA512 hash of a file."""
sha512 = hashlib.sha512()
with open(filename, 'rb') as fhandler:
@jamescherti
jamescherti / python_which.py
Last active December 25, 2022 22:08
which() is a Python method that returns the full path of a (shell) command.
#!/usr/bin/env python
# Author: James Cherti
# License: MIT
# URL: https://gist.github.com/jamescherti/53e5d3bb4da597f24f4fe8c42fe49ea1
"""Which() is a Python method that returns the full path of a (shell) command."""
from pathlib import Path
import os
def which(command: str) -> Path:
@jamescherti
jamescherti / python_human_readable_file_size.py
Last active December 25, 2022 22:07
A python method that returns the human-readable size of a file.
#!/usr/bin/env python
# Author: James Cherti
# License: MIT
# URL: https://gist.github.com/jamescherti/b8badae104576edc082f5095bfe0652f
"""Return the human-readable size of a file."""
from typing import Union
def human_readable_file_size(file_size: Union[int, float]) -> str:
"""Return the human-readable size of a file."""
@jamescherti
jamescherti / python_yes_no_question_timeout.py
Last active December 25, 2022 22:07
Python: Ask the user a yes/no question.
#!/usr/bin/env python
# License: MIT
# Author: James Cherti
# URL: https://gist.github.com/jamescherti/1f7d9adc428db00f9104312a24537d52
"""Ask the user a yes/no question."""
import sys
import signal
import select
@jamescherti
jamescherti / vim_auto_complete_input.vim
Last active December 25, 2022 22:07
Vim input() function: auto-complete from a list.
" Language: Vim script
" Author: James Cherti
" License: MIT
" URL: https://gist.github.com/jamescherti/42d7995184a6ac01175c5e9190763772
" Decsription: Vim input() function: auto-complete from a predefined list
function! AutoCompleteInput(arglead, cmdline, cursorpos)
return ["first_answer", "second_answer"]
endfunction
@jamescherti
jamescherti / vim_switch_between_file_and_netrw.vim
Last active December 25, 2022 22:07
Vim: an easy way to switch between the currently edited file and netrw (the local-directory browser), and vice versa (keyboard mapping: <Leader>e).
" Language: Vim script
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/07aa562b0b263ba414655a5701a3868d
" License: MIT
" Description: NetrwSwitch() will allow you switch between the currently
" edited file and netrw (the local-directory browser),
" and vice versa (keyboard mapping: '<Leader>e').
function! NetrwSwitch()
if &filetype !=# 'netrw' && exists('w:netrw_switch_previous_buffer')
@jamescherti
jamescherti / vim_close_all_vim_terminals.vim
Last active December 25, 2022 22:06
Vim: Close the buffer of all Vim terminals (Vim's built-in terminals).
" Language: Vim script
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/d552078af417a9c3c84c71d7db30f06a
" License: MIT
" Description: Close the buffer of all Vim terminals (Vim's built-in terminals)
function! CloseAllTerminals() abort
let l:buflist = getbufinfo()
for l:buf in l:buflist
if getbufvar(l:buf.bufnr, '&buftype', '') ==# 'terminal'
@jamescherti
jamescherti / vim_git_root_directory.vim
Last active December 25, 2022 22:06
Vim: Return the Git root directory / folder (Git top level).
" Language: Vim script
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/c36c975aad4802a0e47a4a8274d55684
" License: MIT
" Description: Return the Git root directory / folder (Git top level).
" let toplevel = GitRootDir()
" let toplevel = GitRootDir("/path/to/dir")
function! GitRootDir(...) abort
if len(a:000) > 0
@jamescherti
jamescherti / bash_get_urls.sh
Last active December 25, 2022 22:05
Shell: extract all download links from a URL.
#!/usr/bin/env sh
# Description: extract all download links from a URL.
# Usage: get_urls "https://domain.com/page"
# Requirements: lynx, sed, grep, and bash.
# Author: James Cherti
# URL: https://gist.github.com/jamescherti/8ee95c52d283671ab314afb09feab9dc
# License: MIT
get_urls() {
if [ $# -lt 1 ]; then