-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from aergonaut/configurable-messages
Adds configurable option to set message format
- Loading branch information
Showing
14 changed files
with
155 additions
and
46 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
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,13 @@ | ||
module Pronto | ||
module Formatter | ||
class Base | ||
def self.name | ||
Formatter::FORMATTERS.invert[self] | ||
end | ||
|
||
def config | ||
@config ||= Config.new | ||
end | ||
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module Pronto | ||
module Formatter | ||
class NullFormatter | ||
class NullFormatter < Base | ||
def format(_, _, _) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,15 @@ | ||
require 'pronto/formatter/text_message_decorator' | ||
|
||
module Pronto | ||
module Formatter | ||
class TextFormatter | ||
include Colorizable | ||
|
||
LOCATION_COLOR = :cyan | ||
|
||
LEVEL_COLORS = { | ||
info: :yellow, | ||
warning: :magenta, | ||
error: :red, | ||
fatal: :red | ||
}.freeze | ||
|
||
class TextFormatter < Base | ||
def format(messages, _, _) | ||
messages.map do |message| | ||
"#{format_location(message)} #{format_level(message)}: #{message.msg}".strip | ||
end | ||
end | ||
|
||
private | ||
|
||
def format_location(message) | ||
line = message.line | ||
lineno = line.new_lineno if line | ||
path = message.path | ||
commit_sha = message.commit_sha | ||
|
||
if path || lineno | ||
path = colorize(path, LOCATION_COLOR) if path | ||
"#{path}:#{lineno}" | ||
elsif commit_sha | ||
colorize(commit_sha[0..6], LOCATION_COLOR) | ||
message_format = config.message_format(self.class.name) | ||
message_data = TextMessageDecorator.new(message).to_h | ||
(message_format % message_data).strip | ||
end | ||
end | ||
|
||
def format_level(message) | ||
level = message.level | ||
color = LEVEL_COLORS.fetch(level) | ||
|
||
colorize(level[0].upcase, color) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Pronto | ||
module Formatter | ||
class TextMessageDecorator < SimpleDelegator | ||
include Colorizable | ||
|
||
LOCATION_COLOR = :cyan | ||
|
||
LEVEL_COLORS = { | ||
info: :yellow, | ||
warning: :magenta, | ||
error: :red, | ||
fatal: :red | ||
}.freeze | ||
|
||
def to_h | ||
original = __getobj__.to_h | ||
original[:color_level] = format_level(__getobj__) | ||
original[:color_location] = format_location(__getobj__) | ||
original | ||
end | ||
|
||
private | ||
|
||
def format_location(message) | ||
line = message.line | ||
lineno = line.new_lineno if line | ||
path = message.path | ||
commit_sha = message.commit_sha | ||
|
||
if path || lineno | ||
path = colorize(path, LOCATION_COLOR) if path | ||
"#{path}:#{lineno}" | ||
elsif commit_sha | ||
colorize(commit_sha[0..6], LOCATION_COLOR) | ||
end | ||
end | ||
|
||
def format_level(message) | ||
level = message.level | ||
color = LEVEL_COLORS.fetch(level) | ||
|
||
colorize(level[0].upcase, color) | ||
end | ||
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