2f9ac5aebbe911de327f9043a7ad1452607f3843 — Elias Naur 14 days ago 628a97c
ui/text: rename Center Alignment to Middle and drop IsNewline

Middle matches the similar layout.Middle constant, and IsNewline is
too simple to export.

Add documentation while we're here.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
4 files changed, 44 insertions(+), 16 deletions(-)

M ui/measure/measure.go
M ui/text/editor.go
M ui/text/label.go
M ui/text/measure.go
M ui/measure/measure.go => ui/measure/measure.go +1 -1
@@ 179,7 179,7 @@ }
  	for prev.idx < len(str) {
  		c, s := utf8.DecodeRuneInString(str[prev.idx:])
- 		nl := text.IsNewline(c)
+ 		nl := c == '\n'
  		if opts.SingleLine && nl {
  			nl = false
  			c = ' '

M ui/text/editor.go => ui/text/editor.go +1 -1
@@ 334,7 334,7 @@ // up all available space.
  		if len(s) > 0 {
  			r, _ := utf8.DecodeLastRuneInString(s)
- 			if !IsNewline(r) {
+ 			if r != '\n' {
  				dims.Size.X = e.maxWidth
  				break
  			}

M ui/text/label.go => ui/text/label.go +13 -4
@@ 1,5 1,6 @@ // SPDX-License-Identifier: Unlicense OR MIT
  
+ // Package text implements text widgets.
  package text
  
  import (


@@ 15,12 16,20 @@ "golang.org/x/image/math/fixed"
  )
  
+ // Label is a widget for laying out and drawing text.
  type Label struct {
- 	Face      Face
- 	Material  ui.MacroOp
+ 	// Face define the font and style of the text.
+ 	Face Face
+ 	// Material is a macro recording the material to draw the
+ 	// text. Use a ColorOp for colored text.
+ 	Material ui.MacroOp
+ 	// Alignment specify the text alignment.
  	Alignment Alignment
- 	Text      string
- 	MaxLines  int
+ 	// Text is the string to draw.
+ 	Text string
+ 	// MaxLines specify the maximum number of lines the text
+ 	// may fill.
+ 	MaxLines int
  
  	it lineIterator
  }

M ui/text/measure.go => ui/text/measure.go +29 -10
@@ 11,6 11,7 @@ "golang.org/x/image/math/fixed"
  )
  
+ // A Line contains the measurements of a line of text.
  type Line struct {
  	Text String
  	// Width is the width of the line.


@@ 25,22 26,31 @@ }
  
  type String struct {
- 	String   string
+ 	String string
+ 	// Advances contain the advance of each rune in String.
  	Advances []fixed.Int26_6
  }
  
+ // A Layout contains the measurements of a body of text as
+ // a list of Lines.
  type Layout struct {
  	Lines []Line
  }
  
+ // LayoutOptions specify the constraints of a text layout.
  type LayoutOptions struct {
- 	MaxWidth   int
+ 	// MaxWidth set the maximum width of the layout.
+ 	MaxWidth int
+ 	// SingleLine specify that line breaks are ignored.
  	SingleLine bool
  }
  
  type Face interface {
- 	Layout(str string, opts LayoutOptions) *Layout
- 	Path(str String) ui.MacroOp
+ 	// Layout returns the text layout for a string given a set of
+ 	// options.
+ 	Layout(s string, opts LayoutOptions) *Layout
+ 	// Path returns the ClipOp outline of a text recorded in a macro.
+ 	Path(s String) ui.MacroOp
  }
  
  type Alignment uint8


@@ 48,7 58,7 @@ const (
  	Start Alignment = iota
  	End
- 	Center
+ 	Middle
  )
  
  func linesDimens(lines []Line) layout.Dimens {


@@ 77,14 87,10 @@ }
  }
  
- func IsNewline(r rune) bool {
- 	return r == '\n'
- }
- 
  func align(align Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 {
  	mw := fixed.I(maxWidth)
  	switch align {
- 	case Center:
+ 	case Middle:
  		return fixed.I(((mw - width) / 2).Floor())
  	case End:
  		return fixed.I((mw - width).Floor())


@@ 94,3 100,16 @@ panic(fmt.Errorf("unknown alignment %v", align))
  	}
  }
+ 
+ func (a Alignment) String() string {
+ 	switch a {
+ 	case Start:
+ 		return "Start"
+ 	case End:
+ 		return "End"
+ 	case Middle:
+ 		return "Middle"
+ 	default:
+ 		panic("unreachable")
+ 	}
+ }