Advertisement
khambrecht

Untitled

Jul 14th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.58 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #
  3. # SSX - SSH over colorful terminal.
  4. #       Uses consisten colors over the same host names to for better navigation
  5. #       over multiple hosts/windows.
  6. #
  7. # modified to change a running roxterm instance via DBus
  8.  
  9. require 'zlib'
  10.  
  11. # Defaults
  12. DEFAULT_FONT     = "9x15"
  13. DEFAULT_GEOMETRY = "120x25"
  14.  
  15. # Base colors from which color combinations are created.
  16. FG_BASE = %w[
  17.               White
  18.               Yellow
  19.             ]
  20.  
  21. BG_BASE = %w[
  22.                 #000000
  23.                 #0f0f0f
  24.                 #1e1e1e
  25.                 #2d2d2d
  26.                 #3c3c3c
  27.                 #4b4b4b
  28.  
  29.                 #1e0000
  30.                 #2d0000
  31.                 #3c0000
  32.                 #4b0000
  33.                
  34.                 #001e00
  35.                 #002d00
  36.                 #003c00
  37.                 #004b00
  38.  
  39.                 #00001e
  40.                 #00002d
  41.                 #00003c
  42.                 #00004b
  43.  
  44.                 #001e1e
  45.                 #002d2d
  46.                 #003c3c
  47.                 #004b4b
  48.  
  49.                 #1e1e00
  50.                 #2d2d00
  51.                 #3c3c00
  52.                 #4b4b00
  53.  
  54.                 #1e001e
  55.                 #2d002d
  56.                 #3c003c
  57.                 #4b004b
  58.  
  59.             ]
  60.  
  61. # This method maps hostname to pair of colors
  62. # Given a hostname (string), and two arrays containing color names
  63. # it returns an array of two, with the first element be fg color
  64. # and the second element the background color
  65. def get_colors_by_hostname hostname, fg_array, bg_array
  66.   n = [fg_array.size, bg_array.size].min
  67.   idx = Zlib.crc32(hostname) % n
  68.   [fg_array[idx], bg_array[idx]]
  69. end
  70.  
  71.  
  72. # Extract hostname from SSH args. Search for the first argument not without
  73. # a '-' prefix, while skipping those options that have such argument before
  74. # the hostname: i.e. in -l username, discard 'username'.
  75. # Expects: input parameter args to be an array of strings
  76. # Returns: hostname or nil if didn't find one
  77. def extract_hostname_from_args args
  78.   opts_with_arg = %w[b c D e F I i L l m O o p R S W w]
  79.   a = args.dup
  80.   while a.size > 0
  81.     opt = a[0]
  82.     return opt.gsub(/^\w+@/,"") unless opt.chars.first == "-"
  83.     opt_char = opt.chars.to_a[1]
  84.     a.shift
  85.     if opts_with_arg.include? opt_char
  86.       a.shift if a.size > 0
  87.     end
  88.   end
  89.   nil
  90. end
  91.  
  92.  
  93.  
  94. # Generate all possible combinations between fg_base and bg_base colors.
  95. # Returns two arrays of the same size, first contains fg colors, second
  96. # contains bg colors.
  97. def generate_color_combinations fg_base, bg_base
  98.   combinations = fg_base.product(bg_base)
  99.   fg_colors = []
  100.   bg_colors = []
  101.   combinations.each do |c|
  102.     fg_colors.push c[0]
  103.     bg_colors.push c[1]
  104.   end
  105.   [fg_colors, bg_colors]
  106. end
  107.  
  108.  
  109.  
  110. hostname = extract_hostname_from_args ARGV
  111. fg_array, bg_array = generate_color_combinations FG_BASE, BG_BASE
  112. fg_color, bg_color = get_colors_by_hostname hostname, fg_array, bg_array
  113. args_str = ARGV.join ' '
  114. ssh_cmd  = "ssh #{args_str}"
  115. font     = DEFAULT_FONT
  116. geometry = DEFAULT_GEOMETRY
  117.  
  118. roxtermid = ENV["ROXTERM_ID"]
  119.  
  120. rox_change_profile = "dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.SetColourScheme \
  121.             string:#{roxtermid} string:#{bg_color}"
  122. rox_change_color   = "dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.StringOption \
  123.             string:Colours/#{bg_color} string:background 'string:#{bg_color}' "
  124.  
  125. system rox_change_profile
  126. system rox_change_color
  127.  
  128. system ssh_cmd
  129.  
  130. rox_reset = "dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.SetColourScheme \
  131.             string:#{roxtermid} string:Default"
  132.  
  133. system rox_reset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement