diff --git a/README.md b/README.md index 9de936f..b2c4c68 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/all.cr b/examples/all.cr new file mode 100644 index 0000000..e843557 --- /dev/null +++ b/examples/all.cr @@ -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 diff --git a/examples/color.cr b/examples/color.cr new file mode 100644 index 0000000..76583bd --- /dev/null +++ b/examples/color.cr @@ -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 diff --git a/examples/pavyon.cr b/examples/pavyon.cr new file mode 100644 index 0000000..e303f05 --- /dev/null +++ b/examples/pavyon.cr @@ -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 diff --git a/examples/simple.cr b/examples/simple.cr new file mode 100644 index 0000000..1a43138 --- /dev/null +++ b/examples/simple.cr @@ -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 diff --git a/images/all.gif b/images/all.gif new file mode 100644 index 0000000..1f998c9 Binary files /dev/null and b/images/all.gif differ diff --git a/src/spinner.cr b/src/spinner.cr index 4b0c6f3..d2811e3 100644 --- a/src/spinner.cr +++ b/src/spinner.cr @@ -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 diff --git a/src/spinner/charset.cr b/src/spinner/charset.cr new file mode 100644 index 0000000..5bce012 --- /dev/null +++ b/src/spinner/charset.cr @@ -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: {">))'> ", " >))'> ", " >))'> ", " >))'> ", " >))'>", " <'((<", " <'((< ", " <'((< ", " <'((< ", "<'((< "}, +}