▍ humdrum codex / glint v1.0.2
license AGPL-3.0
577 B raw
 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
package editor

import (
	"strings"
	"unicode/utf8"
)

// WordCount returns the whitespace-separated word count of the whole buffer.
func (e *Editor) WordCount() int {
	n := 0
	for _, ln := range e.Lines {
		n += len(strings.Fields(ln))
	}
	return n
}

// SelectionStats returns the character and word count of the active selection,
// with ok=false when nothing is selected.
func (e *Editor) SelectionStats() (chars, words int, ok bool) {
	if !e.HasSelection() {
		return 0, 0, false
	}
	s := e.SelectedText()
	return utf8.RuneCountInString(s), len(strings.Fields(s)), true
}