Skip to content

Commit

Permalink
Add colorization (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram committed Sep 29, 2021
1 parent 31d6d63 commit b39477a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
76 changes: 60 additions & 16 deletions cli/entry.go
Expand Up @@ -14,19 +14,47 @@
// limitations under the License.
//


package cli

import (
"github.com/james4k/rcon"
"os"
"log"
"bufio"
"io"
"fmt"
"github.com/james4k/rcon"
"io"
"log"
"os"
"runtime"
"strings"
)

const SectionSign = "§"
const Reset = "\u001B[0m"

var colors = map[string]string{
"0": "\u001B[30m", // black
"1": "\u001B[34m", // dark blue
"2": "\u001B[32m", // dark green
"3": "\u001B[36m", // dark aqua
"4": "\u001B[31m", // dark red
"5": "\u001B[35m", // dark purple
"6": "\u001B[33m", // gold
"7": "\u001B[37m", // gray
"8": "\u001B[30m", // dark gray
"9": "\u001B[34m", // blue
"a": "\u001B[32m", // green
"b": "\u001B[32m", // aqua
"c": "\u001B[31m", // red
"d": "\u001B[35m", // light purple
"e": "\u001B[33m", // yellow
"f": "\u001B[37m", // white
"k": "", // random
"m": "\u001B[9m", // strikethrough
"o": "\u001B[3m", // italic
"l": "\u001B[1m", // bold
"n": "\u001B[4m", // underline
"r": Reset, // reset
}

func Start(hostPort string, password string, in io.Reader, out io.Writer) {
remoteConsole, err := rcon.Dial(hostPort, password)
if err != nil {
Expand All @@ -35,12 +63,12 @@ func Start(hostPort string, password string, in io.Reader, out io.Writer) {
defer remoteConsole.Close()

scanner := bufio.NewScanner(in)
out.Write([]byte("> "))
_, _ = out.Write([]byte("> "))
for scanner.Scan() {
cmd := scanner.Text()
reqId, err := remoteConsole.Write(cmd)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to send command:", err.Error())
_, _ = fmt.Fprintln(os.Stderr, "Failed to send command:", err.Error())
continue
}

Expand All @@ -49,20 +77,21 @@ func Start(hostPort string, password string, in io.Reader, out io.Writer) {
if err == io.EOF {
return
}
fmt.Fprintln(os.Stderr, "Failed to read command:", err.Error())
_, _ = fmt.Fprintln(os.Stderr, "Failed to read command:", err.Error())
continue
}

if reqId != respReqId {
fmt.Fprintln(out, "Weird. This response is for another request.")
_, _ = fmt.Fprintln(out, "Weird. This response is for another request.")
}

fmt.Fprintln(out, resp)
out.Write([]byte("> "))
resp = colorize(resp)
_, _ = fmt.Fprintln(out, resp)
_, _ = out.Write([]byte("> "))
}

if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
_, _ = fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
}

Expand All @@ -81,13 +110,28 @@ func Execute(hostPort string, password string, out io.Writer, command ...string)
if err == io.EOF {
return
}
fmt.Fprintln(os.Stderr, "Failed to read command:", err.Error())
_, _ = fmt.Fprintln(os.Stderr, "Failed to read command:", err.Error())
return
}

if reqId != respReqId {
fmt.Fprintln(out, "Weird. This response is for another request.")
_, _ = fmt.Fprintln(out, "Weird. This response is for another request.")
}

resp = colorize(resp)
_, _ = fmt.Fprintln(out, resp)
}

func colorize(str string) string {
if runtime.GOOS == "windows" {
return str
}

for code := range colors {
str = strings.ReplaceAll(str, SectionSign+code, colors[code])
}

fmt.Fprintln(out, resp)
}
str = strings.ReplaceAll(str, "\n", "\n"+Reset)

return str
}
12 changes: 12 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,12 @@
version: '3'

services:
minecraft-server:
image: itzg/minecraft-server:latest
environment:
EULA: true
TYPE: paper
ENABLE_RCON: true
RCON_PASSWORD: password
ports:
- "127.0.0.1:25575:25575"

0 comments on commit b39477a

Please sign in to comment.