comparison src/unsleep.go @ 10:b0784443ed87

add netcat and unsleep.
author pyon@macmini
date Sat, 04 Nov 2017 11:07:50 +0900
parents
children c3a589f0521d
comparison
equal deleted inserted replaced
9:54a75ff1c288 10:b0784443ed87
1 package main
2
3 import(
4 "flag"
5 "fmt"
6 "time"
7 "os"
8 )
9
10 var sec = 0
11
12 func main() {
13
14 // arguments
15 t := flag.Int( "t", 1, "interval" )
16 s := flag.Bool( "s", false, "normal sleep" )
17 q := flag.Bool( "q", false, "quiet ( not print count and time )" )
18 c := flag.Int( "c", 0, "loop count ( without [s] )" )
19 m := flag.Bool( "m", false, "print every minute ( without [q] )" )
20 b := flag.Bool( "b", false, "beep" )
21
22 flag.Parse()
23
24 if *t > 0 {
25
26 if !*q {
27 s := fmt.Sprintf( "[%04d] %s ...", *c, time.Now().Format( "15:04:05" ) )
28 fmt.Println( s )
29 }
30
31 if *s {
32 for i := 0; i < *t; i++ {
33 sleep1ms( *q, *m, *b, i + 1 )
34 }
35 os.Exit( 0 )
36 }
37
38 if *c < 0 {
39 fmt.Fprintln( os.Stderr, "bad loop count." )
40 os.Exit( 1 )
41 }
42 if *c > 0 {
43 for i := 0; i < *c; i++ {
44 sleep1ms( *q, *m, *b, *c - i - 1 )
45 }
46 } else {
47 n := 1
48 for {
49 sleep1ms( *q, *m, *b, n )
50 n++
51 }
52 }
53
54 } else {
55 fmt.Fprintln( os.Stderr, "bad time." )
56 os.Exit( 1 )
57 }
58 }
59
60 func sleep1ms( quiet, minute, beep bool, n int ) {
61
62 s := time.Duration( 1.00 * 1000 ) * time.Millisecond
63 time.Sleep( s )
64 sec++
65
66 if minute && sec % 60 == 0 {
67 str := fmt.Sprintf( "[%04d] %s", n / 60, time.Now().Format( "15:04:05" ) )
68 fmt.Println( str )
69 }
70 if !quiet && !minute {
71 str := fmt.Sprintf( "[%04d] %s", n, time.Now().Format( "15:04:05" ) )
72 fmt.Println( str )
73 }
74
75 if beep {
76 //os.Stdout.Write( []byte( "\u0007" ) )
77 fmt.Print( "\a" )
78 }
79 }
80