Skip to content

Instantly share code, notes, and snippets.

@knugie
knugie / keyboard_controller.rb
Last active March 10, 2022 07:57 — forked from efi/keyboard_controller.rb
Send system keyboard events in JRuby via java.awt.Robot
class KeyboardController
def initialize
@robot = java.awt.Robot.new
end
def type *args
[args].flatten.map(&:to_s).map{|s|s.split(/\s+/)}.flatten.map(&:upcase).each do |n|
press, name = (n[0]=="-") ? [false,n[1..-1]] : [true,n]
press ? @robot.key_press(@code) : @robot.key_release(@code) if @code = java.awt.event.KeyEvent.const_get("VK_#{name}")
end
self
@knugie
knugie / macOS.md
Created September 30, 2021 07:43
Open ssh tunnel macOS

image

@knugie
knugie / example_encrypt_decrypt.rb
Created May 28, 2020 19:32
Encrypt, Decrypt large files
# From https://tjay.dev/howto-working-efficiently-with-large-files-in-ruby/
# Encrypt
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
cipher.key = KEY
cipher.iv = IV
file = nil
enc_file = nil
@knugie
knugie / example.sh
Last active August 11, 2019 19:18
ASCII art with figlet
# brew install figlet
# showfigfonts
# example:
figlet -D -n -f fender "The quick brown fox jumps over the lazy dog [ \ ] { | } ~"
# |''||''| '|| '||
# || || '' ||
# || ||''|, .|''|, .|''||` '|| ||` || .|'', || //`
# || || || ||..|| || || || || || || ||<<
# .||. .|| || `|... `|..|| `|..'|. .||. `|..' .|| \\.
@knugie
knugie / solution.rb
Created September 2, 2018 20:47
How many 3 digit numbers have exactly 3 factors?
[*100..999].count{ |x| (1..x).count { |i| x % i == 0 } == 3 }
# => 7
#!/usr/bin/env ruby
###################################################
## Display screen dimensions in macOS using ruby ##
##################################################
# found https://developer.apple.com/reference/coregraphics/1456395-cgdisplaybounds?language=objc
# --> use CoreGraphics framework
# --> CGRect CGDisplayBounds(CGDirectDisplayID display);
#############################################
#!/usr/bin/env bash
size=1024 # MB
mount_point=$HOME/tmp
name=$(basename "$mount_point")
usage() {
echo "usage: $(basename "$0") [mount | umount | remount | check | orphan]" \
"(default: mount)" >&2
}
@knugie
knugie / mts_to_mp4.sh
Last active October 2, 2017 18:20
Convert (Sony) MTS files to MP4
ffmpeg -i in.mts -c:v mpeg4 -qscale:v 5 -acodec libmp3lame -b:a 192k out.mp4
@knugie
knugie / install_makemkv.sh
Last active November 23, 2016 06:17
Ubuntu - Install MakeMKV 1.9.0
sudo apt-get install build-essential pkg-config libc6-dev libssl-dev libexpat1-dev libavcodec-dev libgl1-mesa-dev libqt4-dev
wget http://www.makemkv.com/download/makemkv-oss-1.9.0.tar.gz
wget http://www.makemkv.com/download/makemkv-bin-1.9.0.tar.gz
tar -xvf makemkv-oss-1.9.0.tar.gz
tar -xvf makemkv-bin-1.9.0.tar.gz
cd makemkv-oss-1.9.0/
./configure
make
sudo make install
@knugie
knugie / vector_cluster.rb
Last active June 29, 2016 19:05
Cluster vectors by their similarity defined by a tolerance vector
require 'matrix'
tolerance = Vector[5, 5, 5]
elements = [Vector[2, 1, 4], Vector[20, 100, 25], Vector[21, 98, 21], Vector[1, 2, 3]]
clusters = []
while(elements.any?)
element = elements.pop
cluster, elements = elements.partition do |elem|
diff = tolerance - (elem - element).map(&:abs)