Initial Commit
This commit is contained in:
		
							
								
								
									
										112
									
								
								viewhelper.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								viewhelper.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
			
		||||
package goback
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/jroimartin/gocui"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/* Returns s centered across w characters */
 | 
			
		||||
func CenterText(s string, w int, p string) string {
 | 
			
		||||
	s_w := len(s)
 | 
			
		||||
	repeated := ""
 | 
			
		||||
	if (w-s_w)/2 > 0 {
 | 
			
		||||
		repeated = strings.Repeat(p, (w-s_w)/2)
 | 
			
		||||
	}
 | 
			
		||||
	return repeated + s + repeated
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Print a Label
 | 
			
		||||
 *	s - contents
 | 
			
		||||
 *	x - x position
 | 
			
		||||
 *	y - y position
 | 
			
		||||
 *	w - width
 | 
			
		||||
 *	h - height
 | 
			
		||||
 */
 | 
			
		||||
func PrintLabel(s string, x int, y int, w int, h int, g *gocui.Gui, name string) error {
 | 
			
		||||
	v, err := g.SetView(name, x, y, x+w, y+h)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if err != gocui.ErrorUnkView {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		v.Frame = false
 | 
			
		||||
		fmt.Fprintln(v, s)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Print a Framed Label
 | 
			
		||||
 *	s - contents
 | 
			
		||||
 *	x - x position
 | 
			
		||||
 *	y - y position
 | 
			
		||||
 *	w - width
 | 
			
		||||
 *	h - height
 | 
			
		||||
 */
 | 
			
		||||
func PrintFramedLabel(s string, x int, y int, w int, h int, g *gocui.Gui, name string) error {
 | 
			
		||||
	if v, err := g.SetView(name, x, y, x+w, y+h); err != nil {
 | 
			
		||||
		if err != gocui.ErrorUnkView {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Fprintln(v, s)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func PrintButton(title string, x int, y int, w int, h int, g *gocui.Gui, name string) error {
 | 
			
		||||
	if v, err := g.SetView(name, x, y, x+w, y+h); err != nil {
 | 
			
		||||
		if err != gocui.ErrorUnkView {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		v.Highlight = true
 | 
			
		||||
		fmt.Fprintln(v, " "+CenterText(title, (w-2), " ")+" ")
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func PrintMenu(title string, x int, y int, w int, h int, options []string, g *gocui.Gui, name string) error {
 | 
			
		||||
	if title != "" {
 | 
			
		||||
		if v_title, err := g.SetView(name+"-menu-title", x, y, x+w, y+2); err != nil {
 | 
			
		||||
			if err != gocui.ErrorUnkView {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			fmt.Fprintln(v_title, title)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if v_menu, err := g.SetView(name, x, y+2, x+w, y+h-2); err != nil {
 | 
			
		||||
		if err != gocui.ErrorUnkView {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		v_menu.Highlight = true
 | 
			
		||||
		for _, m_opt := range options {
 | 
			
		||||
			fmt.Fprintln(v_menu, CenterText(m_opt, w, " "))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetLine(g *gocui.Gui, v *gocui.View) (string, error) {
 | 
			
		||||
	var l string
 | 
			
		||||
	var err error
 | 
			
		||||
	_, cy := v.Cursor()
 | 
			
		||||
	if l, err = v.Line(cy); err != nil {
 | 
			
		||||
		l = ""
 | 
			
		||||
	}
 | 
			
		||||
	return l, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Modal Types */
 | 
			
		||||
/*
 | 
			
		||||
const (
 | 
			
		||||
	ModalAlert   = iota // Just a popup
 | 
			
		||||
	ModalConfirm        // Ok/Cancel Popup
 | 
			
		||||
	ModalPrompt         // Prompt for Text Popup
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ModalButton struct {
 | 
			
		||||
	label  string // What is displayed on this button
 | 
			
		||||
	action func() // The function that gets called when 'enter' is pressed
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func PrintModal(title string, contents string, x int, y int, w int, h int, modalType int, g *gocui.Gui, name string) error {
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
		Reference in New Issue
	
	Block a user