| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package widget |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"strconv" |
| 22 |
|
| 23 |
"github.com/mattn/go-runewidth" |
| 24 |
|
| 25 |
"go.mau.fi/mauview" |
| •26 |
"github.com/gdamore/tcell/v2" |
| 27 |
) |
| 28 |
|
| 29 |
func WriteLineSimple(screen mauview.Screen, line string, x, y int) { |
| 30 |
WriteLine(screen, mauview.AlignLeft, line, x, y, 1<<30, tcell.StyleDefault) |
| 31 |
} |
| 32 |
|
| 33 |
func WriteLineSimpleColor(screen mauview.Screen, line string, x, y int, color tcell.Color) { |
| 34 |
WriteLine(screen, mauview.AlignLeft, line, x, y, 1<<30, tcell.StyleDefault.Foreground(color)) |
| 35 |
} |
| 36 |
|
| 37 |
func WriteLineColor(screen mauview.Screen, align int, line string, x, y, maxWidth int, color tcell.Color) { |
| 38 |
WriteLine(screen, align, line, x, y, maxWidth, tcell.StyleDefault.Foreground(color)) |
| 39 |
} |
| 40 |
|
| 41 |
func WriteLine(screen mauview.Screen, align int, line string, x, y, maxWidth int, style tcell.Style) { |
| 42 |
offsetX := 0 |
| 43 |
if align == mauview.AlignRight { |
| 44 |
offsetX = maxWidth - runewidth.StringWidth(line) |
| 45 |
} |
| 46 |
if offsetX < 0 { |
| 47 |
offsetX = 0 |
| 48 |
} |
| 49 |
for _, ch := range line { |
| 50 |
chWidth := runewidth.RuneWidth(ch) |
| 51 |
if chWidth == 0 { |
| 52 |
continue |
| 53 |
} |
| 54 |
|
| 55 |
for localOffset := 0; localOffset < chWidth; localOffset++ { |
| 56 |
screen.SetContent(x+offsetX+localOffset, y, ch, nil, style) |
| 57 |
} |
| 58 |
offsetX += chWidth |
| 59 |
if offsetX >= maxWidth { |
| 60 |
break |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func WriteLinePadded(screen mauview.Screen, align int, line string, x, y, maxWidth int, style tcell.Style) { |
| 66 |
padding := strconv.Itoa(maxWidth) |
| 67 |
if align == mauview.AlignRight { |
| 68 |
line = fmt.Sprintf("%"+padding+"s", line) |
| 69 |
} else { |
| 70 |
line = fmt.Sprintf("%-"+padding+"s", line) |
| 71 |
} |
| 72 |
WriteLine(screen, mauview.AlignLeft, line, x, y, maxWidth, style) |
| 73 |
} |