fix: resolve cyclop, funlen issues by extracting helper methods

This commit is contained in:
user
2026-02-20 03:29:18 -08:00
parent 037202280b
commit 2f6d1f284c
5 changed files with 263 additions and 248 deletions

View File

@@ -97,6 +97,24 @@ func (a *App) handleInput(text string) {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, nick, text))
}
func (a *App) commandHandlers() map[string]func(string) {
return map[string]func(string){
"/connect": a.cmdConnect,
"/nick": a.cmdNick,
"/join": a.cmdJoin,
"/part": a.cmdPart,
"/msg": a.cmdMsg,
"/query": a.cmdQuery,
"/topic": a.cmdTopic,
"/names": func(_ string) { a.cmdNames() },
"/list": func(_ string) { a.cmdList() },
"/window": a.cmdWindow,
"/w": a.cmdWindow,
"/quit": func(_ string) { a.cmdQuit() },
"/help": func(_ string) { a.cmdHelp() },
}
}
func (a *App) handleCommand(text string) {
parts := strings.SplitN(text, " ", splitParts)
cmd := strings.ToLower(parts[0])
@@ -106,32 +124,9 @@ func (a *App) handleCommand(text string) {
args = parts[1]
}
switch cmd {
case "/connect":
a.cmdConnect(args)
case "/nick":
a.cmdNick(args)
case "/join":
a.cmdJoin(args)
case "/part":
a.cmdPart(args)
case "/msg":
a.cmdMsg(args)
case "/query":
a.cmdQuery(args)
case "/topic":
a.cmdTopic(args)
case "/names":
a.cmdNames()
case "/list":
a.cmdList()
case "/window", "/w":
a.cmdWindow(args)
case "/quit":
a.cmdQuit()
case "/help":
a.cmdHelp()
default:
if handler, ok := a.commandHandlers()[cmd]; ok {
handler(args)
} else {
a.ui.AddStatus("[red]Unknown command: " + cmd)
}
}
@@ -543,105 +538,123 @@ func (a *App) pollLoop() {
}
}
func (a *App) handleServerMessage(msg *api.Message) {
var ts string
func (a *App) messageTimestamp(msg *api.Message) string {
if msg.TS != "" {
t := msg.ParseTS()
ts = t.Local().Format("15:04") //nolint:gosmopolitan // CLI displays local time intentionally
} else {
ts = time.Now().Format("15:04")
return t.Local().Format("15:04") //nolint:gosmopolitan // CLI displays local time intentionally
}
return time.Now().Format("15:04")
}
func (a *App) handleServerMessage(msg *api.Message) {
ts := a.messageTimestamp(msg)
a.mu.Lock()
myNick := a.nick
a.mu.Unlock()
switch msg.Command {
case "PRIVMSG":
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if msg.From == myNick {
// Skip our own echoed messages (already displayed locally).
return
}
target := msg.To
if !strings.HasPrefix(target, "#") {
// DM — use sender's nick as buffer name.
target = msg.From
}
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, msg.From, text))
a.handleMsgPrivmsg(msg, ts, myNick)
case "JOIN":
target := msg.To
if target != "" {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has joined %s", ts, msg.From, target))
}
a.handleMsgJoin(msg, ts)
case "PART":
target := msg.To
lines := msg.BodyLines()
reason := strings.Join(lines, " ")
if target != "" {
if reason != "" {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s (%s)", ts, msg.From, target, reason))
} else {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s", ts, msg.From, target))
}
}
a.handleMsgPart(msg, ts)
case "QUIT":
lines := msg.BodyLines()
reason := strings.Join(lines, " ")
if reason != "" {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit (%s)", ts, msg.From, reason))
} else {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit", ts, msg.From))
}
a.handleMsgQuit(msg, ts)
case "NICK":
lines := msg.BodyLines()
newNick := ""
if len(lines) > 0 {
newNick = lines[0]
}
if msg.From == myNick && newNick != "" {
a.mu.Lock()
a.nick = newNick
target := a.target
a.mu.Unlock()
a.ui.SetStatus(newNick, target, "connected")
}
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s is now known as %s", ts, msg.From, newNick))
a.handleMsgNick(msg, ts, myNick)
case "NOTICE":
lines := msg.BodyLines()
text := strings.Join(lines, " ")
a.ui.AddStatus(fmt.Sprintf("[gray]%s [magenta]--%s-- %s", ts, msg.From, text))
a.handleMsgNotice(msg, ts)
case "TOPIC":
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if msg.To != "" {
a.ui.AddLine(msg.To, fmt.Sprintf("[gray]%s [cyan]*** %s set topic: %s", ts, msg.From, text))
}
a.handleMsgTopic(msg, ts)
default:
// Numeric replies and other messages → status window.
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if text != "" {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [white][%s] %s", ts, msg.Command, text))
}
a.handleMsgDefault(msg, ts)
}
}
func (a *App) handleMsgPrivmsg(msg *api.Message, ts, myNick string) {
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if msg.From == myNick {
return
}
target := msg.To
if !strings.HasPrefix(target, "#") {
target = msg.From
}
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, msg.From, text))
}
func (a *App) handleMsgJoin(msg *api.Message, ts string) {
if msg.To != "" {
a.ui.AddLine(msg.To, fmt.Sprintf("[gray]%s [yellow]*** %s has joined %s", ts, msg.From, msg.To))
}
}
func (a *App) handleMsgPart(msg *api.Message, ts string) {
target := msg.To
reason := strings.Join(msg.BodyLines(), " ")
if target == "" {
return
}
if reason != "" {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s (%s)", ts, msg.From, target, reason))
} else {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s", ts, msg.From, target))
}
}
func (a *App) handleMsgQuit(msg *api.Message, ts string) {
reason := strings.Join(msg.BodyLines(), " ")
if reason != "" {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit (%s)", ts, msg.From, reason))
} else {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit", ts, msg.From))
}
}
func (a *App) handleMsgNick(msg *api.Message, ts, myNick string) {
lines := msg.BodyLines()
newNick := ""
if len(lines) > 0 {
newNick = lines[0]
}
if msg.From == myNick && newNick != "" {
a.mu.Lock()
a.nick = newNick
target := a.target
a.mu.Unlock()
a.ui.SetStatus(newNick, target, "connected")
}
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s is now known as %s", ts, msg.From, newNick))
}
func (a *App) handleMsgNotice(msg *api.Message, ts string) {
text := strings.Join(msg.BodyLines(), " ")
a.ui.AddStatus(fmt.Sprintf("[gray]%s [magenta]--%s-- %s", ts, msg.From, text))
}
func (a *App) handleMsgTopic(msg *api.Message, ts string) {
text := strings.Join(msg.BodyLines(), " ")
if msg.To != "" {
a.ui.AddLine(msg.To, fmt.Sprintf("[gray]%s [cyan]*** %s set topic: %s", ts, msg.From, text))
}
}
func (a *App) handleMsgDefault(msg *api.Message, ts string) {
text := strings.Join(msg.BodyLines(), " ")
if text != "" {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [white][%s] %s", ts, msg.Command, text))
}
}