rewrote quoteAnswer

This commit is contained in:
2026-09-12 23:02:44 -04:00
parent 5b0ef3e596
commit 3e9e17323d
+86 -14
View File
@@ -1,7 +1,7 @@
// ============================================================================= // =============================================================================
// Auth: alex // Auth: alex
// File: client.go // File: client.go
// Revn: 09-06-2026 3.0.0 // Revn: 09-12-2026 3.0.2
// Func: ask server for a message of the day quote // Func: ask server for a message of the day quote
// //
// TODO: remove ioutil import // TODO: remove ioutil import
@@ -43,23 +43,28 @@
// 09-04-2026: rewrote in() with bufio instead of scanln // 09-04-2026: rewrote in() with bufio instead of scanln
//#09-06-2026: fixed blank message error in secondEdit() //#09-06-2026: fixed blank message error in secondEdit()
// comments // comments
// 09-07-2026: added fatih/color import
// rewrote quoteAnswer() to handle coloring escape
// sequences
// 09-12-2026: commented quoteAnswer()
// //
// ============================================================================= // =============================================================================
package main package main
import ( import (
"bufio" // NewReader, ReadString "bufio" // NewReader, ReadString
"io" // io.EOF "io" // io.EOF
"io/ioutil" // ReadFile "io/ioutil" // ReadFile
"flag" // BoolVar, StringVar, Parse "flag" // BoolVar, StringVar, Parse
"fmt" // Println "fmt" // Println
"math/rand" // NewSource, New, Intn, *rand.Rand "math/rand" // NewSource, New, Intn, *rand.Rand
"net" // ResolveTCPAddr, DialTCP, conn.Write,Read "net" // ResolveTCPAddr, DialTCP, conn.Write,Read
"os" // Exit, Create, Stdin "os" // Exit, Create, Stdin
"strconv" // Atoi, Itoa "strconv" // Atoi, Itoa
"strings" // Split "strings" // Split
"time" // Now, UnixNano "time" // Now, UnixNano
"github.com/fatih/color" // color.New, Printf, Println
) )
@@ -169,6 +174,74 @@ func sizeAnswer( subcom []string ) string {
// receive quote, print // receive quote, print
func quoteAnswer( subcom []string ) string { func quoteAnswer( subcom []string ) string {
// split subcommand over spaces
words := strings.Split( subcom[0], " " )
// default text is white
c := color.New( color.FgWhite )
for _, w := range words {
// if first char is normal color escape character
if string( w[0] ) == "$" {
// see which color is requested
switch string( w[1] ) {
case "1": // 1 -> red
c = color.New( color.FgRed )
case "2": // 2 -> yellow
c = color.New( color.FgYellow )
case "3": // 3 -> green
c = color.New( color.FgGreen )
case "4": // 4 -> blue
c = color.New( color.FgBlue )
case "5": // 5 -> cyan
c = color.New( color.FgCyan )
case "6": // 6 -> magenta
c = color.New( color.FgMagenta )
case "7": // 7 -> black
c = color.New( color.FgBlack )
case "0": // 0 -> white again
c = color.New( color.FgWhite )
default:
// none of these, continue with the last color
}
// color chosen, print string with trailing space, minus
// first two characters
c.Printf( "%s ", w[2:] )
// if first char is bold color escape character
} else if string( w[0] ) == "#" {
// see which color is requested
switch string( w[1] ) {
case "1": // 1 -> red
c = color.New( color.FgRed, color.Bold )
case "2": // 2 -> yellow
c = color.New( color.FgYellow, color.Bold )
case "3": // 3 -> green
c = color.New( color.FgGreen, color.Bold )
case "4": // 4 -> blue
c = color.New( color.FgBlue, color.Bold )
case "5": // 5 -> cyan
c = color.New( color.FgCyan, color.Bold )
case "6": // 6 -> magenta
c = color.New( color.FgMagenta, color.Bold )
case "7": // 7 -> black
c = color.New( color.FgBlack, color.Bold )
case "0": // 0 -> white again
c = color.New( color.FgWhite, color.Bold )
default:
// none of these, continue with the last color
}
// color chosen, print string with trailing space, minus
// first two characters
c.Printf( "%s ", w[2:] )
} else { // TODO add if for if w[0] is "\"
// using the currently selected color, print the next work
// and a trailing space
c.Printf( "%s ", w )
}
}
c.Println() // print final newline
fmt.Println( subcom[0] ) // whole-ass just print quote fmt.Println( subcom[0] ) // whole-ass just print quote
return "te@success" // set transaction end message return "te@success" // set transaction end message
} }
@@ -480,7 +553,6 @@ func handle( conn net.Conn ) {
//var qsize int // keep track of num of quotes //var qsize int // keep track of num of quotes
var conn net.Conn // keep track of connection var conn net.Conn // keep track of connection
// global flag variables // global flag variables
var add bool // add new quotes to quote file ( on server ) var add bool // add new quotes to quote file ( on server )
var remove bool // remove quotes from quote file ( on server ) var remove bool // remove quotes from quote file ( on server )
@@ -507,7 +579,7 @@ func main() {
flag.Parse() flag.Parse()
// FIXME get better versioning method // FIXME get better versioning method
VERS = "3.0.0" VERS = "3.0.2"
if vv { // display info and exit if vv { // display info and exit
fmt.Println( "ekho client v" + VERS ) fmt.Println( "ekho client v" + VERS )