Path cleanup.
This commit is contained in:
48
notmuch/notmuch.go
Normal file
48
notmuch/notmuch.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package notmuch
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const (
|
||||
notmuchCmd = "notmuch"
|
||||
unreadThreadCountArgs = "count --output=threads tag:unread"
|
||||
unreadMessageCountArgs = "count --output=messages tag:unread"
|
||||
)
|
||||
|
||||
type Notmuch struct {
|
||||
}
|
||||
|
||||
func NewNotmuch(_ string) *Notmuch {
|
||||
return &Notmuch{}
|
||||
}
|
||||
|
||||
func notmuchMust(options []string) string {
|
||||
cmd := exec.Command(notmuchCmd, options...)
|
||||
b, err := cmd.Output()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read from process %v: %s\n", cmd, err)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func intFromCommand(options string) int {
|
||||
output := notmuchMust(strings.Fields(options))
|
||||
cnt, err := strconv.Atoi(strings.TrimSpace(output))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse int from %q: %s\n", output, err)
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
func (nm *Notmuch) UnreadMessageCount() int {
|
||||
return intFromCommand(unreadMessageCountArgs)
|
||||
}
|
||||
|
||||
func (nm *Notmuch) UnreadThreadCount() int {
|
||||
return intFromCommand(unreadThreadCountArgs)
|
||||
}
|
||||
21
notmuch/notmuch_test.go
Normal file
21
notmuch/notmuch_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package notmuch
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnreadMessageCount(t *testing.T) {
|
||||
nm := NewNotmuch("")
|
||||
count := nm.UnreadMessageCount()
|
||||
if count <= 0 {
|
||||
t.Errorf("Count wasn't enough, expected > 0, got %d\n", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnreadThreadCount(t *testing.T) {
|
||||
nm := NewNotmuch("")
|
||||
count := nm.UnreadThreadCount()
|
||||
if count <= 0 {
|
||||
t.Errorf("Count wasn't enough, expected > 0, got %d\n", count)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user