Skip to content
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
11 changes: 9 additions & 2 deletions internal/cmd/gmail_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ type GmailMessagesSearchCmd struct {
Timezone string `name:"timezone" short:"z" help:"Output timezone (IANA name, e.g. America/New_York, UTC). Default: local"`
Local bool `name:"local" help:"Use local timezone (default behavior, useful to override --timezone)"`
IncludeBody bool `name:"include-body" help:"Include decoded message body (JSON is full; text output is truncated)"`
Full bool `name:"full" help:"Show full message bodies without truncation (implies --include-body)"`
}

func (c *GmailMessagesSearchCmd) Run(ctx context.Context, flags *RootFlags) error {
if c.Full {
c.IncludeBody = true
}
u := ui.FromContext(ctx)
account, err := requireAccount(flags)
if err != nil {
Expand Down Expand Up @@ -137,7 +141,7 @@ func (c *GmailMessagesSearchCmd) Run(ctx context.Context, flags *RootFlags) erro
for _, it := range items {
body := ""
if c.IncludeBody {
body = sanitizeMessageBody(it.Body)
body = sanitizeMessageBody(it.Body, c.Full)
}
if c.IncludeBody {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", it.ID, it.ThreadID, it.Date, it.From, it.Subject, strings.Join(it.Labels, ","), body)
Expand Down Expand Up @@ -327,7 +331,7 @@ func fetchMessageDetails(ctx context.Context, svc *gmail.Service, messages []*gm
return items, nil
}

func sanitizeMessageBody(body string) string {
func sanitizeMessageBody(body string, full bool) string {
if body == "" {
return ""
}
Expand All @@ -338,6 +342,9 @@ func sanitizeMessageBody(body string) string {
body = strings.ReplaceAll(body, "\n", " ")
body = strings.ReplaceAll(body, "\r", " ")
body = strings.TrimSpace(body)
if full {
return body
}
return truncateRunes(body, 200)
}

Expand Down
15 changes: 13 additions & 2 deletions internal/cmd/gmail_messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestSanitizeMessageBody_TruncateUTF8(t *testing.T) {
long := strings.Repeat("€", 210)
got := sanitizeMessageBody(long)
got := sanitizeMessageBody(long, false)
if !strings.HasSuffix(got, "...") {
t.Fatalf("expected truncation suffix, got %q", got)
}
Expand All @@ -24,8 +24,19 @@ func TestSanitizeMessageBody_TruncateUTF8(t *testing.T) {
}
}

func TestSanitizeMessageBody_FullSkipsTruncation(t *testing.T) {
long := strings.Repeat("€", 210)
got := sanitizeMessageBody(long, true)
if strings.HasSuffix(got, "...") {
t.Fatalf("expected no truncation with full=true, got %q", got)
}
if len([]rune(got)) != 210 {
t.Fatalf("expected 210 runes, got %d", len([]rune(got)))
}
}

func TestSanitizeMessageBody_StripsHTML(t *testing.T) {
got := sanitizeMessageBody("<html><body>Hi</body></html>")
got := sanitizeMessageBody("<html><body>Hi</body></html>", false)
if got != "Hi" {
t.Fatalf("unexpected sanitized body: %q", got)
}
Expand Down