File: tmplex.go

package info (click to toggle)
golang-github-intel-tfortools 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 676 kB
  • sloc: makefile: 4
file content (127 lines) | stat: -rw-r--r-- 3,911 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
	"flag"
	"fmt"
	"os"
	"text/tabwriter"
	"time"

	"github.com/intel/tfortools"
)

type stock struct {
	Ticker    string    `tfortools:"Ticker Symbol"`
	Name      string    `tfortools:"Stock name"`
	LastTrade time.Time `tfortools:"Time at which the stock was last traded"`
	Current   float64   `tfortools:"The current value of the stock"`
	High      float64   `tfortools:"The highest value the stock has reached today"`
	Low       float64   `tfortools:"The lowest value the stock has reached today"`
	Volume    int       `tfortools:"The number of shares traded today"`
}

// Cols that creates a new type that execludes certain fields from a struct
// Table that generates a new Table
// Percentage that generates a Percentage
// head, tail and rows that act on slices and return an empty slice if no data is available
// ascending which returns a slice of numbers that can be used for iteration purposes
// sorting

var fictionalStocks = []stock{
	{"BCOM.L", "Big Company", time.Date(2017, time.March, 17, 11, 01, 00, 00, time.UTC), 120.23, 150.00, 119.00, 7500000},
	{"SMAL.L", "Small Company", time.Date(2017, time.March, 17, 10, 59, 00, 00, time.UTC), 1.06, 1.06, 1.10, 750},
	{"MEDI.L", "Medium Company", time.Date(2017, time.March, 17, 12, 23, 00, 00, time.UTC), 77.00, 75.11, 81.12, 300122},
	{"PICO.L", "Tiny Corp", time.Date(2017, time.March, 16, 16, 01, 00, 00, time.UTC), 0.59, 0.57, 0.63, 155},
	{"HENT.L", "Happy Enterprises", time.Date(2017, time.March, 17, 9, 45, 00, 00, time.UTC), 756.11, 600.00, 10000, 6395624278},
	{"LONL.L", "Lonely Systems", time.Date(2017, time.March, 17, 13, 45, 00, 00, time.UTC), 1245.00, 1200.00, 1245.00, 19003},
}

var code string

var cfg *tfortools.Config

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s -f [stocks]\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintln(os.Stderr)
		fmt.Fprintln(os.Stderr, tfortools.GenerateUsageDecorated("f", fictionalStocks, cfg))
	}
	flag.StringVar(&code, "f", "", "string containing the template code to execute")
	cfg = tfortools.NewConfig(tfortools.OptAllFns)

	if err := cfg.AddCustomFn(sumVolume, "sumVolume", sumVolumeHelp); err != nil {
		panic(err)
	}
}

func sumVolume(stocks []stock) int {
	total := 0
	for _, s := range stocks {
		total += s.Volume
	}
	return total
}

const sumVolumeHelp = `- sumVolume computes the total volume all stocks in a []stock slice`

func stocks() error {
	if code != "" {
		err := tfortools.OutputToTemplate(os.Stdout, "stocks", code, fictionalStocks, cfg)
		if err != nil {
			return fmt.Errorf("Unable to execute template : %v", err)
		}

		return nil
	}

	w := tabwriter.NewWriter(os.Stdout, 12, 8, 1, ' ', 0)
	fmt.Fprintln(w, "Ticker\tName\tLast Trade\tCurrent\tDay High\tDay Low\tVolume\t")
	for _, s := range fictionalStocks {
		fmt.Fprintf(w, "%s\t%s\t%s\t%.2f\t%.2f\t%.2f\t%d\t\n",
			s.Ticker, s.Name, s.LastTrade.Format(time.Kitchen), s.Current, s.High, s.Low, s.Volume)
	}
	_ = w.Flush()

	return nil
}

var commands = map[string]func() error{
	"stocks": stocks,
}

func main() {
	flag.Parse()

	if len(flag.Args()) != 1 {
		flag.Usage()
		os.Exit(1)
	}

	fn := commands[flag.Args()[0]]
	if fn == nil {
		flag.Usage()
		os.Exit(1)
	}

	if err := fn(); err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(1)
	}
}