Skip to content

Commit

Permalink
Unix dialogs implemented ⚓
Browse files Browse the repository at this point in the history
  • Loading branch information
gabyx committed Feb 23, 2021
1 parent 9575885 commit 7e8f2ca
Show file tree
Hide file tree
Showing 18 changed files with 513 additions and 130 deletions.
7 changes: 7 additions & 0 deletions githooks/apps/dialog/cmd/common/env-darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build darwin

package common

const (
LineBreak = "\n"
)
7 changes: 7 additions & 0 deletions githooks/apps/dialog/cmd/common/env-unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !windows,!darwin

package common

const (
LineBreak = "\n"
)
7 changes: 7 additions & 0 deletions githooks/apps/dialog/cmd/common/env-windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build windows

package common

const (
LineBreak = "\r\n"
)
43 changes: 24 additions & 19 deletions githooks/apps/dialog/cmd/common/flags.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
package common

import (
"gabyx/githooks/apps/dialog/gui"
set "gabyx/githooks/apps/dialog/settings"

"github.com/spf13/cobra"
)

func AddFlagsGeneralSettings(cmd *cobra.Command, s *gui.GeneralSettings) {
func addFlagsGeneral(cmd *cobra.Command, s *set.General) {
cmd.Flags().StringVar(&s.Title, "title", "", "Dialog title.")
cmd.Flags().UintVar(&s.Width, "width", 0, "Dialog width.")
cmd.Flags().UintVar(&s.Height, "height", 0, "Dialog height.")
cmd.Flags().UintVar((*uint)(&s.WindowIcon), "window-icon", 0, "Window icon.")
}

func AddFlagsDefaultButtonSettings(cmd *cobra.Command, s *gui.DefaultButtonSettings) {
func addFlagsDefaultButton(cmd *cobra.Command, s *set.DefaultButton) {
cmd.Flags().StringVar(&s.OkLabel, "ok-label", "", "Ok button label.")
cmd.Flags().StringVar(&s.CancelLabel, "cancel-label", "", "Cancel button label.")
cmd.Flags().BoolVar(&s.DefaultCancel, "default-cancel", false, "Set 'Cancel' as the default button.")

cmd.Flags().StringArrayVar(&s.ExtraButtons, "extra-button", nil, "Extra buttons labels.")

}

func AddFlagsGeneralTextSettings(cmd *cobra.Command, s *gui.GeneralTextSettings) {
func addFlagsGeneralText(cmd *cobra.Command, s *set.GeneralText) {
cmd.Flags().StringVar(&s.Text, "text", "", "The general text of the dialog.")
cmd.Flags().BoolVar(&s.NoWrap, "no-wrap", false, "Don't wrap the text.")
cmd.Flags().BoolVar(&s.Ellipsize, "ellipsize", false, "Ellipsize the text if it doesn't fit.")
}

func AddFlagsMessageSettings(cmd *cobra.Command, s *gui.MessageSettings) {
AddFlagsGeneralSettings(cmd, &s.GeneralSettings)
AddFlagsGeneralTextSettings(cmd, &s.GeneralTextSettings)
AddFlagsDefaultButtonSettings(cmd, &s.DefaultButtonSettings)
func AddFlagsMessage(cmd *cobra.Command, s *set.Message) {
addFlagsGeneral(cmd, &s.General)
addFlagsGeneralText(cmd, &s.GeneralText)
addFlagsDefaultButton(cmd, &s.DefaultButton)

cmd.Flags().UintVar((*uint)(&s.Style), "style", 0, "Message style.")
cmd.Flags().UintVar((*uint)(&s.Icon), "icon", 0, "Message icon.")
}

func AddFlagsOptionsSettings(cmd *cobra.Command, s *gui.OptionsSettings) {
AddFlagsGeneralSettings(cmd, &s.GeneralSettings)
AddFlagsGeneralTextSettings(cmd, &s.GeneralTextSettings)
AddFlagsDefaultButtonSettings(cmd, &s.DefaultButtonSettings)
func AddFlagsOptions(cmd *cobra.Command, s *set.Options) {
addFlagsGeneral(cmd, &s.General)
addFlagsGeneralText(cmd, &s.GeneralText)
addFlagsDefaultButton(cmd, &s.DefaultButton)

cmd.Flags().StringArrayVar(&s.Options, "option", nil, "Option choices.")
cmd.Flags().UintVar(&s.DefaultOption, "default-option", 0, "Option choices.")
Expand All @@ -49,11 +48,17 @@ func AddFlagsOptionsSettings(cmd *cobra.Command, s *gui.OptionsSettings) {
cmd.Flags().BoolVar(&s.MultipleSelection, "multiple-selections", false, "Multiple selections allowed.")
}

func AddFlagsEntrySettings(cmd *cobra.Command, s *gui.EntrySettings) {
AddFlagsGeneralSettings(cmd, &s.GeneralSettings)
AddFlagsGeneralTextSettings(cmd, &s.GeneralTextSettings)
AddFlagsDefaultButtonSettings(cmd, &s.DefaultButtonSettings)
func AddFlagsEntry(cmd *cobra.Command, s *set.Entry) {
addFlagsGeneral(cmd, &s.General)
addFlagsGeneralText(cmd, &s.GeneralText)
addFlagsDefaultButton(cmd, &s.DefaultButton)

cmd.Flags().StringVar(&s.EntryText, "entry-text", "", "Entry text.")
cmd.Flags().BoolVar(&s.HideEntryText, "hide-text", false, "Hide the text in the entry field.")
}

func AddFlagsNotification(cmd *cobra.Command, s *set.Notification) {
addFlagsGeneral(cmd, &s.General)
cmd.Flags().StringVar(&s.Text, "text", "", "Notification text.")

cmd.Flags().StringVar(&s.EntryText, "--entry-text", "", "Entry text.")
cmd.Flags().BoolVar(&s.HideEntry, "ellipsize", true, "Ellipsize the text if it doesn't fit.")
}
71 changes: 43 additions & 28 deletions githooks/apps/dialog/cmd/common/handle-result.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package common

import (
"errors"
dcm "gabyx/githooks/apps/dialog/common"
res "gabyx/githooks/apps/dialog/result"
strs "gabyx/githooks/strings"
"os"
"strings"
Expand All @@ -16,43 +15,59 @@ func indicesToList(l []uint) (s []string) {
return
}

func HandleOtherErrors(ctx *CmdContext, err error) error {

if e, ok := err.(*dcm.ErrExtraButton); ok { //nolint: gocritic

os.Stdout.WriteString(strs.Fmt("%d", e.ButtonIndex))
os.Stdout.WriteString("\n")
ctx.ExitCode = 1
// OutputArray outputs a string array to std output.
func OutputArray(l []string, sep string) (err error) {
if len(l) > 0 {
_, err = os.Stdout.WriteString(strings.Join(l, sep) + LineBreak)
}

return nil
return
}

} else if errors.Is(err, dcm.ErrCancled) {
ctx.ExitCode = 1
// OutputArray outputs an index array to std output.
func OutputIndexArray(l []uint) error {
return OutputArray(indicesToList(l), ",")
}

return nil
func HandleGeneralResult(ctx *CmdContext,
g *res.General,
err error,
okCallback func() error,
cancelCallback func() error) error {

} else if os.IsTimeout(err) {
// Handle expected errors first.
if os.IsTimeout(err) {
ctx.ExitCode = 5

return nil

} else if err != nil {
// All other errors are not handled.
return err
}

// All other errors are not handled.
return err
}

func HandleOutputIndices(ctx *CmdContext, l []uint, err error) error {

if err == nil {

if len(l) > 0 {
os.Stdout.WriteString(strings.Join(indicesToList(l), ","))
os.Stdout.WriteString("\n")
// Handle non-errors.
if g.IsOk() {
ctx.ExitCode = 0
if okCallback != nil {
e := okCallback()
if e != nil {
return e // callback error...
}
}

return nil
} else if g.IsCanceled() {
ctx.ExitCode = 1
if cancelCallback != nil {
e := cancelCallback()
if e != nil {
return e // callback error...
}
}
} else if clicked, idx := g.IsExtraButton(); clicked {
os.Stdout.WriteString(strs.Fmt("%d", idx))
os.Stdout.WriteString(LineBreak)
ctx.ExitCode = 2
}

return HandleOtherErrors(ctx, err)
return nil
}
55 changes: 55 additions & 0 deletions githooks/apps/dialog/cmd/entry/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package entry

import (
"context"
dcm "gabyx/githooks/apps/dialog/cmd/common"
"gabyx/githooks/apps/dialog/gui"
res "gabyx/githooks/apps/dialog/result"
set "gabyx/githooks/apps/dialog/settings"
ccm "gabyx/githooks/cmd/common"
"os"
"time"

"github.com/spf13/cobra"
)

func handleResult(ctx *dcm.CmdContext, res *res.Entry, err error) error {
return dcm.HandleGeneralResult(ctx, &res.General, err, func() error {
_, err := os.Stdout.WriteString(res.Text + dcm.LineBreak)

return err
}, nil)
}

func NewCmd(ctx *dcm.CmdContext) *cobra.Command {

settings := set.Entry{}
var timeout uint

cmd := &cobra.Command{
Use: "entry",
Short: "Shows a entry dialog.",
Long: `Shows a entry dialog similar to 'zenity'.
See 'https://help.gnome.org/users/zenity/3.32' for details.`,
Run: func(cmd *cobra.Command, args []string) {

var cancel func()
var cont context.Context

if timeout > 0 {
cont, cancel = context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
}

res, err := gui.ShowEntry(cont, &settings)
err = handleResult(ctx, &res, err)
ctx.Log.AssertNoErrorPanic(err, "Dialog failed")
}}

cmd.Flags().UintVar(&timeout, "timeout", 0, "Timeout for the dialog")

dcm.AddFlagsEntry(cmd, &settings)
ccm.SetCommandDefaults(ctx.Log, cmd)

return cmd
}
18 changes: 11 additions & 7 deletions githooks/apps/dialog/cmd/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ import (
"context"
dcm "gabyx/githooks/apps/dialog/cmd/common"
"gabyx/githooks/apps/dialog/gui"
res "gabyx/githooks/apps/dialog/result"
set "gabyx/githooks/apps/dialog/settings"
ccm "gabyx/githooks/cmd/common"
"time"

"github.com/spf13/cobra"
)

func handleResult(ctx *dcm.CmdContext, res *res.Message, err error) error {
return dcm.HandleGeneralResult(ctx, &res.General, err, nil, nil)
}

func NewCmd(ctx *dcm.CmdContext) *cobra.Command {

settings := gui.MessageSettings{}
settings := set.Message{}
var timeout uint

cmd := &cobra.Command{
Use: "message",
Short: "Shows a message dialog.",
Long: `Shows a message dialog similar to 'zenity'.
See 'https://help.gnome.org/users/zenity/3.32/list.html.de' for details.`,
See 'https://help.gnome.org/users/zenity/3.32' for details.`,
Run: func(cmd *cobra.Command, args []string) {

var cancel func()
Expand All @@ -30,16 +36,14 @@ See 'https://help.gnome.org/users/zenity/3.32/list.html.de' for details.`,
defer cancel()
}

_, err := gui.ShowMessage(cont, &settings)

err = dcm.HandleOutputIndices(ctx, nil, err)
res, err := gui.ShowMessage(cont, &settings)
err = handleResult(ctx, &res, err)
ctx.Log.AssertNoErrorPanic(err, "Dialog failed")

}}

cmd.Flags().UintVar(&timeout, "timeout", 0, "Timeout for the dialog")

dcm.AddFlagsMessageSettings(cmd, &settings)
dcm.AddFlagsMessage(cmd, &settings)
ccm.SetCommandDefaults(ctx.Log, cmd)

return cmd
Expand Down
53 changes: 53 additions & 0 deletions githooks/apps/dialog/cmd/notify/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package notify

import (
"context"
dcm "gabyx/githooks/apps/dialog/cmd/common"
"gabyx/githooks/apps/dialog/gui"
set "gabyx/githooks/apps/dialog/settings"
ccm "gabyx/githooks/cmd/common"
"time"

"github.com/spf13/cobra"
)

func handleResult(ctx *dcm.CmdContext, err error) error {
if err == nil {
ctx.ExitCode = 0
}

return err
}

func NewCmd(ctx *dcm.CmdContext) *cobra.Command {

settings := set.Notification{}
var timeout uint

cmd := &cobra.Command{
Use: "notify",
Short: "Shows a notification.",
Long: `Shows a notification similar to 'zenity'.
See 'https://help.gnome.org/users/zenity/3.32' for details.`,
Run: func(cmd *cobra.Command, args []string) {

var cancel func()
var cont context.Context

if timeout > 0 {
cont, cancel = context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
}

err := gui.ShowNotification(cont, &settings)
err = handleResult(ctx, err)
ctx.Log.AssertNoErrorPanic(err, "Dialog failed")
}}

cmd.Flags().UintVar(&timeout, "timeout", 0, "Timeout for the dialog")

dcm.AddFlagsNotification(cmd, &settings)
ccm.SetCommandDefaults(ctx.Log, cmd)

return cmd
}
Loading

0 comments on commit 7e8f2ca

Please sign in to comment.