Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-open configured group windows at startup #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions zkclient/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/companyzero/ttk"
Expand Down Expand Up @@ -46,6 +47,9 @@ type Settings struct {
NickColor string
GcColor string
PmColor string

// auto-open groups
OpenGroups []string
}

func textToColor(in string) (int, error) {
Expand Down Expand Up @@ -287,6 +291,24 @@ func ObtainSettings() (*Settings, error) {
s.PmColor = color
}

groups := cfg.Section("groups")
openGroups := make([]string, len(groups))
for window, group := range groups {
n, err := strconv.Atoi(window)
if err != nil {
return nil, fmt.Errorf("groups: %q: %v", group, err)
}
if n <= 0 || n > len(groups)+1 {
return nil, fmt.Errorf("groups: %q: window %d unusable", group, n)
}
g := &openGroups[n-1]
if *g != "" {
return nil, fmt.Errorf("groups: %q: window %d in-use by %q", group, n, *g)
}
*g = group
}
s.OpenGroups = openGroups

return &s, nil
}

Expand Down
7 changes: 7 additions & 0 deletions zkclient/zkclient.conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,12 @@ profiler = 127.0.0.1:6061
nickcolor = bold:na:na
gcothercolor = bold:green:na
pmothercolor = bold:cyan:na

# Joined groups can be auto-opened in a specified order.
# Each key describes the window index, and the value must be the group name.
# Indexes begin at 1 due to index 0 being reserved for the console window.
[groups]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of the section and use the following notation instead:
group[1] = firstgroup
group[2] = secondgroup

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would complicate the parsing as well as the error handling when indexes are missing, but could be done.

because the ini package decodes into a map, we can't (easily) iterate through the settings in the file order either. using a unique section for only these settings allows us to at least initially know how many k/v fields were set.

I'm also thinking that we should use autoopen as the section name (or top-level 'autoopen[N]' keys) in case we want to distinguish between groups and direct chats in the value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sections in INI are worse than INI itself imo. Really would prefer named arrays of sorts. chat[1] group[3] etc.

# 1 = firstgroup
# 2 = secondgroup
`
)
23 changes: 14 additions & 9 deletions zkclient/zkclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,14 +975,7 @@ func (z *ZKC) goOnline() (*rpc.Welcome, error) {
return nil, err
}

welcome, err := z.welcomePhase(kx)
if err != nil {
return nil, err
}

go z.handleRPC()

return welcome, nil
return z.welcomePhase(kx)
}

//
Expand All @@ -1001,8 +994,20 @@ func (z *ZKC) goOnlineAndPrint() error {
default:
err = z.welcomeUser(welcome)
}
if err != nil {
return err
}

for _, group := range z.settings.OpenGroups {
_, _, err = z.groupConversation(group)
if err != nil {
z.PrintfT(0, "Failed to open group window %q", group)
}
}

go z.handleRPC()

return err
return nil
}

func (z *ZKC) goOnlineRetry() {
Expand Down