Module: Crayons

Defined in:
lib/rvvm/crayons.rb

Overview

Utility module providing an API to run shell commands with spinners and stdout mesage formatting.

Examples:

require 'rvvm/crayons'

Crayons.init

# Runn a shell command with a spinner
Crayons.command("Running command...", "ls -aln")

# Run custom code with a spinner
Crayons.spinner_start("New task...")

# do stuff

Crayons.spinner_log("Halfway done! ^^")

# do some more stuff

Crayons.spinner_stop("Completed!", true)
Crayons.log_pass("Oof, im done now...")

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.command(tag, cmd, ignore_errors: false) ⇒ void

This method returns an undefined value.

Runs a shell command with a spinner.

Parameters:

  • tag (String)

    spinner tag

  • cmd (String)

    shell command to be run

  • ignore_errors (Boolean) (defaults to: false)

    if false command exits on shell command failure

Since:

  • 1.1.0



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rvvm/crayons.rb', line 143

def self.command(tag, cmd, ignore_errors: false)
  spinner_start(tag)
  out = @command.run!(cmd)
  spinner_log("#{out.out}\n")
  spinner_stop(nil, out.success?)
  puts ""

  exit(1) unless out.success? && !ignore_errors
rescue Errno::ENOENT
  spinner_stop(nil, false)
  log_error("\nError: '#{cmd.split.first}' command not found!\n")
  exit(1) unless ignore_errors
rescue StandardError => e
  spinner_stop(nil, false)
  log_error("\nAn unexpected error ocurred:\n\n#{e.message}\n")
  exit(1) unless ignore_errors
end

.initvoid

This method returns an undefined value.

Initializes Crayons module.

Since:

  • 1.1.0



47
48
49
# File 'lib/rvvm/crayons.rb', line 47

def self.init
  @command = TTY::Command.new(printer: :null)
end

.log_error(message) ⇒ void

This method returns an undefined value.

Logs a red text message to stdout.

Parameters:

  • message (String)

    message to print

Since:

  • 1.0.0



179
180
181
# File 'lib/rvvm/crayons.rb', line 179

def self.log_error(message)
  puts message.red
end

.log_pass(message) ⇒ void

This method returns an undefined value.

Logs a green text message to stdout.

Parameters:

  • message (String)

    message to print

Since:

  • 1.0.0



168
169
170
# File 'lib/rvvm/crayons.rb', line 168

def self.log_pass(message)
  puts message.green
end

.spinner_log(message) ⇒ void

This method returns an undefined value.

Logs a message during spinner execution removing the need to manually pause and resume spinner.

If a spinner is not running simply prints message.

Parameters:

  • message (String)

    message to prin

Since:

  • 1.0.0



128
129
130
131
132
# File 'lib/rvvm/crayons.rb', line 128

def self.spinner_log(message)
  spinner_pause if @spinner_running
  puts message
  spinner_resume if @spinner_running
end

.spinner_pausevoid

This method returns an undefined value.

Pauses spinner thread execution to enable uninterrupted stdout outputs.

Since:

  • 1.0.0



83
84
85
86
87
88
89
# File 'lib/rvvm/crayons.rb', line 83

def self.spinner_pause
  return unless @spinner_running

  print "\r\e[K"
  @thread_paused = true
  print "\r\e[K"
end

.spinner_resumevoid

This method returns an undefined value.

Resumes spinner thread execution.

Since:

  • 1.0.0



96
97
98
# File 'lib/rvvm/crayons.rb', line 96

def self.spinner_resume
  @thread_paused = false if @spinner_running
end

.spinner_running?Boolean

Returns spinner status.

Returns:

  • (Boolean)

    spinner status

Since:

  • 1.0.0



54
55
56
# File 'lib/rvvm/crayons.rb', line 54

def self.spinner_running?
  @spinner_running
end

.spinner_start(message) ⇒ void

This method returns an undefined value.

Starts a cli spinner appending the provided message.

Parameters:

  • message (String)

    text to append to the spinner

Since:

  • 1.0.0



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rvvm/crayons.rb', line 65

def self.spinner_start(message)
  print "\r\e[K"
  @spinner = TTY::Spinner.new("[:spinner] #{message}", interval: 5)
  @spinner_running = true
  @thread = Thread.new do
    @spinner.auto_spin
    while @thread_sleeping
      print "\r\e[K"
      sleep(0.1)
    end
  end
end

.spinner_stop(message = nil, exit) ⇒ void

This method returns an undefined value.

Stops spinner printing a formated status message.

Parameters:

  • message (String) (defaults to: nil)

    status message to print

  • exit (Boolean)

    return code (true - success, false - error)

Since:

  • 1.0.0



108
109
110
111
112
113
114
115
116
# File 'lib/rvvm/crayons.rb', line 108

def self.spinner_stop(message = nil, exit)
  return unless @spinner_running

  sleep(0.1)
  spinner_message = message || (exit ? "Done!" : "Failed!")
  exit ? @spinner.success(spinner_message.green) : @spinner.error(spinner_message.red)
  @thread.join
  @spinner_running = false
end