Skip to content
This repository was archived by the owner on Apr 27, 2020. It is now read-only.

Commit 8ea50d0

Browse files
committed
Extract DownloadProgressBar into its own class
1 parent 79392d1 commit 8ea50d0

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

Diff for: lib/spotify_to_mp3/app.rb

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'fileutils'
22
require 'spotify_to_mp3/app/stream_queries'
3-
require 'ruby-progressbar'
3+
require 'spotify_to_mp3/app/download_progress_bar'
44

55
module SpotifyToMp3
66
class App
@@ -48,15 +48,12 @@ def run
4848
@grooveshark.download(
4949
track: track,
5050
on_response: Proc.new { |response|
51-
win_half = $stdout.winsize[1] / 2
52-
title = "[#{i.next}/#{tracks_to_download.length}] #{track}"
53-
cut_title = title[0..win_half.pred].ljust win_half
54-
cut_title = cut_title.gsub(/.{3}$/, '...') if title.length > win_half
55-
progress_bar = ProgressBar.create(
56-
:title => cut_title,
57-
:total => response['content-length'].to_i,
58-
:format => "%t %p%% [%B] %E"
59-
)
51+
progress_bar = DownloadProgressBar.new(
52+
track: track,
53+
track_number: i.next,
54+
track_size: response['content-length'].to_i,
55+
total_tracks: tracks_to_download.length
56+
)
6057
},
6158
on_body_chunk: Proc.new { |chunk|
6259
progress_bar.progress += chunk.length

Diff for: lib/spotify_to_mp3/app/download_progress_bar.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'ruby-progressbar'
2+
require 'forwardable'
3+
4+
module SpotifyToMp3
5+
class App
6+
class DownloadProgressBar
7+
extend Forwardable
8+
9+
def_delegators :@progress_bar, :progress, :progress=, :finish
10+
11+
def initialize(options)
12+
track = options.fetch(:track)
13+
track_number = options.fetch(:track_number)
14+
track_size = options.fetch(:track_size)
15+
total_tracks = options.fetch(:total_tracks)
16+
17+
@progress_bar = ProgressBar.create(
18+
title: cut_title("[#{track_number}/#{total_tracks}] #{track}"),
19+
total: track_size,
20+
format: "%t %p%% [%B] %E"
21+
)
22+
end
23+
24+
private
25+
26+
def cut_title(title)
27+
win_half = $stdout.winsize[1] / 2
28+
cut_title = title[0..win_half.pred].ljust win_half
29+
cut_title.gsub(/.{3}$/, '...') if title.length > win_half
30+
cut_title
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)