Skip to content

Instantly share code, notes, and snippets.

View cGuille's full-sized avatar

Guillaume Charmetant cGuille

View GitHub Profile
@cGuille
cGuille / keeptrying
Created March 20, 2024 14:21
A Bash command to keep trying running a command until it succeeds or is canceled.
#!/usr/bin/env bash
set -eu
COLOR_ERROR='\033[0;31m'
COLOR_RESET='\e[0m'
prog_name="$(basename "$0")"
function usage() {
@cGuille
cGuille / autogitfetch
Last active January 16, 2024 14:33
A simple Bash script to automatically git fetch (with delay), to be called in combination with __git_ps1 for your prompt.
#!/usr/bin/env bash
set -eu
MIN_INTERVAL="${AUTOGITFETCH_MIN_INTERVAL:-300}" # seconds
TIMESTAMP_STORE_ROOT="${HOME}/.cache/autogitfetch/"
RESET='\e[0m'
GREY='\e[0;90m'
OUTPUT_TEXT_COLOR="$GREY"
@cGuille
cGuille / git-rt
Created October 6, 2023 16:13
Show status of the current branch vs. its remote tracking branch with `git rt`.
#!/usr/bin/env bash
# Make this file executable: `chmod +x git-rt`.
# Then move it to a PATH directory to be able to run it as a Git subcommand (`git rt`).
set -eu
function status_val() {
local property="${@:$#}" # last parameter
local status_opts="${@:1:$#-1}" # all parameters except the last
@cGuille
cGuille / hr.bash
Created September 26, 2018 08:30
I wrote this `hr` command because sometimes having a separator in your terminal helps you find things in the output of your previous commands
#!/usr/bin/env bash
usage() { echo "Usage: hr [-h] [-c <CHAR (defaults to '=')>] [<LINES (defaults to 1)>]" 1>&2; exit 1; }
CHAR='='
while getopts "h c:" ARG; do
case $ARG in
c)
CHAR="$OPTARG"
@cGuille
cGuille / noindent.bash
Created July 24, 2018 07:45
Remove indentation (leading blank characters) from stdin
#!/usr/bin/env bash
set -eu
sed -r 's/^[[:blank:]]+//'
@cGuille
cGuille / sween.bash
Last active January 12, 2018 12:14
Command line utility to switch between full and low brightness using xbacklight.
#!/usr/bin/env bash
if [[ $* == *-h* ]]; then
cat <<USAGE
Switch between full and low brightness using xbacklight.
sween [low-brightness]
low-brightness defaults to 5, and is a percentage.
USAGE
exit 1
@cGuille
cGuille / slice.bash
Created January 10, 2018 16:24
Slice a text file.
#!/usr/bin/env bash
# Output lines of stdin from $1 to $2.
# $1 and $2 can be line numbers or patterns. Examples:
# slice 10 20 # keep lines 10 to 20
# slice /foo/ /bar/ # keep lines from the one matching foo to the one matching bar.
# slice 31703 '/<\/Product>/' # keep lines from number 31703 to the first one matching </Product>
sed "$1,$2!d" -
@cGuille
cGuille / vidtogif.bash
Created November 18, 2017 10:49
Convert videos to GIF using ffmpeg and gifski.
#!/usr/bin/env bash
video_file_path="$1"
from="$2"
to="$3"
set -error
tmpdir_path="${TMPDIR:-/tmp}/"
#!/usr/bin/env bash
# Installation:
# - Download this file.
# - Name it `missinglines`.
# - Make it executable (`chmod +x missinglines`).
# - Move it to your PATH.
set -o errexit
@cGuille
cGuille / ghopen.bash
Last active July 17, 2017 07:31
Open the Git repository on GitHub
#!/usr/bin/env bash
# INSTALLATION:
# Drop this file in your PATH, name it `ghopen` and give it executable permission.
#
# You can set up the `GHOPEN_BROWSER` env var to define which browser to open.
# Example values: 'google-chrome' (default), 'firefox'…
# -----
# USAGE:
# Just run `ghopen` inside a Git repository having a GitHub remote.