Skip to content

Commit

Permalink
feat: replace slog with charmbracelet/log
Browse files Browse the repository at this point in the history
  • Loading branch information
ayn2op committed Dec 12, 2024
1 parent a278c91 commit a5c5540
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 69 deletions.
15 changes: 8 additions & 7 deletions cmd/guilds_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"fmt"
"log/slog"
"sort"
"strings"

"github.com/ayn2op/discordo/internal/config"
"github.com/charmbracelet/log"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (gt *GuildsTree) createFolderNode(folder gateway.GuildFolder) {
for _, gID := range folder.GuildIDs {
g, err := discordState.Cabinet.Guild(gID)
if err != nil {
slog.Info("guild not found in state", "err", err, "guild", g)
log.Error("failed to get guild from state", "guild_id", gID, "err", err)
continue
}

Expand Down Expand Up @@ -110,9 +110,10 @@ func (gt *GuildsTree) channelToString(c discord.Channel) string {

func (gt *GuildsTree) createChannelNode(n *tview.TreeNode, c discord.Channel) *tview.TreeNode {
if c.Type != discord.DirectMessage && c.Type != discord.GroupDM {
ps, err := discordState.Permissions(c.ID, discordState.Ready().User.ID)
userID := discordState.Ready().User.ID
ps, err := discordState.Permissions(c.ID, userID)
if err != nil {
slog.Error("failed to get permissions", "err", err, "channel_id", c.ID)
log.Error("failed to get permissions for user in channel", "user_id", userID, "channel_id", c.ID, "err", err)
return nil
}

Expand Down Expand Up @@ -186,7 +187,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
case discord.GuildID:
cs, err := discordState.Cabinet.Channels(ref)
if err != nil {
slog.Error("failed to get channels", "err", err, "guild_id", ref)
log.Error("failed to get channels in the guild", "guild_id", ref, "err", err)
return
}

Expand All @@ -201,7 +202,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {

c, err := discordState.Cabinet.Channel(ref)
if err != nil {
slog.Error("failed to get channel", "channel_id", ref)
log.Error("channel not found in state", "channel_id", ref, "err", err)
return
}

Expand All @@ -212,7 +213,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
case nil: // Direct messages
cs, err := discordState.PrivateChannels()
if err != nil {
slog.Error("failed to get private channels", "err", err)
log.Error("failed to get private channels", "err", err)
return
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/layout.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"log/slog"

"github.com/ayn2op/discordo/internal/config"
"github.com/charmbracelet/log"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/zalando/go-keyring"
Expand Down Expand Up @@ -43,12 +42,13 @@ func (l *Layout) show(token string) error {
if token == "" {
loginForm := newLoginForm(func(token string, err error) {
if err != nil {
slog.Error("failed to login", "err", err)
log.Error(err)
return
}

if err := l.show(token); err != nil {
slog.Error("failed to show app", "err", err)
log.Error("failed to show app", "err", err)
return
}
}, l.cfg)
l.app.SetRoot(loginForm, true)
Expand Down Expand Up @@ -110,7 +110,7 @@ func (l *Layout) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey {
l.app.Stop()

if err := keyring.Delete(config.Name, "token"); err != nil {
slog.Error("failed to delete token from keyring", "err", err)
log.Error("failed to delete token from keyring", "err", err)
return nil
}

Expand Down
31 changes: 15 additions & 16 deletions cmd/message_input.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cmd

import (
"log/slog"
"os"
"os/exec"
"strings"

"github.com/atotto/clipboard"
"github.com/ayn2op/discordo/internal/config"
"github.com/charmbracelet/log"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/utils/json/option"
Expand Down Expand Up @@ -85,30 +85,29 @@ func (mi *MessageInput) send() {
return
}

data := api.SendMessageData{
Content: text,
}
if mi.replyMessageID != 0 {
data := api.SendMessageData{
Content: text,
Reference: &discord.MessageReference{MessageID: mi.replyMessageID},
AllowedMentions: &api.AllowedMentions{RepliedUser: option.False},
}
data.Reference = &discord.MessageReference{MessageID: mi.replyMessageID}
data.AllowedMentions = &api.AllowedMentions{RepliedUser: option.False}

if strings.HasPrefix(mi.GetTitle(), "[@]") {
data.AllowedMentions.RepliedUser = option.True
}

go func() {
if _, err := discordState.SendMessageComplex(layout.guildsTree.selectedChannelID, data); err != nil {
slog.Error("failed to send message", "err", err)
}
}()
} else {
go func() {
if _, err := discordState.SendMessage(layout.guildsTree.selectedChannelID, text); err != nil {
slog.Error("failed to send message", "err", err)
}
}()
}

go func() {
if _, err := discordState.SendMessageComplex(layout.guildsTree.selectedChannelID, data); err != nil {
log.Error("failed to send message in channel", "channel_id", layout.guildsTree.selectedChannelID, "err", err)
}
}()

mi.replyMessageID = 0
mi.reset()

Expand All @@ -124,7 +123,7 @@ func (mi *MessageInput) editor() {

f, err := os.CreateTemp("", tmpFilePattern)
if err != nil {
slog.Error("failed to create temporary file", "err", err)
log.Error("failed to create temporary file", "err", err)
return
}
_, _ = f.WriteString(mi.GetText())
Expand All @@ -140,14 +139,14 @@ func (mi *MessageInput) editor() {
mi.app.Suspend(func() {
err := cmd.Run()
if err != nil {
slog.Error("failed to run command", "err", err, "command", cmd)
log.Error("failed to run command", "args", cmd.Args, "err", err)
return
}
})

msg, err := os.ReadFile(f.Name())
if err != nil {
slog.Error("failed to read temporary file", "err", err, "name", f.Name())
log.Error("failed to read temporary file", "name", f.Name(), "err", err)
return
}

Expand Down
28 changes: 14 additions & 14 deletions cmd/messages_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"slices"
"strconv"
"time"

"github.com/atotto/clipboard"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/discordo/internal/markdown"
"github.com/charmbracelet/log"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/ningen/v3/discordmd"
"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -68,7 +68,7 @@ func newMessagesText(app *tview.Application, cfg *config.Config) *MessagesText {
func (mt *MessagesText) drawMsgs(cID discord.ChannelID) {
ms, err := discordState.Messages(cID, uint(mt.cfg.MessagesLimit))
if err != nil {
slog.Error("failed to get messages", "err", err, "channel_id", cID)
log.Error("failed to get messages in channel", "channel_id", cID, "err", err)
return
}

Expand Down Expand Up @@ -223,13 +223,13 @@ func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
func (mt *MessagesText) _select(name string) {
ms, err := discordState.Cabinet.Messages(layout.guildsTree.selectedChannelID)
if err != nil {
slog.Error("failed to get messages", "err", err, "channel_id", layout.guildsTree.selectedChannelID)
log.Error("failed to get messages in channel", "channel_id", layout.guildsTree.selectedChannelID, "err", err)
return
}

messageIdx, err := mt.getSelectedMessageIndex()
if err != nil {
slog.Error("failed to get selected message", "err", err)
log.Error("failed to get selected message", "err", err)
return
}

Expand Down Expand Up @@ -290,7 +290,7 @@ func (mt *MessagesText) onHighlighted(added, removed, remaining []string) {
if len(added) > 0 {
mID, err := strconv.ParseInt(added[0], 10, 64)
if err != nil {
slog.Error("Failed to parse region id as int to use as message id.", "err", err)
log.Error("failed to parse region ID", "id", added[0], "err", err)
return
}
mt.selectedMessageID = discord.MessageID(mID)
Expand All @@ -300,21 +300,21 @@ func (mt *MessagesText) onHighlighted(added, removed, remaining []string) {
func (mt *MessagesText) yank() {
msg, err := mt.getSelectedMessage()
if err != nil {
slog.Error("failed to get selected message", "err", err)
log.Error("failed to get selected message", "err", err)
return
}

err = clipboard.WriteAll(msg.Content)
if err != nil {
slog.Error("failed to write to clipboard", "err", err)
log.Error("failed to write to clipboard", "err", err)
return
}
}

func (mt *MessagesText) open() {
msg, err := mt.getSelectedMessage()
if err != nil {
slog.Error("failed to get selected message", "err", err)
log.Error("failed to get selected message", "err", err)
return
}

Expand All @@ -326,7 +326,7 @@ func (mt *MessagesText) open() {
for _, a := range attachments {
go func() {
if err := open.Start(a.URL); err != nil {
slog.Error("failed to open URL", "err", err, "url", a.URL)
log.Error("failed to open url", "content_type", a.ContentType, "url", a.URL, "err", err)
}
}()
}
Expand All @@ -342,7 +342,7 @@ func (mt *MessagesText) reply(mention bool) {

msg, err := mt.getSelectedMessage()
if err != nil {
slog.Error("failed to get selected message", "err", err)
log.Error("failed to get selected message", "err", err)
return
}

Expand All @@ -355,7 +355,7 @@ func (mt *MessagesText) reply(mention bool) {
func (mt *MessagesText) delete() {
msg, err := mt.getSelectedMessage()
if err != nil {
slog.Error("failed to get selected message", "err", err)
log.Error("failed to get selected message", "err", err)
return
}

Expand All @@ -376,18 +376,18 @@ func (mt *MessagesText) delete() {
}

if err := discordState.DeleteMessage(layout.guildsTree.selectedChannelID, msg.ID, ""); err != nil {
slog.Error("failed to delete message", "err", err, "channel_id", layout.guildsTree.selectedChannelID, "message_id", msg.ID)
log.Error("failed to delete message in channel", "channel_id", layout.guildsTree.selectedChannelID, "message_id", msg.ID, "err", err)
return
}

if err := discordState.MessageRemove(layout.guildsTree.selectedChannelID, msg.ID); err != nil {
slog.Error("failed to delete message", "err", err, "channel_id", layout.guildsTree.selectedChannelID, "message_id", msg.ID)
log.Error("failed to delete message in channel", "channel_id", layout.guildsTree.selectedChannelID, "message_id", msg.ID, "err", err)
return
}

ms, err := discordState.Cabinet.Messages(layout.guildsTree.selectedChannelID)
if err != nil {
slog.Error("failed to delete message", "err", err, "channel_id", layout.guildsTree.selectedChannelID)
log.Error("failed to get messages in channel", "channel_id", layout.guildsTree.selectedChannelID, "err", err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"context"
"log/slog"
"runtime"
"slices"

"github.com/ayn2op/discordo/internal/config"
"github.com/charmbracelet/log"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
Expand Down Expand Up @@ -54,7 +54,7 @@ func openState(token string, app *tview.Application, cfg *config.Config) error {
func (s *State) onRequest(r httpdriver.Request) error {
req, ok := r.(*httpdriver.DefaultRequest)
if ok {
slog.Info("new HTTP request", "method", req.Method, "path", req.URL.Path)
log.Info("new HTTP request", "method", req.Method, "path", req.URL.Path)
}

return nil
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ go 1.23.2
require (
github.com/BurntSushi/toml v1.4.0
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/log v0.4.0
github.com/diamondburned/arikawa/v3 v3.4.0
github.com/diamondburned/ningen/v3 v3.0.1-0.20240808103805-f1a24c0da3d8
github.com/gdamore/tcell/v2 v2.7.4
github.com/lmittmann/tint v1.0.5
github.com/rivo/tview v0.0.0-20241103174730-c76f7879f592
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/yuin/goldmark v1.7.8
Expand All @@ -17,17 +17,24 @@ require (

require (
al.essio.dev/pkg/shellescape v1.5.1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.10.0 // indirect
github.com/danieljoos/wincred v1.2.2 // indirect
github.com/gdamore/encoding v1.0.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gorilla/schema v1.4.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
Loading

0 comments on commit a5c5540

Please sign in to comment.