(ns snake.terminal-reader)

(defn read-input [input-receiver]
  (loop [data (read)]
    (case data
      "\u001B[A" ( ;(send input-receiver move 'up)
              (print "UP")
              ; (recur [data (read)])
              )
      "\u001B[B" (print "DOWN")
      
      (print (str "DEFAULT" data)))
    (recur (str data (read)))))

(defn start [input-receiver]
  (send (agent nil) read-input input-receiver))

(ns snake.terminal-reader)

(defn get-input
  "Reads a string from the given input-reader if there is anything to be read"
  [input-reader]
  
  (def buffer (char-array 1024))
  (if (.ready input-reader)
    (do
      (def num-chars-read (.read input-reader buffer))
      (String/valueOf buffer 0 num-chars-read))
    ""))


(defn read-input
  "Read from standard input and decode the input into symbols
   to be understoud be the input-receiver."
  [input-receiver input-reader]
  (loop [data (get-input input-reader)]
    (case data
      "\u001B[A" ;(send input-receiver move 'up)
              (do
                (print "UP")
                (flush))
              ; (recur [data (read)])
              
      "\u001B[B" (print "DOWN")
      "\u001B[D" (print "LEFT")
      "\u001B[C" (print "RIGHT")
      :nothing)
    (recur (get-input input-reader))))

(defn start
  "Starts a new thread that is reading from standard in and will
   notify an agent about all read input."
  [input-receiver]
  (def input-reader (new java.io.InputStreamReader System/in))
  (.start (Thread. (fn [] (read-input input-receiver input-reader)))))

Read input directly from the System/in
Read input directly from the System/in

- the (read) function did not work for me, using System/in allows me to get the input to interpret cursor keys like done in the Java Snake

Signed-off-by: Stefan Marr <46f1a0bd5592a2f9244ca321b129902a06b53e03@stefan-marr.de>

smarr/Snake,smarr/Snake,smarr/Snake,smarr/Snake,smarr/Snake,smarr/Snake,smarr/Snake,smarr/Snake