From 2aa6f72161f852fd86a4a5e168cc7dc2cb5cb088 Mon Sep 17 00:00:00 2001 From: foglar Date: Thu, 21 Aug 2025 13:05:55 +0200 Subject: [PATCH] cleanup --- internal/tui/chat.go | 2 +- internal/tui/colors.go | 121 +++++++++++++++++++++++++++------------ internal/tui/messages.go | 8 ++- internal/tui/sidebar.go | 4 +- 4 files changed, 95 insertions(+), 40 deletions(-) diff --git a/internal/tui/chat.go b/internal/tui/chat.go index 25e7877..229ec0a 100644 --- a/internal/tui/chat.go +++ b/internal/tui/chat.go @@ -66,7 +66,7 @@ func sendMessage(g *gocui.Gui, v *gocui.View) error { v.SetOrigin(0, 0) if input != "" { - messages = append(messages, "\t\t\t\tYou:\n\t\t\t\t"+input) + messages = append(messages, "\t\t\t\t\tYou:\n\t\t\t\t\t"+input) if chatView, err := g.View("chat"); err == nil { updateChatView(chatView) } diff --git a/internal/tui/colors.go b/internal/tui/colors.go index c12604a..ec2b119 100644 --- a/internal/tui/colors.go +++ b/internal/tui/colors.go @@ -1,39 +1,88 @@ package tui -var Colors = struct { - base00 string - base01 string - base02 string - base03 string - base04 string - base05 string - base06 string - base07 string - base08 string - base09 string - base10 string - base11 string - base12 string - base13 string - base14 string - base15 string - base16 string -}{ - base00: "\033[31;7m", - base01: "\033[32;7m", - base02: "\033[33;7m", - base03: "\033[34;7m", - base04: "\033[35;7m", - base05: "\033[36;7m", - base06: "\033[37;7m", - base07: "\033[38;7m", - base08: "\033[39;7m", - base09: "\033[310;7m", - base10: "\033[311;7m", - base11: "\033[312;7m", - base12: "\033[313;7m", - base13: "\033[314;7m", - base14: "\033[315;7m", - base15: "\033[316;7m", - base16: "\033[0m", +import "fmt" + +// Color holds the numeric part of an ANSI sequence (e.g. 31, 32, 313, ...) +type Color struct{ code int } + +func NewColor(code int) Color { return Color{code} } + +// Text returns the basic foreground sequence, e.g. "\033[31m" +func (c Color) Text() string { return fmt.Sprintf("\033[%dm", c.code) } + +// Underline returns the sequence with underline: "\033[31;4m" +func (c Color) Underline() string { return fmt.Sprintf("\033[%d;4m", c.code) } + +// Blink returns the sequence with blink: "\033[31;5m" (kept for completeness) +func (c Color) Blink() string { return fmt.Sprintf("\033[%d;5m", c.code) } + +// Inverse returns the sequence with inverse/swap: "\033[31;7m" +// (this matches your original usage of ;7m as "background"/inverse) +func (c Color) Inverse() string { return fmt.Sprintf("\033[%d;7m", c.code) } + +// Bg returns a real background color sequence (code + 10 -> 40..47 etc). +// e.g. if code==31 -> Bg() -> "\033[41m" +func (c Color) Bg() string { return fmt.Sprintf("\033[%dm", c.code+10) } + +// Attr lets you compose any extra attribute numbers (e.g. 1 for bold) +func (c Color) Attr(attrs ...int) string { + if len(attrs) == 0 { + return c.Text() + } + s := fmt.Sprintf("\033[%d", c.code) + for _, a := range attrs { + s += fmt.Sprintf(";%d", a) + } + s += "m" + return s +} + +// String implements fmt.Stringer (defaults to Text()) +func (c Color) String() string { return c.Text() } + +type Palette struct { + Base00 Color + Base01 Color + Base02 Color + Base03 Color + Base04 Color + Base05 Color + Base06 Color + Base07 Color + Base08 Color + Base09 Color + Base10 Color + Base11 Color + Base12 Color + Base13 Color + Base14 Color + Base15 Color + Reset string // usually reset +} + +// Helper methods on the palette for convenience: +func (p Palette) Background(c Color) string { return c.Inverse() } // keeps your current ;7m semantics +func (p Palette) Underlined(c Color) string { return c.Underline() } // convenience +func (p Palette) Text(c Color) string { return c.Text() } + +var Colors = Palette{ + // I kept the numeric values that you originally used in your map so behavior stays the same. + // If you'd rather map them to "standard" 30..37 and 90..97 codes, change the integers here. + Base00: NewColor(31), + Base01: NewColor(32), + Base02: NewColor(33), + Base03: NewColor(34), + Base04: NewColor(35), + Base05: NewColor(36), + Base06: NewColor(37), + Base07: NewColor(38), + Base08: NewColor(39), + Base09: NewColor(310), + Base10: NewColor(311), + Base11: NewColor(312), + Base12: NewColor(313), + Base13: NewColor(314), + Base14: NewColor(315), + Base15: NewColor(316), + Reset: "\033[0m", // reset } diff --git a/internal/tui/messages.go b/internal/tui/messages.go index d112e61..26342a7 100644 --- a/internal/tui/messages.go +++ b/internal/tui/messages.go @@ -23,6 +23,7 @@ type ChatData struct { } func LoadContacts(path string) { + users = nil contactsPath := filepath.Join(path, "users") folders, err := os.ReadDir(contactsPath) if err != nil { @@ -30,6 +31,7 @@ func LoadContacts(path string) { return } + // TODO: Instead of just using list create some more complex structure so i can load details about the user in it for _, folder := range folders { if folder.IsDir() { users = append(users, folder.Name()) @@ -64,10 +66,12 @@ func LoadMessages(username string) { t, _ := time.Parse(time.RFC3339, msg.Timestamp) formattedTime := t.Format("2006-01-02 15:04") + // TODO: Move this part to the rendering chat file (./chat.go) and here i should only load all messages and related data to that + // TODO: And in the chat file i should get all data in nice structure and then just select what i need from that if !strings.EqualFold(msg.Sender, username) { - messages = append(messages, "\t\t\t\tYou ("+formattedTime+"):\n\t\t\t\t"+string(decoded)) + messages = append(messages, "\t\t\t\t\t"+Colors.Text(Colors.Base02)+"You ("+formattedTime+"):"+Colors.Reset+"\n\t\t\t\t\t"+string(decoded)) } else { - messages = append(messages, "\t\t\t\t"+msg.Sender+" ("+formattedTime+"):\n\t\t\t\t"+string(decoded)) + messages = append(messages, "\t\t\t\t\t"+Colors.Text(Colors.Base05)+msg.Sender+" ("+formattedTime+"):\n"+Colors.Reset+"\t\t\t\t\t"+string(decoded)) } } } diff --git a/internal/tui/sidebar.go b/internal/tui/sidebar.go index d8e9205..3ff5df0 100644 --- a/internal/tui/sidebar.go +++ b/internal/tui/sidebar.go @@ -30,6 +30,8 @@ func updateUsersView(g *gocui.Gui) error { v.Clear() messages = nil + + // TODO: If no contacts then error, create some add contacts window or hello to WhspBrd LoadMessages(users[selectedUserIdx]) // TODO: Render profile image of users and change colors of each user maybe? @@ -37,7 +39,7 @@ func updateUsersView(g *gocui.Gui) error { // Change Selected User In The TUI Window if i == selectedUserIdx { - fmt.Fprintf(v, "%s%s%s\n", Colors.base06, u, Colors.base16) + fmt.Fprintf(v, "%s%s%s\n", Colors.Background(Colors.Base06), u, Colors.Reset) _, y := v.Size() if i == 0 { v.SetOrigin(0, 0)