File: character.go

package info (click to toggle)
golang-sourcehut-rockorager-vaxis 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: makefile: 9
file content (29 lines) | stat: -rw-r--r-- 665 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package vaxis

import "github.com/rivo/uniseg"

// Character is a single extended-grapheme-cluster. It also contains the width
// of the EGC
type Character struct {
	Grapheme string
	Width    int
}

// Converts a string into a slice of Characters suitable to assign to terminal cells
func Characters(s string) []Character {
	egcs := make([]Character, 0, len(s))
	state := -1
	cluster := ""
	w := 0
	for s != "" {
		cluster, s, w, state = uniseg.FirstGraphemeClusterInString(s, state)
		if cluster == "\t" {
			for i := 0; i < 8; i += 1 {
				egcs = append(egcs, Character{" ", 1})
			}
			continue
		}
		egcs = append(egcs, Character{cluster, w})
	}
	return egcs
}