Skip to content

Commit

Permalink
don' report new updates if the client tag is greater than the server …
Browse files Browse the repository at this point in the history
…motd tag

re: DisposaBoy/GoSublime#964
  • Loading branch information
DisposaBoy committed Mar 9, 2020
1 parent 5db2eb4 commit f5a074a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion extension-example/extension-example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Margo(m mg.Args) {
m.Use(
// MOTD keeps you updated about new versions and important announcements
//
// It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD`
// It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD (check for updates)`
//
// Interval can be set in order to enable automatic update fetching.
//
Expand Down
27 changes: 24 additions & 3 deletions mg/motd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type motdState struct {

// MOTD keeps you updated about new versions and important announcements
//
// It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD`
// It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD (check for updates)`
//
// Interval can be set in order to enable automatic update fetching.
//
Expand Down Expand Up @@ -96,7 +96,7 @@ func (m *MOTD) Reduce(mx *Ctx) *State {
case RunCmd:
st = st.AddBuiltinCmds(BuiltinCmd{Name: "motd.sync", Run: m.motdSyncCmd})
case QueryUserCmds:
st = st.AddUserCmds(UserCmd{Title: "Sync MOTD", Name: "motd.sync"})
st = st.AddUserCmds(UserCmd{Title: "Sync MOTD (check for updates)", Name: "motd.sync"})
case motdAct:
m.msg = act.msg
}
Expand Down Expand Up @@ -179,14 +179,35 @@ func (m *MOTD) sync(mx *Ctx) error {
return nil
}

func (_ *MOTD) fmtTag(s string) (string, error) {
var y, m, d, n int
scanned, err := fmt.Sscanf(s, "%02d.%02d.%02d-%d", &y, &m, &d, &n)
if scanned < 4 {
_, err = fmt.Sscanf(s, "%02d.%02d.%02d", &y, &m, &d)
}
if err != nil {
return "", err
}
if n <= 0 {
n = 1
}
return fmt.Sprintf("%02d.%02d.%02d-%d", y, m, d, n), nil
}

func (m *MOTD) dispatchMsg(mx *Ctx, ms motdState) {
res := ms.Result
act := motdAct{}
ctag := mx.Editor.Client.Tag
srvTag, srvTagErr := m.fmtTag(res.Tag)
cliTag, cliTagErr := m.fmtTag(ctag)
switch {
case ctag == "":
mx.Log.Println("motd: client tag is undefined; you might need to restart the editor")
case res.Tag != ctag:
case srvTagErr != nil:
mx.Log.Println("motd: cannot parse ser`ver tag:", srvTagErr)
case cliTagErr != nil:
mx.Log.Println("motd: cannot parse client tag:", cliTagErr)
case cliTag < srvTag:
act.msg = res.Message
}
mx.Store.Dispatch(act)
Expand Down

0 comments on commit f5a074a

Please sign in to comment.