Skip to content

Commit

Permalink
💡Added code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicPredator committed Feb 20, 2025
1 parent b69c230 commit b26b574
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/ui/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ type ProfileUI struct {
SiteUrl string
}

// Simple Key Value pair used for ordered output
// since map produces unordered kv pairs while iterating
type KV struct {
Key string
Value string
}

func (p *ProfileUI) Render() error {
// convert minutes to days
daysWatched := float32(p.MinutesWatched) / 1440

// populating []KV with data
var dataSlice = []KV{
{"ID", strconv.Itoa(p.Id)},
{"Name", p.Name},
Expand All @@ -35,16 +39,20 @@ func (p *ProfileUI) Render() error {
{"Site URL", p.SiteUrl},
}

// finding the max key length for padded output
maxKeyLen := 0
for _, kv := range dataSlice {
if len(kv.Key) > maxKeyLen {
maxKeyLen = len(kv.Key)
}
}

// define styles for both key and value string
keyStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#FF79C6"))
valueStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#8BE9FD"))

// iterating over dataSlice and printing the KV pairs
// with appropriate padding
for _, kv := range dataSlice {
fmt.Printf(
"%s : %s\n",
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ package ui

import "github.com/charmbracelet/lipgloss"

// displays text in green with ✓ on the left
func SuccessText(msg string) string {
return lipgloss.
NewStyle().
Foreground(lipgloss.Color("#00FF00")).
Render("✓ " + msg)
}

// displays text in red with ✘ on the left
func ErrorText(err error) string {
return lipgloss.
NewStyle().
Foreground(lipgloss.Color("#CC0000")).
Render("✘ Someting went wrong! Reason: ", err.Error())
}

// displays text in cyan foreground
func HighlightedText(msg string) string {
return lipgloss.
NewStyle().
Expand Down
3 changes: 3 additions & 0 deletions internal/viewmodel/profile_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
)

func HandleProfile() error {
// get profile info from API
profile, err := api.GetUserProfile()
if err != nil {
return err
}

// populate ProfileUI struct fields with the data from API
profileUI := ui.ProfileUI{
Id: profile.Data.Viewer.Id,
Name: profile.Data.Viewer.Name,
Expand All @@ -21,6 +23,7 @@ func HandleProfile() error {
SiteUrl: profile.Data.Viewer.SiteUrl,
}

// display profile UI
err = profileUI.Render()
return err
}

0 comments on commit b26b574

Please sign in to comment.