Mercurial > mercurial > hgweb_golang.cgi
view src/unsleep.go @ 10:b0784443ed87
add netcat and unsleep.
author | pyon@macmini |
---|---|
date | Sat, 04 Nov 2017 11:07:50 +0900 |
parents | |
children | c3a589f0521d |
line wrap: on
line source
package main import( "flag" "fmt" "time" "os" ) var sec = 0 func main() { // arguments t := flag.Int( "t", 1, "interval" ) s := flag.Bool( "s", false, "normal sleep" ) q := flag.Bool( "q", false, "quiet ( not print count and time )" ) c := flag.Int( "c", 0, "loop count ( without [s] )" ) m := flag.Bool( "m", false, "print every minute ( without [q] )" ) b := flag.Bool( "b", false, "beep" ) flag.Parse() if *t > 0 { if !*q { s := fmt.Sprintf( "[%04d] %s ...", *c, time.Now().Format( "15:04:05" ) ) fmt.Println( s ) } if *s { for i := 0; i < *t; i++ { sleep1ms( *q, *m, *b, i + 1 ) } os.Exit( 0 ) } if *c < 0 { fmt.Fprintln( os.Stderr, "bad loop count." ) os.Exit( 1 ) } if *c > 0 { for i := 0; i < *c; i++ { sleep1ms( *q, *m, *b, *c - i - 1 ) } } else { n := 1 for { sleep1ms( *q, *m, *b, n ) n++ } } } else { fmt.Fprintln( os.Stderr, "bad time." ) os.Exit( 1 ) } } func sleep1ms( quiet, minute, beep bool, n int ) { s := time.Duration( 1.00 * 1000 ) * time.Millisecond time.Sleep( s ) sec++ if minute && sec % 60 == 0 { str := fmt.Sprintf( "[%04d] %s", n / 60, time.Now().Format( "15:04:05" ) ) fmt.Println( str ) } if !quiet && !minute { str := fmt.Sprintf( "[%04d] %s", n, time.Now().Format( "15:04:05" ) ) fmt.Println( str ) } if beep { //os.Stdout.Write( []byte( "\u0007" ) ) fmt.Print( "\a" ) } }