Skip to content

Commit

Permalink
Set theme for stats in plugin base class
Browse files Browse the repository at this point in the history
Simplifies writing a plugin, so that a theme doesn't need to be passed
when initializing each `Stat`.
  • Loading branch information
spenserblack committed Dec 19, 2022
1 parent 62648b6 commit cc3531b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
20 changes: 16 additions & 4 deletions lib/repofetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,28 @@ def stats
[]
end

# Adds +theme+ to the stats, mutating those stats.
#
# @return [Array<Stat>]
def theme_stats!
stats.each do |stat|
stat.theme = theme if stat.respond_to?(:theme=)
end
end

# Makes an array of stat lines, including the header and separator.
def stat_lines
[header, separator, *stats.map(&:to_s)]
#
# Mutates +stats+ to add the +theme+.
def stat_lines!
[header, separator, *theme_stats!.map(&:to_s)]
end

# Zips ASCII lines with stat lines.
#
# If there are more of one than the other, than the zip will be padded with empty strings.
def zipped_lines
ascii_lines = ascii.lines.map(&:chomp)
stat_lines = stat_lines!
if ascii_lines.length > stat_lines.length
ascii_lines.zip(stat_lines)
else
Expand All @@ -211,17 +223,17 @@ def zipped_lines
# Base class for stats.
class Stat
attr_reader :label, :value, :emoji
attr_writer :theme

# Creates a stat
#
# @param [String] label The label of the stat
# @param value The value of the stat
# @param [String] emoji An optional emoji for the stat
def initialize(label, value, emoji: nil, theme: nil)
def initialize(label, value, emoji: nil)
@label = label
@value = value
@emoji = emoji
@theme = theme
@label_styles = []
end

Expand Down
18 changes: 9 additions & 9 deletions lib/repofetch/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,27 @@ def repo_stats
end

def url
Repofetch::Stat.new('URL', repo_stats['clone_url'], emoji: '🌐', theme: theme)
Repofetch::Stat.new('URL', repo_stats['clone_url'], emoji: '🌐')
end

def stargazers
Repofetch::Stat.new('stargazers', repo_stats['stargazers_count'], emoji: '⭐', theme: theme)
Repofetch::Stat.new('stargazers', repo_stats['stargazers_count'], emoji: '⭐')
end

def subscribers
Repofetch::Stat.new('subscribers', repo_stats['subscribers_count'], emoji: '👀', theme: theme)
Repofetch::Stat.new('subscribers', repo_stats['subscribers_count'], emoji: '👀')
end

def forks
Repofetch::Stat.new('forks', repo_stats['forks_count'], emoji: '🔱', theme: theme)
Repofetch::Stat.new('forks', repo_stats['forks_count'], emoji: '🔱')
end

def created
Repofetch::TimespanStat.new('created', repo_stats['created_at'], emoji: '🐣', theme: theme)
Repofetch::TimespanStat.new('created', repo_stats['created_at'], emoji: '🐣')
end

def updated
Repofetch::TimespanStat.new('updated', repo_stats['updated_at'], emoji: '📤', theme: theme)
Repofetch::TimespanStat.new('updated', repo_stats['updated_at'], emoji: '📤')
end

def size
Expand All @@ -136,17 +136,17 @@ def size
significant: false,
strip_insignificant_zeros: false
)
Repofetch::Stat.new('size', byte_size, emoji: '💽', theme: theme)
Repofetch::Stat.new('size', byte_size, emoji: '💽')
end

def issues
@issue_search = @client.search_issues("repo:#{repo_id} is:issue", per_page: 1, page: 0) if @issue_search.nil?
Repofetch::Stat.new('issues', @issue_search['total_count'], emoji: '❗', theme: theme)
Repofetch::Stat.new('issues', @issue_search['total_count'], emoji: '❗')
end

def pull_requests
@pr_search = @client.search_issues("repo:#{repo_id} is:pr", per_page: 1, page: 0) if @pr_search.nil?
Repofetch::Stat.new('pull requests', @pr_search['total_count'], emoji: '🔀', theme: theme)
Repofetch::Stat.new('pull requests', @pr_search['total_count'], emoji: '🔀')
end
end
end
Expand Down

0 comments on commit cc3531b

Please sign in to comment.