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 / JSConvert12to24Time.js
Last active May 2, 2023 12:24
JavaScript: convert 12 hours time string to 24 hour time string
// NOTE: time string must look like this 07:05:45PM or this 07:05:45AM and account for 12:00:00AM and convert 12:00:00pm to 12:00:00
function timeConversion(s) {
const ampm = s.slice(-2);
const hours = Number(s.slice(0, 2));
let time = s.slice(0, -2);
if (ampm === 'AM') {
if (hours === 12) { // 12am edge-case
return time.replace(s.slice(0, 2), '00');
}
return time;
@apolopena
apolopena / bash-set-and-unset-env-from-file.sh
Last active March 25, 2023 11:14
Set and unset environment variables from an .env file
# set environment vars from an .env file
export $(grep -v '^#' .myenvfile | xargs)
# unset environment vars from an .env file
unset $(grep -v '^#' .myenvfile | awk 'BEGIN { FS = "=" } ; { print $1 }')
@apolopena
apolopena / capture-lolcat-raw-colors.md
Last active December 16, 2022 17:47
Capture a raw dump of lolcat colorized output (for a script or a command) that includes the ANSI escape sequences for the colors.

The Need

  • You need a raw dump of lolcat colorized output that includes the ANSI escape sequences for the colors.
  • You need this dump saved to a file.

The Solution

Pipe your output through lolcat, use the --force flag and then redirect the output to a file.

Examples

  1. Capture a raw dump of the output of a bash script piped through lolcat to the file colors.txt
  • bash my-script.sh | lolcat --force > colors.txt
@apolopena
apolopena / JSHRApplesAndOrangesFallingSolved.js
Created January 21, 2019 20:37
JavaScript: HackerRank Apples and Oranges Tree falling by the house problem solved
// my solution using easy to refactor methods (in case you wanted to either share logic or compare new types of fruit)
function countApplesAndOranges(s, t, a, b, apples, oranges) {
const fLoc = function (treeLoc, arr2d) {
return arr2d.map(fruitLoc => (treeLoc + fruitLoc));
}
const fRange = function (s, t, arr2d) {
let a, b;
a = 0; b = 0;
arr2d.forEach((f, i) => {
if (i === 0) { // apple count
@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