Skip to content

Instantly share code, notes, and snippets.

View apolopena's full-sized avatar
💭
We live to learn!

Apolo Pena apolopena

💭
We live to learn!
View GitHub Profile
@apolopena
apolopena / lspicevm
Last active February 11, 2024 17:57
Securely launch Proxmox VM's in Spice Viewer from the command line
#!/bin/bash
# Globals
version='1.1.1'
username=
password=
vmid=
node=
proxy=
verbose=
# Function patch_wsl_vscode_node
# Note: This is a hack for wsl vscode when using a distor than canno run binaries outside the path
# such as in the case of void linux. This hack also assumes that node is installed on the wsl linux distro
# Every vscode update will change the folder where the binaries that wsl vscode uses
# Hence on every update you will get an error when trying to run code from wsl like the following
# ${HOME}/.vscode-server/bin/8b3775030ed1a69b13e4f4c628c612102e30a681/bin/remote-cli/code: 12: ${HOME}/.vscode-server/bin/8b3775030ed1a69b13e4f4c628c612102e30a681/node: not found
# Pass in the latest hash where the binaries for vscode server on wsl resides
# And a new symlink will be created using the systems node binary
function patch_wsl_vscode_node() {
local hash="$1"
# Function: swap_kitty_theme
# Pass in a Kitty Terminal theme .conf and it will be written to $HOME/.config/kitty/current_colors.conf
# Kitty's configuration will be reloaded automagically
# Assumes you have the following line in your $HOME/.config/kitty/kitty.conf
# include current_colors.conf
# Example: alias k-theme-monalisa="swap_kitty_theme 'Mona Lisa.conf'"
function swap_kitty_theme() {
local base="$HOME/.config/kitty/"
local colors="${base}current_colors.conf"
local theme="${base}$1"
@apolopena
apolopena / bash-remove-element-from-an-array.md
Last active April 8, 2022 20:19
Bash: remove an element from an array

Remove an element from an array

Arrays in bash were not designed for use as mutable data structures. They are primarily used for storing lists of items in a single variable without needing to waste a character as a delimiter (e.g., to store a list of strings which can contain whitespace). We can however remove an element from an array by looping through the array and rebuilding it as a temporary array, set the array to the temporary array and then finally unset the temporary array.

#!/bin/bash

example() {
 local i args tmp_args=()
 args=("$@")
 echo "${#args[@]} args before the filter: ${args[*]}"
 if  [[ " ${args[*]} " =~ " --load-deps-locally " ]]; then
@apolopena
apolopena / unified-diff-sed-colorized.md
Last active April 4, 2022 22:29
Completely colorize a unified diff using sed and without any additional dependencies

The Need

You want fully colorized output for a unified diff of two files or directories (recursively) but you do not want to add any additional dependencies such as colordiff or grc image

The Solution

Pipe output of the unified diff through sed and add colors using ANSI escape sequences based on the pattern of the output.

diff -ur --minimal file1.txt file2.txt | \
sed 's/^[^-+@]/\x1b[38;5;34m&/;s/^-/\x1b[48;5;106m-/;s/^+/\x1b[42m+/;s/^@/\x1b[48;5;130m@/;s/$/\x1b[0m/'

For any of you that are still learning to read sed commands and escape sequences, the rules of parsing the output are as follows:

@apolopena
apolopena / ANSI.md
Created April 4, 2022 16:23 — forked from fnky/ANSI.md
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@apolopena
apolopena / shell-and-bash-cheats-sheet.md
Last active March 28, 2022 04:00
A cheat sheet of useful shell and bash snippets. This is not a POSIX compliant cheat sheet.

Exit codes and doing nothing

You may find yourself needing to manipulate exit codes or how a script behaves with them.

Sometimes in shell programming doing nothing is very useful.

The null utility : always returns an exit code of 0

Use : to:

  • Force a command to never return an error such as when you want the output of an error but not the exit code that goes with it.
@apolopena
apolopena / sed-in-place-replace-line-of-code.md
Created March 27, 2022 18:30
search and replace (parse) a line of code in a file with `sed` and know if sed actually made any changes

The Need

You need to search and replace (parse) a line of code in a file with sed and know if sed actually made any changes

The Solution

sed can save its output to a backup file if you append the file extension to the -i option. You can then used cmp silently to check if the files have changed. cmp checks the bytes of the files so the comparison is not processor intensive. Finally just remove the backup file and there is no mess left behind.

Examples

Simple in place replacement in the file myfile.txt and confirm if changes were made

'sed -i.bak '/TEXT_TO_BE_REPLACED/c\This is the next text.' myfile.txt
@apolopena
apolopena / life_wisdom_poem.js
Last active March 24, 2022 05:33
Life Wisdom: A compilable programatic poem in Javascript by Apolo Pena
/* -------------------------------------------------------------------------------------------
* ----Life Wisdom: A compilable programatic poem in Javascript by Apolo Pena, March 23rd 2019-----
----------------------------------------------------------------------------------------------- */
const/*antly*/ life= /*is a*/ new Array( /*of*/ Math.Infinity );
let Life = true, always=(do_no_harm)=>{with(life){return ['always rememberring LOVE']};};
const antly_be_as_IS = 'forever living in the present moment.';
let love_of_life={Be:('rich'&'meaningful'),abundant:(''&'prosperous'),
and:'remember.',
In:{life:{always:'',breathe:'consciously',in:(''&'out!')}}},
and; var iably; let consciousness=/*be an*/Array.from(love_of_life), when =()=>{}, forward = true;
@apolopena
apolopena / is_long_option.sh
Created March 21, 2022 22:34
Bash function to validate a long option
#!/bin/bash
#
# SPDX-License-Identifier: MIT
# Copyright © 2022 Apolo Pena
### is_long_option ###
# Description:
# Validates a long option using the below rules
# Returns 0 if $1:
# Starts with double dashes followed by any number of uppercase or lowercase letters or integers