From fe126d4d78ce3c1bf17c8e21417bf1e6b8406ce7 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 28 Dec 2025 16:28:55 +0100 Subject: [PATCH] Add padding fields to view --- _examples/custom_frame.go | 4 ++++ view.go | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/_examples/custom_frame.go b/_examples/custom_frame.go index ef4ae9f2..dd7df0ce 100644 --- a/_examples/custom_frame.go +++ b/_examples/custom_frame.go @@ -51,6 +51,7 @@ func layout(g *gocui.Gui) error { } v.Title = "v1" v.Autoscroll = true + v.PaddingX = 1 fmt.Fprintln(v, "View with default frame color") fmt.Fprintln(v, "It's connected to v2 with overlay RIGHT.\n") if _, err = setCurrentViewOnTop(g, "v1"); err != nil { @@ -66,6 +67,7 @@ func layout(g *gocui.Gui) error { v.Wrap = true v.FrameColor = gocui.ColorMagenta v.FrameRunes = []rune{'═', '│'} + v.PaddingX = 1 fmt.Fprintln(v, "View with minimum frame customization and colored frame.") fmt.Fprintln(v, "It's connected to v1 with overlay LEFT.\n") fmt.Fprintln(v, "\033[35;1mInstructions:\033[0m") @@ -83,6 +85,7 @@ func layout(g *gocui.Gui) error { v.FrameColor = gocui.ColorCyan v.TitleColor = gocui.ColorCyan v.FrameRunes = []rune{'═', '║', '╔', '╗', '╚', '╝'} + v.PaddingX = 1 fmt.Fprintln(v, "View with basic frame customization and colored frame and title") fmt.Fprintln(v, "It's not connected to any view.") } @@ -96,6 +99,7 @@ func layout(g *gocui.Gui) error { v.TitleColor = gocui.ColorYellow v.FrameColor = gocui.ColorRed v.FrameRunes = []rune{'═', '║', '╔', '╗', '╚', '╝', '╠', '╣', '╦', '╩', '╬'} + v.PaddingX = 1 fmt.Fprintln(v, "View with fully customized frame and colored title differently.") fmt.Fprintln(v, "It's connected to v3 with overlay LEFT.\n") v.SetCursor(0, 3) diff --git a/view.go b/view.go index e5b5c046..bd13580f 100644 --- a/view.go +++ b/view.go @@ -132,6 +132,12 @@ type View struct { // []rune{'═','║','╔','╗','╚','╝','╠','╣','╦','╩','╬'} FrameRunes []rune + // PaddingX specifies the horizontal padding (left and right) inside the frame. + PaddingX int + + // PaddingY specifies the vertical padding (top and bottom) inside the frame. + PaddingY int + // If Wrap is true, the content that is written to this View is // automatically wrapped when it is longer than its width. If true the // view's x-origin will be ignored. @@ -474,7 +480,7 @@ func (v *View) Height() int { // view is made 1 larger on all sides. I'd like to clean this up at some point, // but for now we live with this weirdness. func (v *View) InnerWidth() int { - innerWidth := v.Width() - 2 + innerWidth := v.Width() - 2 - 2*v.PaddingX if innerWidth < 0 { return 0 } @@ -483,7 +489,7 @@ func (v *View) InnerWidth() int { } func (v *View) InnerHeight() int { - innerHeight := v.Height() - 2 + innerHeight := v.Height() - 2 - 2*v.PaddingY if innerHeight < 0 { return 0 } @@ -551,7 +557,7 @@ func (v *View) setCharacter(x, y int, ch string, fgColor, bgColor Attribute) { ch = " " } - tcellSetCell(v.x0+x+1, v.y0+y+1, ch, fgColor, bgColor, v.outMode) + tcellSetCell(v.x0+x+1+v.PaddingX, v.y0+y+1+v.PaddingY, ch, fgColor, bgColor, v.outMode) } // SetCursor sets the cursor position of the view at the given point,