cli-spinner/LICENSE000644 001750 001750 00000002071 12556156356 014222 0ustar00fikefike000000 000000 The MIT License (MIT) Copyright (c) 2015 Emmanuel Odeke Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cli-spinner/.travis.yml000644 001750 001750 00000000025 12556156356 015323 0ustar00fikefike000000 000000 language: go go: 1.2 cli-spinner/spinner.go000644 001750 001750 00000003426 12556156356 015227 0ustar00fikefike000000 000000 package spinner import ( "fmt" "os" "time" ) var symbolList = []string{ " | ", " / ", " – ", " \\ ", } var symbolMap = map[string]string{ " ⏳ ": "\033[37m", " ⌛ ": "\033[38m", } type Spinner struct { duration time.Duration trigger interface{} sentinel interface{} closed bool // sigChan: is the pipe that receives the start and stop sigChan chan interface{} // waitChan waits till the shutdown has fully propagated waitChan chan interface{} } func New(freq int64) *Spinner { if freq < 1 { freq = 10 } sp := Spinner{ duration: time.Duration(1e9 / freq), sentinel: nil, // sigChan will be created on .Start() sigChan: nil, waitChan: make(chan interface{}), } sp.trigger = &sp return &sp } func (s *Spinner) Start() error { err := s.spin() if err == nil { s.sigChan <- s.trigger } return err } func (s *Spinner) Stop() { if !s.closed && s.sigChan != nil { s.sigChan <- s.sentinel close(s.sigChan) <-s.waitChan s.closed = true } } func (s *Spinner) Reset() { s.Stop() s.sigChan = nil s.closed = true } func (s *Spinner) Duration() time.Duration { return s.duration } func (s *Spinner) spin() error { if s.sigChan != nil { // Already in use return fmt.Errorf("already in use") } s.sigChan = make(chan interface{}) go func() { // Block till the first symbol comes through <-s.sigChan throttle := time.Tick(s.duration) running := true for running { for _, segment := range symbolList { select { case in := <-s.sigChan: if in == s.sentinel { os.Stderr.Sync() running = false s.waitChan <- s.sentinel break } default: } // Print it to stderr to avoid symbol getting into piped content fmt.Fprintf(os.Stderr, "%s\r", segment) <-throttle } } }() return nil } cli-spinner/000755 001750 001750 00000000000 12556156356 013215 5ustar00fikefike000000 000000 cli-spinner/cli-spinner/000755 001750 001750 00000000000 12556156356 015440 5ustar00fikefike000000 000000 cli-spinner/cli-spinner/main.go000644 001750 001750 00000000533 12556156356 016714 0ustar00fikefike000000 000000 package main import ( "os" "os/signal" "time" "github.com/odeke-em/cli-spinner" ) func main() { spin := spinner.New(10) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt, os.Kill) go func() { _ = <-sigChan spin.Stop() os.Exit(-1) }() spin.Start() throttle := time.Tick(1e9 / 1) for { <-throttle } } cli-spinner/README.md000644 001750 001750 00000000416 12556156356 014475 0ustar00fikefike000000 000000 ## cli-spinner [![Build Status](https://travis-ci.org/odeke-em/cli-spinner.svg)](https://travis-ci.org/odeke-em/cli-spinner) Spin until terminated To see the quick demo via install ==== ```shell $ go get github.com/odeke-em/cli-spinner/cli-spinner $ cli-spinner ```