From 6c662f3888f56c22a23ca2e2a6a96a757e4832da Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Fri, 17 Sep 2021 00:05:51 +0300 Subject: [PATCH] fix panic when no sessions are running #3 --- kak/kak.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/kak/kak.go b/kak/kak.go index 270ff4b..ec42743 100644 --- a/kak/kak.go +++ b/kak/kak.go @@ -1,9 +1,10 @@ package kak import ( + "bufio" + "bytes" "errors" "os/exec" - "strings" ) type Context struct { @@ -12,9 +13,11 @@ type Context struct { Buffer Buffer } -type Session struct{ Name string } -type Client struct{ Name string } -type Buffer struct{ Name string } +type ( + Client struct{ Name string } + Session struct{ Name string } + Buffer struct{ Name string } +) func (s *Session) Exists() (exists bool, err error) { sessions, err := Sessions() @@ -45,8 +48,11 @@ func (s *Session) Clients() (clients []Client, err error) { func Sessions() (sessions []Session, err error) { o, err := exec.Command("kak", "-l").Output() - for _, s := range strings.Split(strings.TrimSpace(string(o)), "\n") { - sessions = append(sessions, Session{s}) + scanner := bufio.NewScanner(bytes.NewBuffer(o)) + for scanner.Scan() { + if s := scanner.Text(); s != "" { + sessions = append(sessions, Session{s}) + } } return }