Skip to content

Instantly share code, notes, and snippets.

@Shinichi-Ohki
Last active February 26, 2020 14:17
Show Gist options
  • Save Shinichi-Ohki/17c7e4cf2addd4971e7332b7203c2dc1 to your computer and use it in GitHub Desktop.
Save Shinichi-Ohki/17c7e4cf2addd4971e7332b7203c2dc1 to your computer and use it in GitHub Desktop.
base converter workflow for Alfred 4
def output_16(q16)
output = <<"EOS"
{ "title": "#{q16}",
"arg": "#{q16}",
},
{ "title": "0x#{q16}",
"arg": "0x#{q16}",
}
EOS
output
end
def output_02(q02)
output = <<"EOS"
{ "title": "#{q02}",
"arg": "#{q02}",
},
{ "title": "0b#{q02}",
"arg": "0b#{q02}",
}
EOS
output
end
def output_10(q10)
output = <<"EOS"
{ "title": "#{q10}",
"arg": "#{q10}",
}
EOS
output
end
start_string = "{\"items\": ["
end_string = "]}"
query = ARGV.first
if (/\A[+-]?[0-9]+\z/ =~ query) then
q16 = query.to_i(10).to_s(16)
q02 = query.to_i(10).to_s(2)
output = start_string + output_16(q16) + ',' + output_02(q02) + end_string
elsif (/\A0[xX][0-9A-Fa-f]+/ =~ query) then
q10 = Integer(query)
q02 = q10.to_s(2)
output = start_string + output_10(q10) + ',' + output_02(q02) + end_string
elsif (/\A0[bB][01]+/ =~ query) then
q10 = Integer(query)
q16 = q10.to_s(16)
output = start_string + output_10(q10) + ',' + output_16(q16) + end_string
end
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment