Eclectic Media Git pingstats-go / master
Added some comments to help clarify the program Also adds attribution and places the file under the MIT license Ariana Giroux 1 year, 3 months ago
1 changed file(s) with 16 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
0 // Copyright, Ariana Giroux <ariana.giroux@gmail.com>, circa June 2019 under the MIT license
1 //
2 // Clones basic functionality of git://eclecticmedia.hopto.org/pingstats
3 //
4 // Provides simple method of plotting one's ping to google.ca on the command line.
5 //
6 // Breaks on any OS other than POSIX standard Ubuntu/Debian distributions
07 package main
18
2 // import "errors"
39 import "fmt"
410 import "github.com/guptarohit/asciigraph"
511 import terminal "github.com/wayneashleyberry/terminal-dimensions"
1016
1117 // define a pingstats clone in go
1218 func main() {
13 var returnTimes []float64
19 var returnTimes []float64 // make collection of return times
1420
15 for i := 0; i < 500; i++ {
16 width, height := termsize()
21 for i := 0; i < 500; i++ { // loop for a while, would be infinite in real program
22 width, height := termsize() // obtain termsize, account for changing size
1723 returnTimes = append(returnTimes, get_return_time("google.ca"))
1824
1925 graph := asciigraph.Plot(returnTimes,
2228
2329 fmt.Println(graph)
2430 time.Sleep(100 * time.Millisecond)
25 clear()
31 clear() // fails on windows
2632 }
2733 }
2834
3642 panic(err)
3743 }
3844
45 // note, line searching is ugly, could be easier implemented with regex
46 // note, line searching will break on non debian/ubuntu distributions
3947 for _, line := range strings.Split(string(pingOut), "\n") { // split output by line
4048 if strings.Contains(string(line), "time=") { // line has time value on it
49 // line similar to: 64 bytes from yyz12s06-in-f3.1e100.net (172.217.165.3): icmp_seq=1 ttl=56 time=62.8 ms
4150 timeIndex := strings.Index(line, "time") // get index of time data
4251 subIndex := strings.Index(string(line[timeIndex:]), "=") + 1 // Get index of equals sign, plus one
4352 endIndex := strings.Index(string(line[timeIndex:]), " ") // get index of trailing characters
4453
4554 rTime, err := strconv.ParseFloat(
46 string(line)[timeIndex:][subIndex:endIndex], // return relevant data as sub string
55 string(line)[timeIndex:][subIndex:endIndex], // return relevant data as float
4756 64,
4857 )
4958
7584 }
7685
7786 func clear() { // clear terminal
78 exec.Command("clear")
87 exec.Command("clear") // not available on windows
7988 }