1
0
Fork 0

Sorta working but readline is being annoying

This commit is contained in:
Vivianne 2024-02-02 23:00:52 -05:00
parent 1776832132
commit d308befda3
5 changed files with 52 additions and 29 deletions

View File

@ -6,7 +6,7 @@
(define (^room bcom creator-presence) (define (^room bcom creator-presence)
"The creator owns the canonical room which publishes to subscribers." "The creator owns the canonical room which publishes to subscribers."
(define pubsub (spawn ^pubsub (list creator-presence))) (define pubsub (spawn ^pubsub creator-presence))
(methods (methods
[(add-user presence) [(add-user presence)
($ pubsub 'subscribe presence) ($ pubsub 'subscribe presence)

View File

@ -1,5 +1,6 @@
(define-module (bugafriend ui) (define-module (bugafriend ui)
#:use-module (bugafriend utils registry) #:use-module (bugafriend utils registry)
#:use-module (bugafriend user)
#:use-module (bugafriend room) #:use-module (bugafriend room)
#:use-module (goblins) #:use-module (goblins)
#:use-module (goblins vat) #:use-module (goblins vat)
@ -16,9 +17,15 @@
#:use-module (ice-9 suspendable-ports) #:use-module (ice-9 suspendable-ports)
#:export (say)) #:export (say))
;; https://www.gnu.org/software/guile/manual/guile.html#Non_002dBlocking-I_002fO
(let* ((input (current-input-port))
(flags (fcntl input F_GETFL)))
(fcntl input F_SETFL (logior O_NONBLOCK flags)))
(install-suspendable-ports!)
(define ocapn-registry #f) (define ocapn-registry #f)
(define room-actor #f) (define user-actor #f)
(define (is-command? str) (define (is-command? str)
(and (> (string-length str) 0) (eq? (string-ref str 0) #\/))) (and (> (string-length str) 0) (eq? (string-ref str 0) #\/)))
@ -30,6 +37,9 @@
(help console-command-help) (help console-command-help)
(thunk console-command-thunk)) (thunk console-command-thunk))
(define (get-a-room)
($ user-actor 'room-data))
(define commands (define commands
(list (list
(make-console-command (make-console-command
@ -46,17 +56,17 @@
"/create" "/create"
"- Create a new chat and join it." "- Create a new chat and join it."
(λ (args) (λ (args)
(define my-presence (spawn ^room-presence "user")) ($ user-actor 'make-room)
(set! room-actor (spawn ^room my-presence))
(format #t "Room ID: ~a\n" ($ ocapn-registry 'register room-actor))
(loop! #t))) (loop! #t)))
(make-console-command (make-console-command
"/me" "/me"
"<text> - Me command, you like roleplay or whatever" "<text> - Me command, you like roleplay or whatever"
(λ (args) (λ (args)
(when (room-actor) (let* ((room-data (get-a-room))
(room (room-data-room room-data))
(presence (room-data-presence room-data)))
;; eww, maybe fix mangling the input by joining ;; eww, maybe fix mangling the input by joining
(<- room-actor 'me (string-join args " ")) (<- room 'me presence (string-join args " "))
(loop! #t)))) (loop! #t))))
(make-console-command (make-console-command
"/join" "/join"
@ -72,11 +82,9 @@
(unless ocapn-registry (unless ocapn-registry
(error "Relay not yet connected.")) (error "Relay not yet connected."))
(format #t "Connecting...\n")
(on (<- ocapn-registry 'enliven room-sref) (on (<- ocapn-registry 'enliven room-sref)
(λ (l) (λ (r)
(set! room-actor l) ($ user-actor 'join-room r)
(format #t "Joined chat.\n")
(loop! #t)) (loop! #t))
#:catch #:catch
(λ (e) (λ (e)
@ -114,24 +122,32 @@
((eq? 0 (string-length line)) (loop! #t)) ((eq? 0 (string-length line)) (loop! #t))
((is-command? line) ((is-command? line)
(%eval-command line)) (%eval-command line))
(else (if room-actor (else
(on (<- room-actor 'say line) (λ (val) (loop! val))) (let ((room-data (get-a-room)))
(begin (if room-data
(format #t "Not connected to anyone yet. Use /join <sturdyref>!\n") (let ((room (room-data-room room-data))
(loop! #t)))))))) (presence (room-data-presence room-data)))
(format #t "Presence: ~s\n" presence)
(on (<- room 'say presence line)
(λ (val) (loop! val))))
(begin
(format #t "Not connected to anyone yet. Use /join <sturdyref>!\n")
(loop! #t)))))))))
#:unwind? #t)) #:unwind? #t))
(define (say setup-sref) (define (say setup-sref name)
(define vat (spawn-vat #:name "Speaker Vat")) (define vat (spawn-vat #:name "Speaker Vat"))
(set-readline-prompt! " 🐞 > ") (set-readline-prompt! " 🐞 > ")
(with-vat vat (with-vat
(format #t "Connecting to relay... ~s\n" setup-sref) vat
(format #t "Connecting to relay...\n")
(on (prelay-sref->mycapn-registry setup-sref) (on (prelay-sref->mycapn-registry setup-sref)
(λ (r) (λ (r)
(set! ocapn-registry r) (set! ocapn-registry r)
(format #t "Connected.\n") (format #t "Connected. Creating user actor.\n")
(set! user-actor (spawn ^user name ocapn-registry #f))
(loop! #t)) (loop! #t))
#:catch #:catch
(λ (e) (λ (e)

View File

@ -6,7 +6,11 @@
#:use-module (goblins actor-lib methods) #:use-module (goblins actor-lib methods)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:use-module (srfi srfi-9) #:use-module (srfi srfi-9)
#:export (^user)) #:export (^user
make-room-data
room-data?
room-data-room
room-data-presence))
(define-record-type <room-data> (define-record-type <room-data>
(make-room-data room presence) (make-room-data room presence)
@ -16,6 +20,7 @@
(define (^user bcom name registry joined-room-data) (define (^user bcom name registry joined-room-data)
(methods (methods
[(room-data) joined-room-data]
[(make-room) [(make-room)
(let* ((my-presence (spawn ^room-presence name)) (let* ((my-presence (spawn ^room-presence name))
(room (spawn ^room my-presence)) (room (spawn ^room my-presence))
@ -26,15 +31,17 @@
(bcom (^user bcom name registry room-data)))] (bcom (^user bcom name registry room-data)))]
[(join-room room) [(join-room room)
(format #t "Connecting...\n")
(let ((my-presence (spawn ^room-presence name))) (let ((my-presence (spawn ^room-presence name)))
(when joined-room-data (when joined-room-data
(<-np (room-data-room joined-room-data) 'kick-user (room-data-presence joined-room-data)) (<-np (room-data-room joined-room-data) 'kick-user (room-data-presence joined-room-data)))
(on (<- room 'add-user my-presence)
(λ (_)
(format #t "Joined room.\n")))
(define room-data (make-room-data room my-presence)) (on (<- room 'add-user my-presence)
(bcom (^user bcom name registry room-data))))] (λ (_)
(format #t "Joined room.\n")))
(define room-data (make-room-data room my-presence))
(bcom (^user bcom name registry room-data)))]
[(leave-room room) [(leave-room room)
(when (eq? room (room-data-room joined-room-data)) (when (eq? room (room-data-room joined-room-data))

View File

@ -8,4 +8,4 @@
(fibers conditions) (fibers conditions)
(bugafriend ui)) (bugafriend ui))
(say (string->ocapn-id (list-ref (command-line) 1))) (say (string->ocapn-id (list-ref (command-line) 1)) (list-ref (command-line) 2))

View File

@ -8,4 +8,4 @@
(fibers conditions) (fibers conditions)
(bugafriend ui)) (bugafriend ui))
(say (string->ocapn-id (list-ref (command-line) 1))) (say (string->ocapn-id (list-ref (command-line) 1)) (list-ref (command-line) 2))