From 5f2faec53cd88d723d8e7572e2f2a73949d6025a Mon Sep 17 00:00:00 2001 From: crecher Date: Wed, 21 Oct 2026 22:16:49 +0000 Subject: fix: crash on startup, feat: hot reload Signed-off-by: crecher --- main.go | 69 ++++++++++++++++++++++++++++------------------------------------- 1 file changed, 30 insertions(+), 39 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 30c514f..f0f954c 100644 --- a/main.go +++ b/main.go @@ -146,6 +146,10 @@ func setup(conn *xgb.Conn) { startStatus() + if restoreState(conn) { + return + } + for _, cmd := range startup { exec.Command(cmd) log.Println("Running startup command: " + cmd) @@ -170,9 +174,11 @@ func startStatus() { statusChan = make(chan string, 1) go func() { sc := bufio.NewScanner(os.Stdin) - if sc.Err != nil { - log.Println("scanner failed: ", sc.Err().Error) - } + defer func() { + if err := sc.Err(); err != nil { + log.Println("scanner failed:", err) + } + }() for sc.Scan() { text := sc.Text() select { @@ -404,7 +410,7 @@ func tileVertical(conn *xgb.Conn) { i := 0 for _, c := range ActiveClients { - yOffset := int(barHeight) + (i * heightPerWin) + yOffset := i * heightPerWin height := heightPerWin - gapSize if i == len(ActiveClients)-1 { height = heightPerWin - gapSize*2 @@ -449,8 +455,8 @@ func tileHorizontal(conn *xgb.Conn) { xproto.ConfigWindowWidth| xproto.ConfigWindowHeight, []uint32{ - uint32(xOffset + gapSize), // x - uint32(int(barHeight) + gapSize), // y + uint32(xOffset + gapSize), // x + uint32(gapSize), // y uint32(width), uint32(screenHeight - int(barHeight) - gapSize*2), }) @@ -485,9 +491,9 @@ func tileWithMaster(conn *xgb.Conn) { xproto.ConfigWindowWidth| xproto.ConfigWindowHeight, []uint32{ - uint32(0), // x - uint32(int(barHeight)), // y - uint32(screenWidth), // width + uint32(0), // x + uint32(0), // y + uint32(screenWidth), // width uint32(screenHeight - int(barHeight)), }) } else { @@ -498,14 +504,14 @@ func tileWithMaster(conn *xgb.Conn) { xproto.ConfigWindowHeight, []uint32{ uint32(0 + gapSize), // x - uint32(int(barHeight) + gapSize), // y + uint32(gapSize), // y uint32(screenWidth/2 - int(gapSize)), // width uint32(screenHeight - int(barHeight) - gapSize*2), }) } } else { heightPerWin := usableHeight / (n - 1) - yOffset := int(barHeight) + (i * heightPerWin) + gapSize + yOffset := (i * heightPerWin) + gapSize xOffset := uint32(widthPerWin + gapSize) height := heightPerWin - gapSize*2 // log.Println(len(ActiveClients), i) @@ -548,7 +554,7 @@ func createBar(conn *xgb.Conn) { screen.RootDepth, barWindow, root, - 0, 0, // x, y + 0, int16(int(screen.HeightInPixels)-int(barHeight)), // x, y screen.WidthInPixels, barHeight, 0, // border xproto.WindowClassInputOutput, @@ -568,7 +574,8 @@ func createBar(conn *xgb.Conn) { if h := fontAscent + fontDescent; h > 0 && uint16(h) != barHeight { barHeight = uint16(h) xproto.ConfigureWindow(conn, barWindow, - xproto.ConfigWindowHeight, []uint32{uint32(barHeight)}) + xproto.ConfigWindowY|xproto.ConfigWindowHeight, + []uint32{uint32(int(screen.HeightInPixels) - int(barHeight)), uint32(barHeight)}) } } @@ -677,42 +684,26 @@ func drawBar(conn *xgb.Conn) { drawBarText(conn, barWidth-stw, 0, int(barHeight), statusText, barFg, barColor) } - var occ, focc BitMask - if c, ok := clients[focused]; ok { - focc = c.Tags - } + var occ BitMask for _, c := range clients { occ |= c.Tags } - boxs := int(barHeight) / 9 - boxw := int(barHeight)/6 + 2 - x := 0 for i := 0; i < tagCount; i++ { var mask BitMask = 1 << uint(i) - digit := strconv.Itoa(i + 1) - w := textWidth(conn, digit) + int(barHeight) sel := (GlobalTags & mask) != 0 - fg, bg := barFg, barColor - if sel { - fg, bg = selFg, selBg - rect := xproto.Rectangle{X: int16(x), Y: 0, Width: uint16(w), Height: barHeight} - xproto.ChangeGC(conn, barGC, xproto.GcForeground|xproto.GcBackground, []uint32{selBg, selBg}) - xproto.PolyFillRectangle(conn, xproto.Drawable(barWindow), barGC, []xproto.Rectangle{rect}) + if !sel && occ&mask == 0 { + continue } - drawBarText(conn, x+int(barHeight)/2, 0, int(barHeight), digit, fg, bg) - if occ&mask != 0 { - rect := xproto.Rectangle{X: int16(x + boxs), Y: int16(boxs), Width: uint16(boxw), Height: uint16(boxw)} - if focc&mask != 0 { - xproto.ChangeGC(conn, barGC, xproto.GcForeground|xproto.GcBackground, []uint32{fg, fg}) - xproto.PolyFillRectangle(conn, xproto.Drawable(barWindow), barGC, []xproto.Rectangle{rect}) - } else { - xproto.ChangeGC(conn, barGC, xproto.GcForeground|xproto.GcBackground, []uint32{fg, bg}) - xproto.PolyRectangle(conn, xproto.Drawable(barWindow), barGC, []xproto.Rectangle{rect}) - } + label := strconv.Itoa(i + 1) + fg := barFg + if sel { + label += "*" + fg = selFg } - x += w + drawBarText(conn, x+int(barHeight)/3, 0, int(barHeight), label, fg, barColor) + x += textWidth(conn, label) + int(barHeight)*2/3 } if windowTitle != 0 { -- cgit v1.2.3