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)


100
101
102
# File 'lib/spinning_cursor.rb', line 100

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

#set_banner(banner) ⇒ Object

Sets the banner message during execution



120
121
122
123
124
125
126
127
# File 'lib/spinning_cursor.rb', line 120

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)



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

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

#setup(&block) ⇒ Object



11
12
13
14
15
16
# File 'lib/spinning_cursor.rb', line 11

def setup(&block)
  @parsed = Parser.new(&block)
  @cursor = Cursor.new(@parsed)
  @setup = true
  self
end

#setup?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/spinning_cursor.rb', line 18

def setup?
  @setup
end

#start(&block) ⇒ Object Also known as: run

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.



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

def start(&block)
  setup(&block) if block or not setup?
  stop if alive?

  save_stdout_sync_status
  capture_console
  hide_cursor

  @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



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

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
    @setup = false

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