Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for titles in framed views #36

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

package gocui

import (
import
(
"errors"

"github.com/nsf/termbox-go"
Expand Down Expand Up @@ -330,6 +331,11 @@ func (g *Gui) flush() error {
if err := g.drawFrame(v); err != nil {
return err
}
if v.Title != "" {
if err := g.drawTitle(v); err != nil {
return err
}
}
}

if err := g.draw(v); err != nil {
Expand Down Expand Up @@ -379,6 +385,26 @@ func (g *Gui) drawFrame(v *View) error {
return nil
}

// drawTitle draws the title on top of a framed view 2 spaces left
func (g *Gui) drawTitle(v *View) error {
titleMax := len(v.Title)
for x := v.x0 + 2; x < v.x1-1 && x < g.maxX; x++ {
if x < 0 {
continue
}
if v.y0 > -1 && v.y0 < g.maxY {
if (x-v.x0-2) >= 0 && (x-v.x0-2) < titleMax {
titleChar := rune(v.Title[x-v.x0-2])
if err := g.SetRune(x, v.y0, titleChar/*'x'*/ ); err != nil {
return err
}
}
}
}

return nil
}

// draw manages the cursor and calls the draw function of a view.
func (g *Gui) draw(v *View) error {
if g.Cursor {
Expand Down
4 changes: 4 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type View struct {
// If Autoscroll is true, the View will automatically scroll down when the
// text overflows. If true the view's y-origin will be ignored.
Autoscroll bool

// If te view is framed, allow to show a title on it
Title string
}

type viewLine struct {
Expand All @@ -74,6 +77,7 @@ func newView(name string, x0, y0, x1, y1 int) *View {
y1: y1,
Frame: true,
tainted: true,
Title: "",
}
return v
}
Expand Down