Skip to content

Commit

Permalink
[refactor] merge tui&util, refine tui for pattern seq (#5)
Browse files Browse the repository at this point in the history
* Refactor to merge tui&util, print pattern seq tui

* in table format

* more blocks
  • Loading branch information
hyorigo authored Oct 23, 2023
1 parent 926d846 commit 75d9e1a
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 187 deletions.
3 changes: 1 addition & 2 deletions cmd/act.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

b1 "github.com/b1ug/blink1-go"
"github.com/b1ug/nb1/hdwr"
"github.com/b1ug/nb1/tui"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -34,7 +33,7 @@ var actCmd = &cobra.Command{
log.Debugw("parsed blink(1) state", "state", st)

// perform action
fmt.Println("Perform Action:", tui.FormatLightState(st))
fmt.Println("Perform Action:", util.FormatLightState(st))
if waitComplete {
return hdwr.PlayStateAndWait(st)
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"errors"

"bitbucket.org/ai69/amoy"
"github.com/b1ug/nb1/exchange"
"github.com/b1ug/nb1/schema"
"github.com/b1ug/nb1/util"
Expand Down Expand Up @@ -90,7 +89,7 @@ var convertText2JSONCmd = &cobra.Command{

// output
if convertPreviewPattern {
amoy.PrintOneLineJSON(ps)
util.PrintStateSequence(ps.Sequence)
}
return exchange.SaveAsJSON(ps, outputPath)
},
Expand All @@ -116,9 +115,9 @@ var convertJSON2TextCmd = &cobra.Command{

// output
if convertPreviewPattern {
amoy.PrintJSON(ps)
util.PrintStateSequence(ps.Sequence)
}
ls := exchange.EncodePlayText(&ps)
ls := exchange.EncodePlayText(ps)
return exchange.SaveAsLine(ls, outputPath)
},
}
Expand All @@ -133,7 +132,7 @@ var convertText2ScriptCmd = &cobra.Command{
`),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
//
// TODO: implement
return errors.New("not implemented")
},
}
Expand All @@ -148,7 +147,7 @@ var convertJSON2ScriptCmd = &cobra.Command{
`),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
//
// TODO: implement
return errors.New("not implemented")
},
}
3 changes: 1 addition & 2 deletions cmd/fade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
b1 "github.com/b1ug/blink1-go"
"github.com/b1ug/nb1/exchange"
"github.com/b1ug/nb1/hdwr"
"github.com/b1ug/nb1/tui"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -43,7 +42,7 @@ var fadeCmd = &cobra.Command{
log.Debugw("parsed blink(1) state", "state", st)

// perform action
fmt.Println("Fade to State:", tui.FormatLightState(st))
fmt.Println("Fade to State:", util.FormatLightState(st))
if waitComplete {
return hdwr.PlayStateAndWait(st)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/b1ug/nb1/hdwr"
"github.com/b1ug/nb1/tui"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,9 +36,9 @@ var listCmd = &cobra.Command{
// print device list
log.Debugw("blink(1) devices found", "count", len(dis))
if showFirmwareVersion {
_ = tui.PrintDeviceListWithFirmware(dis)
_ = util.PrintDeviceListWithFirmware(dis)
} else {
_ = tui.PrintDeviceList(dis)
_ = util.PrintDeviceList(dis)
}
return nil
},
Expand All @@ -58,7 +58,7 @@ func init() {
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
listCmd.Flags().BoolVarP(&showFirmwareVersion, "firmware", "f", false, "show firmware version")
listCmd.Flags().BoolVarP(&showAllHIDDevices, "all", "a", false, "show all HID devices as well")
listCmd.Flags().BoolVar(&showAllHIDDevices, "all", false, "show all HID devices as well")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
Expand Down
4 changes: 2 additions & 2 deletions cmd/off.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

b1 "github.com/b1ug/blink1-go"
"github.com/b1ug/nb1/hdwr"
"github.com/b1ug/nb1/tui"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +20,7 @@ var offCmd = &cobra.Command{
Args: cobra.NoArgs,
PersistentPreRunE: openBlink1Device,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Turn to", tui.FormatNamedColor(b1.ColorBlack))
fmt.Println("Turn to", util.FormatNamedColor(b1.ColorBlack))
return hdwr.StopPlaying()
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/on.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

b1 "github.com/b1ug/blink1-go"
"github.com/b1ug/nb1/hdwr"
"github.com/b1ug/nb1/tui"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
)

Expand All @@ -21,7 +21,7 @@ var onCmd = &cobra.Command{
PersistentPreRunE: openBlink1Device,
RunE: func(cmd *cobra.Command, args []string) error {
cl := b1.ColorWhite
fmt.Println("Turn to", tui.FormatNamedColor(cl))
fmt.Println("Turn to", util.FormatNamedColor(cl))
return hdwr.SetColor(cl)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&debugMode, "debug", "D", false, "enable debug mode (if true, also use debug log level)")
rootCmd.PersistentFlags().StringP("device", "d", "", "preferred blink(1) device (if non-empty, use this device)")
rootCmd.PersistentFlags().BoolVarP(&waitComplete, "wait", "w", false, "wait for completion")
rootCmd.PersistentFlags().BoolVar(&allowAbsent, "allow-absent", false, "ignore opening errors for absent blink(1) devices")
rootCmd.PersistentFlags().BoolVarP(&allowAbsent, "allow-absent", "a", false, "ignore opening error for absent blink(1) devices")
// _ = rootCmd.MarkPersistentFlagRequired("config")

viper.BindPFlag("device", rootCmd.PersistentFlags().Lookup("device"))
Expand Down
2 changes: 0 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"bitbucket.org/ai69/amoy"
"github.com/b1ug/nb1/config"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -26,7 +25,6 @@ var serverCmd = &cobra.Command{
fmt.Println("Port:", config.GetPort())
fmt.Println("Base:", config.GetBaseURL())
fmt.Println(amoy.Quote())
fmt.Println(util.Voice())

return errNotImplemented
},
Expand Down
40 changes: 22 additions & 18 deletions exchange/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/b1ug/nb1/util"
)

// ParsePlayText parses a slice of strings in play.txt format into a pattern set.
func ParsePlayText(lines []string) (*schema.PatternSet, error) {
// parse it
log.Infow("parse input file", "lines", len(lines))
Expand Down Expand Up @@ -73,27 +74,30 @@ func ParsePlayText(lines []string) (*schema.PatternSet, error) {
}, nil
}

// EncodePlayText encodes a pattern set into a slice of strings.
func EncodePlayText(ps *schema.PatternSet) []string {
if ps == nil {
return nil
}

// EncodePlayText encodes a pattern set into a slice of strings in play.txt format.
func EncodePlayText(ps schema.PatternSet) []string {
ls := make([]string, 0, len(ps.Sequence)+2)

// title
if t := ps.Name; ystring.IsNotBlank(t) {
ls = append(ls, "Title: "+t)
}

if r := ps.RepeatTimes; r == 0 {
ls = append(ls, "(Repeat Forever)")
} else if r == 1 {
ls = append(ls, "(Repeat Once)")
} else if r == 2 {
ls = append(ls, "(Repeat Twice)")
} else {
ls = append(ls, fmt.Sprintf("(Repeat: %d times)", r))
// repeat times
var rt string
switch ps.RepeatTimes {
case 0:
rt = "Repeat Forever"
case 1:
rt = "Repeat Once"
case 2:
rt = "Repeat Twice"
default:
rt = fmt.Sprintf("Repeat: %d times", ps.RepeatTimes)
}
ls = append(ls, "("+rt+")")

// sequence
var (
lastLED blink1.LEDIndex
lastColor string
Expand All @@ -102,7 +106,7 @@ func EncodePlayText(ps *schema.PatternSet) []string {
// color
hn, ok := util.ConvColorToNameOrHex(st.Color)
if ok {
hn = strings.ToTitle(hn)
hn = strings.Title(hn)
}

// led
Expand All @@ -124,11 +128,11 @@ func EncodePlayText(ps *schema.PatternSet) []string {
t = fmt.Sprintf("%v seconds", f.Seconds())
}

// check state
// special flags
instantly := st.FadeTime < 10*time.Millisecond
isMaintain := lastLED == st.LED && lastColor == hn

// sentence
// combined as sentence
var sent string
if isMaintain {
if instantly {
Expand All @@ -144,7 +148,7 @@ func EncodePlayText(ps *schema.PatternSet) []string {
}
}

// add index
// add with index
ls = append(ls, strconv.Itoa(i+1)+". "+sent)

// for next run
Expand Down
1 change: 1 addition & 0 deletions exchange/z_log.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package exchange contains functions for converting between different formats.
package exchange

import (
Expand Down
39 changes: 0 additions & 39 deletions tui/color.go

This file was deleted.

61 changes: 0 additions & 61 deletions tui/convert.go

This file was deleted.

Loading

0 comments on commit 75d9e1a

Please sign in to comment.