Skip to content

Commit

Permalink
Move template to viewer settings
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-jozwikowski committed Jan 23, 2024
1 parent b963b21 commit fecb4fc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
9 changes: 4 additions & 5 deletions mcv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ func main() {
return
}

t := initiateTemplate() //get template from the CLI params, or the default one

colors.SetEnabled(!*cli.RuntimeConfig.NoColors) // enable or disable the colors based on CLI flag
viewer.SetSettings(viewer.Settings{
NoNewLine: *cli.RuntimeConfig.NoNewLine, // don't add empty lines
ShowFileChangeLine: *cli.RuntimeConfig.ShowFileChange, // show file change from tail
ShowParsedLinesOnly: *cli.RuntimeConfig.ParsedLineOnly, // don't show unparsed lines
Template: initiateTemplate(), // get template from the CLI params, or the default one
})
if *cli.RuntimeConfig.Test == true {
// test values @todo - put those to tests
Expand All @@ -40,7 +39,7 @@ func main() {

for _, line := range values {
logLineItem := viewer.InitLogLine(line)
fmt.Print(logLineItem.GetFormattedString(t))
fmt.Print(logLineItem.GetFormattedString())
}
return
}
Expand All @@ -51,8 +50,8 @@ func main() {
for scanner.Scan() { // wait for a line of text
line := scanner.Text()

logLineItem := viewer.InitLogLine(line) // init the logItem from line string
fmt.Print(logLineItem.GetFormattedString(t)) // format it and print it
logLineItem := viewer.InitLogLine(line) // init the logItem from line string
fmt.Print(logLineItem.GetFormattedString()) // format it and print it
}

if err := scanner.Err(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions src/viewer/logLine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"monolog-cli-viewer/src/colors"
"strconv"
"text/template"
"time"

"github.com/stretchr/objx"
Expand All @@ -30,7 +29,7 @@ func LogLineFromObjx(json objx.Map, rawLine string) *LogLine {
return &l
}

func (item *LogLine) GetFormattedString(t *template.Template) string {
func (item *LogLine) GetFormattedString() string {

if item.json == nil { // if the item has no json - there was a problem decoding it
if item.raw != "" {
Expand All @@ -54,7 +53,7 @@ func (item *LogLine) GetFormattedString(t *template.Template) string {
}

var tpl bytes.Buffer
err2 := t.Execute(&tpl, item.json)
err2 := settings.Template.Execute(&tpl, item.json)
if err2 != nil {
panic(err2) // there was an error executing the template and it wasn't in the data
}
Expand Down
3 changes: 3 additions & 0 deletions src/viewer/settings.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package viewer

import "text/template"

type Settings struct {
NoNewLine bool
ShowFileChangeLine bool
ShowParsedLinesOnly bool
Template *template.Template
}

var settings Settings = Settings{
Expand Down

0 comments on commit fecb4fc

Please sign in to comment.