(feature): logs in ui

This commit is contained in:
TheDevisi
2026-04-11 13:36:53 +03:00
parent c8125770a7
commit f6cb725542
3 changed files with 73 additions and 4 deletions

View File

@@ -2,10 +2,22 @@ package main
import "fmt"
var currentProgram *Program
func log(msg string, args ...interface{}) {
var formattedMsg string
if len(args) > 0 {
fmt.Printf(msg+"\n", args...)
formattedMsg = fmt.Sprintf(msg, args...)
fmt.Println(formattedMsg)
} else {
formattedMsg = msg
fmt.Println(msg)
}
if currentProgram != nil && currentProgram.LogsChannel != nil {
select {
case currentProgram.LogsChannel <- formattedMsg:
default:
}
}
}

View File

@@ -5,8 +5,11 @@ import (
"runtime"
"sync"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
@@ -20,6 +23,11 @@ type Program struct {
Config *Config
Cmd *exec.Cmd
CmdMu sync.Mutex
LogsArea *widget.RichText
LogsText *widget.Label
LogsChannel chan string
LogsContent string
LogsMu sync.Mutex
}
func main() {
@@ -33,8 +41,10 @@ func NewProgram() *Program {
uOs := runtime.GOOS
log("RUNTIME: Detected OS - %v", uOs)
p := &Program{
App: app.New(),
App: app.New(),
LogsChannel: make(chan string, 100),
}
currentProgram = p
cfg := p.loadConfig()
cfg.Os = uOs
p.Config = cfg
@@ -65,10 +75,55 @@ func (p *Program) Run() {
}
})
w.SetContent(container.NewBorder(
// Create logs display area
p.LogsText = widget.NewLabel("")
p.LogsText.Wrapping = fyne.TextWrapWord
logsScroll := container.NewScroll(p.LogsText)
logsScroll.SetMinSize(fyne.NewSize(0, 300))
// Create styled logs box with darker background
bgRect := canvas.NewRectangle(color.NRGBA{R: 40, G: 40, B: 40, A: 255})
logsWithPadding := container.NewBorder(
widget.NewLabel("Logs"),
nil, nil, nil,
logsScroll,
)
logsBox := container.NewStack(
bgRect,
container.NewBorder(
nil, nil, nil, nil,
logsWithPadding,
),
)
topBar := container.NewBorder(
settingsBtn,
p.RunCheck, nil, nil,
))
)
mainContent := container.NewVBox(
topBar,
logsBox,
)
w.SetContent(mainContent)
go p.listenLogs()
log("Window created and running...")
w.ShowAndRun()
}
func (p *Program) listenLogs() {
for logMsg := range p.LogsChannel {
fyne.Do(func() {
if p.LogsText != nil {
p.LogsMu.Lock()
p.LogsContent += logMsg + "\n"
logsToDisplay := p.LogsContent
p.LogsMu.Unlock()
p.LogsText.SetText(logsToDisplay)
}
})
}
}

View File

@@ -1,7 +1,9 @@
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"time"