This commit is contained in:
Aşkın Gedik 2015-12-30 13:55:14 +02:00
parent a27b0e1e63
commit a7b2e9a6f3
8 changed files with 129 additions and 10 deletions

View file

@ -1,10 +1,11 @@
# spinner
# Spinner
TODO: Write a description here
Terminal Spinner for Crystal Programming Language
![All](/images/all.gif)
## Installation
Add this to your application's `shard.yml`:
```yaml
@ -13,20 +14,23 @@ dependencies:
github: askn/spinner
```
## Usage
```crystal
require "spinner"
sp = Spin.new
sp.start
sleep 3
sp.stop
```
## Options
TODO: Write usage instructions here
`delay`: `default = 0.1`
## Development
TODO: Write development instructions here
`chars`: you can choose charset from [CHARSET](src/spinner/charset.cr) `default = CHARSET[:pipe]`
## Contributing

8
examples/all.cr Normal file
View file

@ -0,0 +1,8 @@
require "../src/spinner"
CHARSET.values.each do |chars|
sp = Spin.new(0.1, chars)
sp.start
sleep 2
sp.stop
end

11
examples/color.cr Normal file
View file

@ -0,0 +1,11 @@
require "../src/spinner"
require "colorize"
chars = CHARSET[:arrow].map do |c|
c.colorize(:light_green)
end
spin = Spin.new(0.2, chars)
spin.start
sleep 3
spin.stop

12
examples/pavyon.cr Normal file
View file

@ -0,0 +1,12 @@
require "../src/spinner"
require "colorize"
colors = {:red, :light_red, :yellow, :green, :light_blue, :blue, :magenta}
chars = colors.map do |color|
".xXx.".colorize(color)
end
spin = Spin.new(0.01, chars)
spin.start
sleep 3
spin.stop

19
examples/simple.cr Normal file
View file

@ -0,0 +1,19 @@
require "../src/spinner"
puts "starting..."
sp = Spin.new(0.1)
sp.start
sleep 3
sp.stop
puts "end!"
sp1 = Spin.new(0.1, CHARSET[:three_dots])
sp1.start
sleep 3
sp1.stop
sp1 = Spin.new(0.1)
sp1.chars = CHARSET[:arrow]
sp1.start
sleep 3
sp1.stop

BIN
images/all.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

View file

@ -1,5 +1,38 @@
require "./spinner/*"
module Spinner
# TODO Put your code here
class Spin
property delay, chars
CL = STDOUT.tty? ? "\u001b[0G" : "\u000d \u000d"
CLEAR = STDOUT.tty? ? "\u001b[2K" : "\u000d"
def initialize(@delay = 0.1, @chars = CHARSET[:pipe])
@state = true
end
def stop
@state = false
print CLEAR
end
private def clear(count)
print CL * count
end
def start
spawn do
i = 0
while @state
chars = @chars[i % @chars.size]
print "#{chars}"
# to_s for colorize
clear(chars.to_s.size)
sleep @delay
i += 1
end
end
end
end

32
src/spinner/charset.cr Normal file
View file

@ -0,0 +1,32 @@
CHARSET = {
arrow: {"", "", "", "", "", "", "", ""},
fan: {"", "", "", "", "", "", "", ""},
fan1: {"+", "x"},
fan2: {"v", "<", "^", ">"},
triangle: {"", "", "", ""},
square: {"", "", "", ""},
circle: {"", "", "", ""},
big_circle: {"", "", "", ""},
boom: {".", "o", "O", "@", "*"},
pipe: {"|", "/", "-", "\\"},
alphabet: {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"},
three_dots: {". ", ".. ", "..."},
eyes: {"◡◡", "⊙⊙", "◠◠"},
dots: {"", "", "", "", "", "", "", ""},
snake: {"", "", "", "", "", "", "", ""},
snake1: {"", "", "", "", "", "", "", "", "", ""},
snake2: {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
square1: {"", "", "", ""},
square2: {"", "", "", ""},
square3: {"", "", "", "", "", "", "", "", "", "", "", "", ""},
square4: {"", "", "", "", "", "", "", "", "", "", "", "", ""},
square5: {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
thin_arrow: {"", "", "", ""},
big_arraw: {"", "", "", "", "", "", "", ""},
baloon: {".", "o", "O", "°", "O", "o", "."},
progress: {"[ ]", "[= ]", "[== ]", "[=== ]", "[==== ]", "[===== ]", "[====== ]", "[======= ]", "[======== ]", "[========= ]", "[==========]"},
progress1: {"(*---------)", "(-*--------)", "(--*-------)", "(---*------)", "(----*-----)", "(-----*----)", "(------*---)", "(-------*--)", "(--------*-)", "(---------*)"},
progress2: {"█▒▒▒▒▒▒▒▒▒", "███▒▒▒▒▒▒▒", "█████▒▒▒▒▒", "███████▒▒▒", "██████████"},
progress3: {"[ ]", "[=> ]", "[===> ]", "[=====> ]", "[======> ]", "[========> ]", "[==========> ]", "[============> ]", "[==============> ]", "[================> ]", "[==================> ]", "[===================>]"},
fish: {">))'> ", " >))'> ", " >))'> ", " >))'> ", " >))'>", " <'((<", " <'((< ", " <'((< ", " <'((< ", "<'((< "},
}