...

Source file src/github.com/gdamore/tcell/v2/views/_demos/hbox.go

Documentation: github.com/gdamore/tcell/v2/views/_demos

     1  // +build ignore
     2  
     3  // Copyright 2015 The Tops'l Authors
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use file except in compliance with the License.
     7  // You may obtain a copy of the license at
     8  //
     9  //    http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/gdamore/tcell/v2"
    24  	"github.com/gdamore/tcell/v2/views"
    25  )
    26  
    27  type boxL struct {
    28  	views.BoxLayout
    29  }
    30  
    31  var app = &views.Application{}
    32  var box = &boxL{}
    33  
    34  func (m *boxL) HandleEvent(ev tcell.Event) bool {
    35  	switch ev := ev.(type) {
    36  	case *tcell.EventKey:
    37  		if ev.Key() == tcell.KeyEscape {
    38  			app.Quit()
    39  			return true
    40  		}
    41  	}
    42  	return m.BoxLayout.HandleEvent(ev)
    43  }
    44  
    45  func main() {
    46  
    47  	title := &views.TextBar{}
    48  	title.SetStyle(tcell.StyleDefault.
    49  		Background(tcell.ColorYellow).
    50  		Foreground(tcell.ColorBlack))
    51  	title.SetCenter("Horizontal Boxes", tcell.StyleDefault)
    52  	title.SetLeft("ESC to exit", tcell.StyleDefault.
    53  		Background(tcell.ColorBlue).
    54  		Foreground(tcell.ColorWhite))
    55  	title.SetRight("==>X", tcell.StyleDefault)
    56  
    57  	inner := views.NewBoxLayout(views.Horizontal)
    58  
    59  	l := views.NewText()
    60  	m := views.NewText()
    61  	r := views.NewText()
    62  
    63  	l.SetText("Left (0.0)")
    64  	m.SetText("Middle (0.7)")
    65  	r.SetText("Right (0.3)")
    66  	l.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).
    67  		Background(tcell.ColorRed))
    68  	m.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).
    69  		Background(tcell.ColorLime))
    70  	r.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).
    71  		Background(tcell.ColorBlue))
    72  	l.SetAlignment(views.AlignBegin)
    73  	m.SetAlignment(views.AlignMiddle)
    74  	r.SetAlignment(views.AlignEnd)
    75  
    76  	inner.AddWidget(l, 0)
    77  	inner.AddWidget(m, 0.7)
    78  	inner.AddWidget(r, 0.3)
    79  
    80  	box.SetOrientation(views.Vertical)
    81  	box.AddWidget(title, 0)
    82  	box.AddWidget(inner, 1)
    83  	app.SetRootWidget(box)
    84  	if e := app.Run(); e != nil {
    85  		fmt.Fprintln(os.Stderr, e.Error())
    86  		os.Exit(1)
    87  	}
    88  }
    89  

View as plain text