Skip to content

Feature/dynamic logo from skin #3225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions assets/dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
██████╗ ███████╗██╗ ██╗
██╔══██╗██╔════╝██║ ██║
██║ ██║█████╗ ██║ ██║
██║ ██║██╔══╝ ╚██╗ ██╔╝
██████╔╝███████╗ ╚████╔╝
╚═════╝ ╚══════╝ ╚═══╝
6 changes: 6 additions & 0 deletions assets/prod.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
██████╗ ██████╗ ██████╗ ██████╗
██╔══██╗██╔══██╗██╔═══██╗██╔══██╗
██████╔╝██████╔╝██║ ██║██║ ██║
██╔═══╝ ██╔══██╗██║ ██║██║ ██║
██║ ██║ ██║╚██████╔╝██████╔╝
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝
6 changes: 6 additions & 0 deletions assets/rc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
██████╗ ██████╗
██╔══██╗██╔════╝
██████╔╝██║
██╔══██╗██║
██║ ██║╚██████╗
╚═╝ ╚═╝ ╚═════╝
6 changes: 6 additions & 0 deletions assets/sandbox.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
██████╗██████╗ ██╗ ██╗
██╔════╝██╔══██╗╚██╗██╔╝
███████╗██████╔╝ ╚███╔╝
╚════██║██╔══██╗ ██╔██╗
██████╔╝██████╔╝██╔╝ ██╗
╚═════╝ ╚═════╝ ╚═╝ ╚═╝
3 changes: 2 additions & 1 deletion internal/config/json/schemas/skin.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"properties": {
"fgColor": {"type": "string"},
"bgColor": {"type": "string"},
"logoColor": {"type": "string"}
"logoColor": {"type": "string"},
"logoUrl": {"type": "string"}
}
},
"prompt": {
Expand Down
3 changes: 3 additions & 0 deletions internal/config/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@

// Body tracks body styles.
Body struct {
FgColor Color `json:"fgColor" yaml:"fgColor"`

Check failure on line 87 in internal/config/styles.go

View workflow job for this annotation

GitHub Actions / golangci

File is not properly formatted (gofmt)
BgColor Color `json:"bgColor" yaml:"bgColor"`
LogoColor Color `json:"logoColor" yaml:"logoColor"`
LogoColorMsg Color `json:"logoColorMsg" yaml:"logoColorMsg"`
LogoColorInfo Color `json:"logoColorInfo" yaml:"logoColorInfo"`
LogoColorWarn Color `json:"logoColorWarn" yaml:"logoColorWarn"`
LogoColorError Color `json:"logoColorError" yaml:"logoColorError"`
LogoUrl string `json:"logoUrl" yaml:"logoUrl"`
}

// Dialog tracks dialog styles.
Expand Down Expand Up @@ -329,6 +330,7 @@
LogoColorInfo: "green",
LogoColorWarn: "mediumvioletred",
LogoColorError: "red",
LogoUrl: "",
}
}

Expand Down Expand Up @@ -554,6 +556,7 @@

// Load K9s configuration from file.
func (s *Styles) Load(path string) error {
s.K9s.Body.LogoUrl = ""
bb, err := os.ReadFile(path)
if err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion internal/ui/logo.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (l *Logo) StylesChanged(s *config.Styles) {
l.SetBackgroundColor(l.styles.BgColor())
l.status.SetBackgroundColor(l.styles.BgColor())
l.logo.SetBackgroundColor(l.styles.BgColor())
l.updateLogo()
l.refreshLogo(l.styles.Body().LogoColor)
}

Expand Down Expand Up @@ -112,11 +113,15 @@ func (l *Logo) refreshLogo(c config.Color) {
}
}

func (l *Logo) updateLogo() {
GetLogo(l.styles.Body().LogoUrl)
}

func logo() *tview.TextView {
v := tview.NewTextView()
v.SetWordWrap(false)
v.SetWrap(false)
v.SetTextAlign(tview.AlignLeft)
v.SetTextAlign(tview.AlignCenter)
v.SetDynamicColors(true)

return v
Expand Down
85 changes: 77 additions & 8 deletions internal/ui/splash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@

import (
"fmt"
"io"
"log/slog"
"net/http"
"os"
"strings"

"github.com/derailed/k9s/internal/config"
"github.com/derailed/tview"
)

// LogoSmall K9s small log.
var LogoSmall = []string{
` ____ __ ________ `,
`| |/ / __ \______`,
`| /\____ / ___/`,
`| \ \ / /\___ \`,
`|____|\__ \/____//____ /`,
` \/ \/ `,
}
var LogoSmall []string


Check failure on line 21 in internal/ui/splash.go

View workflow job for this annotation

GitHub Actions / golangci

File is not properly formatted (gofmt)
// LogoBig K9s big logo for splash page.
var LogoBig = []string{
` ____ __ ________ _______ ____ ___ `,
Expand Down Expand Up @@ -69,3 +67,74 @@
func (s *Splash) layoutRev(t *tview.TextView, rev string, styles *config.Styles) {
fmt.Fprintf(t, "[%s::b]Revision [red::b]%s", styles.Body().FgColor, rev)
}

// function to get the logo []string from the LogoUrl
// by making a request to the LogoUrl
func GetLogo(logoUrl string) {
slog.Debug("Fetching logo from URL", slog.String("url", logoUrl))

Check failure on line 74 in internal/ui/splash.go

View workflow job for this annotation

GitHub Actions / golangci

attributes should not be used (sloglint)
defaultLogo := []string{
` ____ __ ________ `,
`| |/ / __ \______`,
`| /\____ / ___/`,
`| \ \ / /\___ \`,
`|____|\__ \/____//____ /`,
` \/ \/ `,
}

if logoUrl == "" {
LogoSmall = defaultLogo
return
}

if strings.HasPrefix(logoUrl, "file:") {
filePath := strings.TrimPrefix(logoUrl, "file:")
body, err := os.ReadFile(filePath)
if err != nil {
slog.Error("Error reading logo from file", slog.String("file", filePath), slog.Any("error", err))

Check failure on line 93 in internal/ui/splash.go

View workflow job for this annotation

GitHub Actions / golangci

attributes should not be used (sloglint)
LogoSmall = defaultLogo
return
}
logo := strings.Split(string(body), "\n")
// last line is empty, remove it
if len(logo) > 0 && logo[len(logo)-1] == "" {
logo = logo[:len(logo)-1]
}
slog.Debug("Successfully fetched logo from file", slog.String("file", filePath))

Check failure on line 102 in internal/ui/splash.go

View workflow job for this annotation

GitHub Actions / golangci

attributes should not be used (sloglint)
LogoSmall = logo
return
}

resp, err := http.Get(logoUrl)
if err != nil {
slog.Error("Error fetching logo from URL", slog.String("url", logoUrl), slog.Any("error", err))
LogoSmall = defaultLogo
return
}
defer func() {
if resp != nil {
resp.Body.Close()
}
}()

if resp.StatusCode != http.StatusOK {
slog.Error("Non-OK HTTP status", slog.String("status", resp.Status))
LogoSmall = defaultLogo
return
}

body, err := io.ReadAll(resp.Body)
if err != nil {
slog.Error("Error reading response body", slog.Any("error", err))
LogoSmall = defaultLogo
return
}

logo := strings.Split(string(body), "\n")
// last line is empty, remove it
if len(logo) > 0 && logo[len(logo)-1] == "" {
logo = logo[:len(logo)-1]
}
slog.Debug("Successfully fetched logo from URL", slog.String("url", logoUrl))
LogoSmall = logo
}

2 changes: 1 addition & 1 deletion internal/view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (a *App) buildHeader() tview.Primitive {
header.AddItem(a.Menu(), 0, 1, false)

if a.showLogo {
header.AddItem(a.Logo(), 26, 1, false)
header.AddItem(a.Logo(), 40, 1, false)
}

return header
Expand Down
Loading