comparison src/unsleep.go @ 11:c3a589f0521d

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