This commit is contained in:
foglar 2025-08-21 13:05:55 +02:00
parent 64503b7281
commit 2aa6f72161
4 changed files with 95 additions and 40 deletions

View File

@ -66,7 +66,7 @@ func sendMessage(g *gocui.Gui, v *gocui.View) error {
v.SetOrigin(0, 0) v.SetOrigin(0, 0)
if input != "" { 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 { if chatView, err := g.View("chat"); err == nil {
updateChatView(chatView) updateChatView(chatView)
} }

View File

@ -1,39 +1,88 @@
package tui package tui
var Colors = struct { import "fmt"
base00 string
base01 string // Color holds the numeric part of an ANSI sequence (e.g. 31, 32, 313, ...)
base02 string type Color struct{ code int }
base03 string
base04 string func NewColor(code int) Color { return Color{code} }
base05 string
base06 string // Text returns the basic foreground sequence, e.g. "\033[31m"
base07 string func (c Color) Text() string { return fmt.Sprintf("\033[%dm", c.code) }
base08 string
base09 string // Underline returns the sequence with underline: "\033[31;4m"
base10 string func (c Color) Underline() string { return fmt.Sprintf("\033[%d;4m", c.code) }
base11 string
base12 string // Blink returns the sequence with blink: "\033[31;5m" (kept for completeness)
base13 string func (c Color) Blink() string { return fmt.Sprintf("\033[%d;5m", c.code) }
base14 string
base15 string // Inverse returns the sequence with inverse/swap: "\033[31;7m"
base16 string // (this matches your original usage of ;7m as "background"/inverse)
}{ func (c Color) Inverse() string { return fmt.Sprintf("\033[%d;7m", c.code) }
base00: "\033[31;7m",
base01: "\033[32;7m", // Bg returns a real background color sequence (code + 10 -> 40..47 etc).
base02: "\033[33;7m", // e.g. if code==31 -> Bg() -> "\033[41m"
base03: "\033[34;7m", func (c Color) Bg() string { return fmt.Sprintf("\033[%dm", c.code+10) }
base04: "\033[35;7m",
base05: "\033[36;7m", // Attr lets you compose any extra attribute numbers (e.g. 1 for bold)
base06: "\033[37;7m", func (c Color) Attr(attrs ...int) string {
base07: "\033[38;7m", if len(attrs) == 0 {
base08: "\033[39;7m", return c.Text()
base09: "\033[310;7m", }
base10: "\033[311;7m", s := fmt.Sprintf("\033[%d", c.code)
base11: "\033[312;7m", for _, a := range attrs {
base12: "\033[313;7m", s += fmt.Sprintf(";%d", a)
base13: "\033[314;7m", }
base14: "\033[315;7m", s += "m"
base15: "\033[316;7m", return s
base16: "\033[0m", }
// 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
} }

View File

@ -23,6 +23,7 @@ type ChatData struct {
} }
func LoadContacts(path string) { func LoadContacts(path string) {
users = nil
contactsPath := filepath.Join(path, "users") contactsPath := filepath.Join(path, "users")
folders, err := os.ReadDir(contactsPath) folders, err := os.ReadDir(contactsPath)
if err != nil { if err != nil {
@ -30,6 +31,7 @@ func LoadContacts(path string) {
return 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 { for _, folder := range folders {
if folder.IsDir() { if folder.IsDir() {
users = append(users, folder.Name()) users = append(users, folder.Name())
@ -64,10 +66,12 @@ func LoadMessages(username string) {
t, _ := time.Parse(time.RFC3339, msg.Timestamp) t, _ := time.Parse(time.RFC3339, msg.Timestamp)
formattedTime := t.Format("2006-01-02 15:04") 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) { 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 { } 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))
} }
} }
} }

View File

@ -30,6 +30,8 @@ func updateUsersView(g *gocui.Gui) error {
v.Clear() v.Clear()
messages = nil messages = nil
// TODO: If no contacts then error, create some add contacts window or hello to WhspBrd
LoadMessages(users[selectedUserIdx]) LoadMessages(users[selectedUserIdx])
// TODO: Render profile image of users and change colors of each user maybe? // 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 // Change Selected User In The TUI Window
if i == selectedUserIdx { 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() _, y := v.Size()
if i == 0 { if i == 0 {
v.SetOrigin(0, 0) v.SetOrigin(0, 0)