Browse Source

Add a "simple" formatter

Thomas Jost 3 years ago
parent
commit
c4b200c27f
Signed by: Thomas Jost <schnouki@schnouki.net> GPG Key ID: 3F3B990246141762
4 changed files with 136 additions and 24 deletions
  1. 18
    24
      cmd/root.go
  2. 35
    0
      format/interface.go
  3. 44
    0
      format/simple.go
  4. 39
    0
      format/value.go

+ 18
- 24
cmd/root.go View File

@@ -11,19 +11,26 @@ import (
11 11
 	"github.com/spf13/cobra"
12 12
 
13 13
 	"islas/fav"
14
+	"islas/format"
14 15
 )
15 16
 
16
-// rootCmd represents the base command when called without any subcommands
17
-var rootCmd = &cobra.Command{
18
-	Use:   "islas",
19
-	Short: "Quickly get timetables for STAN bus and tramway lines",
20
-	Long: `A tool that allows to quickly get infos about STAN bus and tramway lines,
17
+var (
18
+	// rootCmd represents the base command when called without any subcommands
19
+	rootCmd = &cobra.Command{
20
+		Use:   "islas",
21
+		Short: "Quickly get timetables for STAN bus and tramway lines",
22
+		Long: `A tool that allows to quickly get infos about STAN bus and tramway lines,
21 23
 and get real-time timetables for any stop in the STAN network.`,
22
-	Run: rootCommand,
23
-}
24
+		Run: rootCommand,
25
+	}
26
+
27
+	formatVal = format.DefaultFormatterValue()
28
+)
24 29
 
25 30
 func init() {
26 31
 	xdg.SetName("islas")
32
+
33
+	rootCmd.Flags().VarP(&formatVal, "format", "f", "Output format")
27 34
 }
28 35
 
29 36
 // Execute runs the root command.
@@ -63,27 +70,14 @@ func rootCommand(cmd *cobra.Command, args []string) {
63 70
 		log.WithError(err).Fatal("Cannot get a time table")
64 71
 	}
65 72
 
73
+	formatter := formatVal.Formatter()
66 74
 	for idx := 0; idx < len(tts); idx++ {
67 75
 		tt, ok := tts[idx]
68 76
 		if !ok {
69 77
 			continue
70 78
 		}
71
-		fmt.Printf("%s (%s -> %s)\n", tt.StopName, tt.LineNumber, tt.Direction)
72
-		for ptidx, pt := range tt.PassingTimes {
73
-			prefix := "    "
74
-			if pt.Realtime {
75
-				prefix = "  * "
76
-			}
77
-			time := pt.Time.String()
78
-			delta := pt.Time.DeltaNow()
79
-			if delta < 0 {
80
-				continue
81
-			}
82
-			if delta <= 15 || ptidx == 0 {
83
-				time = fmt.Sprintf("%s (%d min)", pt.Time, delta)
84
-			}
85
-			fmt.Printf("%s%s\n", prefix, time)
86
-		}
87
-		fmt.Println()
79
+		formatter.Add(tt)
88 80
 	}
81
+	output := formatter.Format()
82
+	fmt.Print(output)
89 83
 }

+ 35
- 0
format/interface.go View File

@@ -0,0 +1,35 @@
1
+package format
2
+
3
+import (
4
+	"errors"
5
+
6
+	"islas/fav"
7
+)
8
+
9
+// Formatter is the interface for time table formatters.
10
+type Formatter interface {
11
+	Add(tt *fav.TimeTable)
12
+	Format() string
13
+}
14
+
15
+var (
16
+	formatters = map[string]Formatter{}
17
+
18
+	// ErrNotFound is the error indicating that a formatter could not be
19
+	// found (by its name).
20
+	ErrNotFound = errors.New("No such formatter")
21
+)
22
+
23
+// Register registers a new formatter.
24
+func Register(name string, f Formatter) {
25
+	formatters[name] = f
26
+}
27
+
28
+// ByName gets a formatter by name.
29
+func ByName(name string) (Formatter, error) {
30
+	f, ok := formatters[name]
31
+	if ok {
32
+		return f, nil
33
+	}
34
+	return nil, ErrNotFound
35
+}

+ 44
- 0
format/simple.go View File

@@ -0,0 +1,44 @@
1
+package format
2
+
3
+import (
4
+	"fmt"
5
+
6
+	"islas/fav"
7
+)
8
+
9
+func init() {
10
+	Register("simple", &SimpleFormatter{})
11
+}
12
+
13
+// SimpleFormatter is a very basic time table formatter.
14
+type SimpleFormatter struct {
15
+	tts []*fav.TimeTable
16
+}
17
+
18
+func (f *SimpleFormatter) Add(tt *fav.TimeTable) {
19
+	f.tts = append(f.tts, tt)
20
+}
21
+
22
+func (f *SimpleFormatter) Format() string {
23
+	s := ""
24
+	for _, tt := range f.tts {
25
+		s += fmt.Sprintf("%s (%s -> %s)\n", tt.StopName, tt.LineNumber, tt.Direction)
26
+		for ptidx, pt := range tt.PassingTimes {
27
+			prefix := "    "
28
+			if pt.Realtime {
29
+				prefix = "  * "
30
+			}
31
+			time := pt.Time.String()
32
+			delta := pt.Time.DeltaNow()
33
+			if delta < 0 {
34
+				continue
35
+			}
36
+			if delta <= 15 || ptidx == 0 {
37
+				time = fmt.Sprintf("%s (%d min)", pt.Time, delta)
38
+			}
39
+			s += fmt.Sprintf("%s%s\n", prefix, time)
40
+		}
41
+		s += "\n"
42
+	}
43
+	return s
44
+}

+ 39
- 0
format/value.go View File

@@ -0,0 +1,39 @@
1
+package format
2
+
3
+const (
4
+	defaultFormatter = "simple"
5
+)
6
+
7
+type FormatterValue struct {
8
+	name string
9
+	fmt  Formatter
10
+}
11
+
12
+func DefaultFormatterValue() FormatterValue {
13
+	fv := FormatterValue{}
14
+	if err := fv.Set(defaultFormatter); err != nil {
15
+		panic(err)
16
+	}
17
+	return fv
18
+}
19
+
20
+func (val *FormatterValue) String() string {
21
+	return val.name
22
+}
23
+
24
+func (val *FormatterValue) Set(name string) error {
25
+	fmt, err := ByName(name)
26
+	if err != nil {
27
+		return err
28
+	}
29
+	val.fmt = fmt
30
+	return nil
31
+}
32
+
33
+func (val *FormatterValue) Type() string {
34
+	return "formatter"
35
+}
36
+
37
+func (val *FormatterValue) Formatter() Formatter {
38
+	return val.fmt
39
+}

Loading…
Cancel
Save