so im making a casino on a telnet server for me and my friends to play as a learning project and my friend asked me if i could make "clickable" buttons like htop has i was wondering how this is possible? ive been researching for quite a while on how to achive this however ive only ran into goncurses which from my understanding is only supported on linux is there anyway i can do this?
#Mouse Interaction?
7 messages · Page 1 of 1 (latest)
If a Telnet server is not required, you may consider using this method for interaction.
package main
import (
"embed"
"encoding/json"
"fmt"
"log"
"net/http"
)
//go:embed main.html
var mainHTML embed.FS
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// http.ServeFile(w, r, "main.html")
// http.FileServer(http.FS(mainHTML))
fmt.Fprint(w, "XXX")
})
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
name := query.Get("name")
nameMap := map[string]func(para string){
"Test": func(para string) {
log.Println("hello world!")
_, _ = w.Write([]byte("ok"))
},
"Say": func(para string) {
var input = struct {
Msg string
}{}
if err := json.Unmarshal([]byte(para), &input); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Println(input.Msg)
_, _ = w.Write([]byte("ok!"))
},
}
if apiFunc, exists := nameMap[name]; !exists {
http.Error(w, "command not exists", http.StatusBadRequest)
} else {
apiFunc(query.Get("para"))
}
})
server := http.Server{Addr: "127.0.0.1:8888", Handler: mux}
if err := server.ListenAndServe(); err != nil {
log.Println(err)
}
}
Sorry, I must not be understanding - what does that have to do with clickable buttons?
Where XXX equal
<h1>Welcome to XXX</h1>
<textarea placeholder="input command..." cols="100" rows="5" aria-label=""></textarea><br>
<pre id="status-bar"></pre>
<script>
const textArea = document.querySelector("textarea")
document.onkeydown = async (e) => {
if (e.key !== "Enter" || e.shiftKey) { // Enter+shift=new line no trigger
return
}
const [name, para] = textArea.value.split("|")
e.preventDefault()
const params = new URLSearchParams()
params.append('name', name)
params.append('para', para)
const response = await fetch('/api/?' + params.toString())
if (!response.ok) {
const errMsg = await response.text()
alert(errMsg)
}
document.querySelector("#status-bar").innerHTML = await response.text()
}
</script>
If I merge them together, Discord seems to have an issue, so I can only paste them separately.
Ohh, okay
I'm unfamiliar with the restrictions of a telnet server, but if it only needs to run in a terminal, it might be fine to use something like tview to create a terminal app to handle the UI: https://github.com/rivo/tview
I used it to create a terminal app that communicates remotely with a GUI program running on the same computer, and that works rather well.