Skip to content

Instantly share code, notes, and snippets.

@redperadot
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save redperadot/189be393a1d7568b403d to your computer and use it in GitHub Desktop.

Select an option

Save redperadot/189be393a1d7568b403d to your computer and use it in GitHub Desktop.

Revisions

  1. Cody Hannafon revised this gist Nov 19, 2014. 1 changed file with 18 additions and 21 deletions.
    39 changes: 18 additions & 21 deletions pong.rb
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,19 @@
    #!/usr/bin/env ruby
    #
    # pong.rb v1.2 - Monitor network addresses.
    # pong.rb v0.1.3 - Monitor network addresses.
    # Made with ❤︎ by [email protected]

    # Script Utilitys
    module Scripter extend self

    # Sets a clean exit with control-c. - Scripter.clean_exit("May the command line live forever.")
    def clean_exit(message = nil)
    Signal.trap("INT") { |signo| Scripter.tput('clear'); puts; puts message if message; exit 0 }
    end

    # Splits a version number into a hash. - Scripter.parse_version(6.2.4) => [major: 6, minor: 2, patch: 4,]
    def parse_version(ver)
    ver = ver.to_s.split('.')
    ver = { major: ver[0].to_i, minor: ver[1].to_i, patch: ver[2].to_i }
    end

    # Tries to require gems; if not found it installs them. - Scripter.acquire_gems('io/console', 'colorize', 'yaml',)
    def acquire_gems(*gems)
    gems.each do |_gem|
    begin require _gem
    @@ -64,7 +60,7 @@ def tput(set, unset = nil)
    def clear_screen
    rows = $stdin.winsize[0]
    cols = $stdin.winsize[1]
    chrs = rows * cols
    chrs = rows * cols - cols
    self.tput('home')
    print ' ' * chrs
    system("tput cup #{cols} 0")
    @@ -80,14 +76,16 @@ def center(line)

    class Pong

    attr_reader :ball, :addr
    attr_reader :ball, :addr, :name
    attr_writer :sleep, :tmout, :colr
    @@sleep = 2
    @@tmout = 3
    @@refsh = Time.now.to_i
    @@scren = lambda {{ rows: $stdin.winsize[0], cols: $stdin.winsize[1] }}
    @@colrs = [:default, :green, :red, :light_black, :light_red, :light_green, :light_yellow, :light_blue, :light_magenta, :light_cyan, :light_white]

    def initialize(host)
    @name = name
    @host = Net::Ping::External.new(host); @host.timeout = @@tmout
    @addr = @host.host.to_s
    @colr = String.colors.sample while @colr.nil? || @@colrs.include?(@colr); @@colrs.push(@colr)
    @@ -99,7 +97,7 @@ def initialize(host)
    next if @ball[:prev] == @ball[:aval]
    @ball[:chng] = Time.now
    @ball[:prev] = @ball[:aval]
    @ball[:tabl] << [ @ball[:chng].to_i, '::'.colorize(@colr) + "#{@ball[:chng].strftime('%y:%m:%d:%H:%M:%S')}" + ' • '.colorize(@colr) + "#{@addr} is #{( @ball[:aval] ? 'up'.green : 'down'.red )}." ]
    @ball[:tabl] << [ @ball[:chng].to_i, "#{@ball[:chng].strftime('%y:%m:%d:%H:%M:%S')}" + ' • '.colorize(@colr) + "#{@addr} is #{( @ball[:aval] ? 'up'.green : 'down'.red )}." ]
    Thread.current[:state] = @ball
    end
    }
    @@ -124,22 +122,22 @@ def self.refresh
    return true
    end

    def self.hosts # Returns an array of class instences.
    def self.hosts
    ObjectSpace.each_object(self).to_a
    end

    def self.net # Genorate an updated status bar.
    def self.net
    stat_bar = String.new
    hosts.each { |host| stat_bar << host.status }
    return stat_bar
    end

    def self.monitor
    loop do
    screen = { rows: $stdin.winsize[0], cols: $stdin.winsize[1] }
    @@scren = { rows: $stdin.winsize[0], cols: $stdin.winsize[1] }
    Scripter.clear_screen
    table(screen[:rows] - 2).each { |line| puts line }
    puts '⎓' * (screen[:cols] - 1)
    table(@@scren[:rows] - 2).each { |line| puts line }
    puts '⎓' * (@@scren[:cols] - 1)
    puts net
    sleep(@@sleep) until refresh
    end
    @@ -148,6 +146,9 @@ def self.monitor
    def self.splash
    Scripter.tput('civis', 'cvvis')

    relnam = %x(dscl . read /Users/`whoami` RealName).split[1..2]
    aspeed = 0.06
    runs = (@@tmout * (aspeed * 100)).round
    logo = [
    ' ::::::::: :::::::: :::: ::: ::::::::',
    ' :+: :+: :+: :+: :+:+: :+: :+: :+:',
    @@ -157,16 +158,12 @@ def self.splash
    ' #+# #+# #+# #+# #+#+# #+# #+# ',
    '### ######## ### #### ######## ',
    ]

    relnam = %x(dscl . read /Users/`whoami` RealName).split[1..2]
    mesage = [ "Hello #{relnam[0]}, let's start a game of ping pong!", "Loading #{hosts.size} paddles..." ]
    colors = [ :cyan, :magenta, :yellow ]
    mesage = [ "Hello #{relnam[0]}, let's start a game of ping pong!", "Loading #{hosts.size} hosts..." ]
    colors = [ :cyan, :magenta, :yellow ] # [ :gold[:],:black[],:cyan[] ] # String.colors.shuffle
    header = "\n" * (( $stdin.winsize[0] - logo.size ) / 2 - 2 )
    aspeed = 0.08
    runits = (colors.length * (aspeed * 100)).round

    Scripter.tput('clear')
    (runits).times do |i|
    (runs).times do |i|
    print header
    logo.each do |line|
    puts Scripter.center(line).colorize(colors[0])
    @@ -189,4 +186,4 @@ def self.splash

    ARGV.each { |host| Pong.new(host) }
    Pong.splash
    Pong.monitor
    Pong.monitor
  2. Cody Hannafon revised this gist Nov 15, 2014. 1 changed file with 92 additions and 11 deletions.
    103 changes: 92 additions & 11 deletions pong.rb
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,14 @@
    #!/usr/bin/env ruby
    #
    # pong.rb v1.0 - Monitor network addresses.
    # pong.rb v1.2 - Monitor network addresses.
    # Made with ❤︎ by [email protected]

    # Script Utilitys
    module Scripter extend self

    # Sets a clean exit with control-c. - Scripter.clean_exit("May the command line live forever.")
    def clean_exit(message = nil)
    Signal.trap("INT") { |signo| puts; puts message if message; exit 0 }
    Signal.trap("INT") { |signo| Scripter.tput('clear'); puts; puts message if message; exit 0 }
    end

    # Splits a version number into a hash. - Scripter.parse_version(6.2.4) => [major: 6, minor: 2, patch: 4,]
    @@ -30,17 +30,65 @@ def acquire_gems(*gems)
    end
    end

    def notify(level, message)
    case level
    when :ask # Ask user yes or no
    print message + ' [y/n]: '.green
    a = STDIN.getch.downcase.strip
    a == "y" || a.empty? ? a = true : a = false
    puts ( a ? 'yes' : 'no' )
    return a
    when :continue # Wait for interaction.
    print message + " [Press Enter to Continue]".magenta
    $stdin.noecho(&:gets)
    puts
    when :debug # Leave bread crumbs for debugging.
    return unless $config[:debug]
    puts "[Debug] ".blue + message
    when :info # Let the user know what's going on.
    puts "[Info] ".cyan + message
    when :warn # Give a warning about something.
    puts "[Warning] ".yellow.blink + message
    when :error # Exit 1 when we have an error.
    abort("[Error] ".red.blink + message)
    else
    puts "[#{level.capitalize}] ".white + message
    end
    end

    def tput(set, unset = nil)
    system 'tput', set
    at_exit { system 'tput', unset } unless unset.nil?
    end

    def clear_screen
    rows = $stdin.winsize[0]
    cols = $stdin.winsize[1]
    chrs = rows * cols
    self.tput('home')
    print ' ' * chrs
    system("tput cup #{cols} 0")
    end

    def center(line)
    cols = $stdin.winsize[1]
    marg = ' ' * ((cols - line.to_s.length) / 2 )
    return marg + line.to_s
    end

    end

    class Pong

    attr_reader :ball, :addr
    @@sleep = 5
    attr_writer :sleep, :tmout, :colr
    @@sleep = 2
    @@tmout = 3
    @@refsh = Time.now.to_i
    @@colrs = [:default, :green, :red, :light_black, :light_red, :light_green, :light_yellow, :light_blue, :light_magenta, :light_cyan, :light_white]

    def initialize(host)
    @host = Net::Ping::External.new(host)
    @host = Net::Ping::External.new(host); @host.timeout = @@tmout
    @addr = @host.host.to_s
    @colr = String.colors.sample while @colr.nil? || @@colrs.include?(@colr); @@colrs.push(@colr)
    @ball = { aval: false, prev: false, chng: Time.now, tabl: Array.new }
    @@ -59,7 +107,7 @@ def initialize(host)
    end

    def status
    '[' + @addr.colorize(@colr) + '|' + ( @ball[:aval] ? '●'.green : "●\a".red ) + ']'
    '[' + @addr.colorize(@colr) + '|' + ( @ball[:aval] ? '●'.green : "●".red ) + ']'
    end

    def self.table(lines)
    @@ -87,18 +135,51 @@ def self.net # Genorate an updated status bar.
    end

    def self.monitor
    system('tput civis')
    at_exit { system('tput cvvis') }
    loop do
    screen = { cler: "\033[2J", rows: $stdin.winsize[0], cols: $stdin.winsize[1] }
    print screen[:cler]
    screen = { rows: $stdin.winsize[0], cols: $stdin.winsize[1] }
    Scripter.clear_screen
    table(screen[:rows] - 2).each { |line| puts line }
    puts '⎓' * screen[:cols]
    puts '⎓' * (screen[:cols] - 1)
    puts net
    sleep(@@sleep) until refresh
    end
    end

    def self.splash
    Scripter.tput('civis', 'cvvis')

    logo = [
    ' ::::::::: :::::::: :::: ::: ::::::::',
    ' :+: :+: :+: :+: :+:+: :+: :+: :+:',
    ' +:+ +:+ +:+ +:+ :+:+:+ +:+ +:+ ',
    ' +#++:++#+ +#+ +:+ +#+ +:+ +#+ :#: ',
    ' +#+ +#+ +#+ +#+ +#+#+# +#+ +#+# ',
    ' #+# #+# #+# #+# #+#+# #+# #+# ',
    '### ######## ### #### ######## ',
    ]

    relnam = %x(dscl . read /Users/`whoami` RealName).split[1..2]
    mesage = [ "Hello #{relnam[0]}, let's start a game of ping pong!", "Loading #{hosts.size} paddles..." ]
    colors = [ :cyan, :magenta, :yellow ]
    header = "\n" * (( $stdin.winsize[0] - logo.size ) / 2 - 2 )
    aspeed = 0.08
    runits = (colors.length * (aspeed * 100)).round

    Scripter.tput('clear')
    (runits).times do |i|
    print header
    logo.each do |line|
    puts Scripter.center(line).colorize(colors[0])
    sleep(aspeed)
    end
    puts Scripter.center('╍' * (logo[0].length + 2))
    mesage.each { |line| puts Scripter.center(line) }
    colors.rotate!
    Scripter.tput('home')
    end

    end

    private_class_method :net, :table, :hosts, :refresh

    end
    @@ -107,5 +188,5 @@ def self.monitor
    Scripter.acquire_gems('optparse', 'io/console', 'colorize', 'net/ping', 'socket')

    ARGV.each { |host| Pong.new(host) }

    Pong.splash
    Pong.monitor
  3. Cody Hannafon created this gist Nov 14, 2014.
    111 changes: 111 additions & 0 deletions pong.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    #!/usr/bin/env ruby
    #
    # pong.rb v1.0 - Monitor network addresses.
    # Made with ❤︎ by [email protected]

    # Script Utilitys
    module Scripter extend self

    # Sets a clean exit with control-c. - Scripter.clean_exit("May the command line live forever.")
    def clean_exit(message = nil)
    Signal.trap("INT") { |signo| puts; puts message if message; exit 0 }
    end

    # Splits a version number into a hash. - Scripter.parse_version(6.2.4) => [major: 6, minor: 2, patch: 4,]
    def parse_version(ver)
    ver = ver.to_s.split('.')
    ver = { major: ver[0].to_i, minor: ver[1].to_i, patch: ver[2].to_i }
    end

    # Tries to require gems; if not found it installs them. - Scripter.acquire_gems('io/console', 'colorize', 'yaml',)
    def acquire_gems(*gems)
    gems.each do |_gem|
    begin require _gem
    rescue LoadError
    dep = _gem.split('/')[0]
    puts "[Info] Installing the dependency '#{dep}'."
    system("gem install #{dep} > /dev/null 2>&1")
    require _gem
    end
    end
    end

    end

    class Pong

    attr_reader :ball, :addr
    @@sleep = 5
    @@refsh = Time.now.to_i
    @@colrs = [:default, :green, :red, :light_black, :light_red, :light_green, :light_yellow, :light_blue, :light_magenta, :light_cyan, :light_white]

    def initialize(host)
    @host = Net::Ping::External.new(host)
    @addr = @host.host.to_s
    @colr = String.colors.sample while @colr.nil? || @@colrs.include?(@colr); @@colrs.push(@colr)
    @ball = { aval: false, prev: false, chng: Time.now, tabl: Array.new }
    @padl = Thread.new {
    loop do
    sleep(@@sleep)
    @ball[:aval] = @host.ping?
    next if @ball[:prev] == @ball[:aval]
    @ball[:chng] = Time.now
    @ball[:prev] = @ball[:aval]
    @ball[:tabl] << [ @ball[:chng].to_i, '::'.colorize(@colr) + "#{@ball[:chng].strftime('%y:%m:%d:%H:%M:%S')}" + ' • '.colorize(@colr) + "#{@addr} is #{( @ball[:aval] ? 'up'.green : 'down'.red )}." ]
    Thread.current[:state] = @ball
    end
    }

    end

    def status
    '[' + @addr.colorize(@colr) + '|' + ( @ball[:aval] ? '●'.green : "●\a".red ) + ']'
    end

    def self.table(lines)
    tables = Array.new
    hosts.each { |host| tables.concat(host.ball[:tabl]) }
    tables.sort.map { |item| item[1] }[0..lines]
    end

    def self.refresh
    changes = Array.new
    hosts.each { |host| changes << host.ball[:chng].to_i }
    return false if changes.sort.last < @@refsh
    @@refsh = Time.now.to_i
    return true
    end

    def self.hosts # Returns an array of class instences.
    ObjectSpace.each_object(self).to_a
    end

    def self.net # Genorate an updated status bar.
    stat_bar = String.new
    hosts.each { |host| stat_bar << host.status }
    return stat_bar
    end

    def self.monitor
    system('tput civis')
    at_exit { system('tput cvvis') }
    loop do
    screen = { cler: "\033[2J", rows: $stdin.winsize[0], cols: $stdin.winsize[1] }
    print screen[:cler]
    table(screen[:rows] - 2).each { |line| puts line }
    puts '⎓' * screen[:cols]
    puts net
    sleep(@@sleep) until refresh
    end
    end

    private_class_method :net, :table, :hosts, :refresh

    end

    Scripter.clean_exit(' - May the command line live forever. - '.center(`tput cols`.to_i))
    Scripter.acquire_gems('optparse', 'io/console', 'colorize', 'net/ping', 'socket')

    ARGV.each { |host| Pong.new(host) }

    Pong.monitor