Class: Repofetch::Plugin Abstract

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/repofetch/plugin.rb

Overview

This class is abstract.

Subclass to create a plugin.

Direct Known Subclasses

BitbucketCloud, Github, Gitlab

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#clean_ansi, #default_remote, #default_remote_url, #remove_format_params

Constructor Details

#initializePlugin

Plugin intializer arguments should come from the from_git or from_args class methods.

[View source]

14
# File 'lib/repofetch/plugin.rb', line 14

def initialize(*) end

Class Method Details

.from_args(_args) ⇒ Plugin

This method is abstract.

This will receive an array of strings (e.g. ARGV) and call Plugin.new.

Parameters:

  • _args (Array)

    The arguments to process.

Returns:

Raises:

  • (NoMethodError)
[View source]

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 method is abstract.

This should use a git instance and call Plugin.new.

Parameters:

  • _git (Git::Base)

    The Git repository object to use when calling Plugin.new.

  • _args (Array)

    The arguments to process.

Returns:

Raises:

  • (NoMethodError)
[View source]

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 method is abstract.

This should use a path and call Plugin.new.

Parameters:

  • _path (String)

    The path to use when calling Plugin.new.

  • _args (Array)

    The arguments to process.

Returns:

Raises:

  • (NoMethodError)
[View source]

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

This method is abstract.

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.

Parameters:

  • _path (String)

    The path to check

Returns:

  • (Boolean)
[View source]

45
46
47
# File 'lib/repofetch/plugin.rb', line 45

def self.matches_path?(_path)
  false
end

.matches_repo?(_git) ⇒ Boolean

This method is abstract.

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.

Parameters:

  • _git (Git::Base)

    The Git repository object

Returns:

  • (Boolean)
[View source]

35
36
37
# File 'lib/repofetch/plugin.rb', line 35

def self.matches_repo?(_git)
  false
end

.registerObject

Registers this plugin class for repofetch.

[View source]

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.

Parameters:

  • old (Plugin)

    The plugin to replace

[View source]

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

#asciiObject

This method is abstract.

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).

Raises:

  • (NoMethodError)
[View source]

87
88
89
# File 'lib/repofetch/plugin.rb', line 87

def ascii
  raise NoMethodError, 'ascii must be overridden by the plugin subclass'
end

#formatted_headerString

Returns the header, formatted with the primary color and bold, and joined with the header joiner.

Returns:

  • (String)
[View source]

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

#headerString+

This method is abstract.

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.

Returns:

  • (String, Array<String>)

Raises:

  • (NoMethodError)
[View source]

98
99
100
# File 'lib/repofetch/plugin.rb', line 98

def header
  raise NoMethodError, 'header must be overridden by the plugin subclass'
end

#header_joinerString

A string to join header text.

Override to use a different string.

Returns:

  • (String)
[View source]

106
107
108
# File 'lib/repofetch/plugin.rb', line 106

def header_joiner
  ' @ '
end

#primary_colorSymbol

The primary color to use for the header and stats.

Override to use a different color from the theme.

Returns:

  • (Symbol)

See Also:

[View source]

141
142
143
# File 'lib/repofetch/plugin.rb', line 141

def primary_color
  :default
end

#separatorObject

Creates the separator that appears underneath the header

[View source]

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_linesObject

Makes an array of stat lines, including the header and separator.

[View source]

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

#statsArray<Stat>

This method is abstract.

An array of stats that will be displayed to the right of the ASCII art.

Returns:

[View source]

131
132
133
# File 'lib/repofetch/plugin.rb', line 131

def stats
  []
end

#themeObject

Gets the plugin's theme. Override to use a theme besides the default.

[View source]

79
80
81
# File 'lib/repofetch/plugin.rb', line 79

def theme
  Repofetch::DEFAULT_THEME
end

#to_sObject

[View source]

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_linesObject

Zips ASCII lines with stat lines.

If there are more of one than the other, than the zip will be padded with empty strings.

[View source]

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