Git Product home page Git Product logo

clispinner's Introduction

CLISpinner

Travis

60+ spinners for use in the terminal

Shamelessly ripped off from sindresorhus/cli-spinners.

Install

.package(url: "https://github.com/kiliankoe/CLISpinner", from: "see latest release")

Usage

Just want to display a simple spinner for two seconds?

let s = Spinner(pattern: .dots)
s.start()
sleep(2)
s.stop()

Want some changing text and patterns?

let s = Spinner(pattern: .dots, text: "Foobar...", color: .lightCyan)
s.start()
sleep(2)
s.succeed(text: "Barfoo")
// will change the displayed text to 'โœ” Barfoo'

Made your own custom pattern?

let pattern = try Pattern.load(from: "/path/to/your/pattern.json")
let s = spinner(pattern: pattern)
s.start()
sleep(2)
s.stop()

Want all the patterns from sindresorhus/cli-spinners?

let patterns = try Patterns(from: "/path/to/spinners.json")
let s = spinner(pattern: patterns["christmas"]!)
s.start()
sleep(2)
s.stop()

That's basically it ๐Ÿ‘Œ

Creating your own Pattern

The Pattern type can read in patterns from a JSON file using the following format:

{
    "frames": [
        "1",
        "2",
        "3",
        "4",
        "5"
    ],
    "speed": 0.08
}

To keep multiple patterns in a single file:

{
    "pattern-name1": {
        "frames": [
            "<(**<)",
            "<(**)>",
            "(>**)>"
        ],
        "speed": 0.01
    },
    "pattern-name2": {
        "frames": [
            "1",
            "2",
            "3",
            "2"
        ],
        "speed": 0.12
    }
}

Caveat

To look nice the spinner hides the user's cursor as long as it's running and displays it again when stopped. The issue with this is that the cursor will still be hidden if the user interrupts the process (by sending a SIGINT through ctrl+c for example). The best way to handle this is by setting up a signal handler in your code and calling spinner.unhideCursor() on exiting. This library purposefully does not do that for you so as not to interfere with any possible signal handlers you might already have set up.

See IBM-Swift/BlueSignals for a clean and safe way of handling signals. The appropriate signal handler for your project could look something like this.

import Signals

let spinner = Spinner(pattern: .dots)
// ...

Signals.trap(signal: .int) { _ in
    spinner.unhideCursor()
    exit(0)
}

Used by

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.