2018-04-10 18:31:28 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
package tstring
|
2018-04-10 18:31:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
"maunium.net/go/tcell"
|
2018-04-10 18:31:28 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
)
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
type TString []Cell
|
2018-04-10 18:31:28 +02:00
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func NewTString(str string) TString {
|
2018-04-10 18:31:28 +02:00
|
|
|
newStr := make([]Cell, len(str))
|
|
|
|
for i, char := range str {
|
|
|
|
newStr[i] = NewCell(char)
|
|
|
|
}
|
|
|
|
return newStr
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func NewColorTString(str string, color tcell.Color) TString {
|
2018-04-10 18:31:28 +02:00
|
|
|
newStr := make([]Cell, len(str))
|
|
|
|
for i, char := range str {
|
|
|
|
newStr[i] = NewColorCell(char, color)
|
|
|
|
}
|
|
|
|
return newStr
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func NewStyleTString(str string, style tcell.Style) TString {
|
2018-04-10 18:31:28 +02:00
|
|
|
newStr := make([]Cell, len(str))
|
|
|
|
for i, char := range str {
|
|
|
|
newStr[i] = NewStyleCell(char, style)
|
|
|
|
}
|
|
|
|
return newStr
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) Colorize(from, length int, color tcell.Color) {
|
2018-04-10 18:31:28 +02:00
|
|
|
for i := from; i < from+length; i++ {
|
|
|
|
str[i].Style = str[i].Style.Foreground(color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) Draw(screen tcell.Screen, x, y int) {
|
2018-04-10 18:31:28 +02:00
|
|
|
offsetX := 0
|
|
|
|
for _, cell := range str {
|
|
|
|
offsetX += cell.Draw(screen, x+offsetX, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) RuneWidth() (width int) {
|
2018-04-10 18:31:28 +02:00
|
|
|
for _, cell := range str {
|
|
|
|
width += runewidth.RuneWidth(cell.Char)
|
|
|
|
}
|
|
|
|
return width
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) String() string {
|
2018-04-10 18:31:28 +02:00
|
|
|
var buf strings.Builder
|
|
|
|
for _, cell := range str {
|
|
|
|
buf.WriteRune(cell.Char)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate return string truncated with w cells
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) Truncate(w int) TString {
|
2018-04-10 18:31:28 +02:00
|
|
|
if str.RuneWidth() <= w {
|
|
|
|
return str[:]
|
|
|
|
}
|
|
|
|
width := 0
|
|
|
|
i := 0
|
|
|
|
for ; i < len(str); i++ {
|
|
|
|
cw := runewidth.RuneWidth(str[i].Char)
|
|
|
|
if width+cw > w {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
width += cw
|
|
|
|
}
|
|
|
|
return str[0:i]
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) IndexFrom(r rune, from int) int {
|
2018-04-10 18:31:28 +02:00
|
|
|
for i := from; i < len(str); i++ {
|
|
|
|
if str[i].Char == r {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) Index(r rune) int {
|
2018-04-10 18:31:28 +02:00
|
|
|
return str.IndexFrom(r, 0)
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) Count(r rune) (counter int) {
|
2018-04-10 18:31:28 +02:00
|
|
|
index := 0
|
|
|
|
for {
|
|
|
|
index = str.IndexFrom(r, index)
|
|
|
|
if index < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func (str TString) Split(sep rune) []TString {
|
|
|
|
a := make([]TString, str.Count(sep)+1)
|
2018-04-10 18:31:28 +02:00
|
|
|
i := 0
|
|
|
|
orig := str
|
|
|
|
for {
|
|
|
|
m := orig.Index(sep)
|
|
|
|
if m < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
a[i] = orig[:m]
|
|
|
|
orig = orig[m+1:]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
a[i] = orig
|
|
|
|
return a[:i+1]
|
|
|
|
}
|