diff --git a/README.md b/README.md index 2883f3e..643d450 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,8 @@ The client uses the following flags -ip - specify alternate server destination -l - list all quotes -r [i] - remove; may be run with or without argument --e - edit +-e [i] - edit; may be run with or without argument +-c - list color format specifiers with examples -s - specify the size of the receipt buffer -v - display version and exit -vv - display version and about info and exit @@ -82,7 +83,6 @@ The server uses the following flags - Log all uses/transaction data each day - Request, send, and receive the logfile - User-specific quotes/quotefiles -- Colored printing and coloring quotes [golang]: [just]: diff --git a/client.go b/client.go index 89aaf04..160f0e4 100644 --- a/client.go +++ b/client.go @@ -1,7 +1,7 @@ // ============================================================================= // Auth: alex // File: client.go -// Revn: 09-06-2026 3.0.0 +// Revn: 09-16-2026 4.0.0 // Func: ask server for a message of the day quote // // TODO: remove ioutil import @@ -43,23 +43,34 @@ // 09-04-2026: rewrote in() with bufio instead of scanln //#09-06-2026: fixed blank message error in secondEdit() // comments +// 09-07-2026: added fatih/color import +// rewrote quoteAnswer() to handle coloring escape +// sequences +//*09-12-2026: commented quoteAnswer() +//*09-15-2026: added -c flag and color code example printing +// changed edit command to be add and then remove instead +// of remove and then add, fixing server OOB error when +// editting only quote in list +//#09-15-2026: finished pretty print implementation +// made listAnswer() print list with color print // // ============================================================================= package main import ( - "bufio" // NewReader, ReadString - "io" // io.EOF - "io/ioutil" // ReadFile - "flag" // BoolVar, StringVar, Parse - "fmt" // Println - "math/rand" // NewSource, New, Intn, *rand.Rand - "net" // ResolveTCPAddr, DialTCP, conn.Write,Read - "os" // Exit, Create, Stdin - "strconv" // Atoi, Itoa - "strings" // Split - "time" // Now, UnixNano + "bufio" // NewReader, ReadString + "io" // io.EOF + "io/ioutil" // ReadFile + "flag" // BoolVar, StringVar, Parse + "fmt" // Println + "math/rand" // NewSource, New, Intn, *rand.Rand + "net" // ResolveTCPAddr, DialTCP, conn.Write,Read + "os" // Exit, Create, Stdin + "strconv" // Atoi, Itoa + "strings" // Split + "time" // Now, UnixNano + "github.com/fatih/color" // color.New, Printf, Println ) @@ -169,7 +180,75 @@ func sizeAnswer( subcom []string ) string { // receive quote, print func quoteAnswer( subcom []string ) string { - fmt.Println( subcom[0] ) // whole-ass just print quote + + // 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 return "te@success" // set transaction end message } @@ -243,7 +322,8 @@ func listAnswer( subcom []string ) string { quotes := strings.Split( subcom[0], "\n" ) for p, v := range quotes { // iterate over quotes // print num of quote, followed by that quote - fmt.Println( p + 1, "\t", v ) + fmt.Print( p + 1, "\t" ) + quoteAnswer( []string{ v } ) } resp = "te@success" // set transaction end message } @@ -315,16 +395,18 @@ func secondEdit( subcom []string ) string { return "te@success" } - // edit command is a removal and an add + // edit command is an add and then a removal var resp string - resp = "cr@r2@" + subcom[0] + "@ar@" - // server response is text to be edited + resp = "cr@ar@" + fmt.Println( "as is: " + subcom[1] ) - fmt.Print( "preview: " ) // won't be useful until pretty print + fmt.Print( "preview: " ) // show raw of current quote _ = quoteAnswer( subcom[1:] ) // display quote properly - // get user input for new quote and send + // server response is user input for new quote resp = resp + in( "\nupdate -> " ) + // and position of old quote to be removed + resp = resp + "@r2@" + subcom[0] return resp } @@ -480,12 +562,12 @@ func handle( conn net.Conn ) { //var qsize int // keep track of num of quotes var conn net.Conn // keep track of connection - // global flag variables var add bool // add new quotes to quote file ( on server ) var remove bool // remove quotes from quote file ( on server ) var list bool // request entire quote file from server var edit bool // edit a pre-existing quote from quote file ( on server ) +var co bool // list color codes var dest string // provide a new destination ip and port var tsize int // size of transmission buffer var VERS string // version @@ -496,18 +578,19 @@ func main() { var vv bool // big version flag // define flags - flag.BoolVar( &add, "a", false, "add quote to list" ) - flag.BoolVar( &remove, "r", false, "remove quote from list" ) - flag.BoolVar( &list, "l", false, "print quote list" ) - flag.BoolVar( &edit, "e", false, "edit quote from list" ) - flag.StringVar( &dest, "ip", "127.0.0.1:1300", "ip:port of server" ) - flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" ) - flag.BoolVar( &v, "v", false, "display version and exit" ) - flag.BoolVar( &vv, "vv", false, "display version and about info and exit" ) + flag.BoolVar( &add, "a", false, "add quote to list" ) + flag.BoolVar( &remove, "r", false, "remove quote from list" ) + flag.BoolVar( &list, "l", false, "print quote list" ) + flag.BoolVar( &edit, "e", false, "edit quote from list" ) + flag.BoolVar( &co, "c", false, "list color codes with examples" ) + flag.StringVar( &dest, "ip", "127.0.0.1:1300", "ip:port of server" ) + flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" ) + flag.BoolVar( &v, "v", false, "display version and exit" ) + flag.BoolVar( &vv, "vv", false, "display version and about info and exit" ) flag.Parse() // FIXME get better versioning method - VERS = "3.0.0" + VERS = "4.0.0" if vv { // display info and exit fmt.Println( "ekho client v" + VERS ) @@ -520,6 +603,54 @@ func main() { return } + if co { // display color codes and examples and quit + // print color code and color example on same line + // color.XYZ print inserts newline + fmt.Print( "$1: " ) + color.Red( "red" ) + fmt.Print( "$2: " ) + color.Yellow( "yellow" ) + fmt.Print( "$3: " ) + color.Green( "green" ) + fmt.Print( "$4: " ) + color.Blue( "blue" ) + fmt.Print( "$5: " ) + color.Cyan( "cyan" ) + fmt.Print( "$6: " ) + color.Magenta( "magenta" ) + fmt.Print( "$7: " ) + color.Black( "black" ) + fmt.Print( "$0: " ) + color.White( "white (reset)" ) + // print bold codes + fmt.Print( "\n#1: " ) + // create bold color object, change out actual color and print + bold := color.New( color.Bold, color.FgRed ) + bold.Println( "bold red" ) + fmt.Print( "#2: " ) + bold.Add( color.FgYellow ) + bold.Println( "bold yellow" ) + fmt.Print( "#3: " ) + bold.Add( color.FgGreen ) + bold.Println( "bold green" ) + fmt.Print( "#4: " ) + bold.Add( color.FgBlue ) + bold.Println( "bold blue" ) + fmt.Print( "#5: " ) + bold.Add( color.FgCyan ) + bold.Println( "bold Cyan" ) + fmt.Print( "#6: " ) + bold.Add( color.FgMagenta ) + bold.Println( "bold magenta" ) + fmt.Print( "#7: " ) + bold.Add( color.FgBlack ) + bold.Println( "bold black" ) + fmt.Print( "#0: " ) + bold.Add( color.FgWhite ) + bold.Println( "bold white" ) + return // bail + } + service := dest // declare server ip:port // create address object diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7d99eef --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module earth-1.net/oracle/ekho + +go 1.27.1 + +require github.com/fatih/color v1.19.0 + +require ( + github.com/mattn/go-colorable v0.1.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + golang.org/x/sys v0.42.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3d002f3 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w= +github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= +golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= diff --git a/justfile b/justfile index a0bbbbb..6433258 100644 --- a/justfile +++ b/justfile @@ -53,7 +53,6 @@ ins_server: echo "systemctl status ekho" systemctl status ekho - # install client ins_client: #!/usr/bin/bash @@ -67,3 +66,57 @@ ins_client: mv ekho /usr/local/bin/ echo "add 'ekho' to shell config (config.fish, .bashrc, etc)" +# test server +stest: + @echo "creating server test session" + @tmux new-session -Ad -s "server test" + @tmux split-window -h -t "server test:1" -l 10% + @tmux split-window -v -t "server test:1.2" + @tmux send-keys -t "server test:1.1" "clear" Enter + @tmux send-keys -t "server test:1.2" "clear" Enter + @tmux send-keys -t "server test:1.3" "clear" Enter + @tmux rename-window "server test" + @tmux send-keys -t "server test:1.1" "vim server.go" Enter + @tmux send-keys -t "server test:1.2" "./server" + @tmux send-keys -t "server test:1.3" "./client" + @tmux attach -t "server test" + +# test client +ctest: + @echo "creating client test session" + @tmux new-session -Ad -s "client test" + @tmux split-window -h -t "client test:1" -l 10% + @tmux split-window -v -t "client test:1.2" + @tmux send-keys -t "client test:1.1" "clear" Enter + @tmux send-keys -t "client test:1.2" "clear" Enter + @tmux send-keys -t "client test:1.3" "clear" Enter + @tmux rename-window "client test" + @tmux send-keys -t "client test:1.1" "vim client.go" Enter + @tmux send-keys -t "client test:1.2" "./server" Enter + @tmux send-keys -t "client test:1.3" "./client" + @tmux attach -t "client test" + +# dev work +dev: + #!/usr/bin/bash + REPO=$(basename $(git rev-parse --show-toplevel)) + BRANCH=$(git branch --show-current) + tmux has-session -t "$REPO->$BRANCH" + if [[ $? -eq 0 ]]; then + tmux attach-session -t "$REPO->$BRANCH" + else + tmux new-session -Ad -s "$REPO->$BRANCH" + tmux rename-window "write" + tmux new-window -a -t 1 -n "test" + tmux new-window -a -t 2 -n "git" lazygit + tmux new-window -a -t 3 -n "man" + tmux select-window -t 2 + tmux select-window -t 1 + tmux attach -t "$REPO->$BRANCH" + fi + +# prep repo to be committed +prep: + mv justfile test_justfile + mv prod_justfile justfile + diff --git a/list.q b/list.q index 0a199fd..544a748 100644 --- a/list.q +++ b/list.q @@ -1 +1 @@ -thank you for using ekho \ No newline at end of file +thank you for using $1ekho \ No newline at end of file diff --git a/server.go b/server.go index 212e71f..3353273 100644 --- a/server.go +++ b/server.go @@ -1,14 +1,13 @@ // ============================================================================= // Auth: alex // File: server.go -// Revn: 09-06-2026 3.0.0 +// Revn: 09-15-2026 3.0.2 // Func: host MOTD connection // // TODO: add user-specific prints // write usage and errors to logfile // remove ioutil import // send logfile to client on request -// add color printing to qfile // add flags? // add more /codes/ to handle // ============================================================================= @@ -52,11 +51,10 @@ // 05-12-2026: added v and vv input flags // 09-05-2026: buncha shit happened, dropped the commit //#09-06-2026: added /e1/, /e2/, and /cr/, got edit working -//H09-12-2026: minor comments in receive() -// hotfix -> accidentally trying to Atoi the command -// instead of the argument -// hotfix -> subtracting 1 from int before getting quote -// from list +// 09-12-2026: minor comments in receive() +//*09-15-2026: fixed OOB error in /qa/ for perhaps the last time +// fixed bad Atoi conversion bug for perhaps the last time +// // ============================================================================= package main @@ -155,27 +153,18 @@ func receive( conn net.Conn ) { n, err := conn.Read( buffer[:] ) checkN( err, conn ) // make sure read worked - // message anatomy - // command@number[@user] - // @ @ delimiter - // command command, like list request or quote request - // number sequence or quote number - // user quote username if applicable, default if not give - // add request message anatomy - // command@quote[@user] - // quote quote to be added - // user quote username, default if not given - // cast message to string, take first n bytes, split over @ command := strings.Split( string( buffer[:n] ), "@" ) + // if command is to end transaction if command[0] == "te" { - conn.Close() - return + conn.Close() // end transaction + return // and bail } + // process command get response resp := handle( command ) - + // write response to client _, err = conn.Write( []byte( resp ) ) checkN( err, conn ) // make sure write worked @@ -442,7 +431,6 @@ func handle( command []string ) string { // globals var qlist []string // list of quotes -//var qlist = make( map[string][]string ) var qfile string // file object var tsize int // size of transmission buffer var VERS string // version @@ -466,7 +454,7 @@ func main() { flag.Parse() // FIXME get better versioning method - VERS = "3.0.0" + VERS = "3.0.2" if vv { // display info and exit fmt.Println( "ekho server v" + VERS )