Class: Repofetch::Plugin Abstract
- Inherits:
-
Object
- Object
- Repofetch::Plugin
- Includes:
- Util
- Defined in:
- lib/repofetch/plugin.rb
Overview
Subclass to create a plugin.
Direct Known Subclasses
Class Method Summary collapse
- .from_args(_args) ⇒ Plugin abstract
- .from_git(_git, _args) ⇒ Plugin abstract
- .from_path(_path, _args) ⇒ Plugin abstract
-
.matches_path?(_path) ⇒ Boolean
abstract
This is intended to be more generic than
matches_repo?
, and support any path. -
.matches_repo?(_git) ⇒ Boolean
abstract
An example implementation is checking if
Repofetch.default_remote_url
matches a regular expression. -
.register ⇒ Object
Registers this plugin class for repofetch.
-
.replace_or_register(old) ⇒ Object
Tries to replace another plugin.
Instance Method Summary collapse
-
#ascii ⇒ Object
abstract
This should be overridden by the plugin subclass.
-
#formatted_header ⇒ String
Returns the header, formatted with the primary color and bold, and joined with the header joiner.
-
#header ⇒ String+
abstract
This should be overridden by the plugin subclass.
-
#header_joiner ⇒ String
A string to join header text.
-
#initialize ⇒ Plugin
constructor
Plugin intializer arguments should come from the
from_git
orfrom_args
class methods. -
#primary_color ⇒ Symbol
The primary color to use for the header and stats.
-
#separator ⇒ Object
Creates the separator that appears underneath the header.
-
#stat_lines ⇒ Object
Makes an array of stat lines, including the header and separator.
- #stats ⇒ Array<Stat> abstract
-
#theme ⇒ Object
Gets the plugin's theme.
- #to_s ⇒ Object
-
#zipped_lines ⇒ Object
Zips ASCII lines with stat lines.
Methods included from Util
#clean_ansi, #default_remote, #default_remote_url, #remove_format_params
Constructor Details
#initialize ⇒ Plugin
Plugin intializer arguments should come from the from_git
or from_args
class methods.
14 |
# File 'lib/repofetch/plugin.rb', line 14 def initialize(*) end |
Class Method Details
.from_args(_args) ⇒ Plugin
This will receive an array of strings (e.g. ARGV
) and call Plugin.new
.
74 75 76 |
# File 'lib/repofetch/plugin.rb', line 74 def self.from_args(_args) raise NoMethodError, 'from_args must be overridden by the plugin subclass' end |
.from_git(_git, _args) ⇒ Plugin
This should use a git instance and call Plugin.new
.
55 56 57 |
# File 'lib/repofetch/plugin.rb', line 55 def self.from_git(_git, _args) raise NoMethodError, 'from_git must be overridden by the plugin subclass' end |
.from_path(_path, _args) ⇒ Plugin
This should use a path and call Plugin.new
.
65 66 67 |
# File 'lib/repofetch/plugin.rb', line 65 def self.from_path(_path, _args) raise NoMethodError, 'from_path must be overridden by the plugin subclass' end |
.matches_path?(_path) ⇒ Boolean
Detects that this plugin should be used. Should be overridden by subclasses.
This is intended to be more generic than matches_repo?
, and support any path.
An example implementation is checking if an expected file exists in this path.
45 46 47 |
# File 'lib/repofetch/plugin.rb', line 45 def self.matches_path?(_path) false end |
.matches_repo?(_git) ⇒ Boolean
Detects that this plugin should be used. Should be overridden by subclasses.
An example implementation is checking if Repofetch.default_remote_url
matches a regular expression.
35 36 37 |
# File 'lib/repofetch/plugin.rb', line 35 def self.matches_repo?(_git) false end |
.register ⇒ Object
Registers this plugin class for repofetch.
17 18 19 |
# File 'lib/repofetch/plugin.rb', line 17 def self.register Repofetch.register_plugin(self) end |
.replace_or_register(old) ⇒ Object
Tries to replace another plugin. An example use case might be if this plugin extends another registered plugin.
25 26 27 |
# File 'lib/repofetch/plugin.rb', line 25 def self.replace_or_register(old) Repofetch.replace_or_register_plugin(old, self) end |
Instance Method Details
#ascii ⇒ Object
The ASCII to be printed alongside the stats.
This should be overridden by the plugin subclass. Should be within the bounds 40x20 (width x height).
87 88 89 |
# File 'lib/repofetch/plugin.rb', line 87 def ascii raise NoMethodError, 'ascii must be overridden by the plugin subclass' end |
#formatted_header ⇒ String
Returns the header, formatted with the primary color and bold, and joined with the header joiner.
148 149 150 |
# File 'lib/repofetch/plugin.rb', line 148 def formatted_header (header.is_a?(Array) ? header : [header]).map { |h| apply_styles(h, :bold, primary_color) }.join(header_joiner) end |
#header ⇒ String+
The header to show for the plugin.
This should be overridden by the plugin subclass.
If an array is returned, it will be joined by header_joiner
.
98 99 100 |
# File 'lib/repofetch/plugin.rb', line 98 def header raise NoMethodError, 'header must be overridden by the plugin subclass' end |
#header_joiner ⇒ String
A string to join header text.
Override to use a different string.
106 107 108 |
# File 'lib/repofetch/plugin.rb', line 106 def header_joiner ' @ ' end |
#primary_color ⇒ Symbol
The primary color to use for the header and stats.
Override to use a different color from the theme.
141 142 143 |
# File 'lib/repofetch/plugin.rb', line 141 def primary_color :default end |
#separator ⇒ Object
Creates the separator that appears underneath the header
111 112 113 114 115 116 117 |
# File 'lib/repofetch/plugin.rb', line 111 def separator return '-' * header.length unless header.is_a?(Array) header_length = header.map(&:length).sum + ((header.length - 1) * header_joiner.length) '-' * header_length end |
#stat_lines ⇒ Object
Makes an array of stat lines, including the header and separator.
153 154 155 156 157 158 159 160 |
# File 'lib/repofetch/plugin.rb', line 153 def stat_lines styled_stats = stats.map do |stat| next stat unless stat.is_a?(Repofetch::Stat) stat.style_label(:bold, primary_color).format(theme) end [formatted_header, separator, *styled_stats] end |
#stats ⇒ Array<Stat>
An array of stats that will be displayed to the right of the ASCII art.
131 132 133 |
# File 'lib/repofetch/plugin.rb', line 131 def stats [] end |
#theme ⇒ Object
Gets the plugin's theme. Override to use a theme besides the default.
79 80 81 |
# File 'lib/repofetch/plugin.rb', line 79 def theme Repofetch::DEFAULT_THEME end |
#to_s ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/repofetch/plugin.rb', line 119 def to_s zipped_lines.map do |ascii_line, stat_line| cleaned_ascii = remove_format_params(ascii_line) styled_ascii = (ascii_line % theme.to_h) + theme.style(:reset) aligned_stat_line = "#{' ' * (MAX_ASCII_WIDTH + 5)}#{stat_line}" "#{styled_ascii}#{aligned_stat_line.slice(cleaned_ascii.length..)}\n" end.join end |
#zipped_lines ⇒ Object
Zips ASCII lines with stat lines.
If there are more of one than the other, than the zip will be padded with empty strings.
165 166 167 168 169 170 171 172 |
# File 'lib/repofetch/plugin.rb', line 165 def zipped_lines ascii_lines = ascii.lines.map(&:chomp) if ascii_lines.length > stat_lines.length ascii_lines.zip(stat_lines) else stat_lines.zip(ascii_lines).map(&:reverse) end.map { |ascii, stat| [ascii.to_s, stat.to_s] } end |