Skip to content

Instantly share code, notes, and snippets.

View Shannarra's full-sized avatar
🏠
Working from home

Petar Shannarra

🏠
Working from home
View GitHub Profile
@Shannarra
Shannarra / pre-commit
Created October 10, 2023 11:23
A Git pre-commit hook for Ruby (on Rails) and Rubocop that can be run on docker
#!/bin/bash
echo "\nRunning rubocop 🤖 🚓 💨 \n"
declare -a ERRORS=()
if git diff HEAD --name-only | grep -q -E '\.rb$'; then
ERRORS+=$(git diff HEAD --name-only | \
grep -E '.rb' | \
xargs docker compose run --rm web bundle exec rubocop | \
@Shannarra
Shannarra / raylib_input.c
Created August 21, 2023 10:15
Customizable and constrainable input field created with Raylib, useful for any user input.
#include "raylib.h"
#include <string.h>
typedef struct S_input_properties {
Color outline_color;
Color text_color;
int font_size;
} input_properties;
input_properties create_input_properties(Color outline_col, Color text_col, int font_size) {
@Shannarra
Shannarra / goto_in_ruby.rb
Last active June 23, 2023 13:21
A quick and easy-to-understand implementation of the `goto` directive in Ruby. Just wanted to prove that you can have goto in Ruby.
class Object
HASH = {}
def label(name, &block)
HASH[name] = proc(&block)
end
def goto(name)
raise "No label with name #{name} found" unless HASH.key?(name)
# http://www.mathrecreation.com/2014/09/squashing-multiples.html
inp = ARGV[0].to_i
raise 'Expected a number as an input' if inp.nil? || ARGV[0].nil?
while inp > 10
dig = inp.to_s.chars.map(&:to_i).sum
puts dig
inp = dig
end
@Shannarra
Shannarra / bouncy-box.html
Created January 10, 2022 11:12
A bouncy box using pure HTML and JS.
<html style='background-color: black'>
<div id="my_bouncing_box" style= "background-color: blue; border-radius: 3px; position: absolute; left: 0px; top: 0px; min-width: 100px; min-height: 100px; text-align: center; font-weight: bold; color: #999;" > Bouny bounce</div>
<script type="text/javascript">
const BOX = document.getElementById('my_bouncing_box'),
SIZE = 100,
WINDOW_X = window.innerWidth - SIZE;
WINDOW_Y = window.innerHeight - SIZE;
let x = 0;
let y = 0;
@Shannarra
Shannarra / interpolate.cpp
Last active January 7, 2022 15:03
Basic c++ in-place interpolation. It preserves the base string and expands it with the arguments provided in the place of the template string combination "@x".
/*
//Basic usage:
std::string base("@X, \"@X @X\"@X");
std::string args[4] = { "Hello", "John", "Doe", "!"};
std::cout << '\"' << interpolate(base, args) << '\"' << '\n';
// It can also work with arguments next to one another
// and with std::initializer_list
; String length calculation function
strlen:
push rdi
mov ebx, eax
__nextc:
cmp byte [eax], 0
jz __finish
inc eax
jmp __nextc
@Shannarra
Shannarra / kill_all_on_x.sh
Created December 16, 2021 15:45
Kill all processes on any port
PORT=$1 #the script will expect one argumnet - the port number.
if [ ! -z "$PORT" ]; then
kill -9 $(lsof -t -i:$PORT -sTCP:LISTEN);
echo "Killed all processes on port $PORT"
else
echo 'Provide a port to kill!'
fi