1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
|
// Package yacspin provides Yet Another CLi Spinner for Go, taking inspiration
// (and some utility code) from the https://github.com/briandowns/spinner
// project. Specifically this project borrows the default character sets, and
// color mappings to github.com/fatih/color colors, from that project.
//
// This spinner should support all major operating systems, and is tested
// against Linux, MacOS, and Windows.
//
// This spinner also supports an alternate mode of operation when the TERM
// environment variable is set to "dumb". This is discovered automatically when
// constructing the spinner.
//
// Within the yacspin package there are some default spinners stored in the
// yacspin.CharSets variable, and you can also provide your own. There is also a
// list of known colors in the yacspin.ValidColors variable, if you'd like to
// see what's supported. If you've used github.com/fatih/color before, they
// should look familiar.
//
// cfg := yacspin.Config{
// Frequency: 100 * time.Millisecond,
// CharSet: yacspin.CharSets[59],
// Suffix: " backing up database to S3",
// Message: "exporting data",
// StopCharacter: "✓",
// StopColors: []string{"fgGreen"},
// }
//
// spinner, err := yacspin.New(cfg)
// // handle the error
//
// spinner.Start()
//
// // doing some work
// time.Sleep(2 * time.Second)
//
// spinner.Message("uploading data")
//
// // upload...
// time.Sleep(2 * time.Second)
//
// spinner.Stop()
//
// Check out the Config struct to see all of the possible configuration options
// supported by the Spinner.
package yacspin
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"os"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/mattn/go-runewidth"
)
type character struct {
Value string
Size int
}
func setToCharSlice(ss []string) ([]character, int) {
if len(ss) == 0 {
return nil, 0
}
var maxWidth int
c := make([]character, len(ss))
for i, s := range ss {
n := runewidth.StringWidth(s)
if n > maxWidth {
maxWidth = n
}
c[i] = character{
Value: s,
Size: n,
}
}
return c, maxWidth
}
// TerminalMode is a type to represent the bit flag controlling the terminal
// mode of the spinner, accepted as a field on the Config struct. See the
// comments on the exported constants for more info.
type TerminalMode uint32
const (
// AutomaticMode configures the constructor function to try and determine if
// the application using yacspin is being executed within a interactive
// (teletype [TTY]) session.
AutomaticMode TerminalMode = 1 << iota
// ForceTTYMode configures the spinner to operate as if it's running within
// a TTY session.
ForceTTYMode
// ForceNoTTYMode configures the spinner to operate as if it's not running
// within a TTY session. This mode causes the spinner to only animate when
// data is being updated. Each animation is rendered on a new line. You can
// trigger an animation by calling the Message() method, including with the
// last value it was called with.
ForceNoTTYMode
// ForceDumbTerminalMode configures the spinner to operate as if it's
// running within a dumb terminal. This means the spinner will not use ANSI
// escape sequences to print colors or to erase each line. Line erasure to
// animate the spinner is accomplished by overwriting the line with space
// characters.
ForceDumbTerminalMode
// ForceSmartTerminalMode configures the spinner to operate as if it's
// running within a terminal that supports ANSI escape sequences (VT100).
// This includes printing of stylized text, and more better line erasure to
// animate the spinner.
ForceSmartTerminalMode
)
func termModeAuto(t TerminalMode) bool { return t&AutomaticMode > 0 }
func termModeForceTTY(t TerminalMode) bool { return t&ForceTTYMode > 0 }
func termModeForceNoTTY(t TerminalMode) bool { return t&ForceNoTTYMode > 0 }
func termModeForceDumb(t TerminalMode) bool { return t&ForceDumbTerminalMode > 0 }
func termModeForceSmart(t TerminalMode) bool { return t&ForceSmartTerminalMode > 0 }
// Config is the configuration structure for the Spinner type, which you provide
// to the New() function. Some of the fields can be updated after the *Spinner
// is constructed, others can only be set when calling the constructor. Please
// read the comments for those details.
type Config struct {
// Frequency specifies how often to animate the spinner. Optimal value
// depends on the character set you use.
Frequency time.Duration
// Writer is the place where we are outputting the spinner, and can't be
// changed after the *Spinner has been constructed. If omitted (nil), this
// defaults to os.Stdout.
Writer io.Writer
// ShowCursor specifies that the cursor should be shown by the spinner while
// animating. If it is not shown, the cursor will be restored when the
// spinner stops. This can't be changed after the *Spinner has been
// constructed.
//
// Please note, if you do not set this to true and the program crashes or is
// killed, you may need to reset your terminal for the cursor to appear
// again.
ShowCursor bool
// HideCursor describes whether the cursor should be hidden by the spinner
// while animating. If it is hidden, it will be restored when the spinner
// stops. This can't be changed after the *Spinner has been constructed.
//
// Please note, if the program crashes or is killed you may need to reset
// your terminal for the cursor to appear again.
//
// Deprecated: use ShowCursor instead.
HideCursor bool
// SpinnerAtEnd configures the spinner to render the animation at the end of
// the line instead of the beginning. The default behavior is to render the
// animated spinner at the beginning of the line.
SpinnerAtEnd bool
// ColorAll describes whether to color everything (all) or just the spinner
// character(s). This cannot be changed after the *Spinner has been
// constructed.
ColorAll bool
// Colors are the colors used for the different printed messages. This
// respects the ColorAll field.
Colors []string
// CharSet is the list of characters to iterate through to draw the spinner.
CharSet []string
// Prefix is the string printed immediately before the spinner.
//
// If SpinnerAtEnd is set to true, it's recommended that this string start
// with a space character (` `).
Prefix string
// Suffix is the string printed immediately after the spinner and before the
// message.
//
// If SpinnerAtEnd is set to false, it's recommended that this string starts
// with an space character (` `).
Suffix string
// SuffixAutoColon configures whether the spinner adds a colon after the
// suffix automatically. If there is a message, a colon followed by a space
// is added to the suffix. Otherwise, if there is no message, or the suffix
// is only space characters, the colon is omitted.
//
// If SpinnerAtEnd is set to true, this option is ignored.
SuffixAutoColon bool
// Message is the message string printed by the spinner. If SpinnerAtEnd is
// set to false and SuffixAutoColon is set to true, the printed line will
// look like:
//
// <prefix><spinner><suffix>: <message>
//
// If SpinnerAtEnd is set to true, the printed line will instead look like
// this:
//
// <message><prefix><spinner><suffix>
//
// In this case, it may be preferred to set the Prefix to empty space (` `).
Message string
// StopMessage is the message used when Stop() is called.
StopMessage string
// StopCharacter is spinner character used when Stop() is called.
// Recommended character is ✓, and can be more than just one character.
StopCharacter string
// StopColors are the colors used for the Stop() printed line. This respects
// the ColorAll field.
StopColors []string
// StopFailMessage is the message used when StopFail() is called.
StopFailMessage string
// StopFailCharacter is the spinner character used when StopFail() is
// called. Recommended character is ✗, and can be more than just one
// character.
StopFailCharacter string
// StopFailColors are the colors used for the StopFail() printed line. This
// respects the ColorAll field.
StopFailColors []string
// TerminalMode is a bitflag field to control how the internal TTY / "dumb
// terminal" detection works, to allow consumers to override the internal
// behaviors. To set this value, it's recommended to use the TerminalMode
// constants exported by this package.
//
// If not set, the New() function implicitly sets it to AutomaticMode. The
// New() function also returns an error if you have conflicting flags, such
// as setting ForceTTYMode and ForceNoTTYMode, or if you set AutomaticMode
// and any other flags set.
//
// When in AutomaticMode, the New() function attempts to determine if the
// current application is running within an interactive (teletype [TTY])
// session. If it does not appear to be within a TTY, it sets this field
// value to ForceNoTTYMode | ForceDumbTerminalMode.
//
// If this does appear to be a TTY, the ForceTTYMode bitflag will bet set.
// Similarly, if it's a TTY and the TERM environment variable isn't set to
// "dumb" the ForceSmartTerminalMode bitflag will also be set.
//
// If the deprecated NoTTY Config struct field is set to true, and this
// field is AutomaticMode, the New() function sets field to the value of
// ForceNoTTYMode | ForceDumbTerminalMode.
TerminalMode TerminalMode
// NotTTY tells the spinner that the Writer should not be treated as a TTY.
// This results in the animation being disabled, with the animation only
// happening whenever the data is updated. This mode also renders each
// update on new line, versus reusing the current line.
//
// Deprecated: use TerminalMode field instead by setting it to:
// ForceNoTTYMode | ForceDumbTerminalMode. This will be removed in a future
// release.
NotTTY bool
}
// Spinner is a type representing an animated CLi terminal spinner. The Spinner
// is constructed by the New() function of this package, which accepts a Config
// struct as the only argument. Some of the configuration values cannot be
// changed after the spinner is constructed, so be sure to read the comments
// within the Config type.
//
// Please note, by default the spinner will hide the terminal cursor when
// animating the spinner. If you do not set Config.ShowCursor to true, you need
// to make sure to call the Stop() or StopFail() method to reset the cursor in
// the terminal. Otherwise, after the program exits the cursor will be hidden
// and the user will need to `reset` their terminal.
type Spinner struct {
writer io.Writer
buffer *bytes.Buffer
colorAll bool
cursorHidden bool
suffixAutoColon bool
termMode TerminalMode
spinnerAtEnd bool
status *uint32
lastPrintLen int
cancelCh chan struct{} // send: Stop(), close: StopFail(); both stop painter
doneCh chan struct{}
pauseCh chan struct{}
unpauseCh chan struct{}
unpausedCh chan struct{}
// mutex hat and the fields wearing it
mu *sync.Mutex
frequency time.Duration
chars []character
maxWidth int
index int
prefix string
suffix string
message string
colorFn func(format string, a ...interface{}) string
stopMsg string
stopChar character
stopColorFn func(format string, a ...interface{}) string
stopFailMsg string
stopFailChar character
stopFailColorFn func(format string, a ...interface{}) string
frequencyUpdateCh chan time.Duration
dataUpdateCh chan struct{}
}
const (
statusStopped uint32 = iota
statusStarting
statusRunning
statusStopping
statusPausing
statusPaused
statusUnpausing
)
// New creates a new unstarted spinner. If stdout does not appear to be a TTY,
// this constructor implicitly sets cfg.NotTTY to true.
func New(cfg Config) (*Spinner, error) {
if cfg.ShowCursor && cfg.HideCursor {
return nil, errors.New("cfg.ShowCursor and cfg.HideCursor cannot be true")
}
if cfg.TerminalMode == 0 {
cfg.TerminalMode = AutomaticMode
}
// AutomaticMode flag has been set, but so have others
if termModeAuto(cfg.TerminalMode) && cfg.TerminalMode != AutomaticMode {
return nil, errors.New("cfg.TerminalMode cannot have AutomaticMode flag set if others are set")
}
if termModeForceTTY(cfg.TerminalMode) && termModeForceNoTTY(cfg.TerminalMode) {
return nil, errors.New("cfg.TerminalMode cannot have both ForceTTYMode and ForceNoTTYMode flags set")
}
if termModeForceDumb(cfg.TerminalMode) && termModeForceSmart(cfg.TerminalMode) {
return nil, errors.New("cfg.TerminalMode cannot have both ForceDumbTerminalMode and ForceSmartTerminalMode flags set")
}
if cfg.HideCursor {
cfg.ShowCursor = false
}
// cfg.NotTTY compatibility
if cfg.TerminalMode == AutomaticMode && cfg.NotTTY {
cfg.TerminalMode = ForceNoTTYMode | ForceDumbTerminalMode
}
// is this a dumb terminal / not a TTY?
if cfg.TerminalMode == AutomaticMode && !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
cfg.TerminalMode = ForceNoTTYMode | ForceDumbTerminalMode
}
// if cfg.TerminalMode is still equal to AutomaticMode, this is a TTY
if cfg.TerminalMode == AutomaticMode {
cfg.TerminalMode = ForceTTYMode
if os.Getenv("TERM") == "dumb" {
cfg.TerminalMode |= ForceDumbTerminalMode
} else {
cfg.TerminalMode |= ForceSmartTerminalMode
}
}
buf := bytes.NewBuffer(make([]byte, 2048))
buf.Reset()
s := &Spinner{
buffer: buf,
mu: &sync.Mutex{},
frequency: cfg.Frequency,
status: uint32Ptr(0),
frequencyUpdateCh: make(chan time.Duration), // use unbuffered for now to avoid .Frequency() panic
dataUpdateCh: make(chan struct{}),
colorAll: cfg.ColorAll,
cursorHidden: !cfg.ShowCursor,
spinnerAtEnd: cfg.SpinnerAtEnd,
suffixAutoColon: cfg.SuffixAutoColon,
termMode: cfg.TerminalMode,
colorFn: fmt.Sprintf,
stopColorFn: fmt.Sprintf,
stopFailColorFn: fmt.Sprintf,
}
if err := s.Colors(cfg.Colors...); err != nil {
return nil, err
}
if err := s.StopColors(cfg.StopColors...); err != nil {
return nil, err
}
if err := s.StopFailColors(cfg.StopFailColors...); err != nil {
return nil, err
}
if len(cfg.CharSet) == 0 {
cfg.CharSet = CharSets[9]
}
// can only error if the charset is empty, and we prevent that above
_ = s.CharSet(cfg.CharSet)
if termModeForceNoTTY(s.termMode) {
// hack to prevent the animation from running if not a TTY
s.frequency = time.Duration(math.MaxInt64)
}
if cfg.Writer == nil {
cfg.Writer = colorable.NewColorableStdout()
}
s.writer = cfg.Writer
if len(cfg.Prefix) > 0 {
s.Prefix(cfg.Prefix)
}
if len(cfg.Suffix) > 0 {
s.Suffix(cfg.Suffix)
}
if len(cfg.Message) > 0 {
s.Message(cfg.Message)
}
if len(cfg.StopMessage) > 0 {
s.StopMessage(cfg.StopMessage)
}
if len(cfg.StopCharacter) > 0 {
s.StopCharacter(cfg.StopCharacter)
}
if len(cfg.StopFailMessage) > 0 {
s.StopFailMessage(cfg.StopFailMessage)
}
if len(cfg.StopFailCharacter) > 0 {
s.StopFailCharacter(cfg.StopFailCharacter)
}
return s, nil
}
func (s *Spinner) notifyDataChange() {
// non-blocking notification
select {
case s.dataUpdateCh <- struct{}{}:
default:
}
}
// SpinnerStatus describes the status of the spinner. See the package constants
// for the list of all possible statuses
type SpinnerStatus uint32
const (
// SpinnerStopped is a stopped spinner
SpinnerStopped SpinnerStatus = iota
// SpinnerStarting is a starting spinner
SpinnerStarting
// SpinnerRunning is a running spinner
SpinnerRunning
// SpinnerStopping is a stopping spinner
SpinnerStopping
// SpinnerPausing is a pausing spinner
SpinnerPausing
// SpinnerPaused is a paused spinner
SpinnerPaused
// SpinnerUnpausing is an unpausing spinner
SpinnerUnpausing
)
func (s SpinnerStatus) String() string {
switch s {
case SpinnerStopped:
return "stopped"
case SpinnerStarting:
return "starting"
case SpinnerRunning:
return "running"
case SpinnerStopping:
return "stopping"
case SpinnerPausing:
return "pausing"
case SpinnerPaused:
return "paused"
case SpinnerUnpausing:
return "unpausing"
default:
return fmt.Sprintf("unknown (%d)", s)
}
}
// Status returns the current status of the spinner. The returned value is of
// type SpinnerStatus, which can be compared against the exported Spinner*
// package-level constants (e.g., SpinnerRunning).
func (s *Spinner) Status() SpinnerStatus {
return SpinnerStatus(atomic.LoadUint32(s.status))
}
// Start begins the spinner on the Writer in the Config provided to New(). Only
// possible error is if the spinner is already runninng.
func (s *Spinner) Start() error {
// move us to the starting state
if !atomic.CompareAndSwapUint32(s.status, statusStopped, statusStarting) {
return errors.New("spinner already running or shutting down")
}
// we now have atomic guarantees of no other goroutines starting or running
s.mu.Lock()
if s.frequency < 1 && termModeForceTTY(s.termMode) {
return errors.New("spinner Frequency duration must be greater than 0 when used within a TTY")
}
if len(s.chars) == 0 {
s.mu.Unlock()
// move us to the stopped state
if !atomic.CompareAndSwapUint32(s.status, statusStarting, statusStopped) {
panic("atomic invariant encountered")
}
return errors.New("before starting the spinner a CharSet must be set")
}
s.frequencyUpdateCh = make(chan time.Duration, 4)
s.dataUpdateCh, s.cancelCh = make(chan struct{}, 1), make(chan struct{}, 1)
s.mu.Unlock()
// because of the atomic swap above, we know it's safe to mutate these
// values outside of mutex
s.doneCh = make(chan struct{})
s.pauseCh = make(chan struct{}) // unbuffered since we want this to be synchronous
go s.painter(s.cancelCh, s.dataUpdateCh, s.pauseCh, s.doneCh, s.frequencyUpdateCh)
// move us to the running state
if !atomic.CompareAndSwapUint32(s.status, statusStarting, statusRunning) {
panic("atomic invariant encountered")
}
return nil
}
// Pause puts the spinner in a state where it no longer animates or renders
// updates to data. This function blocks until the spinner's internal painting
// goroutine enters a paused state.
//
// If you want to make a few configuration changes and have them to appear at
// the same time, like changing the suffix, message, and color, you can Pause()
// the spinner first and then Unpause() after making the changes.
//
// If the spinner is not running (stopped, paused, or in transition to another
// state) this returns an error.
func (s *Spinner) Pause() error {
if !atomic.CompareAndSwapUint32(s.status, statusRunning, statusPausing) {
return errors.New("spinner not running")
}
// set up the channels the painter will use
s.unpauseCh, s.unpausedCh = make(chan struct{}), make(chan struct{})
// inform the painter to pause as a blocking send
s.pauseCh <- struct{}{}
if !atomic.CompareAndSwapUint32(s.status, statusPausing, statusPaused) {
panic("atomic invariant encountered")
}
return nil
}
// Unpause returns the spinner back to a running state after pausing. See
// Pause() documentation for more detail. This function blocks until the
// spinner's internal painting goroutine acknowledges the request to unpause.
//
// If the spinner is not paused this returns an error.
func (s *Spinner) Unpause() error {
if !atomic.CompareAndSwapUint32(s.status, statusPaused, statusUnpausing) {
return errors.New("spinner not paused")
}
s.unpause()
if !atomic.CompareAndSwapUint32(s.status, statusUnpausing, statusRunning) {
panic("atomic invariant encountered")
}
return nil
}
func (s *Spinner) unpause() {
// tell the painter to unpause
close(s.unpauseCh)
// wait for the painter to signal it will continue
<-s.unpausedCh
// clear the no longer needed channels
s.unpauseCh = nil
s.unpausedCh = nil
}
// Stop disables the spinner, and prints the StopCharacter with the StopMessage
// using the StopColors. This blocks until the stopped message is printed. Only
// possible error is if the spinner is not running.
func (s *Spinner) Stop() error {
return s.stop(false)
}
// StopFail disables the spinner, and prints the StopFailCharacter with the
// StopFailMessage using the StopFailColors. This blocks until the stopped
// message is printed. Only possible error is if the spinner is not running.
func (s *Spinner) StopFail() error {
return s.stop(true)
}
func (s *Spinner) stop(fail bool) error {
// move us to a stopping state to protect against concurrent Stop() calls
wasRunning := atomic.CompareAndSwapUint32(s.status, statusRunning, statusStopping)
wasPaused := atomic.CompareAndSwapUint32(s.status, statusPaused, statusStopping)
if !wasRunning && !wasPaused {
return errors.New("spinner not running or paused")
}
// we now have an atomic guarantees of no other threads invoking state changes
if !fail {
// this tells the painter to print the StopMessage and not the
// StopFailMessage
s.cancelCh <- struct{}{}
}
close(s.cancelCh)
if wasPaused {
s.unpause()
}
// wait for the painter to stop
<-s.doneCh
s.mu.Lock()
s.dataUpdateCh = make(chan struct{}) // prevent panic() in various setter methods
s.frequencyUpdateCh = make(chan time.Duration) // prevent panic() in .Frequency()
s.mu.Unlock()
// because of atomic swaps and channel receive above we know it's
// safe to mutate these fields outside of the mutex
s.index = 0
s.cancelCh = nil
s.doneCh = nil
s.pauseCh = nil
// move us to the stopped state
if !atomic.CompareAndSwapUint32(s.status, statusStopping, statusStopped) {
panic("atomic invariant encountered")
}
return nil
}
// handleFrequencyUpdate is for when the frequency was changed. This tries to
// see if we should fire the timer now, or change its current duration to match
// the new duration.
func handleFrequencyUpdate(newFrequency time.Duration, timer *time.Timer, lastTick time.Time) {
// if timer fired, drain the channel
if !timer.Stop() {
timerLoop:
for {
select {
case <-timer.C:
default:
break timerLoop
}
}
}
timeSince := time.Since(lastTick)
// if we've exceeded the new delay trigger timer immediately
if timeSince >= newFrequency {
timer.Reset(0)
return
}
timer.Reset(newFrequency - timeSince)
}
func (s *Spinner) painter(cancel, dataUpdate, pause <-chan struct{}, done chan<- struct{}, frequencyUpdate <-chan time.Duration) {
timer := time.NewTimer(0)
var lastTick time.Time
for {
select {
case <-timer.C:
lastTick = time.Now()
s.paintUpdate(timer, true)
case <-pause:
<-s.unpauseCh
close(s.unpausedCh)
case <-dataUpdate:
// if this is not a TTY: animate the spinner on the data update
s.paintUpdate(timer, termModeForceNoTTY(s.termMode))
case frequency := <-frequencyUpdate:
handleFrequencyUpdate(frequency, timer, lastTick)
case _, ok := <-cancel:
defer close(done)
timer.Stop()
s.paintStop(ok)
return
}
}
}
func (s *Spinner) paintUpdate(timer *time.Timer, animate bool) {
s.mu.Lock()
p := s.prefix
m := s.message
suf := s.suffix
mw := s.maxWidth
cFn := s.colorFn
d := s.frequency
index := s.index
if animate {
s.index++
if s.index == len(s.chars) {
s.index = 0
}
} else {
// for data updates use the last spinner char
index--
if index < 0 {
index = len(s.chars) - 1
}
}
c := s.chars[index]
s.mu.Unlock()
defer s.buffer.Reset()
if termModeForceSmart(s.termMode) {
if err := erase(s.buffer); err != nil {
panic(fmt.Sprintf("failed to erase line: %v", err))
}
if s.cursorHidden {
if err := hideCursor(s.buffer); err != nil {
panic(fmt.Sprintf("failed to hide cursor: %v", err))
}
}
if _, err := paint(s.buffer, mw, c, p, m, suf, s.suffixAutoColon, s.colorAll, s.spinnerAtEnd, false, termModeForceNoTTY(s.termMode), cFn); err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
}
} else {
if err := s.eraseDumbTerm(s.buffer); err != nil {
panic(fmt.Sprintf("failed to erase line: %v", err))
}
n, err := paint(s.buffer, mw, c, p, m, suf, s.suffixAutoColon, false, s.spinnerAtEnd, false, termModeForceNoTTY(s.termMode), fmt.Sprintf)
if err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
}
s.lastPrintLen = n
}
if s.buffer.Len() > 0 {
if _, err := s.writer.Write(s.buffer.Bytes()); err != nil {
panic(fmt.Sprintf("failed to output buffer to writer: %v", err))
}
}
if animate {
timer.Reset(d)
}
}
func (s *Spinner) paintStop(chanOk bool) {
var m string
var c character
var cFn func(format string, a ...interface{}) string
s.mu.Lock()
if chanOk {
c = s.stopChar
cFn = s.stopColorFn
m = s.stopMsg
} else {
c = s.stopFailChar
cFn = s.stopFailColorFn
m = s.stopFailMsg
}
p := s.prefix
suf := s.suffix
mw := s.maxWidth
s.mu.Unlock()
defer s.buffer.Reset()
if termModeForceSmart(s.termMode) {
if err := erase(s.buffer); err != nil {
panic(fmt.Sprintf("failed to erase line: %v", err))
}
if s.cursorHidden {
if err := unhideCursor(s.buffer); err != nil {
panic(fmt.Sprintf("failed to hide cursor: %v", err))
}
}
if c.Size > 0 || len(m) > 0 {
// paint the line with a newline as it's the final line
if _, err := paint(s.buffer, mw, c, p, m, suf, s.suffixAutoColon, s.colorAll, s.spinnerAtEnd, true, termModeForceNoTTY(s.termMode), cFn); err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
}
}
} else {
if err := s.eraseDumbTerm(s.buffer); err != nil {
panic(fmt.Sprintf("failed to erase line: %v", err))
}
if c.Size > 0 || len(m) > 0 {
if _, err := paint(s.buffer, mw, c, p, m, suf, s.suffixAutoColon, false, s.spinnerAtEnd, true, termModeForceNoTTY(s.termMode), fmt.Sprintf); err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
}
}
s.lastPrintLen = 0
}
if s.buffer.Len() > 0 {
if _, err := s.writer.Write(s.buffer.Bytes()); err != nil {
panic(fmt.Sprintf("failed to output buffer to writer: %v", err))
}
}
}
// erase clears the line
func erase(w io.Writer) error {
_, err := fmt.Fprint(w, "\r\033[K\r")
return err
}
// eraseDumbTerm clears the line on dumb terminals
func (s *Spinner) eraseDumbTerm(w io.Writer) error {
if termModeForceNoTTY(s.termMode) {
// non-TTY outputs use \n instead of line erasure,
// so return early
return nil
}
clear := "\r" + strings.Repeat(" ", s.lastPrintLen) + "\r"
_, err := fmt.Fprint(w, clear)
return err
}
func hideCursor(w io.Writer) error {
_, err := fmt.Fprint(w, "\r\033[?25l\r")
return err
}
func unhideCursor(w io.Writer) error {
_, err := fmt.Fprint(w, "\r\033[?25h\r")
return err
}
// padChar pads the spinner character so suffix / message offset from left is
// consistent
func padChar(char character, maxWidth int) string {
padSize := maxWidth - char.Size
return char.Value + strings.Repeat(" ", padSize)
}
// paint writes a single line to the w, using the provided character, message,
// and color function
func paint(w io.Writer, maxWidth int, char character, prefix, message, suffix string, suffixAutoColon, colorAll, spinnerAtEnd, finalPaint, notTTY bool, colorFn func(format string, a ...interface{}) string) (int, error) {
var output string
switch char.Size {
case 0:
if colorAll {
output = colorFn(message)
break
}
output = message
default:
c := padChar(char, maxWidth)
if spinnerAtEnd {
if colorAll {
output = colorFn("%s%s%s%s", message, prefix, c, suffix)
break
}
output = fmt.Sprintf("%s%s%s%s", message, prefix, colorFn(c), suffix)
break
}
if suffixAutoColon { // also implicitly !spinnerAtEnd
if len(strings.TrimSpace(suffix)) > 0 && len(message) > 0 && message != "\n" {
suffix += ": "
}
}
if colorAll {
output = colorFn("%s%s%s%s", prefix, c, suffix, message)
break
}
output = fmt.Sprintf("%s%s%s%s", prefix, colorFn(c), suffix, message)
}
if finalPaint || notTTY {
output += "\n"
}
return fmt.Fprint(w, output)
}
// Frequency updates the frequency of the spinner being animated.
func (s *Spinner) Frequency(d time.Duration) error {
if d < 1 {
return errors.New("duration must be greater than 0")
}
if termModeForceNoTTY(s.termMode) {
// when output target is not a TTY, we don't animate spinner
// so there is no need to update the frequency
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
s.frequency = d
// non-blocking notification
select {
case s.frequencyUpdateCh <- d:
default:
}
return nil
}
// Prefix updates the Prefix before the spinner character.
func (s *Spinner) Prefix(prefix string) {
s.mu.Lock()
defer s.mu.Unlock()
s.prefix = prefix
s.notifyDataChange()
}
// Suffix updates the Suffix printed after the spinner character and before the
// message. It's recommended that this start with an empty space.
func (s *Spinner) Suffix(suffix string) {
s.mu.Lock()
defer s.mu.Unlock()
s.suffix = suffix
s.notifyDataChange()
}
// Message updates the Message displayed after the suffix.
func (s *Spinner) Message(message string) {
s.mu.Lock()
defer s.mu.Unlock()
s.message = message
s.notifyDataChange()
}
// Colors updates the github.com/fatih/colors for printing the spinner line.
// ColorAll config parameter controls whether only the spinner character is
// printed with these colors, or the whole line.
//
// StopColors() is the method to control the colors in the stop message.
func (s *Spinner) Colors(colors ...string) error {
colorFn, err := colorFunc(colors...)
if err != nil {
return fmt.Errorf("failed to build color function: %w", err)
}
s.mu.Lock()
defer s.mu.Unlock()
s.colorFn = colorFn
s.notifyDataChange()
return nil
}
// StopMessage updates the Message used when Stop() is called.
func (s *Spinner) StopMessage(message string) {
s.mu.Lock()
defer s.mu.Unlock()
s.stopMsg = message
s.notifyDataChange()
}
// StopColors updates the colors used for the stop message. See Colors() method
// documentation for more context.
//
// StopFailColors() is the method to control the colors in the failed stop
// message.
func (s *Spinner) StopColors(colors ...string) error {
colorFn, err := colorFunc(colors...)
if err != nil {
return fmt.Errorf("failed to build stop color function: %w", err)
}
s.mu.Lock()
defer s.mu.Unlock()
s.stopColorFn = colorFn
s.notifyDataChange()
return nil
}
// StopCharacter sets the single "character" to use for the spinner when
// stopping. Recommended character is ✓.
func (s *Spinner) StopCharacter(char string) {
n := runewidth.StringWidth(char)
s.mu.Lock()
defer s.mu.Unlock()
s.stopChar = character{Value: char, Size: n}
if n > s.maxWidth {
s.maxWidth = n
}
s.notifyDataChange()
}
// StopFailMessage updates the Message used when StopFail() is called.
func (s *Spinner) StopFailMessage(message string) {
s.mu.Lock()
defer s.mu.Unlock()
s.stopFailMsg = message
s.notifyDataChange()
}
// StopFailColors updates the colors used for the StopFail message. See Colors() method
// documentation for more context.
func (s *Spinner) StopFailColors(colors ...string) error {
colorFn, err := colorFunc(colors...)
if err != nil {
return fmt.Errorf("failed to build stop fail color function: %w", err)
}
s.mu.Lock()
defer s.mu.Unlock()
s.stopFailColorFn = colorFn
s.notifyDataChange()
return nil
}
// StopFailCharacter sets the single "character" to use for the spinner when
// stopping for a failure. Recommended character is ✗.
func (s *Spinner) StopFailCharacter(char string) {
n := runewidth.StringWidth(char)
s.mu.Lock()
defer s.mu.Unlock()
s.stopFailChar = character{Value: char, Size: n}
if n > s.maxWidth {
s.maxWidth = n
}
s.notifyDataChange()
}
// CharSet updates the set of characters (strings) to use for the spinner. You
// can provide your own, or use one from the yacspin.CharSets variable.
//
// The character sets available in the CharSets variable are from the
// https://github.com/briandowns/spinner project.
func (s *Spinner) CharSet(cs []string) error {
if len(cs) == 0 {
return errors.New("failed to set character set: must provide at least one string")
}
chars, mw := setToCharSlice(cs)
s.mu.Lock()
defer s.mu.Unlock()
if n := s.stopChar.Size; n > mw {
mw = s.stopChar.Size
}
if n := s.stopFailChar.Size; n > mw {
mw = n
}
s.chars = chars
s.maxWidth = mw
s.index = 0
return nil
}
// Reverse flips the character set order of the spinner characters.
func (s *Spinner) Reverse() {
s.mu.Lock()
defer s.mu.Unlock()
for i, j := 0, len(s.chars)-1; i < j; {
s.chars[i], s.chars[j] = s.chars[j], s.chars[i]
i++
j--
}
s.index = 0
}
func uint32Ptr(u uint32) *uint32 { return &u }
|