#!/usr/bin/env ruby
#
# ,hex - view number in hex bin and dec

input = ARGV.first
raise "Usage: #{$0.split('/').last} <number>" if input.nil? || input.empty?

# Check if binary
bases = if input[/^0b[01]+$/]
  [2]
elsif input[/[a-fA-F]/] || input[/^0?x/]
  [16]
# Check if not binary
elsif input[/[2-9]/]
  [10, 16]
# Assume it's hex, bin, or dec
else
  [10, 16, 2]
end

$GREEN = 32
$YELLOW = 33
$BLUE = 34

def num2all(int)
  alt_color = $BLUE
  hex = ""
  bin = ""
  int.to_s(16).split("").reverse.each_with_index do |nibble, i|
    color = (i / 4).even? ? 0 : alt_color
    spacer = (i % 4).zero? ? "_" : ""
    bits = nibble.to_i(16).to_s(2).rjust(4, "0")
    hex = nibble.color(color) + spacer + hex
    bin = bits.color(color) + "_#{bin}"
  end
  puts "#{'Hex'.bold_color($YELLOW)}: #{hex.chomp('_')}"
  puts "#{'Bin'.bold_color($YELLOW)}: #{bin.chomp('_')}"

  dec = ""
  int.to_s(10).split("").reverse.each_with_index do |digit, i|
    color = (i / 6).even? ? 0 : alt_color
    spacer = (i % 3).zero? ? "," : ""
    dec = digit.color(color) + spacer + dec
  end
  puts "#{'Dec'.bold_color($YELLOW)}: #{dec.chomp(',')}"
end

class String
  def color(n)
    "\e[#{n}m#{self}\e[0m"
  end

  def bold_color(n)
    color("1;#{n}")
  end
end

prefix = { 10 => "", 16 => "'h", 2 => "'b" }

bases.each do |base|
  puts "==> #{prefix[base]}#{input} <==".bold_color($GREEN)
  num2all(input.to_i(base))
end
