Added <right>now()</right>

master
Michael Dvorkin 12 years ago
parent 5ace0edc38
commit cf92c5f858
  1. 15
      lib/format.go
  2. 46
      lib/screen.go
  3. 53
      mop.go

@ -5,11 +5,21 @@ package mop
import ( import (
"bytes" "bytes"
"text/template" "text/template"
"time"
) )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func Format(message []Message) string { func Format(message []Message) string {
markup := `{{range .}}<green>{{.Ticker}}</green> ${{.LastTrade}} <red>{{.Change}}</red> vars := struct {
Now string
Stocks []Message
}{
time.Now().Format("3:04:05pm PST"),
message,
}
markup := `Hello<right>{{.Now}}</right>
{{range .Stocks}}<green>{{.Ticker}}</green> ${{.LastTrade}} <red>{{.Change}}</red>
{{end}}...` {{end}}...`
template, err := template.New("screen").Parse(markup) template, err := template.New("screen").Parse(markup)
@ -18,9 +28,10 @@ func Format(message []Message) string {
} }
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
err = template.Execute(buffer, message) err = template.Execute(buffer, vars)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return buffer.String() return buffer.String()
} }

@ -3,10 +3,10 @@
package mop package mop
import ( import (
"regexp"
"strings"
"github.com/michaeldv/just" "github.com/michaeldv/just"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
"regexp"
"strings"
) )
// Can combine attributes and a single color using bitwise OR. // Can combine attributes and a single color using bitwise OR.
@ -15,7 +15,7 @@ import (
// AttrUnderline // AttrUnderline
// AttrReverse // AttrReverse
// //
var colors = map[string]termbox.Attribute{ var tags = map[string]termbox.Attribute{
"black": termbox.ColorBlack, "black": termbox.ColorBlack,
"red": termbox.ColorRed, "red": termbox.ColorRed,
"green": termbox.ColorGreen, "green": termbox.ColorGreen,
@ -24,6 +24,7 @@ var colors = map[string]termbox.Attribute{
"magenta": termbox.ColorMagenta, "magenta": termbox.ColorMagenta,
"cyan": termbox.ColorCyan, "cyan": termbox.ColorCyan,
"white": termbox.ColorWhite, "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> // </?black>|</?red>| ... |</?white>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func tagsRegexp() *regexp.Regexp { func tagsRegexp() *regexp.Regexp {
tags := []string{} arr := []string{}
for color, _ := range colors { for tag, _ := range tags {
tags = append(tags, "</?"+color+">") 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) { func drawLine(x int, y int, str string) {
column := 0 column, right := 0, false
foreground, background := termbox.ColorDefault, termbox.ColorDefault foreground, background := termbox.ColorDefault, termbox.ColorDefault
for _, token := range just.Split(tagsRegexp(), str) { for _, token := range just.Split(tagsRegexp(), str) {
if tag, open := isTag(token); tag { if tag, open := isTag(token); tag {
if color, ok := colors[tagName(token)]; ok { key := tagName(token)
if value, ok := tags[key]; ok {
token = "" token = ""
if open { switch key {
foreground = color case "right":
} else { right = open
foreground = termbox.ColorDefault default:
if open {
foreground = value
} else {
foreground = termbox.ColorDefault
}
} }
} }
} }
for _, char := range token { for i, char := range token {
termbox.SetCell(x+column, y, char, foreground, background) 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 column += 1
} }
} }
@ -104,5 +116,5 @@ func drawScreen(str string) {
} }
func DrawScreen(str string) { func DrawScreen(str string) {
drawScreen(str) drawScreen(str)
} }

@ -3,10 +3,9 @@
package main package main
import ( import (
"fmt"
"time"
"github.com/michaeldv/mop/lib" "github.com/michaeldv/mop/lib"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
"time"
) )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -19,44 +18,40 @@ func initTermbox() {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func mainLoop(profile string) { func mainLoop(profile string) {
event_queue := make(chan termbox.Event) event_queue := make(chan termbox.Event)
event_tick := time.NewTicker(1 * time.Second) event_tick := time.NewTicker(1 * time.Second)
go func() { go func() {
for { for {
event_queue <- termbox.PollEvent() event_queue <- termbox.PollEvent()
} }
}() }()
mop.Draw(profile) mop.Draw(profile)
loop: loop:
for { for {
select { select {
case event := <- event_queue: case event := <-event_queue:
switch event.Type { switch event.Type {
case termbox.EventKey: case termbox.EventKey:
if event.Key == termbox.KeyEsc { if event.Key == termbox.KeyEsc {
break loop break loop
} }
case termbox.EventResize: case termbox.EventResize:
// Draw(profile) mop.Draw(profile)
// x, y := termbox.Size() }
str := fmt.Sprintf("(%d:%d)", event.Width, event.Height) case <-event_tick.C:
mop.DrawScreen(str + ": <red>Hello world</red>, how <white>are</white> <blue>you?</blue>") mop.Draw(profile)
} }
case <-event_tick.C:
mop.DrawScreen(time.Now().Format("3:04:05pm PST"))
//mop.Draw(profile)
}
} }
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func main() { func main() {
initTermbox() initTermbox()
defer termbox.Close() defer termbox.Close()
profile := mop.LoadProfile() profile := mop.LoadProfile()
mainLoop(profile) mainLoop(profile)
} }

Loading…
Cancel
Save