diff --git a/lib/format.go b/lib/format.go
index b6e62ea..44a48d6 100644
--- a/lib/format.go
+++ b/lib/format.go
@@ -5,11 +5,21 @@ package mop
import (
"bytes"
"text/template"
+ "time"
)
//-----------------------------------------------------------------------------
func Format(message []Message) string {
- markup := `{{range .}}{{.Ticker}} ${{.LastTrade}} {{.Change}}
+ vars := struct {
+ Now string
+ Stocks []Message
+ }{
+ time.Now().Format("3:04:05pm PST"),
+ message,
+ }
+
+ markup := `Hello{{.Now}}
+{{range .Stocks}}{{.Ticker}} ${{.LastTrade}} {{.Change}}
{{end}}...`
template, err := template.New("screen").Parse(markup)
@@ -18,9 +28,10 @@ func Format(message []Message) string {
}
buffer := new(bytes.Buffer)
- err = template.Execute(buffer, message)
+ err = template.Execute(buffer, vars)
if err != nil {
panic(err)
}
+
return buffer.String()
}
diff --git a/lib/screen.go b/lib/screen.go
index 512a62c..dc3c32b 100644
--- a/lib/screen.go
+++ b/lib/screen.go
@@ -3,10 +3,10 @@
package mop
import (
- "regexp"
- "strings"
"github.com/michaeldv/just"
"github.com/nsf/termbox-go"
+ "regexp"
+ "strings"
)
// Can combine attributes and a single color using bitwise OR.
@@ -15,7 +15,7 @@ import (
// AttrUnderline
// AttrReverse
//
-var colors = map[string]termbox.Attribute{
+var tags = map[string]termbox.Attribute{
"black": termbox.ColorBlack,
"red": termbox.ColorRed,
"green": termbox.ColorGreen,
@@ -24,6 +24,7 @@ var colors = map[string]termbox.Attribute{
"magenta": termbox.ColorMagenta,
"cyan": termbox.ColorCyan,
"white": termbox.ColorWhite,
+ "right": termbox.ColorDefault,
}
//-----------------------------------------------------------------------------
@@ -37,17 +38,17 @@ func Draw(stocks string) {
}
//
-// Return regular expression that matches all possible color tags, i.e.
+// Return regular expression that matches all possible tags, i.e.
// ?black>|?red>| ... |?white>
//-----------------------------------------------------------------------------
func tagsRegexp() *regexp.Regexp {
- tags := []string{}
+ arr := []string{}
- for color, _ := range colors {
- tags = append(tags, "?"+color+">")
+ for tag, _ := range tags {
+ arr = append(arr, "?"+tag+">")
}
- return regexp.MustCompile(strings.Join(tags, "|"))
+ return regexp.MustCompile(strings.Join(arr, "|"))
}
//
@@ -72,23 +73,34 @@ func tagName(str string) string {
//-----------------------------------------------------------------------------
func drawLine(x int, y int, str string) {
- column := 0
+ column, right := 0, false
foreground, background := termbox.ColorDefault, termbox.ColorDefault
for _, token := range just.Split(tagsRegexp(), str) {
if tag, open := isTag(token); tag {
- if color, ok := colors[tagName(token)]; ok {
+ key := tagName(token)
+ if value, ok := tags[key]; ok {
token = ""
- if open {
- foreground = color
- } else {
- foreground = termbox.ColorDefault
+ switch key {
+ case "right":
+ right = open
+ default:
+ if open {
+ foreground = value
+ } else {
+ foreground = termbox.ColorDefault
+ }
}
}
}
- for _, char := range token {
- termbox.SetCell(x+column, y, char, foreground, background)
+ for i, char := range token {
+ if !right {
+ termbox.SetCell(x+column, y, char, foreground, background)
+ } else {
+ width, _ := termbox.Size()
+ termbox.SetCell(width-len(token)+i, y, char, foreground, background)
+ }
column += 1
}
}
@@ -104,5 +116,5 @@ func drawScreen(str string) {
}
func DrawScreen(str string) {
- drawScreen(str)
+ drawScreen(str)
}
diff --git a/mop.go b/mop.go
index 3b90dab..1e62124 100644
--- a/mop.go
+++ b/mop.go
@@ -3,10 +3,9 @@
package main
import (
- "fmt"
- "time"
"github.com/michaeldv/mop/lib"
"github.com/nsf/termbox-go"
+ "time"
)
//-----------------------------------------------------------------------------
@@ -19,44 +18,40 @@ func initTermbox() {
//-----------------------------------------------------------------------------
func mainLoop(profile string) {
- event_queue := make(chan termbox.Event)
- event_tick := time.NewTicker(1 * time.Second)
+ event_queue := make(chan termbox.Event)
+ event_tick := time.NewTicker(1 * time.Second)
- go func() {
- for {
- event_queue <- termbox.PollEvent()
- }
- }()
+ go func() {
+ for {
+ event_queue <- termbox.PollEvent()
+ }
+ }()
- mop.Draw(profile)
+ mop.Draw(profile)
loop:
for {
- select {
- case event := <- event_queue:
- switch event.Type {
- case termbox.EventKey:
- if event.Key == termbox.KeyEsc {
- break loop
- }
- case termbox.EventResize:
- // Draw(profile)
- // x, y := termbox.Size()
- str := fmt.Sprintf("(%d:%d)", event.Width, event.Height)
- mop.DrawScreen(str + ": Hello world, how are you?")
- }
- case <-event_tick.C:
- mop.DrawScreen(time.Now().Format("3:04:05pm PST"))
- //mop.Draw(profile)
- }
+ select {
+ case event := <-event_queue:
+ switch event.Type {
+ case termbox.EventKey:
+ if event.Key == termbox.KeyEsc {
+ break loop
+ }
+ case termbox.EventResize:
+ mop.Draw(profile)
+ }
+ case <-event_tick.C:
+ mop.Draw(profile)
+ }
}
}
//-----------------------------------------------------------------------------
func main() {
- initTermbox()
+ initTermbox()
defer termbox.Close()
profile := mop.LoadProfile()
- mainLoop(profile)
+ mainLoop(profile)
}