Skip to content

Instantly share code, notes, and snippets.

@BlindLemonLipschitz
Last active June 7, 2017 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlindLemonLipschitz/02fa5cbca244a25aa672 to your computer and use it in GitHub Desktop.
Save BlindLemonLipschitz/02fa5cbca244a25aa672 to your computer and use it in GitHub Desktop.
Convert a string to macro syntax for TMK keyboard firmware(https://github.com/tmk/tmk_keyboard)
#!/bin/env ruby
# convert a string into macro syntax for TMK firmware
# be sure to escape quotes e.g. ruby macro.rb "Pass\"word1"
# usage: ruby macro.rb "STRING" NAME_OF_MACRO
# Example:
# prompt# ruby macro.rb "this\"is aPa33word" name
# "T(T), T(H), T(I), T(S), D(LSFT), T(QUOT), U(LSFT), T(I), T(S), D(LSFT),/
# T(SPC), U(LSFT), T(A), D(LSFT), T(P), U(LSFT), T(A), T(3), T(3), T(W),/
# T(O), T(R), T(D), "
#
name = if ARGV[1]
ARGV[1].upcase
else
"NAME"
end
shifted_special_chars = { "+" => "EQL", "_" => "MINS", "!" => "1", "@" => "2", "#" => "3", "$" => "4", "%" => "5", "^" => "6", "&" => "7", "*" => "8", "(" => "9", ")" => "0" , "~" => "GRV", "?" => "SLSH", "<" => "COMM", ">" => "DOT", "{" => "LBRC", "}" => "RBRC", "|" => "BSLS", '"' => "QUOT", " " => "SPC", ":" => "SCLN"}
unshifted_special_chars = { "=" => "EQL", "-" => "MINS", "`" => "GRV", "/" => "SLSH", "," => "COMM", ";" => "SCLN", "." => "DOT", "[" => "LBRC", "]" => "RBRC", "\\" => "BSLS", '"' => "QUOT" }
def type(char)
"T(#{char})"
end
def shift(char)
"D(LSFT), #{char}, U(LSFT)"
end
@mac = ""
ARGV[0].each_char do |char|
case
when char =~ /[A-Z]/
@mac << shift("#{type(char)}")
when char =~ /[a-z\d]/
@mac << type(char).upcase
when shifted_special_chars.has_key?(char)
char = shifted_special_chars[char]
@mac << shift("#{type(char)}")
when unshifted_special_chars.has_key?(char)
char = unshifted_special_chars[char]
@mac << type(char)
end
@mac << ", "
end
space = ' ' * (14 - name.length)
puts "#define MACRO_#{name}" + "#{space}" + "MACRO( \\"
puts " I(15), \\"
puts @mac
puts " END) \\"
@scramble45
Copy link

Any chance we could could get a JS port of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment