Skip to content

Commit

Permalink
Update tui, github
Browse files Browse the repository at this point in the history
  • Loading branch information
irevenko committed Mar 21, 2021
1 parent 69cf952 commit eb93365
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 32 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
# 🐙🐱🖥️ octotui - GitHub stats in terminal
> Inspired by <a href="https://github.com/lowlighter/metrics">metrics</a> & <a href="https://github.com/vn7n24fzkq/github-profile-summary-cards">github-profile-summary-cards</a> 💖
> Inspired by <a href="https://github.com/lowlighter/metrics">metrics</a> & <a href="https://github.com/vn7n24fzkq/github-profile-summary-cards">github-profile-summary-cards</a> & <a href="https://github.com/skanehira/github-tui">github-tui</a> 💖
<p align="center"> Data - <a href="https://github.com/irevenko/octostats">irevenko/octostats</a>
TUI - <a href="https://github.com/gizak/termui">termui</a> </p> <br>
<img src="preview.png">

# TUI
- [x] Users/Orgs search
- [x] Profile Image & user basic info/stats
- [x] Most starred, forked repos
- [x] Languages, by repo, commits, stars, forks
- [x] Activity sparkline
- [ ] Other colors
- [ ] Loading progress bar while fetching stats

# ToDo
- [ ] colors
- [ ] refactoring
- [ ] Fix graphql pagination (first 100)
- [ ] graceful exit ?termenv
- [ ] orgzanization support
- [ ] Input validation
- [ ] token configuration
- [ ] custom arguments
- [ ] re-render component (render_stats profile info prev results)
- [ ] loading progress bar while fetching profile stats
- [ ] test with various gh profiles
rofile stats
- [ ] test with various gh profiles
- [ ] test on windows
9 changes: 7 additions & 2 deletions github/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/shurcooL/githubv4"
)

func FetchStats(ctx context.Context, restClient *github.Client, qlClient *githubv4.Client, username string, allRepos []*github.Repository) (int, int, int, int, int) {
func FetchStats(ctx context.Context, restClient *github.Client, qlClient *githubv4.Client, username string, allRepos []*github.Repository) (int, int, int, int, int, int) {
totalStars := r.TotalStars(restClient, allRepos)
totalForks := r.TotalForks(restClient, allRepos)

Expand All @@ -20,6 +20,7 @@ func FetchStats(ctx context.Context, restClient *github.Client, qlClient *github
var totalCommits int
var totalIssues int
var totalPrs int
var totalReviews int

for _, v := range contribs.CommitContributionsByRepository {
totalCommits += v.Contributions.TotalCount
Expand All @@ -33,5 +34,9 @@ func FetchStats(ctx context.Context, restClient *github.Client, qlClient *github
totalPrs += v.Contributions.TotalCount
}

return totalStars, totalForks, totalCommits, totalIssues, totalPrs
for _, v := range contribs.PullRequestReviewContributionsByRepository {
totalReviews += v.Contributions.TotalCount
}

return totalStars, totalForks, totalCommits, totalIssues, totalPrs, totalReviews
}
17 changes: 9 additions & 8 deletions github/stats_text_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,23 @@ func BuildProfileInfo(user g.User) string {
func BuildProfileStats(ctx context.Context, restClient *github.Client, qlClient *githubv4.Client, user g.User, allRepos []*github.Repository) string {
var baseStats string

s, f, c, i, p := FetchStats(ctx, restClient, qlClient, user.Login, allRepos)
s, f, c, i, p, rp := FetchStats(ctx, restClient, qlClient, user.Login, allRepos)
usedLicenses, _ := r.MostUsedLicenses(restClient, allRepos)

baseStats = "Profile Statistics" + "\n" +
"[Total repos:](fg:magenta) " + strconv.Itoa(user.Repositories.TotalCount) + "\n" +
"[Total gists:](fg:magenta) " + strconv.Itoa(user.Gists.TotalCount) + "\n" +
"[Total stars:](fg:magenta) " + strconv.Itoa(s) + "\n" +
"[Total forks:](fg:magenta) " + strconv.Itoa(f) + "\n" +
"[Total commits (last year):](fg:magenta) " + strconv.Itoa(c) + "\n" +
"[Total issues (last year):](fg:magenta) " + strconv.Itoa(i) + "\n" +
"[Total PRs (last year):](fg:magenta) " + strconv.Itoa(p) + "\n" +
"[Total repos:](fg:magenta) " + strconv.Itoa(user.Repositories.TotalCount) + "\n" +
"[Total gists:](fg:magenta) " + strconv.Itoa(user.Gists.TotalCount) + "\n" +
"[Opened issues (last year):](fg:magenta) " + strconv.Itoa(i) + "\n" +
"[Opened PRs (last year):](fg:magenta) " + strconv.Itoa(p) + "\n" +
"[Reviewed PRs (last year):](fg:magenta) " + strconv.Itoa(rp) + "\n" +
"[Total packages:](fg:magenta) " + strconv.Itoa(user.Packages.TotalCount) + "\n" +
"[Total projects:](fg:magenta) " + strconv.Itoa(user.Projects.TotalCount) + "\n" +
"[Organizations:](fg:magenta) " + strconv.Itoa(len(user.Organizations.Nodes)) + "\n" +
"[Sponsors:](fg:magenta) " + strconv.Itoa(len(user.SponsorshipsAsMaintainer.Nodes)) + "\n" +
"[Sponsoring:](fg:magenta) " + strconv.Itoa(len(user.SponsorshipsAsSponsor.Nodes)) + "\n" +
"[Organizations:](fg:magenta) " + strconv.Itoa(user.Organizations.TotalCount) + "\n" +
"[Sponsors:](fg:magenta) " + strconv.Itoa(user.SponsorshipsAsMaintainer.TotalCount) + "\n" +
"[Sponsoring:](fg:magenta) " + strconv.Itoa(user.SponsorshipsAsSponsor.TotalCount) + " people \n" +
"[Watching:](fg:magenta) " + strconv.Itoa(user.Watching.TotalCount) + " repos\n"

if len(usedLicenses) > 0 {
Expand Down
Binary file modified preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions tui/render_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ func RenderList(results *github.UsersSearchResult) {
defer ui.Close()

var rowsData []string

for _, v := range results.Users {
login := *v.Login
accountType := *v.Type
rowsData = append(rowsData, login+" ("+strings.ToLower(accountType)+")")
}

l := widgets.NewList()

l.Title = "Search Results"
l.Rows = rowsData
l.TextStyle = ui.NewStyle(ui.ColorRed)
l.TextStyle = ui.NewStyle(ui.ColorBlue)
l.WrapText = true
l.SetRect(0, 7, 100, 30)

Expand Down
2 changes: 1 addition & 1 deletion tui/render_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
token = ""
token = "fb71a5aa6f42d225f4210fb6b20bdebb81ce7cf5"
)

var (
Expand Down
65 changes: 54 additions & 11 deletions tui/widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
func SetupProfileInfo(user g.User) *widgets.Paragraph {
p := widgets.NewParagraph()
p.WrapText = true
p.Text = gh.BuildProfileInfo(user)
p.Border = true
p.Text = gh.BuildProfileInfo(user)
p.SetRect(0, 35, 35, 14)

return p
Expand Down Expand Up @@ -74,14 +74,20 @@ func SetupImage(profileImg string, login string) (*widgets.Image, []image.Image)
}

func SetupLangsByCommits(user g.User) *widgets.PieChart {
year, _, _ := time.Now().Date()
langs, commits := g.LanguagesByCommit(qlClient, user.Login, year-1, year)

pc := widgets.NewPieChart()
pc.Title = "Languages by commit"
pc.SetRect(35, 35, 70, 20)

year, _, _ := time.Now().Date()
langs, commits := g.LanguagesByCommit(qlClient, user.Login, year-1, year)
boundNum := defineDataBound(langs)
if boundNum == 0 {
pc.Title = "Languages by commit (no commits)"
} else {
pc.Data = commits[:boundNum]
}

pc.Data = commits[:4]
pc.AngleOffset = .15 * math.Pi
pc.LabelFormatter = func(i int, v float64) string {
return fmt.Sprintf("%.00f"+" %s", v, langs[i])
Expand All @@ -96,7 +102,14 @@ func SetupLangsByRepo(user g.User, allRepos []*github.Repository) *widgets.PieCh
pc := widgets.NewPieChart()
pc.Title = "Languages by repo"
pc.SetRect(105, 35, 70, 20)
pc.Data = langsNum[:4]

boundNum := defineDataBound(usedLangs)
if boundNum == 0 {
pc.Title = "Languages by repo (no repos)"
} else {
pc.Data = langsNum[:boundNum]
}

pc.AngleOffset = .15 * math.Pi
pc.LabelFormatter = func(i int, v float64) string {
return fmt.Sprintf("%.00f"+" %s", v, usedLangs[i])
Expand All @@ -109,11 +122,17 @@ func SetupStarsPerLangs(user g.User, allRepos []*github.Repository) *widgets.Bar
starsPerL, starsNum := r.StarsPerLanguage(restClient, allRepos)

bc := widgets.NewBarChart()
bc.Data = starsNum[:4]
bc.Labels = starsPerL[:4]
bc.Title = "Stars per language"
bc.SetRect(150, 35, 105, 20)
bc.BarWidth = 5

boundNum := defineDataBound(starsPerL)
if boundNum == 0 {
bc.Title = "Stars per language (no languages)"
} else {
bc.Data = starsNum[:boundNum]
bc.Labels = starsPerL[:boundNum]
}

bc.BarColors = []ui.Color{ui.ColorMagenta, ui.ColorGreen, ui.ColorYellow, ui.ColorBlue}
bc.LabelStyles = []ui.Style{ui.NewStyle(ui.ColorCyan)}
bc.NumStyles = []ui.Style{ui.NewStyle(ui.ColorWhite)}
Expand All @@ -127,11 +146,17 @@ func SetupForksPerLangs(user g.User, allRepos []*github.Repository) *widgets.Bar
forksPerL, forksNum := r.ForksPerLanguage(restClient, allRepos)

bc := widgets.NewBarChart()
bc.Data = forksNum[:4]
bc.Labels = forksPerL[:4]
bc.Title = "Forks per language"
bc.SetRect(150, 10, 105, 20)
bc.BarWidth = 5

boundNum := defineDataBound(forksPerL)
if boundNum == 0 {
bc.Title = "Forks per language (no languages)"
} else {
bc.Data = forksNum[:boundNum]
bc.Labels = forksPerL[:boundNum]
}

bc.BarColors = []ui.Color{ui.ColorMagenta, ui.ColorGreen, ui.ColorYellow, ui.ColorBlue}
bc.LabelStyles = []ui.Style{ui.NewStyle(ui.ColorCyan)}
bc.NumStyles = []ui.Style{ui.NewStyle(ui.ColorWhite)}
Expand All @@ -156,3 +181,21 @@ func SetupContribsSparkline(user g.User) *widgets.SparklineGroup {

return slg
}

func defineDataBound(slice []string) int {
if len(slice) > 4 { // 4 is max amount of entries for pie/bar chart
return 4
} else if len(slice) < 1 {
return 0
} else if len(slice) < 2 {
return 1
} else if len(slice) < 3 {
return 2
} else if len(slice) < 4 {
return 3
} else if len(slice) < 5 {
return 4
}

return 0
}

0 comments on commit eb93365

Please sign in to comment.