1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 19:42:20 +00:00

Use ANSI background color for telnet package

This commit is contained in:
Alexandre Iooss 2020-10-16 20:09:59 +02:00
parent fd606be15c
commit b5907fee1a
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02

View File

@ -2,6 +2,7 @@
package telnet package telnet
import ( import (
"fmt"
"io" "io"
"log" "log"
"net" "net"
@ -15,8 +16,6 @@ var (
Cfg *Options Cfg *Options
currentMessage map[string]*string currentMessage map[string]*string
clientCount map[string]int clientCount map[string]int
asciiChars = []byte{' ', '.', ',', ':', ';', '+', '*', '?', '%', '$', '#', '@'}
) )
// Options holds telnet package configuration // Options holds telnet package configuration
@ -99,6 +98,9 @@ func Serve(config *Options) {
clientCount[streamID]++ clientCount[streamID]++
// Hide terminal cursor
_, _ = s.Write([]byte("\033[?25l"))
for { for {
n, err := s.Write([]byte(*currentMessage[streamID])) n, err := s.Write([]byte(*currentMessage[streamID]))
if err != nil { if err != nil {
@ -152,7 +154,7 @@ func StartASCIIArtStream(streamID string, reader io.ReadCloser) {
// Header // Header
textBuff.Reset() textBuff.Reset()
textBuff.Grow((2*Cfg.Width + 1) * Cfg.Height) textBuff.Grow((40*Cfg.Width+6)*Cfg.Height + 47)
for i := 0; i < 42; i++ { for i := 0; i < 42; i++ {
textBuff.WriteByte('\n') textBuff.WriteByte('\n')
} }
@ -161,13 +163,15 @@ func StartASCIIArtStream(streamID string, reader io.ReadCloser) {
for i, pixel := range pixelBuff { for i, pixel := range pixelBuff {
if i%Cfg.Width == 0 { if i%Cfg.Width == 0 {
// New line // New line
textBuff.WriteByte('\n') textBuff.WriteString("\033[49m\n")
} }
// Print two times the character to make a square // Print two times the character to make a square
textBuff.WriteByte(asciiChars[pixel/22]) text := fmt.Sprintf("\033[48;2;%d;%d;%dm ", pixel, pixel, pixel)
textBuff.WriteByte(asciiChars[pixel/22]) textBuff.WriteString(text)
textBuff.WriteString(text)
} }
textBuff.WriteString("\033[49m")
*(currentMessage[streamID]) = textBuff.String() *(currentMessage[streamID]) = textBuff.String()
} }