fewald-worklog 0.3.18 → 0.3.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 691934914763f18fbf4695873e5bd5c529a31fc5b2d67c68ec19b9d30647dded
4
- data.tar.gz: ef97e71d9881018b85071d16339980196a250f34ef449ae295ec330176606fab
3
+ metadata.gz: 33d69c646f75c4ff235bf6f6d314ae91de000c2043b8369e0427db9d0b80e08f
4
+ data.tar.gz: b1d9e80dcc075878a260f6baee8992e8959bcf29786f28575e4e8b918cb704b3
5
5
  SHA512:
6
- metadata.gz: 005733caa2baaed390cebb2f8c8d55ccfefa0e8fe225a2a1776b98c733075453b9ba09193d6dbbe6e5020db10af965d818360401a79292d04cc9ac936337c485
7
- data.tar.gz: 10eb61fab0d9d51a50bb8bb61ca793af7986f0cc7490c7b34799310422b1bce4aac3c1dc6306a3b53888a300630b962ee66b3a9c1d5bbcf4e2b3e70f318cca97
6
+ metadata.gz: 514b631b513aff4bff09165cf247711bc9739fc210aa0024e4a2e860a2405c0831d9ad650148af0fc5d315339ee87615151411f3b6ca8ad4af80f37b32c2c049
7
+ data.tar.gz: 91debf86e2527915247ec3d7e12f2a0335945b2b3ffe7cf7c772bcf5e86b2f9759cf2a93331235652e8af1ea067af2f1d30b85765a01fa62606ce5c958573305
data/.version CHANGED
@@ -1 +1 @@
1
- 0.3.18
1
+ 0.3.19
data/lib/log_entry.rb CHANGED
@@ -4,6 +4,7 @@ require 'yaml'
4
4
  require 'rainbow'
5
5
  require 'daily_log'
6
6
  require 'hash'
7
+ require 'log_entry_formatters'
7
8
 
8
9
  module Worklog
9
10
  # A single log entry in a DailyLog.
@@ -61,41 +62,19 @@ module Worklog
61
62
 
62
63
  # Returns the message string with formatting without the time.
63
64
  # @param known_people Hash[String, Person] A hash of people with their handles as keys.
64
- def message_string(known_people = nil)
65
- # replace all mentions of people with their names.
66
- msg = @message.dup
67
- people.each do |person|
68
- next unless known_people && known_people[person]
69
-
70
- msg.gsub!(/[~@]#{person}/) do |match|
71
- s = String.new
72
- s << ' ' if match[0] == ' '
73
- s << "#{Rainbow(known_people[person].name).underline} (~#{person})" if known_people && known_people[person]
74
- s
75
- end
76
- end
77
-
78
- s = String.new
79
-
80
- # Prefix with [EPIC] if epic
81
- s << epic_prefix if epic?
82
-
83
- # Print the message
84
- s << if source == 'github'
85
- Rainbow(msg).fg(:green)
86
- else
87
- msg
88
- end
89
-
90
- s << format_metadata
91
- s
65
+ # @param formatter [BaseFormatter] the formatter to use for formatting the message. If nil, a default
66
+ # ConsoleFormatter is used.
67
+ # @return [String] the formatted message string
68
+ def message_string(known_people = nil, formatter = nil)
69
+ formatter ||= LogEntryFormatters::ConsoleFormatter.new(known_people)
70
+ formatter.format(self)
92
71
  end
93
72
 
73
+ # Return people that are mentioned in the entry. People are defined as character sequences
74
+ # starting with @ or ~. Whitespaces are used to separate people. Punctuation is ignored.
75
+ # Empty set if no people are mentioned.
76
+ # @return [Set<String>]
94
77
  def people
95
- # Return people that are mentioned in the entry. People are defined as character sequences
96
- # starting with @ or ~. Whitespaces are used to separate people. Punctuation is ignored.
97
- # Empty set if no people are mentioned.
98
- # @return [Set<String>]
99
78
  @message.scan(PERSON_REGEX).flatten.uniq.sort.to_set
100
79
  end
101
80
 
@@ -116,6 +95,7 @@ module Worklog
116
95
  end
117
96
 
118
97
  # Convert the log entry to YAML format.
98
+ # @return [String] the YAML representation of the log entry.
119
99
  def to_yaml
120
100
  to_hash.to_yaml
121
101
  end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rainbow'
4
+
5
+ module Worklog
6
+ module LogEntryFormatters
7
+ # The base formatter provides common functionality and prints the message including
8
+ # the metadata (ticket, tags, url, project) in a formatted way. It also replaces
9
+ # people handles with their names if known.
10
+ class BaseFormatter
11
+ # Constructor
12
+ # @param known_people [Hash<String, Person>] A hash of people with their handles as keys.
13
+ def initialize(known_people = nil)
14
+ @known_people = known_people
15
+ end
16
+
17
+ # Format the log entry message with metadata.
18
+ # @param log_entry [LogEntry] the log entry to format.
19
+ # @return [String] the formatted message.
20
+ def format(log_entry)
21
+ # replace all mentions of people with their names.
22
+ msg = log_entry.message.dup
23
+ s = String.new
24
+ s << epic_prefix if log_entry.epic
25
+ s << replace_people_handles(log_entry, msg)
26
+ # Add a space between the message and the metadata if there is any metadata to add.
27
+ s << ' ' unless metadata(log_entry).empty?
28
+ s << metadata(log_entry)
29
+ s
30
+ end
31
+
32
+ protected
33
+
34
+ # Prefix for epic entries.
35
+ # @return [String]
36
+ def epic_prefix
37
+ '⭐️ '
38
+ end
39
+
40
+ # Format metadata for display and add some emojis for prettier visualization.
41
+ # @return [String] Formatted metadata
42
+ def metadata(log_entry)
43
+ metadata_parts = []
44
+ metadata_parts << "🎫#{Rainbow(log_entry.ticket).fg(:blue)}" if log_entry.ticket
45
+ metadata_parts << "🏷️#{log_entry.tags.join(', ')}" if log_entry.tags&.any?
46
+ metadata_parts << "🔗#{log_entry.url}" if log_entry.url && log_entry.url != ''
47
+ metadata_parts << "📘#{log_entry.project}" if log_entry.project && log_entry.project != ''
48
+
49
+ metadata_parts.empty? ? '' : metadata_parts.join(' ')
50
+ end
51
+
52
+ # Replace people handles in the message with their names.
53
+ # @param known_people Hash[String, Person] A hash of people with their handles as keys.
54
+ # @param msg [String] the message to replace handles in.
55
+ # @return [String] the message with replaced handles.
56
+ def replace_people_handles(log_entry, msg)
57
+ log_entry.people.each do |person|
58
+ next unless @known_people && @known_people[person]
59
+
60
+ msg.gsub!(/[~@]#{person}/) do |match|
61
+ s = String.new
62
+ s << ' ' if match[0] == ' '
63
+ if @known_people && @known_people[person]
64
+ s << "#{Rainbow(@known_people[person].name).underline} (~#{person})"
65
+ end
66
+ s
67
+ end
68
+ end
69
+ msg
70
+ end
71
+ end
72
+
73
+ # Formatter for console output. It adds colors and formatting to the message.
74
+ # It also adds emojis for tickets, tags, urls, and projects.
75
+ # This formatter should be used instead of the BaseFormatter when outputting to the console.
76
+ class ConsoleFormatter < BaseFormatter; end
77
+
78
+ # Simple formatter that doesn't add any colors or metadata to the output.
79
+ # This formatter should be used when the output is meant to be consumed by other tools or
80
+ # when the formatting is not desired.
81
+ class SimpleFormatter < BaseFormatter
82
+ # Format the log entry message without any metadata.
83
+ # @param log_entry [LogEntry] the log entry to format.
84
+ # @return [String] the formatted message.
85
+ def format(log_entry)
86
+ replace_people_handles(log_entry, log_entry.message.dup)
87
+ end
88
+ end
89
+ end
90
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fewald-worklog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.18
4
+ version: 0.3.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Friedrich Ewald
@@ -150,6 +150,7 @@ files:
150
150
  - lib/hash.rb
151
151
  - lib/hasher.rb
152
152
  - lib/log_entry.rb
153
+ - lib/log_entry_formatters.rb
153
154
  - lib/people_storage.rb
154
155
  - lib/person.rb
155
156
  - lib/printer.rb