view src/unsleep.go @ 61:49656dc40069

add qtuti.
author pyon@macmini
date Fri, 11 Sep 2020 20:06:27 +0900
parents c3a589f0521d
children
line wrap: on
line source

package main

import(
    "flag"
    "fmt"
    "math"
    "time"
    "os"
)

var version = 0.1

func main() {

    // arguments
    q := flag.Bool( "q", false, "quiet ( don't print count and time ) [ default:false ]" )
    l := flag.Int( "l", 0, "loop count ( l seconds / l minute ) [ default:0 ( no-limit ) ]" )
    m := flag.Bool( "m", false, "use minute ( not second ) [ default:false ( use second )]" )
    b := flag.Bool( "b", false, "sound beep [ default:false ]" )
    v := flag.Bool( "v", false, "print version")

    flag.Parse()

    if *v {
        fmt.Fprintf( os.Stderr, "unsleep : version %v\n", version )
        os.Exit( 0 )
    }

    if *l < 0 {
        fmt.Fprintln( os.Stderr, "bad loop count." )
        os.Exit( 1 )
    }

    if !*q {
        s := fmt.Sprintf( "[%04d] %s ...", *l, time.Now().Format( "15:04:05" ) )
        fmt.Println( s )
    }
    mysleep( *q, *m, *b, *l )
}

func mysleep( quiet, minute, beep bool, limit int ) {

    c := time.Tick( 1 * time.Second )
    if minute {
        c = time.Tick( 1 * time.Minute )
    }

    n := 1
    for range c {
        if !quiet {
            str := fmt.Sprintf( "[%04d] %s", int( math.Abs( float64( limit - n ) ) ), time.Now().Format( "15:04:05" ) )
            fmt.Println( str )
        }
        if beep {
            alarm( 1 )
        }
        n++
        if limit < n && limit != 0 {
            break
        }
    }
    if beep {
        alarm( 3 )
    }
}

func alarm( n int ) {
    for i:= 0; i < n; i++ {
        //os.Stdout.Write( []byte( "\u0007" ) )
        fmt.Print( "\a" )
    }
}