Coder Social home page Coder Social logo

ansi's Introduction

ansi

ANSI utilities

Install

To get started, you need a working Go environment. Once available, grab the package here:

$ go get github.com/vutran/ansi

Usage

Cursor

To be documented.

Display

To be documented.

Style

Simple functions to apply a style to a string of text.

package main

import (
	"github.com/vutran/ansi/styles"
)

func main() {
	msg: = styles.Bold("Hello, world")
	fmt.Print(msg)
}

Colors

Simple functions to apply a foreground or background color to a string of text.

package main

import (
	"github.com/vutran/ansi/colors"
)

func main() {
	msg := colors.Blue("Hello, world")
	fmt.Print(msg)
}

Loaders

Display an animated progress loader.

loader

package main

import (
	"github.com/vutran/ansi"
	"github.com/vutran/ansi/loaders"
	"time"
)

func main() {
	s := ansi.Loader(loaders.Dots, 100)
	s.SetValue("Loading")
	s.Start()
	time.Sleep(2 * time.Second)
	s.SetValue("Finalizing...")
	time.Sleep(2 * time.Second)
	s.Stop()
}

Available Loaders

  • Circle
  • Clock
  • Dots
  • Lines
  • Moon
  • Triangle

Mix and Match

You can mix and decorate your text however you prefer.

package main

import (
	"github.com/vutran/ansi/colors"
	"github.com/vutran/ansi/styles"
)

func main() {
	// bold, blue text
	msg := styles.Bold(colors.Blue("Hello, world"))
	fmt.Print(msg)
}

License

MIT © Vu Tran

ansi's People

Contributors

vutran avatar xyclos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

xyclos

ansi's Issues

race condition on loader.value access?

There appears to be a race condition here where loader.value can potentially be accessed by the Start goroutine and by another goroutine calling SetValue

diff --git a/loader.go b/loader.go
index 7a35f74..95fdb5f 100644
--- a/loader.go
+++ b/loader.go
@@ -2,6 +2,7 @@ package ansi
 
 import (
        "fmt"
+       "sync"
        "time"
 )
 
@@ -19,6 +20,7 @@ type LoaderControl struct {
        max    int          // The length of the animation list
        ticker *time.Ticker // Pointer to the ticker instance
        done   chan bool    // Channel used to stop the loader
+       mu     *sync.Mutex  // lock for access to loader.value
 }
 
 // Start will start the loader
@@ -29,6 +31,10 @@ func (loader *LoaderControl) Start() {
                        case <-loader.done:
                                return
                        case <-loader.ticker.C:
+                               loader.mu.Lock()
+                               value := loader.value
+                               loader.mu.Unlock()
+
                                fmt.Print(HideCursor())
                                fmt.Print(EraseLine(2))
                                fmt.Print(CursorStart(1))
@@ -37,7 +43,7 @@ func (loader *LoaderControl) Start() {
                                        loader.cursor = 0
                                }
 
-                               fmt.Print(loader.anim[loader.cursor] + " " + loader.value)
+                               fmt.Print(loader.anim[loader.cursor] + " " + value)
 
                                loader.cursor++
                        }
@@ -53,6 +59,9 @@ func (loader *LoaderControl) Stop() {
 
 // SetValue will set a new value for the label
 func (loader *LoaderControl) SetValue(value string) {
+       loader.mu.Lock()
+       defer loader.mu.Unlock()
+
        loader.value = value
 }
 
@@ -65,5 +74,6 @@ func Loader(anim []string, speed int) *LoaderControl {
                max:    len(anim),
                ticker: time.NewTicker(time.Millisecond * time.Duration(speed)),
                done:   make(chan bool),
+               mu:     &sync.Mutex{},
        }
 }

Wrong CSI Code for EraseDisplay

according the Wikipedia,you may have a mistake with

// EraseDisplay clears the screen
func EraseDisplay(code int) string {
	return Esc + "[" + strconv.Itoa(code) + "2J" //Should be single "J"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.