-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carlos Alexandro Becker <[email protected]>
- Loading branch information
Showing
3 changed files
with
139 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package csv | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strconv" | ||
|
||
"github.com/caarlos0/org-stats/orgstats" | ||
) | ||
|
||
func Write(w io.Writer, s orgstats.Stats, includeReviews bool) error { | ||
cw := csv.NewWriter(w) | ||
defer cw.Flush() | ||
|
||
headers := []string{"login", "commits", "lines-added", "lines-removed"} | ||
if includeReviews { | ||
headers = append(headers, "reviews") | ||
} | ||
if err := cw.Write(headers); err != nil { | ||
return fmt.Errorf("failed to write csv: %w", err) | ||
} | ||
|
||
logins := s.Logins() | ||
sort.Strings(logins) | ||
|
||
for _, login := range logins { | ||
stat := s.For(login) | ||
record := []string{ | ||
login, | ||
strconv.Itoa(stat.Commits), | ||
strconv.Itoa(stat.Additions), | ||
strconv.Itoa(stat.Deletions), | ||
} | ||
if includeReviews { | ||
record = append(record, strconv.Itoa(stat.Reviews)) | ||
} | ||
if err := cw.Write(record); err != nil { | ||
return fmt.Errorf("failed to write csv: %w", err) | ||
} | ||
} | ||
|
||
return cw.Error() | ||
} |
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,89 @@ | ||
package highlights | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/caarlos0/org-stats/orgstats" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
func Write(w io.Writer, s orgstats.Stats, top int, includeReviews bool) error { | ||
data := []statHighlight{ | ||
{ | ||
stats: orgstats.Sort(s, orgstats.ExtractCommits), | ||
trophy: "Commits", | ||
kind: "commits", | ||
}, { | ||
stats: orgstats.Sort(s, orgstats.ExtractAdditions), | ||
trophy: "Lines Added", | ||
kind: "lines added", | ||
}, { | ||
stats: orgstats.Sort(s, orgstats.ExtractDeletions), | ||
trophy: "Housekeeper", | ||
kind: "lines removed", | ||
}, | ||
} | ||
|
||
if includeReviews { | ||
data = append(data, statHighlight{ | ||
stats: orgstats.Sort(s, orgstats.Reviews), | ||
trophy: "Pull Requests Reviewed", | ||
kind: "pull requests reviewed", | ||
}) | ||
} | ||
|
||
var headerStyle = lipgloss.NewStyle(). | ||
Bold(true). | ||
Foreground(lipgloss.AdaptiveColor{ | ||
Dark: "#BD7EFC", | ||
Light: "#7D56F4", | ||
}) | ||
|
||
var bodyStyle = lipgloss.NewStyle(). | ||
PaddingLeft(2) | ||
|
||
// TODO: handle no results for a given topic | ||
for _, d := range data { | ||
if _, err := fmt.Fprintln(w, headerStyle.Render(d.trophy+" champions are:")); err != nil { | ||
return err | ||
} | ||
j := top | ||
if len(d.stats) < j { | ||
j = len(d.stats) | ||
} | ||
for i := 0; i < j; i++ { | ||
if _, err := fmt.Fprintln(w, | ||
bodyStyle.Render( | ||
fmt.Sprintf( | ||
"%s %s with %d %s!", | ||
emojiForPos(i), | ||
d.stats[i].Key, | ||
d.stats[i].Value, | ||
d.kind, | ||
), | ||
), | ||
); err != nil { | ||
return err | ||
} | ||
} | ||
if _, err := fmt.Fprintln(w); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func emojiForPos(pos int) string { | ||
emojis := []string{"\U0001f3c6", "\U0001f948", "\U0001f949"} | ||
if pos < len(emojis) { | ||
return emojis[pos] | ||
} | ||
return " " | ||
} | ||
|
||
type statHighlight struct { | ||
stats []orgstats.StatPair | ||
trophy string | ||
kind string | ||
} |
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