Module: SpinningCursor

Extended by:
SpinningCursor, selfself::ConsoleHelpers
Includes:
selfself::ConsoleHelpers
Included in:
SpinningCursor
Defined in:
lib/spinning_cursor.rb,
lib/spinning_cursor/cursor.rb,
lib/spinning_cursor/parser.rb,
lib/spinning_cursor/console_helpers.rb

Defined Under Namespace

Modules: ConsoleHelpers Classes: Cursor, CursorNotRunning, NoTaskError, Parser

Instance Method Summary collapse

Instance Method Details

#alive?Boolean

Determines whether the cursor thread is still running

Returns:

  • (Boolean)


87
88
89
# File 'lib/spinning_cursor.rb', line 87

def alive?
  @spinner and @spinner.alive?
end

#set_banner(banner) ⇒ Object

Sets the banner message during execution



107
108
109
110
111
112
113
114
# File 'lib/spinning_cursor.rb', line 107

def set_banner(banner)
  begin
    @parsed.banner banner
  rescue NameError
    raise CursorNotRunning.new "Cursor isn't running... are you sure " +
      "you're calling this from an action block?"
  end
end

#set_message(msg) ⇒ Object

Sets the finish message (to be used inside the action for non-deterministic output)



95
96
97
98
99
100
101
102
# File 'lib/spinning_cursor.rb', line 95

def set_message(msg)
  begin
    @parsed.message msg
  rescue NameError
    raise CursorNotRunning.new "Cursor isn't running... are you sure " +
      "you're calling this from an action block?"
  end
end

#start(&block) ⇒ Object

Sends passed block to Parser, and starts cursor thread It will execute the action block and kill the cursor thread if an action block is passed.



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
# File 'lib/spinning_cursor.rb', line 16

def start(&block)
  stop if alive?

  save_stdout_sync_status
  capture_console
  hide_cursor

  @parsed = Parser.new(&block)
  @cursor = Cursor.new(@parsed)
  @spinner = Thread.new do
    abort_on_exception = true
    @cursor.spin
  end

  @stop_watch = StopWatch.new

  if @parsed.action
    # The action
    begin
      @stop_watch.measure do
        @parsed.outer_scope_object.instance_eval &@parsed.action
      end
    rescue StandardError => e
      set_message "#{e.message}\n#{e.backtrace.join("\n")}"
      raise
    ensure
      stop
    end
  else
    # record start time
    @stop_watch.start
  end
end

#stopObject

Kills the cursor thread and prints the finished message Returns execution time



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
# File 'lib/spinning_cursor.rb', line 54

def stop
  begin
    @spinner.kill
    # Wait for the cursor to die -- can cause problems otherwise
    @spinner.join
    # Set cursor to nil so set_banner method only works
    # when cursor is actually running.
    @cursor = nil

    restore_stdout_sync_status
    if console_captured?
      $console.print ESC_R_AND_CLR + $stdout.string
      release_console
    end
    show_cursor

    reset_line
    puts @parsed.message
    # Set parsed to nil so set_message method only works
    # when cursor is actually running.
    @parsed = nil

    # Return execution time
    @stop_watch.stop
    @stop_watch.timing
  rescue NameError
    raise CursorNotRunning.new "Can't stop, no cursor running."
  end
end