-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use curses for in-place updates on status
- Loading branch information
1 parent
a3dacbc
commit 43dacdc
Showing
7 changed files
with
173 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require 'curses' | ||
|
||
Curses.noecho | ||
Curses.init_screen | ||
|
||
module SafeUpdate | ||
class Presenter | ||
# outdated_gems is an array of instances of SafeUpdate::OutdatedGem | ||
|
||
SPINNER_STATES = ['|', '/', '-', '\\'] | ||
|
||
def initialize | ||
@tick = 1 | ||
@running = true | ||
end | ||
|
||
def call(outdated_gems) | ||
@outdated_gems = outdated_gems | ||
while @running do | ||
@tick += 1 | ||
update_screen | ||
sleep 0.3 | ||
end | ||
end | ||
|
||
def stop | ||
@running = false | ||
print_final_output | ||
end | ||
|
||
private | ||
|
||
def print_final_output | ||
Curses.close_screen | ||
puts title | ||
puts header | ||
@outdated_gems.each do |outdated_gem| | ||
puts present_single_gem(outdated_gem) | ||
end | ||
end | ||
|
||
def update_screen | ||
Curses.setpos(0, 0) | ||
Curses.addstr(title) | ||
Curses.refresh | ||
|
||
Curses.setpos(1, 0) | ||
Curses.addstr(header) | ||
Curses.refresh | ||
|
||
@outdated_gems.each_with_index do |outdated_gem, i| | ||
Curses.setpos(i + 2, 0) | ||
line = present_single_gem(outdated_gem) | ||
Curses.addstr(line) | ||
Curses.refresh | ||
end | ||
end | ||
|
||
def title | ||
'=> Updating your gems... safely ' + current_spinner_state | ||
end | ||
|
||
def current_spinner_state | ||
div, remainder = @tick.divmod(SPINNER_STATES.length) | ||
SPINNER_STATES[remainder] | ||
end | ||
|
||
def header | ||
return [ | ||
fixed_length_string('GEM', 10), | ||
fixed_length_string('INSTALLED', 10), | ||
fixed_length_string('REQUESTED', 10), | ||
fixed_length_string('NEWEST', 7), | ||
fixed_length_string('STATUS', 10) | ||
].join(' | ') | ||
end | ||
|
||
def present_single_gem(outdated_gem) | ||
return [ | ||
fixed_length_string(outdated_gem.gem_name, 10), | ||
fixed_length_string(outdated_gem.installed, 10), | ||
fixed_length_string(outdated_gem.requested || ' -', 10), | ||
fixed_length_string(outdated_gem.newest, 7), | ||
fixed_length_string(outdated_gem.current_status, 10) | ||
].join(' | ') | ||
end | ||
|
||
# inspired by http://stackoverflow.com/questions/14714936/fix-ruby-string-to-n-characters | ||
def fixed_length_string(str, length) | ||
"%-#{length}.#{length}s" % str | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
require 'safe_update' | ||
|
||
outdated_gems = [] | ||
outdated_gems << SafeUpdate::OutdatedGem.new( | ||
gem_name: 'rails', | ||
newest: '1.2.3', | ||
installed: '1.2.1', | ||
requested: '~> 1.2.0', | ||
) | ||
|
||
outdated_gems << SafeUpdate::OutdatedGem.new( | ||
gem_name: 'rspec', | ||
newest: '1.2.3', | ||
installed: '1.2.1', | ||
requested: '~> 1.2.0', | ||
) | ||
|
||
outdated_gems << SafeUpdate::OutdatedGem.new( | ||
gem_name: 'byebug', | ||
newest: '1.2.3', | ||
installed: '1.2.1', | ||
requested: '~> 1.2.0', | ||
) | ||
|
||
outdated_gems << SafeUpdate::OutdatedGem.new( | ||
gem_name: 'bullet', | ||
newest: '3.2.1', | ||
installed: '1.2.1', | ||
) | ||
|
||
states = [ | ||
SafeUpdate::OutdatedGem::STATUS_PENDING, | ||
SafeUpdate::OutdatedGem::STATUS_UPDATING, | ||
SafeUpdate::OutdatedGem::STATUS_TESTING, | ||
SafeUpdate::OutdatedGem::STATUS_UPDATED, | ||
SafeUpdate::OutdatedGem::STATUS_UNCHANGED, | ||
SafeUpdate::OutdatedGem::STATUS_TESTS_FAIL, | ||
] | ||
|
||
outdated_gems.map do |outdated_gem| | ||
outdated_gem.instance_variable_set( | ||
:@current_status, | ||
states.sample | ||
) | ||
end | ||
|
||
presenter = SafeUpdate::Presenter.new | ||
Thread.new { presenter.call(outdated_gems) } | ||
20.times do | ||
sleep 0.25 | ||
outdated_gems.sample.instance_variable_set( | ||
:@current_status, | ||
states.sample | ||
) | ||
end | ||
presenter.stop |