Skip to content

Commit

Permalink
Go fmt all files
Browse files Browse the repository at this point in the history
  • Loading branch information
SonarBeserk authored and dixonwille committed Oct 30, 2023
1 parent f23028b commit a0c2cee
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 68 deletions.
2 changes: 1 addition & 1 deletion clearScreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
}
}

//Clear simply clears the command line interface (os.Stdout only).
// Clear simply clears the command line interface (os.Stdout only).
func Clear() {
value, ok := clear[runtime.GOOS]
if ok {
Expand Down
16 changes: 8 additions & 8 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ var (
ErrDuplicate = errors.New("duplicated response")
)

//MenuError records menu errors
// MenuError records menu errors
type MenuError struct {
Err error
Res string
TriesLeft int
}

//Error prints the error in an easy to read string.
// Error prints the error in an easy to read string.
func (e *MenuError) Error() string {
if e.Res != "" {
return e.Err.Error() + ": " + e.Res
Expand All @@ -39,7 +39,7 @@ func newMenuError(err error, res string, tries int) *MenuError {
}
}

//IsInvalidErr checks to see if err is of type invalid error returned by menu.
// IsInvalidErr checks to see if err is of type invalid error returned by menu.
func IsInvalidErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrInvalid {
Expand All @@ -48,7 +48,7 @@ func IsInvalidErr(err error) bool {
return false
}

//IsNoResponseErr checks to see if err is of type no response returned by menu.
// IsNoResponseErr checks to see if err is of type no response returned by menu.
func IsNoResponseErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrNoResponse {
Expand All @@ -57,7 +57,7 @@ func IsNoResponseErr(err error) bool {
return false
}

//IsTooManyErr checks to see if err is of type too many returned by menu.
// IsTooManyErr checks to see if err is of type too many returned by menu.
func IsTooManyErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrTooMany {
Expand All @@ -66,7 +66,7 @@ func IsTooManyErr(err error) bool {
return false
}

//IsDuplicateErr checks to see if err is of type duplicate returned by menu.
// IsDuplicateErr checks to see if err is of type duplicate returned by menu.
func IsDuplicateErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrDuplicate {
Expand All @@ -75,8 +75,8 @@ func IsDuplicateErr(err error) bool {
return false
}

//IsMenuErr checks to see if it is a menu err.
//This is a general check not a specific one.
// IsMenuErr checks to see if it is a menu err.
// This is a general check not a specific one.
func IsMenuErr(err error) bool {
_, ok := err.(*MenuError)
return ok
Expand Down
114 changes: 57 additions & 57 deletions menu.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//Package wmenu creates menus for cli programs.
//It uses wlog for it's interface with the command line.
//It uses os.Stdin, os.Stdout, and os.Stderr with concurrency by default.
//wmenu allows you to change the color of the different parts of the menu.
//This package also creates it's own error structure so you can type assert if you need to.
//wmenu will validate all responses before calling any function.
//It will also figure out which function should be called so you don't have to.
// Package wmenu creates menus for cli programs.
// It uses wlog for it's interface with the command line.
// It uses os.Stdin, os.Stdout, and os.Stderr with concurrency by default.
// wmenu allows you to change the color of the different parts of the menu.
// This package also creates it's own error structure so you can type assert if you need to.
// wmenu will validate all responses before calling any function.
// It will also figure out which function should be called so you don't have to.
package wmenu

import (
Expand All @@ -19,7 +19,7 @@ import (
"github.com/mattn/go-isatty"
)

//DefaultYN is used to specify what the default answer is to a yes/no question.
// DefaultYN is used to specify what the default answer is to a yes/no question.
type DefaultYN int

const (
Expand All @@ -34,8 +34,8 @@ var (
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
)

//Menu is used to display options to a user.
//A user can then select options and Menu will validate the response and perform the correct action.
// Menu is used to display options to a user.
// A user can then select options and Menu will validate the response and perform the correct action.
type Menu struct {
question string
function func([]Opt) error
Expand All @@ -53,7 +53,7 @@ type Menu struct {
initialIndex int
}

//NewMenu creates a menu with a wlog.UI as the writer.
// NewMenu creates a menu with a wlog.UI as the writer.
func NewMenu(question string) *Menu {
//Create a default ui to use for menu
var ui wlog.UI
Expand All @@ -77,11 +77,11 @@ func NewMenu(question string) *Menu {
}
}

//AddColor will change the color of the menu items.
//optionColor changes the color of the options.
//questionColor changes the color of the questions.
//errorColor changes the color of the question.
//Use wlog.None if you do not want to change the color.
// AddColor will change the color of the menu items.
// optionColor changes the color of the options.
// questionColor changes the color of the questions.
// errorColor changes the color of the question.
// Use wlog.None if you do not want to change the color.
func (m *Menu) AddColor(optionColor, questionColor, responseColor, errorColor wlog.Color) {
if !noColor {
m.ui = wlog.AddColor(questionColor, errorColor, wlog.None, wlog.None, optionColor, responseColor, wlog.None, wlog.None, wlog.None, m.ui)
Expand All @@ -93,40 +93,40 @@ func (m *Menu) PadOptionID() {
m.padOptionID = true
}

//ClearOnMenuRun will clear the screen when a menu is ran.
//This is checked when LoopOnInvalid is activated.
//Meaning if an error occurred then it will clear the screen before asking again.
// ClearOnMenuRun will clear the screen when a menu is ran.
// This is checked when LoopOnInvalid is activated.
// Meaning if an error occurred then it will clear the screen before asking again.
func (m *Menu) ClearOnMenuRun() {
m.clear = true
}

//SetSeparator sets the separator to use when multiple options are valid responses.
//Default value is a space.
// SetSeparator sets the separator to use when multiple options are valid responses.
// Default value is a space.
func (m *Menu) SetSeparator(sep string) {
m.multiSeparator = sep
}

//SetTries sets the number of tries on the loop before failing out.
//Default is 3.
//Negative values act like 0.
// SetTries sets the number of tries on the loop before failing out.
// Default is 3.
// Negative values act like 0.
func (m *Menu) SetTries(i int) {
m.tries = i
}

//LoopOnInvalid is used if an invalid option was given then it will prompt the user again.
// LoopOnInvalid is used if an invalid option was given then it will prompt the user again.
func (m *Menu) LoopOnInvalid() {
m.loopOnInvalid = true
}

//SetDefaultIcon sets the icon used to identify which options will be selected by default
// SetDefaultIcon sets the icon used to identify which options will be selected by default
func (m *Menu) SetDefaultIcon(icon string) {
m.defIcon = icon
}

//IsYesNo sets the menu to a yes/no state.
//Does not show options but does ask question.
//Will also parse the answer to allow for all variants of yes/no (IE Y yes No ...)
//Both will call the Action function you specified.
// IsYesNo sets the menu to a yes/no state.
// Does not show options but does ask question.
// Will also parse the answer to allow for all variants of yes/no (IE Y yes No ...)
// Both will call the Action function you specified.
// Opt{ID: 1, Text: "y"} for yes and Opt{ID: 2, Text: "n"} for no will be passed to the function.
func (m *Menu) IsYesNo(def DefaultYN) {
m.isYN = true
Expand All @@ -138,43 +138,43 @@ func (m *Menu) InitialIndex(index int) {
m.initialIndex = index
}

//Option adds an option to the menu for the user to select from.
//value is an empty interface that can be used to pass anything through to the function.
//title is the string the user will select
//isDefault is whether this option is a default option (IE when no options are selected).
//function is what is called when only this option is selected.
//If function is nil then it will default to the menu's Action.
// Option adds an option to the menu for the user to select from.
// value is an empty interface that can be used to pass anything through to the function.
// title is the string the user will select
// isDefault is whether this option is a default option (IE when no options are selected).
// function is what is called when only this option is selected.
// If function is nil then it will default to the menu's Action.
func (m *Menu) Option(title string, value interface{}, isDefault bool, function func(Opt) error) {
option := newOption(len(m.options), title, value, isDefault, function)
m.options = append(m.options, *option)
}

//Action adds a default action to use in certain scenarios.
//If the selected option (by default or user selected) does not have a function applied to it this will be called.
//If there are no default options and no option was selected this will be called with an option that has an ID of -1.
// Action adds a default action to use in certain scenarios.
// If the selected option (by default or user selected) does not have a function applied to it this will be called.
// If there are no default options and no option was selected this will be called with an option that has an ID of -1.
func (m *Menu) Action(function func([]Opt) error) {
m.function = function
}

//AllowMultiple will tell the menu to allow multiple selections.
//The menu will fail if this is not called and mulple selections were selected.
// AllowMultiple will tell the menu to allow multiple selections.
// The menu will fail if this is not called and mulple selections were selected.
func (m *Menu) AllowMultiple() {
m.allowMultiple = true
}

//ChangeReaderWriter changes where the menu listens and writes to.
//reader is where user input is collected.
//writer and errorWriter is where the menu should write to.
// ChangeReaderWriter changes where the menu listens and writes to.
// reader is where user input is collected.
// writer and errorWriter is where the menu should write to.
func (m *Menu) ChangeReaderWriter(reader io.Reader, writer, errorWriter io.Writer) {
ui := wlog.New(reader, writer, errorWriter)
m.ui = ui
}

//Run is used to execute the menu.
//It will print to options and question to the screen.
//It will only clear the screen if ClearOnMenuRun is activated.
//This will validate all responses.
//Errors are of type MenuError.
// Run is used to execute the menu.
// It will print to options and question to the screen.
// It will only clear the screen if ClearOnMenuRun is activated.
// This will validate all responses.
// Errors are of type MenuError.
func (m *Menu) Run() error {
if m.clear {
Clear()
Expand Down Expand Up @@ -232,7 +232,7 @@ func (m *Menu) callAppropriateNoOptions() (err error) {
return m.function(options)
}

//hide options when this is a yes or no
// hide options when this is a yes or no
func (m *Menu) print() {
if !m.isYN {
outputFormat := "%d) %s%s"
Expand Down Expand Up @@ -311,7 +311,7 @@ func (m *Menu) ask() ([]Opt, error) {
return finalOptions, nil
}

//Converts the response string to a slice of ints, also validates along the way.
// Converts the response string to a slice of ints, also validates along the way.
func (m *Menu) resToInt(res string) ([]int, error) {
resStrings := strings.Split(res, m.multiSeparator)
//Check if we don't want multiple responses
Expand Down Expand Up @@ -349,8 +349,8 @@ func (m *Menu) ynResParse(res string) ([]int, error) {
return []int{int(DefN)}, nil
}

//Check if response is in the range of options
//If it is make sure it is not duplicated
// Check if response is in the range of options
// If it is make sure it is not duplicated
func (m *Menu) validateResponses(responses []int) error {
var tmp []int
for _, response := range responses {
Expand All @@ -368,7 +368,7 @@ func (m *Menu) validateResponses(responses []int) error {
return nil
}

//Simply checks if number exists in the slice
// Simply checks if number exists in the slice
func exist(slice []int, number int) bool {
for _, s := range slice {
if number == s {
Expand All @@ -378,7 +378,7 @@ func exist(slice []int, number int) bool {
return false
}

//gets a list of default options
// gets a list of default options
func (m *Menu) getDefault() []Opt {
var opt []Opt
for _, o := range m.options {
Expand All @@ -389,8 +389,8 @@ func (m *Menu) getDefault() []Opt {
return opt
}

//make sure that there is an action available to be called in certain cases
//returns false if it chould not find an action for the number options available
// make sure that there is an action available to be called in certain cases
// returns false if it chould not find an action for the number options available
func (m *Menu) validOptAndFunc(opt []Opt) bool {
if m.function == nil {
if len(opt) == 1 && opt[0].function != nil {
Expand Down
4 changes: 2 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package wmenu

//Opt is what Menu uses to display options to screen.
//Also holds information on what should run and if it is a default option
// Opt is what Menu uses to display options to screen.
// Also holds information on what should run and if it is a default option
type Opt struct {
ID int
Text string
Expand Down

0 comments on commit a0c2cee

Please sign in to comment.