mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
using enum modeflags, use my own usage template, generated from manpage so I don't have to maintain it twice, it's also nicer
This commit is contained in:
@@ -17,15 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gookit/color"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const DefaultSeparator string = `(\s\s+|\t)`
|
||||
const ValidOutputModes string = "(orgtbl|markdown|extended|ascii|yaml|shell)"
|
||||
const Version string = "v1.0.11"
|
||||
const Version string = "v1.0.12"
|
||||
|
||||
var VERSION string // maintained by -x
|
||||
|
||||
@@ -35,7 +32,7 @@ type Config struct {
|
||||
Columns string
|
||||
UseColumns []int
|
||||
Separator string
|
||||
OutputMode string
|
||||
OutputMode int
|
||||
InvertMatch bool
|
||||
Pattern string
|
||||
|
||||
@@ -64,6 +61,16 @@ type Modeflag struct {
|
||||
A bool
|
||||
}
|
||||
|
||||
// used for switching printers
|
||||
const (
|
||||
Extended = iota + 1
|
||||
Orgtbl
|
||||
Markdown
|
||||
Shell
|
||||
Yaml
|
||||
Ascii
|
||||
)
|
||||
|
||||
// various sort types
|
||||
type Sortmode struct {
|
||||
Numeric bool
|
||||
@@ -97,39 +104,6 @@ func Getversion() string {
|
||||
return fmt.Sprintf("This is tablizer version %s", VERSION)
|
||||
}
|
||||
|
||||
func (conf *Config) PrepareModeFlags(flag Modeflag, mode string) error {
|
||||
if len(mode) == 0 {
|
||||
// associate short flags like -X with mode selector
|
||||
switch {
|
||||
case flag.X:
|
||||
conf.OutputMode = "extended"
|
||||
case flag.M:
|
||||
conf.OutputMode = "markdown"
|
||||
case flag.O:
|
||||
conf.OutputMode = "orgtbl"
|
||||
case flag.S:
|
||||
conf.OutputMode = "shell"
|
||||
conf.NoNumbering = true
|
||||
case flag.Y:
|
||||
conf.OutputMode = "yaml"
|
||||
conf.NoNumbering = true
|
||||
default:
|
||||
conf.OutputMode = "ascii"
|
||||
}
|
||||
} else {
|
||||
r, _ := regexp.Compile(ValidOutputModes) // hardcoded, no fail expected
|
||||
match := r.MatchString(mode)
|
||||
|
||||
if !match {
|
||||
return errors.New("Invalid output mode!")
|
||||
}
|
||||
|
||||
conf.OutputMode = mode
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (conf *Config) PrepareSortFlags(flag Sortmode) {
|
||||
switch {
|
||||
case flag.Numeric:
|
||||
@@ -142,3 +116,20 @@ func (conf *Config) PrepareSortFlags(flag Sortmode) {
|
||||
conf.SortMode = "string"
|
||||
}
|
||||
}
|
||||
|
||||
func (conf *Config) PrepareModeFlags(flag Modeflag) {
|
||||
switch {
|
||||
case flag.X:
|
||||
conf.OutputMode = Extended
|
||||
case flag.O:
|
||||
conf.OutputMode = Orgtbl
|
||||
case flag.M:
|
||||
conf.OutputMode = Markdown
|
||||
case flag.S:
|
||||
conf.OutputMode = Shell
|
||||
case flag.Y:
|
||||
conf.OutputMode = Yaml
|
||||
default:
|
||||
conf.OutputMode = Ascii
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,46 +26,26 @@ import (
|
||||
func TestPrepareModeFlags(t *testing.T) {
|
||||
var tests = []struct {
|
||||
flag Modeflag
|
||||
mode string // input, if any
|
||||
expect string // output
|
||||
want bool
|
||||
expect int // output (constant enum)
|
||||
}{
|
||||
// short commandline flags like -M
|
||||
{Modeflag{X: true}, "", "extended", false},
|
||||
{Modeflag{S: true}, "", "shell", false},
|
||||
{Modeflag{O: true}, "", "orgtbl", false},
|
||||
{Modeflag{Y: true}, "", "yaml", false},
|
||||
{Modeflag{M: true}, "", "markdown", false},
|
||||
{Modeflag{}, "", "ascii", false},
|
||||
|
||||
// long flags like -o yaml
|
||||
{Modeflag{}, "extended", "extended", false},
|
||||
{Modeflag{}, "shell", "shell", false},
|
||||
{Modeflag{}, "orgtbl", "orgtbl", false},
|
||||
{Modeflag{}, "yaml", "yaml", false},
|
||||
{Modeflag{}, "markdown", "markdown", false},
|
||||
|
||||
// failing
|
||||
{Modeflag{}, "blah", "", true},
|
||||
{Modeflag{X: true}, Extended},
|
||||
{Modeflag{S: true}, Shell},
|
||||
{Modeflag{O: true}, Orgtbl},
|
||||
{Modeflag{Y: true}, Yaml},
|
||||
{Modeflag{M: true}, Markdown},
|
||||
{Modeflag{}, Ascii},
|
||||
}
|
||||
|
||||
// FIXME: use a map for easier printing
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("PrepareModeFlags-flags-mode-%s-expect-%s-want-%t",
|
||||
tt.mode, tt.expect, tt.want)
|
||||
testname := fmt.Sprintf("PrepareModeFlags-expect-%d", tt.expect)
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
c := Config{OutputMode: tt.mode}
|
||||
c := Config{}
|
||||
|
||||
// check either flag or pre filled mode, whatever is defined in tt
|
||||
err := c.PrepareModeFlags(tt.flag, tt.mode)
|
||||
if err != nil {
|
||||
if !tt.want {
|
||||
// expect to fail
|
||||
t.Fatalf("PrepareModeFlags returned unexpected error: %s", err)
|
||||
}
|
||||
} else {
|
||||
if c.OutputMode != tt.expect {
|
||||
t.Errorf("got: %s, expect: %s", c.OutputMode, tt.expect)
|
||||
}
|
||||
c.PrepareModeFlags(tt.flag)
|
||||
if c.OutputMode != tt.expect {
|
||||
t.Errorf("got: %d, expect: %d", c.OutputMode, tt.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user