...

Source file src/gitlab.com/tslocum/cview/textview.go

Documentation: gitlab.com/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"bytes"
     5  	"regexp"
     6  	"sync"
     7  	"unicode"
     8  	"unicode/utf8"
     9  
    10  	"github.com/gdamore/tcell/v2"
    11  	"github.com/lucasb-eyer/go-colorful"
    12  	"github.com/mattn/go-runewidth"
    13  	"github.com/rivo/uniseg"
    14  )
    15  
    16  var (
    17  	openColorRegex  = regexp.MustCompile(`\[([a-zA-Z]*|#[0-9a-zA-Z]*)$`)
    18  	openRegionRegex = regexp.MustCompile(`\["[a-zA-Z0-9_,;: \-\.]*"?$`)
    19  	newLineRegex    = regexp.MustCompile(`\r?\n`)
    20  
    21  	// TabSize is the number of spaces with which a tab character will be replaced.
    22  	TabSize = 4
    23  )
    24  
    25  // textViewIndex contains information about each line displayed in the text
    26  // view.
    27  type textViewIndex struct {
    28  	Line            int    // The index into the "buffer" variable.
    29  	Pos             int    // The index into the "buffer" line ([]byte position).
    30  	NextPos         int    // The (byte) index of the next character in this buffer line.
    31  	Width           int    // The screen width of this line.
    32  	ForegroundColor string // The starting foreground color ("" = don't change, "-" = reset).
    33  	BackgroundColor string // The starting background color ("" = don't change, "-" = reset).
    34  	Attributes      string // The starting attributes ("" = don't change, "-" = reset).
    35  	Region          []byte // The starting region ID.
    36  }
    37  
    38  // textViewRegion contains information about a region.
    39  type textViewRegion struct {
    40  	// The region ID.
    41  	ID []byte
    42  
    43  	// The starting and end screen position of the region as determined the last
    44  	// time Draw() was called. A negative value indicates out-of-rect positions.
    45  	FromX, FromY, ToX, ToY int
    46  }
    47  
    48  // TextView is a box which displays text. It implements the io.Writer interface
    49  // so you can stream text to it. This does not trigger a redraw automatically
    50  // but if a handler is installed via SetChangedFunc(), you can cause it to be
    51  // redrawn. (See SetChangedFunc() for more details.)
    52  //
    53  // Navigation
    54  //
    55  // If the text view is scrollable (the default), text is kept in a buffer which
    56  // may be larger than the screen and can be navigated similarly to Vim:
    57  //
    58  //   - h, left arrow: Move left.
    59  //   - l, right arrow: Move right.
    60  //   - j, down arrow: Move down.
    61  //   - k, up arrow: Move up.
    62  //   - g, home: Move to the top.
    63  //   - G, end: Move to the bottom.
    64  //   - Ctrl-F, page down: Move down by one page.
    65  //   - Ctrl-B, page up: Move up by one page.
    66  //
    67  // If the text is not scrollable, any text above the top visible line is
    68  // discarded.
    69  //
    70  // Use SetInputCapture() to override or modify keyboard input.
    71  //
    72  // Colors
    73  //
    74  // If dynamic colors are enabled via SetDynamicColors(), text color can be
    75  // changed dynamically by embedding color strings in square brackets. This works
    76  // the same way as anywhere else. Please see the package documentation for more
    77  // information.
    78  //
    79  // Regions and Highlights
    80  //
    81  // If regions are enabled via SetRegions(), you can define text regions within
    82  // the text and assign region IDs to them. Text regions start with region tags.
    83  // Region tags are square brackets that contain a region ID in double quotes,
    84  // for example:
    85  //
    86  //   We define a ["rg"]region[""] here.
    87  //
    88  // A text region ends with the next region tag. Tags with no region ID ([""])
    89  // don't start new regions. They can therefore be used to mark the end of a
    90  // region. Region IDs must satisfy the following regular expression:
    91  //
    92  //   [a-zA-Z0-9_,;: \-\.]+
    93  //
    94  // Regions can be highlighted by calling the Highlight() function with one or
    95  // more region IDs. This can be used to display search results, for example.
    96  //
    97  // The ScrollToHighlight() function can be used to jump to the currently
    98  // highlighted region once when the text view is drawn the next time.
    99  type TextView struct {
   100  	*Box
   101  
   102  	// The text buffer.
   103  	buffer [][]byte
   104  
   105  	// The last bytes that have been received but are not part of the buffer yet.
   106  	recentBytes []byte
   107  
   108  	// The last width and height of the text view.
   109  	lastWidth, lastHeight int
   110  
   111  	// The processed line index. This is nil if the buffer has changed and needs
   112  	// to be re-indexed.
   113  	index []*textViewIndex
   114  
   115  	// The width of the text view buffer index.
   116  	indexWidth int
   117  
   118  	// If set to true, the buffer will be reindexed each time it is modified.
   119  	reindex bool
   120  
   121  	// The text alignment, one of AlignLeft, AlignCenter, or AlignRight.
   122  	align int
   123  
   124  	// Information about visible regions as of the last call to Draw().
   125  	regionInfos []*textViewRegion
   126  
   127  	// Indices into the "index" slice which correspond to the first line of the
   128  	// first highlight and the last line of the last highlight. This is calculated
   129  	// during re-indexing. Set to -1 if there is no current highlight.
   130  	fromHighlight, toHighlight int
   131  
   132  	// The screen space column of the highlight in its first line. Set to -1 if
   133  	// there is no current highlight.
   134  	posHighlight int
   135  
   136  	// A set of region IDs that are currently highlighted.
   137  	highlights map[string]struct{}
   138  
   139  	// The screen width of the longest line in the index (not the buffer).
   140  	longestLine int
   141  
   142  	// The index of the first line shown in the text view.
   143  	lineOffset int
   144  
   145  	// The maximum number of newlines the text view will hold (0 = unlimited).
   146  	maxLines int
   147  
   148  	// If set to true, the text view will always remain at the end of the content.
   149  	trackEnd bool
   150  
   151  	// The number of characters to be skipped on each line (not in wrap mode).
   152  	columnOffset int
   153  
   154  	// The height of the content the last time the text view was drawn.
   155  	pageSize int
   156  
   157  	// If set to true, the text view will keep a buffer of text which can be
   158  	// navigated when the text is longer than what fits into the box.
   159  	scrollable bool
   160  
   161  	// Visibility of the scroll bar.
   162  	scrollBarVisibility ScrollBarVisibility
   163  
   164  	// The scroll bar color.
   165  	scrollBarColor tcell.Color
   166  
   167  	// If set to true, lines that are longer than the available width are wrapped
   168  	// onto the next line. If set to false, any characters beyond the available
   169  	// width are discarded.
   170  	wrap bool
   171  
   172  	// The maximum line width when wrapping (0 = use TextView width).
   173  	wrapWidth int
   174  
   175  	// If set to true and if wrap is also true, lines are split at spaces or
   176  	// after punctuation characters.
   177  	wordWrap bool
   178  
   179  	// The (starting) color of the text.
   180  	textColor tcell.Color
   181  
   182  	// If set to true, the text color can be changed dynamically by piping color
   183  	// strings in square brackets to the text view.
   184  	dynamicColors bool
   185  
   186  	// If set to true, region tags can be used to define regions.
   187  	regions bool
   188  
   189  	// A temporary flag which, when true, will automatically bring the current
   190  	// highlight(s) into the visible screen.
   191  	scrollToHighlights bool
   192  
   193  	// If true, setting new highlights will be a XOR instead of an overwrite
   194  	// operation.
   195  	toggleHighlights bool
   196  
   197  	// An optional function which is called when the content of the text view has
   198  	// changed.
   199  	changed func()
   200  
   201  	// An optional function which is called when the user presses one of the
   202  	// following keys: Escape, Enter, Tab, Backtab.
   203  	done func(tcell.Key)
   204  
   205  	// An optional function which is called when one or more regions were
   206  	// highlighted.
   207  	highlighted func(added, removed, remaining []string)
   208  
   209  	sync.RWMutex
   210  }
   211  
   212  // NewTextView returns a new text view.
   213  func NewTextView() *TextView {
   214  	return &TextView{
   215  		Box:                 NewBox(),
   216  		highlights:          make(map[string]struct{}),
   217  		lineOffset:          -1,
   218  		reindex:             true,
   219  		scrollable:          true,
   220  		scrollBarVisibility: ScrollBarAuto,
   221  		scrollBarColor:      Styles.ScrollBarColor,
   222  		align:               AlignLeft,
   223  		wrap:                true,
   224  		textColor:           Styles.PrimaryTextColor,
   225  	}
   226  }
   227  
   228  // SetScrollable sets the flag that decides whether or not the text view is
   229  // scrollable. If true, text is kept in a buffer and can be navigated. If false,
   230  // the last line will always be visible.
   231  func (t *TextView) SetScrollable(scrollable bool) {
   232  	t.Lock()
   233  	defer t.Unlock()
   234  
   235  	t.scrollable = scrollable
   236  	if !scrollable {
   237  		t.trackEnd = true
   238  	}
   239  }
   240  
   241  // SetScrollBarVisibility specifies the display of the scroll bar.
   242  func (t *TextView) SetScrollBarVisibility(visibility ScrollBarVisibility) {
   243  	t.Lock()
   244  	defer t.Unlock()
   245  
   246  	t.scrollBarVisibility = visibility
   247  }
   248  
   249  // SetScrollBarColor sets the color of the scroll bar.
   250  func (t *TextView) SetScrollBarColor(color tcell.Color) {
   251  	t.Lock()
   252  	defer t.Unlock()
   253  
   254  	t.scrollBarColor = color
   255  }
   256  
   257  // SetWrap sets the flag that, if true, leads to lines that are longer than the
   258  // available width being wrapped onto the next line. If false, any characters
   259  // beyond the available width are not displayed.
   260  func (t *TextView) SetWrap(wrap bool) {
   261  	t.Lock()
   262  	defer t.Unlock()
   263  
   264  	if t.wrap != wrap {
   265  		t.index = nil
   266  	}
   267  	t.wrap = wrap
   268  }
   269  
   270  // SetWordWrap sets the flag that, if true and if the "wrap" flag is also true
   271  // (see SetWrap()), wraps the line at spaces or after punctuation marks. Note
   272  // that trailing spaces will not be printed.
   273  //
   274  // This flag is ignored if the "wrap" flag is false.
   275  func (t *TextView) SetWordWrap(wrapOnWords bool) {
   276  	t.Lock()
   277  	defer t.Unlock()
   278  
   279  	if t.wordWrap != wrapOnWords {
   280  		t.index = nil
   281  	}
   282  	t.wordWrap = wrapOnWords
   283  }
   284  
   285  // SetTextAlign sets the text alignment within the text view. This must be
   286  // either AlignLeft, AlignCenter, or AlignRight.
   287  func (t *TextView) SetTextAlign(align int) {
   288  	t.Lock()
   289  	defer t.Unlock()
   290  
   291  	if t.align != align {
   292  		t.index = nil
   293  	}
   294  	t.align = align
   295  }
   296  
   297  // SetTextColor sets the initial color of the text (which can be changed
   298  // dynamically by sending color strings in square brackets to the text view if
   299  // dynamic colors are enabled).
   300  func (t *TextView) SetTextColor(color tcell.Color) {
   301  	t.Lock()
   302  	defer t.Unlock()
   303  
   304  	t.textColor = color
   305  }
   306  
   307  // SetBytes sets the text of this text view to the provided byte slice.
   308  // Previously contained text will be removed.
   309  func (t *TextView) SetBytes(text []byte) {
   310  	t.Lock()
   311  	defer t.Unlock()
   312  
   313  	t.clear()
   314  	t.write(text)
   315  }
   316  
   317  // SetText sets the text of this text view to the provided string. Previously
   318  // contained text will be removed.
   319  func (t *TextView) SetText(text string) {
   320  	t.SetBytes([]byte(text))
   321  }
   322  
   323  // GetBytes returns the current text of this text view. If "stripTags" is set
   324  // to true, any region/color tags are stripped from the text.
   325  func (t *TextView) GetBytes(stripTags bool) []byte {
   326  	t.RLock()
   327  	defer t.RUnlock()
   328  
   329  	if !stripTags {
   330  		if len(t.recentBytes) > 0 {
   331  			return bytes.Join(append(t.buffer, t.recentBytes), []byte("\n"))
   332  		}
   333  		return bytes.Join(t.buffer, []byte("\n"))
   334  	}
   335  
   336  	buffer := bytes.Join(t.buffer, []byte("\n"))
   337  	return StripTags(buffer, t.dynamicColors, t.regions)
   338  }
   339  
   340  // GetText returns the current text of this text view. If "stripTags" is set
   341  // to true, any region/color tags are stripped from the text.
   342  func (t *TextView) GetText(stripTags bool) string {
   343  	return string(t.GetBytes(stripTags))
   344  }
   345  
   346  // SetDynamicColors sets the flag that allows the text color to be changed
   347  // dynamically. See class description for details.
   348  func (t *TextView) SetDynamicColors(dynamic bool) {
   349  	t.Lock()
   350  	defer t.Unlock()
   351  
   352  	if t.dynamicColors != dynamic {
   353  		t.index = nil
   354  	}
   355  	t.dynamicColors = dynamic
   356  }
   357  
   358  // SetRegions sets the flag that allows to define regions in the text. See class
   359  // description for details.
   360  func (t *TextView) SetRegions(regions bool) {
   361  	t.Lock()
   362  	defer t.Unlock()
   363  
   364  	if t.regions != regions {
   365  		t.index = nil
   366  	}
   367  	t.regions = regions
   368  }
   369  
   370  // SetChangedFunc sets a handler function which is called when the text of the
   371  // text view has changed. This is useful when text is written to this io.Writer
   372  // in a separate goroutine. Doing so does not automatically cause the screen to
   373  // be refreshed so you may want to use the "changed" handler to redraw the
   374  // screen.
   375  //
   376  // Note that to avoid race conditions or deadlocks, there are a few rules you
   377  // should follow:
   378  //
   379  //   - You can call Application.Draw() from this handler.
   380  //   - You can call TextView.HasFocus() from this handler.
   381  //   - During the execution of this handler, access to any other variables from
   382  //     this primitive or any other primitive should be queued using
   383  //     Application.QueueUpdate().
   384  //
   385  // See package description for details on dealing with concurrency.
   386  func (t *TextView) SetChangedFunc(handler func()) {
   387  	t.Lock()
   388  	defer t.Unlock()
   389  
   390  	t.changed = handler
   391  }
   392  
   393  // SetDoneFunc sets a handler which is called when the user presses on the
   394  // following keys: Escape, Enter, Tab, Backtab. The key is passed to the
   395  // handler.
   396  func (t *TextView) SetDoneFunc(handler func(key tcell.Key)) {
   397  	t.Lock()
   398  	defer t.Unlock()
   399  
   400  	t.done = handler
   401  }
   402  
   403  // SetHighlightedFunc sets a handler which is called when the list of currently
   404  // highlighted regions change. It receives a list of region IDs which were newly
   405  // highlighted, those that are not highlighted anymore, and those that remain
   406  // highlighted.
   407  //
   408  // Note that because regions are only determined during drawing, this function
   409  // can only fire for regions that have existed during the last call to Draw().
   410  func (t *TextView) SetHighlightedFunc(handler func(added, removed, remaining []string)) {
   411  	t.highlighted = handler
   412  }
   413  
   414  func (t *TextView) clipBuffer() {
   415  	if t.maxLines <= 0 {
   416  		return
   417  	}
   418  
   419  	lenbuf := len(t.buffer)
   420  	if lenbuf > t.maxLines {
   421  		t.buffer = t.buffer[lenbuf-t.maxLines:]
   422  	}
   423  }
   424  
   425  // SetMaxLines sets the maximum number of newlines the text view will hold
   426  // before discarding older data from the buffer.
   427  func (t *TextView) SetMaxLines(maxLines int) {
   428  	t.maxLines = maxLines
   429  	t.clipBuffer()
   430  }
   431  
   432  // ScrollTo scrolls to the specified row and column (both starting with 0).
   433  func (t *TextView) ScrollTo(row, column int) {
   434  	t.Lock()
   435  	defer t.Unlock()
   436  
   437  	if !t.scrollable {
   438  		return
   439  	}
   440  	t.lineOffset = row
   441  	t.columnOffset = column
   442  	t.trackEnd = false
   443  }
   444  
   445  // ScrollToBeginning scrolls to the top left corner of the text if the text view
   446  // is scrollable.
   447  func (t *TextView) ScrollToBeginning() {
   448  	t.Lock()
   449  	defer t.Unlock()
   450  
   451  	if !t.scrollable {
   452  		return
   453  	}
   454  	t.trackEnd = false
   455  	t.lineOffset = 0
   456  	t.columnOffset = 0
   457  }
   458  
   459  // ScrollToEnd scrolls to the bottom left corner of the text if the text view
   460  // is scrollable. Adding new rows to the end of the text view will cause it to
   461  // scroll with the new data.
   462  func (t *TextView) ScrollToEnd() {
   463  	t.Lock()
   464  	defer t.Unlock()
   465  
   466  	if !t.scrollable {
   467  		return
   468  	}
   469  	t.trackEnd = true
   470  	t.columnOffset = 0
   471  }
   472  
   473  // GetScrollOffset returns the number of rows and columns that are skipped at
   474  // the top left corner when the text view has been scrolled.
   475  func (t *TextView) GetScrollOffset() (row, column int) {
   476  	t.RLock()
   477  	defer t.RUnlock()
   478  
   479  	return t.lineOffset, t.columnOffset
   480  }
   481  
   482  // Clear removes all text from the buffer.
   483  func (t *TextView) Clear() {
   484  	t.Lock()
   485  	defer t.Unlock()
   486  
   487  	t.clear()
   488  }
   489  
   490  func (t *TextView) clear() {
   491  	t.buffer = nil
   492  	t.recentBytes = nil
   493  	if t.reindex {
   494  		t.index = nil
   495  	}
   496  }
   497  
   498  // Highlight specifies which regions should be highlighted. If highlight
   499  // toggling is set to true (see SetToggleHighlights()), the highlight of the
   500  // provided regions is toggled (highlighted regions are un-highlighted and vice
   501  // versa). If toggling is set to false, the provided regions are highlighted and
   502  // all other regions will not be highlighted (you may also provide nil to turn
   503  // off all highlights).
   504  //
   505  // For more information on regions, see class description. Empty region strings
   506  // are ignored.
   507  //
   508  // Text in highlighted regions will be drawn inverted, i.e. with their
   509  // background and foreground colors swapped.
   510  func (t *TextView) Highlight(regionIDs ...string) {
   511  	t.Lock()
   512  
   513  	// Toggle highlights.
   514  	if t.toggleHighlights {
   515  		var newIDs []string
   516  	HighlightLoop:
   517  		for regionID := range t.highlights {
   518  			for _, id := range regionIDs {
   519  				if regionID == id {
   520  					continue HighlightLoop
   521  				}
   522  			}
   523  			newIDs = append(newIDs, regionID)
   524  		}
   525  		for _, regionID := range regionIDs {
   526  			if _, ok := t.highlights[regionID]; !ok {
   527  				newIDs = append(newIDs, regionID)
   528  			}
   529  		}
   530  		regionIDs = newIDs
   531  	} // Now we have a list of region IDs that end up being highlighted.
   532  
   533  	// Determine added and removed regions.
   534  	var added, removed, remaining []string
   535  	if t.highlighted != nil {
   536  		for _, regionID := range regionIDs {
   537  			if _, ok := t.highlights[regionID]; ok {
   538  				remaining = append(remaining, regionID)
   539  				delete(t.highlights, regionID)
   540  			} else {
   541  				added = append(added, regionID)
   542  			}
   543  		}
   544  		for regionID := range t.highlights {
   545  			removed = append(removed, regionID)
   546  		}
   547  	}
   548  
   549  	// Make new selection.
   550  	t.highlights = make(map[string]struct{})
   551  	for _, id := range regionIDs {
   552  		if id == "" {
   553  			continue
   554  		}
   555  		t.highlights[id] = struct{}{}
   556  	}
   557  	t.index = nil
   558  
   559  	// Notify.
   560  	if t.highlighted != nil && (len(added) > 0 || len(removed) > 0) {
   561  		t.Unlock()
   562  		t.highlighted(added, removed, remaining)
   563  	} else {
   564  		t.Unlock()
   565  	}
   566  }
   567  
   568  // GetHighlights returns the IDs of all currently highlighted regions.
   569  func (t *TextView) GetHighlights() (regionIDs []string) {
   570  	t.RLock()
   571  	defer t.RUnlock()
   572  
   573  	for id := range t.highlights {
   574  		regionIDs = append(regionIDs, id)
   575  	}
   576  	return
   577  }
   578  
   579  // SetToggleHighlights sets a flag to determine how regions are highlighted.
   580  // When set to true, the Highlight() function (or a mouse click) will toggle the
   581  // provided/selected regions. When set to false, Highlight() (or a mouse click)
   582  // will simply highlight the provided regions.
   583  func (t *TextView) SetToggleHighlights(toggle bool) {
   584  	t.toggleHighlights = toggle
   585  }
   586  
   587  // ScrollToHighlight will cause the visible area to be scrolled so that the
   588  // highlighted regions appear in the visible area of the text view. This
   589  // repositioning happens the next time the text view is drawn. It happens only
   590  // once so you will need to call this function repeatedly to always keep
   591  // highlighted regions in view.
   592  //
   593  // Nothing happens if there are no highlighted regions or if the text view is
   594  // not scrollable.
   595  func (t *TextView) ScrollToHighlight() {
   596  	t.Lock()
   597  	defer t.Unlock()
   598  
   599  	if len(t.highlights) == 0 || !t.scrollable || !t.regions {
   600  		return
   601  	}
   602  	t.index = nil
   603  	t.scrollToHighlights = true
   604  	t.trackEnd = false
   605  }
   606  
   607  // GetRegionText returns the text of the region with the given ID. If dynamic
   608  // colors are enabled, color tags are stripped from the text. Newlines are
   609  // always returned as '\n' runes.
   610  //
   611  // If the region does not exist or if regions are turned off, an empty string
   612  // is returned.
   613  func (t *TextView) GetRegionText(regionID string) string {
   614  	t.RLock()
   615  	defer t.RUnlock()
   616  
   617  	if !t.regions || len(regionID) == 0 {
   618  		return ""
   619  	}
   620  
   621  	var (
   622  		buffer          bytes.Buffer
   623  		currentRegionID string
   624  	)
   625  
   626  	for _, str := range t.buffer {
   627  		// Find all color tags in this line.
   628  		var colorTagIndices [][]int
   629  		if t.dynamicColors {
   630  			colorTagIndices = colorPattern.FindAllIndex(str, -1)
   631  		}
   632  
   633  		// Find all regions in this line.
   634  		var (
   635  			regionIndices [][]int
   636  			regions       [][][]byte
   637  		)
   638  		if t.regions {
   639  			regionIndices = regionPattern.FindAllIndex(str, -1)
   640  			regions = regionPattern.FindAllSubmatch(str, -1)
   641  		}
   642  
   643  		// Analyze this line.
   644  		var currentTag, currentRegion int
   645  		for pos, ch := range str {
   646  			// Skip any color tags.
   647  			if currentTag < len(colorTagIndices) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
   648  				if pos == colorTagIndices[currentTag][1]-1 {
   649  					currentTag++
   650  				}
   651  				if colorTagIndices[currentTag][1]-colorTagIndices[currentTag][0] > 2 {
   652  					continue
   653  				}
   654  			}
   655  
   656  			// Skip any regions.
   657  			if currentRegion < len(regionIndices) && pos >= regionIndices[currentRegion][0] && pos < regionIndices[currentRegion][1] {
   658  				if pos == regionIndices[currentRegion][1]-1 {
   659  					if currentRegionID == regionID {
   660  						// This is the end of the requested region. We're done.
   661  						return buffer.String()
   662  					}
   663  					currentRegionID = string(regions[currentRegion][1])
   664  					currentRegion++
   665  				}
   666  				continue
   667  			}
   668  
   669  			// Add this rune.
   670  			if currentRegionID == regionID {
   671  				buffer.WriteByte(ch)
   672  			}
   673  		}
   674  
   675  		// Add newline.
   676  		if currentRegionID == regionID {
   677  			buffer.WriteRune('\n')
   678  		}
   679  	}
   680  
   681  	return escapePattern.ReplaceAllString(buffer.String(), `[$1$2]`)
   682  }
   683  
   684  // Focus is called when this primitive receives focus.
   685  func (t *TextView) Focus(delegate func(p Primitive)) {
   686  	t.Lock()
   687  	defer t.Unlock()
   688  
   689  	// Implemented here with locking because this is used by layout primitives.
   690  	t.hasFocus = true
   691  }
   692  
   693  // HasFocus returns whether or not this primitive has focus.
   694  func (t *TextView) HasFocus() bool {
   695  	t.RLock()
   696  	defer t.RUnlock()
   697  
   698  	// Implemented here with locking because this may be used in the "changed"
   699  	// callback.
   700  	return t.hasFocus
   701  }
   702  
   703  // Write lets us implement the io.Writer interface. Tab characters will be
   704  // replaced with TabSize space characters. A "\n" or "\r\n" will be interpreted
   705  // as a new line.
   706  func (t *TextView) Write(p []byte) (n int, err error) {
   707  	t.Lock()
   708  	changed := t.changed
   709  	if changed != nil {
   710  		// Notify at the end.
   711  		defer changed()
   712  	}
   713  	defer t.Unlock()
   714  
   715  	return t.write(p)
   716  }
   717  
   718  func (t *TextView) write(p []byte) (n int, err error) {
   719  	// Copy data over.
   720  	newBytes := append(t.recentBytes, p...)
   721  	t.recentBytes = nil
   722  
   723  	// If we have a trailing invalid UTF-8 byte, we'll wait.
   724  	if r, _ := utf8.DecodeLastRune(p); r == utf8.RuneError {
   725  		t.recentBytes = newBytes
   726  		return len(p), nil
   727  	}
   728  
   729  	// If we have a trailing open dynamic color, exclude it.
   730  	if t.dynamicColors {
   731  		location := openColorRegex.FindIndex(newBytes)
   732  		if location != nil {
   733  			t.recentBytes = newBytes[location[0]:]
   734  			newBytes = newBytes[:location[0]]
   735  		}
   736  	}
   737  
   738  	// If we have a trailing open region, exclude it.
   739  	if t.regions {
   740  		location := openRegionRegex.FindIndex(newBytes)
   741  		if location != nil {
   742  			t.recentBytes = newBytes[location[0]:]
   743  			newBytes = newBytes[:location[0]]
   744  		}
   745  	}
   746  
   747  	// Transform the new bytes into strings.
   748  	newBytes = bytes.Replace(newBytes, []byte{'\t'}, bytes.Repeat([]byte{' '}, TabSize), -1)
   749  	for index, line := range bytes.Split(newBytes, []byte("\n")) {
   750  		if index == 0 {
   751  			if len(t.buffer) == 0 {
   752  				t.buffer = [][]byte{line}
   753  			} else {
   754  				t.buffer[len(t.buffer)-1] = append(t.buffer[len(t.buffer)-1], line...)
   755  			}
   756  		} else {
   757  			t.buffer = append(t.buffer, line)
   758  		}
   759  	}
   760  
   761  	t.clipBuffer()
   762  
   763  	// Reset the index.
   764  	if t.reindex {
   765  		t.index = nil
   766  	}
   767  
   768  	return len(p), nil
   769  }
   770  
   771  // SetWrapWidth set the maximum width of lines when wrapping is enabled.
   772  // When set to 0 the width of the TextView is used.
   773  func (t *TextView) SetWrapWidth(width int) {
   774  	t.Lock()
   775  	defer t.Unlock()
   776  
   777  	t.wrapWidth = width
   778  }
   779  
   780  // SetReindexBuffer set a flag controlling whether the buffer is reindexed when
   781  // it is modified. This improves the performance of TextViews whose contents
   782  // always have line-breaks in the same location. This must be called after the
   783  // buffer has been indexed.
   784  func (t *TextView) SetReindexBuffer(reindex bool) {
   785  	t.Lock()
   786  	defer t.Unlock()
   787  
   788  	t.reindex = reindex
   789  
   790  	if reindex {
   791  		t.index = nil
   792  	}
   793  }
   794  
   795  // reindexBuffer re-indexes the buffer such that we can use it to easily draw
   796  // the buffer onto the screen. Each line in the index will contain a pointer
   797  // into the buffer from which on we will print text. It will also contain the
   798  // color with which the line starts.
   799  func (t *TextView) reindexBuffer(width int) {
   800  	if t.index != nil && (!t.wrap || width == t.indexWidth) {
   801  		return // Nothing has changed. We can still use the current index.
   802  	}
   803  	t.index = nil
   804  	t.indexWidth = width
   805  	t.fromHighlight, t.toHighlight, t.posHighlight = -1, -1, -1
   806  
   807  	// If there's no space, there's no index.
   808  	if width < 1 {
   809  		return
   810  	}
   811  
   812  	if t.wrapWidth > 0 && t.wrapWidth < width {
   813  		width = t.wrapWidth
   814  	}
   815  
   816  	// Initial states.
   817  	var regionID []byte
   818  	var (
   819  		highlighted                                  bool
   820  		foregroundColor, backgroundColor, attributes string
   821  	)
   822  
   823  	// Go through each line in the buffer.
   824  	for bufferIndex, buf := range t.buffer {
   825  		colorTagIndices, colorTags, regionIndices, regions, escapeIndices, strippedStr, _ := decomposeText(buf, t.dynamicColors, t.regions)
   826  
   827  		// Split the line if required.
   828  		var splitLines []string
   829  		str := string(strippedStr)
   830  		if t.wrap && len(str) > 0 {
   831  			for len(str) > 0 {
   832  				extract := runewidth.Truncate(str, width, "")
   833  				if len(extract) == 0 {
   834  					// We'll extract at least one grapheme cluster.
   835  					gr := uniseg.NewGraphemes(str)
   836  					gr.Next()
   837  					_, to := gr.Positions()
   838  					extract = str[:to]
   839  				}
   840  				if t.wordWrap && len(extract) < len(str) {
   841  					// Add any spaces from the next line.
   842  					if spaces := spacePattern.FindStringIndex(str[len(extract):]); spaces != nil && spaces[0] == 0 {
   843  						extract = str[:len(extract)+spaces[1]]
   844  					}
   845  
   846  					// Can we split before the mandatory end?
   847  					matches := boundaryPattern.FindAllStringIndex(extract, -1)
   848  					if len(matches) > 0 {
   849  						// Yes. Let's split there.
   850  						extract = extract[:matches[len(matches)-1][1]]
   851  					}
   852  				}
   853  				splitLines = append(splitLines, extract)
   854  				str = str[len(extract):]
   855  			}
   856  		} else {
   857  			// No need to split the line.
   858  			splitLines = []string{str}
   859  		}
   860  
   861  		// Create index from split lines.
   862  		var originalPos, colorPos, regionPos, escapePos int
   863  		for _, splitLine := range splitLines {
   864  			line := &textViewIndex{
   865  				Line:            bufferIndex,
   866  				Pos:             originalPos,
   867  				ForegroundColor: foregroundColor,
   868  				BackgroundColor: backgroundColor,
   869  				Attributes:      attributes,
   870  				Region:          regionID,
   871  			}
   872  
   873  			// Shift original position with tags.
   874  			lineLength := len(splitLine)
   875  			remainingLength := lineLength
   876  			tagEnd := originalPos
   877  			totalTagLength := 0
   878  			for {
   879  				// Which tag comes next?
   880  				nextTag := make([][3]int, 0, 3)
   881  				if colorPos < len(colorTagIndices) {
   882  					nextTag = append(nextTag, [3]int{colorTagIndices[colorPos][0], colorTagIndices[colorPos][1], 0}) // 0 = color tag.
   883  				}
   884  				if regionPos < len(regionIndices) {
   885  					nextTag = append(nextTag, [3]int{regionIndices[regionPos][0], regionIndices[regionPos][1], 1}) // 1 = region tag.
   886  				}
   887  				if escapePos < len(escapeIndices) {
   888  					nextTag = append(nextTag, [3]int{escapeIndices[escapePos][0], escapeIndices[escapePos][1], 2}) // 2 = escape tag.
   889  				}
   890  				minPos := -1
   891  				tagIndex := -1
   892  				for index, pair := range nextTag {
   893  					if minPos < 0 || pair[0] < minPos {
   894  						minPos = pair[0]
   895  						tagIndex = index
   896  					}
   897  				}
   898  
   899  				// Is the next tag in range?
   900  				if tagIndex < 0 || minPos > tagEnd+remainingLength {
   901  					break // No. We're done with this line.
   902  				}
   903  
   904  				// Advance.
   905  				strippedTagStart := nextTag[tagIndex][0] - originalPos - totalTagLength
   906  				tagEnd = nextTag[tagIndex][1]
   907  				tagLength := tagEnd - nextTag[tagIndex][0]
   908  				if nextTag[tagIndex][2] == 2 {
   909  					tagLength = 1
   910  				}
   911  				totalTagLength += tagLength
   912  				remainingLength = lineLength - (tagEnd - originalPos - totalTagLength)
   913  
   914  				// Process the tag.
   915  				switch nextTag[tagIndex][2] {
   916  				case 0:
   917  					// Process color tags.
   918  					foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[colorPos])
   919  					colorPos++
   920  				case 1:
   921  					// Process region tags.
   922  					regionID = regions[regionPos][1]
   923  					_, highlighted = t.highlights[string(regionID)]
   924  
   925  					// Update highlight range.
   926  					if highlighted {
   927  						line := len(t.index)
   928  						if t.fromHighlight < 0 {
   929  							t.fromHighlight, t.toHighlight = line, line
   930  							t.posHighlight = runewidth.StringWidth(splitLine[:strippedTagStart])
   931  						} else if line > t.toHighlight {
   932  							t.toHighlight = line
   933  						}
   934  					}
   935  
   936  					regionPos++
   937  				case 2:
   938  					// Process escape tags.
   939  					escapePos++
   940  				}
   941  			}
   942  
   943  			// Advance to next line.
   944  			originalPos += lineLength + totalTagLength
   945  
   946  			// Append this line.
   947  			line.NextPos = originalPos
   948  			line.Width = runewidth.StringWidth(splitLine)
   949  			t.index = append(t.index, line)
   950  		}
   951  
   952  		// Word-wrapped lines may have trailing whitespace. Remove it.
   953  		if t.wrap && t.wordWrap {
   954  			for _, line := range t.index {
   955  				str := t.buffer[line.Line][line.Pos:line.NextPos]
   956  				trimmed := bytes.TrimRightFunc(str, unicode.IsSpace)
   957  				if len(trimmed) != len(str) {
   958  					oldNextPos := line.NextPos
   959  					line.NextPos -= len(str) - len(trimmed)
   960  					line.Width -= runewidth.StringWidth(string(t.buffer[line.Line][line.NextPos:oldNextPos]))
   961  				}
   962  			}
   963  		}
   964  	}
   965  
   966  	// Calculate longest line.
   967  	t.longestLine = 0
   968  	for _, line := range t.index {
   969  		if line.Width > t.longestLine {
   970  			t.longestLine = line.Width
   971  		}
   972  	}
   973  }
   974  
   975  // Draw draws this primitive onto the screen.
   976  func (t *TextView) Draw(screen tcell.Screen) {
   977  	if !t.GetVisible() {
   978  		return
   979  	}
   980  
   981  	t.Box.Draw(screen)
   982  
   983  	t.Lock()
   984  	defer t.Unlock()
   985  
   986  	// Get the available size.
   987  	x, y, width, height := t.GetInnerRect()
   988  	if height == 0 {
   989  		return
   990  	}
   991  	t.pageSize = height
   992  
   993  	if t.index == nil || width != t.lastWidth || height != t.lastHeight {
   994  		t.reindexBuffer(width)
   995  	}
   996  	t.lastWidth, t.lastHeight = width, height
   997  
   998  	showVerticalScrollBar := t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && len(t.index) > height)
   999  	if showVerticalScrollBar {
  1000  		width-- // Subtract space for scroll bar.
  1001  	}
  1002  
  1003  	t.reindexBuffer(width)
  1004  	if t.regions {
  1005  		t.regionInfos = nil
  1006  	}
  1007  
  1008  	// If we don't have an index, there's nothing to draw.
  1009  	if t.index == nil {
  1010  		return
  1011  	}
  1012  
  1013  	// Move to highlighted regions.
  1014  	if t.regions && t.scrollToHighlights && t.fromHighlight >= 0 {
  1015  		// Do we fit the entire height?
  1016  		if t.toHighlight-t.fromHighlight+1 < height {
  1017  			// Yes, let's center the highlights.
  1018  			t.lineOffset = (t.fromHighlight + t.toHighlight - height) / 2
  1019  		} else {
  1020  			// No, let's move to the start of the highlights.
  1021  			t.lineOffset = t.fromHighlight
  1022  		}
  1023  
  1024  		// If the highlight is too far to the right, move it to the middle.
  1025  		if t.posHighlight-t.columnOffset > 3*width/4 {
  1026  			t.columnOffset = t.posHighlight - width/2
  1027  		}
  1028  
  1029  		// If the highlight is offscreen on the left, move it onscreen.
  1030  		if t.posHighlight-t.columnOffset < 0 {
  1031  			t.columnOffset = t.posHighlight - width/4
  1032  		}
  1033  	}
  1034  	t.scrollToHighlights = false
  1035  
  1036  	// Adjust line offset.
  1037  	if t.lineOffset+height > len(t.index) {
  1038  		t.trackEnd = true
  1039  	}
  1040  	if t.trackEnd {
  1041  		t.lineOffset = len(t.index) - height
  1042  	}
  1043  	if t.lineOffset < 0 {
  1044  		t.lineOffset = 0
  1045  	}
  1046  
  1047  	// Adjust column offset.
  1048  	if t.align == AlignLeft {
  1049  		if t.columnOffset+width > t.longestLine {
  1050  			t.columnOffset = t.longestLine - width
  1051  		}
  1052  		if t.columnOffset < 0 {
  1053  			t.columnOffset = 0
  1054  		}
  1055  	} else if t.align == AlignRight {
  1056  		if t.columnOffset-width < -t.longestLine {
  1057  			t.columnOffset = width - t.longestLine
  1058  		}
  1059  		if t.columnOffset > 0 {
  1060  			t.columnOffset = 0
  1061  		}
  1062  	} else { // AlignCenter.
  1063  		half := (t.longestLine - width) / 2
  1064  		if half > 0 {
  1065  			if t.columnOffset > half {
  1066  				t.columnOffset = half
  1067  			}
  1068  			if t.columnOffset < -half {
  1069  				t.columnOffset = -half
  1070  			}
  1071  		} else {
  1072  			t.columnOffset = 0
  1073  		}
  1074  	}
  1075  
  1076  	// Draw the buffer.
  1077  	defaultStyle := tcell.StyleDefault.Foreground(t.textColor)
  1078  	for line := t.lineOffset; line < len(t.index); line++ {
  1079  		// Are we done?
  1080  		if line-t.lineOffset >= height {
  1081  			break
  1082  		}
  1083  
  1084  		// Get the text for this line.
  1085  		index := t.index[line]
  1086  		text := t.buffer[index.Line][index.Pos:index.NextPos]
  1087  		foregroundColor := index.ForegroundColor
  1088  		backgroundColor := index.BackgroundColor
  1089  		attributes := index.Attributes
  1090  		regionID := index.Region
  1091  		if t.regions && len(regionID) > 0 && (len(t.regionInfos) == 0 || !bytes.Equal(t.regionInfos[len(t.regionInfos)-1].ID, regionID)) {
  1092  			t.regionInfos = append(t.regionInfos, &textViewRegion{
  1093  				ID:    regionID,
  1094  				FromX: x,
  1095  				FromY: y + line - t.lineOffset,
  1096  				ToX:   -1,
  1097  				ToY:   -1,
  1098  			})
  1099  		}
  1100  
  1101  		// Process tags.
  1102  		colorTagIndices, colorTags, regionIndices, regions, escapeIndices, strippedText, _ := decomposeText(text, t.dynamicColors, t.regions)
  1103  
  1104  		// Calculate the position of the line.
  1105  		var skip, posX int
  1106  		if t.align == AlignLeft {
  1107  			posX = -t.columnOffset
  1108  		} else if t.align == AlignRight {
  1109  			posX = width - index.Width - t.columnOffset
  1110  		} else { // AlignCenter.
  1111  			posX = (width-index.Width)/2 - t.columnOffset
  1112  		}
  1113  		if posX < 0 {
  1114  			skip = -posX
  1115  			posX = 0
  1116  		}
  1117  
  1118  		// Print the line.
  1119  		if y+line-t.lineOffset >= 0 {
  1120  			var colorPos, regionPos, escapePos, tagOffset, skipped int
  1121  			iterateString(string(strippedText), func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool {
  1122  				// Process tags.
  1123  				for {
  1124  					if colorPos < len(colorTags) && textPos+tagOffset >= colorTagIndices[colorPos][0] && textPos+tagOffset < colorTagIndices[colorPos][1] {
  1125  						// Get the color.
  1126  						foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[colorPos])
  1127  						tagOffset += colorTagIndices[colorPos][1] - colorTagIndices[colorPos][0]
  1128  						colorPos++
  1129  					} else if regionPos < len(regionIndices) && textPos+tagOffset >= regionIndices[regionPos][0] && textPos+tagOffset < regionIndices[regionPos][1] {
  1130  						// Get the region.
  1131  						if len(regionID) > 0 && len(t.regionInfos) > 0 && bytes.Equal(t.regionInfos[len(t.regionInfos)-1].ID, regionID) {
  1132  							// End last region.
  1133  							t.regionInfos[len(t.regionInfos)-1].ToX = x + posX
  1134  							t.regionInfos[len(t.regionInfos)-1].ToY = y + line - t.lineOffset
  1135  						}
  1136  						regionID = regions[regionPos][1]
  1137  						if len(regionID) > 0 {
  1138  							// Start new region.
  1139  							t.regionInfos = append(t.regionInfos, &textViewRegion{
  1140  								ID:    regionID,
  1141  								FromX: x + posX,
  1142  								FromY: y + line - t.lineOffset,
  1143  								ToX:   -1,
  1144  								ToY:   -1,
  1145  							})
  1146  						}
  1147  						tagOffset += regionIndices[regionPos][1] - regionIndices[regionPos][0]
  1148  						regionPos++
  1149  					} else {
  1150  						break
  1151  					}
  1152  				}
  1153  
  1154  				// Skip the second-to-last character of an escape tag.
  1155  				if escapePos < len(escapeIndices) && textPos+tagOffset == escapeIndices[escapePos][1]-2 {
  1156  					tagOffset++
  1157  					escapePos++
  1158  				}
  1159  
  1160  				// Mix the existing style with the new style.
  1161  				_, _, existingStyle, _ := screen.GetContent(x+posX, y+line-t.lineOffset)
  1162  				_, background, _ := existingStyle.Decompose()
  1163  				style := overlayStyle(background, defaultStyle, foregroundColor, backgroundColor, attributes)
  1164  
  1165  				// Do we highlight this character?
  1166  				var highlighted bool
  1167  				if len(regionID) > 0 {
  1168  					if _, ok := t.highlights[string(regionID)]; ok {
  1169  						highlighted = true
  1170  					}
  1171  				}
  1172  				if highlighted {
  1173  					fg, bg, _ := style.Decompose()
  1174  					if bg == tcell.ColorDefault {
  1175  						r, g, b := fg.RGB()
  1176  						c := colorful.Color{R: float64(r) / 255, G: float64(g) / 255, B: float64(b) / 255}
  1177  						_, _, li := c.Hcl()
  1178  						if li < .5 {
  1179  							bg = tcell.ColorWhite.TrueColor()
  1180  						} else {
  1181  							bg = tcell.ColorBlack.TrueColor()
  1182  						}
  1183  					}
  1184  					style = style.Background(fg).Foreground(bg)
  1185  				}
  1186  
  1187  				// Skip to the right.
  1188  				if !t.wrap && skipped < skip {
  1189  					skipped += screenWidth
  1190  					return false
  1191  				}
  1192  
  1193  				// Stop at the right border.
  1194  				if posX+screenWidth > width {
  1195  					return true
  1196  				}
  1197  
  1198  				// Draw the character.
  1199  				for offset := screenWidth - 1; offset >= 0; offset-- {
  1200  					if offset == 0 {
  1201  						screen.SetContent(x+posX+offset, y+line-t.lineOffset, main, comb, style)
  1202  					} else {
  1203  						screen.SetContent(x+posX+offset, y+line-t.lineOffset, ' ', nil, style)
  1204  					}
  1205  				}
  1206  
  1207  				// Advance.
  1208  				posX += screenWidth
  1209  				return false
  1210  			})
  1211  		}
  1212  	}
  1213  
  1214  	// Draw scroll bar.
  1215  	if showVerticalScrollBar {
  1216  		cursor := int(float64(len(t.index)) * (float64(t.lineOffset) / float64(len(t.index)-height)))
  1217  		for printed := 0; printed < height; printed++ {
  1218  			RenderScrollBar(screen, t.scrollBarVisibility, x+width, y+printed, height, len(t.index), cursor, printed, t.hasFocus, t.scrollBarColor)
  1219  		}
  1220  	}
  1221  
  1222  	// If this view is not scrollable, we'll purge the buffer of lines that have
  1223  	// scrolled out of view.
  1224  	if !t.scrollable && t.lineOffset > 0 {
  1225  		if t.lineOffset >= len(t.index) {
  1226  			t.buffer = nil
  1227  		} else {
  1228  			t.buffer = t.buffer[t.index[t.lineOffset].Line:]
  1229  		}
  1230  		t.index = nil
  1231  		t.lineOffset = 0
  1232  	}
  1233  }
  1234  
  1235  // InputHandler returns the handler for this primitive.
  1236  func (t *TextView) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
  1237  	return t.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
  1238  		key := event.Key()
  1239  
  1240  		if HitShortcut(event, Keys.Cancel, Keys.Select, Keys.Select2, Keys.MovePreviousField, Keys.MoveNextField) {
  1241  			if t.done != nil {
  1242  				t.done(key)
  1243  			}
  1244  			return
  1245  		}
  1246  
  1247  		t.Lock()
  1248  		defer t.Unlock()
  1249  
  1250  		if !t.scrollable {
  1251  			return
  1252  		}
  1253  
  1254  		if HitShortcut(event, Keys.MoveFirst, Keys.MoveFirst2) {
  1255  			t.trackEnd = false
  1256  			t.lineOffset = 0
  1257  			t.columnOffset = 0
  1258  		} else if HitShortcut(event, Keys.MoveLast, Keys.MoveLast2) {
  1259  			t.trackEnd = true
  1260  			t.columnOffset = 0
  1261  		} else if HitShortcut(event, Keys.MoveUp, Keys.MoveUp2) {
  1262  			t.trackEnd = false
  1263  			t.lineOffset--
  1264  		} else if HitShortcut(event, Keys.MoveDown, Keys.MoveDown2) {
  1265  			t.lineOffset++
  1266  		} else if HitShortcut(event, Keys.MoveLeft, Keys.MoveLeft2) {
  1267  			t.columnOffset--
  1268  		} else if HitShortcut(event, Keys.MoveRight, Keys.MoveRight2) {
  1269  			t.columnOffset++
  1270  		} else if HitShortcut(event, Keys.MovePreviousPage) {
  1271  			t.trackEnd = false
  1272  			t.lineOffset -= t.pageSize
  1273  		} else if HitShortcut(event, Keys.MoveNextPage) {
  1274  			t.lineOffset += t.pageSize
  1275  		}
  1276  	})
  1277  }
  1278  
  1279  // MouseHandler returns the mouse handler for this primitive.
  1280  func (t *TextView) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  1281  	return t.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  1282  		x, y := event.Position()
  1283  		if !t.InRect(x, y) {
  1284  			return false, nil
  1285  		}
  1286  
  1287  		switch action {
  1288  		case MouseLeftClick:
  1289  			if t.regions {
  1290  				// Find a region to highlight.
  1291  				for _, region := range t.regionInfos {
  1292  					if y == region.FromY && x < region.FromX ||
  1293  						y == region.ToY && x >= region.ToX ||
  1294  						region.FromY >= 0 && y < region.FromY ||
  1295  						region.ToY >= 0 && y > region.ToY {
  1296  						continue
  1297  					}
  1298  					t.Highlight(string(region.ID))
  1299  					break
  1300  				}
  1301  			}
  1302  			consumed = true
  1303  			setFocus(t)
  1304  		case MouseScrollUp:
  1305  			if t.scrollable {
  1306  				t.trackEnd = false
  1307  				t.lineOffset--
  1308  				consumed = true
  1309  			}
  1310  		case MouseScrollDown:
  1311  			if t.scrollable {
  1312  				t.lineOffset++
  1313  				consumed = true
  1314  			}
  1315  		}
  1316  
  1317  		return
  1318  	})
  1319  }
  1320  

View as plain text