Class: PgVerify::Shell::LoadingAnimation

Inherits:
Object
  • Object
show all
Defined in:
lib/pg-verify/shell/loading/loading_animation.rb

Direct Known Subclasses

LineAnimation, NoAnimation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loading_message) ⇒ LoadingAnimation

Returns a new instance of LoadingAnimation.



8
9
10
11
12
13
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 8

def initialize(loading_message)
    @loading_message = loading_message
    @last_output_length = 0
    @animation_thread = nil
    @start_time = nil
end

Instance Attribute Details

#last_output_lengthObject (readonly)

Returns the value of attribute last_output_length.



6
7
8
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 6

def last_output_length
  @last_output_length
end

#loading_messageObject (readonly)

Returns the value of attribute loading_message.



6
7
8
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 6

def loading_message
  @loading_message
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



6
7
8
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 6

def start_time
  @start_time
end

Instance Method Details

#on_anim(passed_time) ⇒ Object



58
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 58

def on_anim(passed_time); end

#on_startObject



59
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 59

def on_start(); end

#on_stop(prompt, finish_message) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 62

def on_stop(prompt, finish_message)
    message = "#{prompt} #{@loading_message}"
    message += " | #{finish_message}".c_sidenote unless finish_message.nil? || finish_message.empty?
    if Settings.print_loading_times
        duration = TimeUtil.duration_string((Time.now - @start_time).to_i)
        duration_string = duration.nil? ? "" : "Took #{duration}".c_sidenote
        return Shell.expand_to_console(message, duration_string)
    else
        return message
    end
end


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 46

def print_anim(string)
    return if string.blank?
    string = string.to_s
    # Fill the string with spaces to overwrite the last printed string
    print_string = string + ( " " * [0, @last_output_length - string.length].max )
    # Set the length of the last print to the string length without spaces, 
    # since they do not need to be overwritten.
    @last_output_length = string.length
    # Print the string with spaces.
    print("\r#{print_string}")
end

#printl(string) ⇒ Object



60
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 60

def printl(string); end

#startObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 15

def start
    return unless @animation_thread.nil?
    @start_time = Time.now
    on_start()
    @animation_thread = Thread.new {
        begin
            while true do
                passed_time = Time.now - @start_time
                string = on_anim(passed_time)
                print_anim(string) unless string.nil?
                sleep(1.0 / 10)
            end
        rescue => e
            puts e
            puts e.backtrace 
            raise e
        end
    }
    self
end

#started?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 74

def started?()
    !@animation_thread.nil?
end

#stop(status, finish_message) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/pg-verify/shell/loading/loading_animation.rb', line 36

def stop(status, finish_message)
    return if @animation_thread.nil?
    @animation_thread.exit
    prompt = Shell.gen_prompt(status)
    string = on_stop(prompt, finish_message)
    @animation_thread = nil
    print_anim(string)
    puts
end