repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
36f1b93
parent
6203700
author
Eric Bower
date
2024-10-06 15:39:22 +0000 UTC
chore(pico): logs work
2 files changed,  +43, -14
M shared/sendlog.go
+2, -2
 1@@ -340,9 +340,9 @@ func AnyToStr(mp map[string]any, key string) string {
 2 	return ""
 3 }
 4 
 5-func AnyToInt(mp map[string]any, key string) int64 {
 6+func AnyToFloat(mp map[string]any, key string) float64 {
 7 	if value, ok := mp[key]; ok {
 8-		if value, ok := value.(int64); ok {
 9+		if value, ok := value.(float64); ok {
10 			return value
11 		}
12 	}
M tui/logs/logs.go
+41, -12
  1@@ -41,6 +41,8 @@ func headerWidth(w int) int {
  2 	return w - 2
  3 }
  4 
  5+var defMsg = "Logs will show up here in realtime as they are generated.  There is no log buffer."
  6+
  7 func NewModel(shrd common.SharedModel) Model {
  8 	im := input.New()
  9 	im.Cursor.Style = shrd.Styles.Cursor
 10@@ -55,6 +57,7 @@ func NewModel(shrd common.SharedModel) Model {
 11 	inputHeight := lipgloss.Height(im.View())
 12 	viewport := viewport.New(ww, shrd.Height-hh-inputHeight)
 13 	viewport.YPosition = hh
 14+	viewport.SetContent(defMsg)
 15 
 16 	return Model{
 17 		shared:   shrd,
 18@@ -82,6 +85,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 19 		inputHeight := lipgloss.Height(m.input.View())
 20 		hh := headerHeight(m.shared)
 21 		m.viewport.Height = msg.Height - hh - inputHeight
 22+		m.viewport.SetContent(logsToStr(m.shared.Styles, m.logData, m.input.Value()))
 23 	case logLineLoadedMsg:
 24 		m.logData = append(m.logData, msg)
 25 		lng := len(m.logData)
 26@@ -92,7 +96,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 27 		if m.viewport.AtBottom() {
 28 			wasAt = true
 29 		}
 30-		m.viewport.SetContent(logsToStr(m.logData, m.input.Value()))
 31+		m.viewport.SetContent(logsToStr(m.shared.Styles, m.logData, m.input.Value()))
 32 		if wasAt {
 33 			m.viewport.GotoBottom()
 34 		}
 35@@ -108,7 +112,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 36 				cmds = append(cmds, m.input.Focus())
 37 			}
 38 		default:
 39-			m.viewport.SetContent(logsToStr(m.logData, m.input.Value()))
 40+			m.viewport.SetContent(logsToStr(m.shared.Styles, m.logData, m.input.Value()))
 41 			m.viewport.GotoBottom()
 42 		}
 43 	}
 44@@ -168,13 +172,13 @@ func matched(str, match string) bool {
 45 	return strings.Contains(prim, mtch)
 46 }
 47 
 48-func logToStr(data map[string]any, match string) string {
 49+func logToStr(styles common.Styles, data map[string]any, match string) string {
 50 	time := shared.AnyToStr(data, "time")
 51 	service := shared.AnyToStr(data, "service")
 52 	level := shared.AnyToStr(data, "level")
 53 	msg := shared.AnyToStr(data, "msg")
 54 	errMsg := shared.AnyToStr(data, "err")
 55-	status := shared.AnyToInt(data, "status")
 56+	status := shared.AnyToFloat(data, "status")
 57 	url := shared.AnyToStr(data, "url")
 58 
 59 	if match != "" {
 60@@ -183,32 +187,57 @@ func logToStr(data map[string]any, match string) string {
 61 		serviceMatch := matched(service, match)
 62 		errMatch := matched(errMsg, match)
 63 		urlMatch := matched(url, match)
 64-		if !lvlMatch && !msgMatch && !serviceMatch && !errMatch && !urlMatch {
 65+		statusMatch := matched(fmt.Sprintf("%d", int(status)), match)
 66+		if !lvlMatch && !msgMatch && !serviceMatch && !errMatch && !urlMatch && !statusMatch {
 67 			return ""
 68 		}
 69 	}
 70 
 71 	acc := fmt.Sprintf(
 72-		"%s\t%s\t%s\t%s\t%s\t%d\t%s",
 73+		"%s %s %s %s %s %s %s",
 74 		time,
 75 		service,
 76-		level,
 77+		levelView(styles, level),
 78 		msg,
 79-		errMsg,
 80-		status,
 81+		styles.Error.Render(errMsg),
 82+		statusView(styles, int(status)),
 83 		url,
 84 	)
 85-	acc += "\n"
 86 	return acc
 87 }
 88 
 89-func logsToStr(data []map[string]any, filter string) string {
 90+func statusView(styles common.Styles, status int) string {
 91+	statusStr := fmt.Sprintf("%d", status)
 92+	if status >= 200 && status < 300 {
 93+		return statusStr
 94+	}
 95+	return styles.Error.Render(statusStr)
 96+}
 97+
 98+func levelView(styles common.Styles, level string) string {
 99+	if level == "ERROR" {
100+		return styles.Error.Render(level)
101+	}
102+	return styles.Note.Render(level)
103+}
104+
105+func logsToStr(styles common.Styles, data []map[string]any, filter string) string {
106 	acc := ""
107 	for _, d := range data {
108-		res := logToStr(d, filter)
109+		res := logToStr(styles, d, filter)
110 		if res != "" {
111 			acc += res
112+			acc += "\n"
113 		}
114 	}
115+
116+	if acc == "" {
117+		if filter == "" {
118+			return defMsg
119+		} else {
120+			return "No results found for filter provided."
121+		}
122+	}
123+
124 	return acc
125 }