Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions spark-ping
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,29 @@ end

def spark(durations)
return "" unless durations.size > 1
maxd = 0
mind = 0

ticks=%w[▁ ▂ ▃ ▄ ▅ ▆ ▇]

range = durations.max - durations.min
scale = ticks.length - 1
distance = durations.max.to_f / ticks.size

if durations.compact.max
maxd = durations.compact.max
end

if durations.compact.min
mind = durations.compact.min
end

range = maxd - mind
distance = maxd / (ticks.size-1)
str = ''
durations.each do |val|
tick = (val / distance).round - 1
str << ticks[tick]
if val
tick = (val / distance).round(0)
str << ticks.at(tick)
else
str << "X"
end
end
str
end
Expand All @@ -60,9 +72,28 @@ if __FILE__ == $0

(columns,lines) = detect_terminal_size
durations = []

pingtime = 500 # milliseconds
if RUBY_PLATFORM =~ /linux/
# Linux platform is different...
pingtime = 1 # Seconds(WTF!?)
end

minimal_ping = <<-EOM
# ping wrapper shell script from http://superuser.com/a/866045/177165
while :; do
ping -W #{pingtime} -c 1 #{ARGV.join(' ')} | grep 'bytes from '
case $? in
0 ) sleep 1 ;;
1 ) echo -e "request timeout" ;;
* ) exit ;;
esac
done
EOM

begin
PTY.spawn( "ping #{ARGV.join(' ')}" ) do |stdin, stdout, pid|
#PTY.spawn( "ping #{ARGV.join(' ')}" ) do |stdin, stdout, pid|
PTY.spawn( "#{minimal_ping}" ) do |stdin, stdout, pid|
begin
stdin.each do |line|
## if we see 'usage:' or 'ping: ..' an error probably occurred
Expand All @@ -73,11 +104,17 @@ if __FILE__ == $0
exit 1
end

duration = line.scan( /time=(\S+)/ ).last
durations << duration.first.to_f unless duration.nil?

duration = line.scan( /time=(.*)\ ms/ ).last
if duration
durations << duration.first.to_f
else
durations << nil
end
sparkline = spark( durations )
output = "#{sparkline} #{durations.last} ms\r"
output = "#{sparkline} TIMEOUT\r"
if durations.last
output = "#{sparkline} #{durations.last} ms\r"
end
print output

# if we reach the end of the line, reset and start on next line.
Expand All @@ -97,4 +134,4 @@ if __FILE__ == $0
end

puts
end
end