Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- #
- # SSX - SSH over colorful terminal.
- # Uses consisten colors over the same host names to for better navigation
- # over multiple hosts/windows.
- #
- # modified to change a running roxterm instance via DBus
- require 'zlib'
- # Defaults
- DEFAULT_FONT = "9x15"
- DEFAULT_GEOMETRY = "120x25"
- # Base colors from which color combinations are created.
- FG_BASE = %w[
- White
- Yellow
- ]
- BG_BASE = %w[
- #000000
- #0f0f0f
- #1e1e1e
- #2d2d2d
- #3c3c3c
- #4b4b4b
- #1e0000
- #2d0000
- #3c0000
- #4b0000
- #001e00
- #002d00
- #003c00
- #004b00
- #00001e
- #00002d
- #00003c
- #00004b
- #001e1e
- #002d2d
- #003c3c
- #004b4b
- #1e1e00
- #2d2d00
- #3c3c00
- #4b4b00
- #1e001e
- #2d002d
- #3c003c
- #4b004b
- ]
- # This method maps hostname to pair of colors
- # Given a hostname (string), and two arrays containing color names
- # it returns an array of two, with the first element be fg color
- # and the second element the background color
- def get_colors_by_hostname hostname, fg_array, bg_array
- n = [fg_array.size, bg_array.size].min
- idx = Zlib.crc32(hostname) % n
- [fg_array[idx], bg_array[idx]]
- end
- # Extract hostname from SSH args. Search for the first argument not without
- # a '-' prefix, while skipping those options that have such argument before
- # the hostname: i.e. in -l username, discard 'username'.
- # Expects: input parameter args to be an array of strings
- # Returns: hostname or nil if didn't find one
- def extract_hostname_from_args args
- opts_with_arg = %w[b c D e F I i L l m O o p R S W w]
- a = args.dup
- while a.size > 0
- opt = a[0]
- return opt.gsub(/^\w+@/,"") unless opt.chars.first == "-"
- opt_char = opt.chars.to_a[1]
- a.shift
- if opts_with_arg.include? opt_char
- a.shift if a.size > 0
- end
- end
- nil
- end
- # Generate all possible combinations between fg_base and bg_base colors.
- # Returns two arrays of the same size, first contains fg colors, second
- # contains bg colors.
- def generate_color_combinations fg_base, bg_base
- combinations = fg_base.product(bg_base)
- fg_colors = []
- bg_colors = []
- combinations.each do |c|
- fg_colors.push c[0]
- bg_colors.push c[1]
- end
- [fg_colors, bg_colors]
- end
- hostname = extract_hostname_from_args ARGV
- fg_array, bg_array = generate_color_combinations FG_BASE, BG_BASE
- fg_color, bg_color = get_colors_by_hostname hostname, fg_array, bg_array
- args_str = ARGV.join ' '
- ssh_cmd = "ssh #{args_str}"
- font = DEFAULT_FONT
- geometry = DEFAULT_GEOMETRY
- roxtermid = ENV["ROXTERM_ID"]
- rox_change_profile = "dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.SetColourScheme \
- string:#{roxtermid} string:#{bg_color}"
- rox_change_color = "dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.StringOption \
- string:Colours/#{bg_color} string:background 'string:#{bg_color}' "
- system rox_change_profile
- system rox_change_color
- system ssh_cmd
- rox_reset = "dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.SetColourScheme \
- string:#{roxtermid} string:Default"
- system rox_reset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement