libtmux.core-async/observe!
Starts an owned virtual worker over a borrowed control connection.
Source and package
- Qualified name
-
libtmux.core-async/ observe! - Module
- libtmux.core-async
- Declared in
libtmux.core-async- Package
- io.github.libtmux:libtmux-clojure-core-async
- Source
- libtmux-clojure-core-async/src/main/clojure/libtmux/core_async.clj
- (observe! connection)(observe! connection options)
-
Public owner:
libtmux.core-asyncDescription#
:event-capacitydefaults to zero. The worker can retain one pending event; a fixed buffer retains that many more.:upstream-capacitybounds the Java subscription, which reports overflow as a:gapvalue ahead of the events that remain;:encodingis:bytesor:utf-8.Closing the events channel or adapter stops blocked reads and puts. A stopped outcome retains the final:upstream/terminal, including loss, cause and trailing text. Adapter loss stays separate under:adapter/events.The borrowed connection remains open. Borrow a connection fromlibtmux.control/attach!; readeventsandterminal.Examples#
Read bounded core.async events and stop without closing the borrowed connection. Use JDK 25 or newer, the Clojure CLI, Git and tmux on Linux or macOS, with /bin/cat and /bin/sh available. These packages are unpublished. In an empty directory, install this exact source into the consumer's isolated Maven repository:
Terminal window $ git clone https://github.com/libtmux/libtmux-java libtmux-source && \git -C libtmux-source checkout --detach 5f88402151ff08fc0390dd438b90963692ec3fe4 && \(cd libtmux-source && ./gradlew \:libtmux:publishToMavenLocal \:libtmux-clojure:publishToMavenLocal \:libtmux-clojure-core-async:publishToMavenLocal \--console=plain \"-Dmaven.repo.local=$PWD/../m2")Save this file as
deps.edn. Dependencies resolve from the isolated m2 directory; the core.async dependency is only enabled by its alias. View source{:paths ["src/main/clojure"]:mvn/local-repo "m2":deps {org.clojure/clojure {:mvn/version "1.12.6"}io.github.libtmux/libtmux-clojure {:mvn/version "0.1.2-SNAPSHOT"}}:aliases{:core-async{:extra-deps{io.github.libtmux/libtmux-clojure-core-async {:mvn/version "0.1.2-SNAPSHOT"}}}}}Save this file as
run.sh. The launcher creates and stops a private server, including after program failure, and reports leaked servers as errors. View source#!/bin/shset -euif [ "$#" -eq 0 ]; thenprintf 'Usage: sh run.sh program [arguments...]\n' >&2exit 2fibinary=$(command -v "${TMUX_BIN:-tmux}")root=${API_RUN_ROOT:-/tmp/libtmux-java-dev}# A client that names no socket follows TMUX or LIBTMUX_SOCKET*, and otherwise lands on the default# socket under TMUX_TMPDIR. The program gets none of them, and a TMUX_TMPDIR of its own below.unset TMUX TMUX_PANEfor name in $(env | sed -n 's/^\(LIBTMUX_SOCKET[A-Za-z0-9_]*\)=.*/\1/p'); dounset "$name"donemkdir -p "$root"directory=$(mktemp -d "$root/api.XXXXXX")tmpdir=$(mktemp -d "$root/tmpdir.XXXXXX")socket="$directory/tmux.sock"config="$directory/tmux.conf"serves() {"$binary" -S "$1" display-message -p '#{pid}' >/dev/null 2>&1}# Stops the server on the socket, then waits for it to stop answering.stop() {"$binary" -S "$1" kill-server || return 1attempts=0while serves "$1" && [ "$attempts" -lt 60 ]; dosleep 0.05attempts=$((attempts + 1))done! serves "$1"}cleanup() {status=$?trap - 0 HUP INT TERM PIPE# A closed output must not end the cleanup, whose messages are the only thing left to write.trap '' PIPEset +estuck=left=if [ -S "$socket" ] && serves "$socket" && ! stop "$socket"; thenstuck="$stuck tmux.sock"fi# Every socket anywhere under either directory, without following links. Only one a server answers# on is a leak; the runner's own is expected to be there.old_ifs=$IFSIFS=''set -ffor candidate in $(find "$tmpdir" "$directory" -type s 2>/dev/null); do[ "$candidate" = "$socket" ] && continueserves "$candidate" || continuecase "$candidate" in"$tmpdir"/*) name=${candidate#"$tmpdir"/} ;;*) name=${candidate#"$directory"/} ;;esacleft="$left $name"stop "$candidate" || stuck="$stuck $name"doneset +fIFS=$old_ifsif [ -n "$stuck" ]; thenprintf 'tmux still answers on%s after kill-server; kept %s and %s under the runner root\n' \"$stuck" "${directory##*/}" "${tmpdir##*/}" >&2exit 1firm -rf "$directory" "$tmpdir" || exit 1if [ -n "$left" ]; thenprintf 'The program left a tmux server running:%s\n' "$left" >&2exit 1fiexit "$status"}trap cleanup 0trap 'exit 1' HUP INT TERM PIPE# A default socket path longer than this does not fit in sun_path.default_socket="$tmpdir/tmux-$(id -u)/default"if [ "$(printf '%s' "$default_socket" | wc -c)" -gt 100 ]; thenprintf 'API_RUN_ROOT is too long for a tmux socket\n' >&2exit 1fiTMUX_TMPDIR=$tmpdirexport TMUX_TMPDIRprintf 'set-option -g default-shell /bin/sh\n' > "$config""$binary" -S "$socket" -f "$config" new-session -d -s work-one -n editor /bin/cat"$binary" -S "$socket" split-window -h -t '=work-one:editor' /bin/cat"$binary" -S "$socket" new-window -d -t '=work-one' -n logs /bin/cat"$binary" -S "$socket" new-session -d -s work-two -n editor /bin/cat"$@" "$binary" "$socket" "$config""$binary" -S "$socket" has-session -t '=work-one'"$binary" -S "$socket" has-session -t '=work-two'Save the complete program as
src/main/clojure/libtmux/examples/api/observe_with_async.clj, creating its parent directories. Closing its client leaves the launcher's server running. View source(ns libtmux.examples.api.observe-with-async"Read bounded core.async events and close the consumer before draining."(:require [clojure.core.async :as channel][clojure.string :as str][libtmux.control :as control][libtmux.core :as tm][libtmux.core-async :as adapter][libtmux.data :as data])(:import [java.lang AutoCloseable][java.time Duration]))(defn -main [& args](when-not (= 3 (count args))(throw (IllegalArgumentException. "expected: tmux-binary socket-path config-file")))(let [[binary socket config] args](with-open [server (tm/open! {:binary binary :socket-path socket :config-file config})](let [session (tm/new-session! server {:name "async-example" :command ["/bin/sh"]})pane (data/exactly-one (data/panes session))marker "channel-ready"](try(with-open [^AutoCloseable connection (control/attach! (:tmux/ref session) (Duration/ofMillis 900))];; Retain the default upstream bound for fragmented terminal output.(with-open [^AutoCloseable owner (adapter/observe! connection{:event-capacity 1 :encoding :utf-8})](let [deadline (channel/timeout 900)](tm/send-literal! (:tmux/ref pane) "printf 'channel-%s\\n' ready")(tm/send-keys! (:tmux/ref pane) ["Enter"])(loop [suffix ""](let [[step port] (channel/alts!! [(adapter/events owner) deadline] :priority true)](when (or (identical? port deadline) (not= :event (:stream/status step)))(throw (ex-info "Channel observation timed out, lost output or ended" {:step step})))(let [text (if (= (:pane/id pane) (:pane/id step))(str suffix (:output/text step)) suffix)](when-not (str/includes? text marker)(recur (subs text (max 0 (- (count text) (dec (count marker)))))))))))(channel/close! (adapter/events owner))(let [terminal-channel (adapter/terminal owner)[outcome port] (channel/alts!! [terminal-channel (channel/timeout 900)] :priority true)](assert (identical? terminal-channel port))(assert (= :adapter-stopped (:reason outcome)))))(assert (= :complete (:reply/status(control/send! connection ["display-message" "-p" "#{session_name}"]))))(println (str "observed=" marker))(println "adapter-stopped=true borrowed-open=true"))(catch clojure.lang.ExceptionInfo failure(if (= :tmux/unsupported-feature (:tmux/error (ex-data failure)))(do(assert (= :not-dispatched (:tmux/dispatch (ex-data failure))))(assert (empty? (tm/clients! server)))(println "requires tmux 3.7+"))(throw failure))))))))Run it with the private fixture. Set TMUX_BIN to select another tmux binary:
Terminal window $ sh run.sh clojure \-J-Dclojure.main.report=stderr \-M:core-async \-m libtmux.examples.api.observe-with-asyncExpected output on tmux 3.7 or newer:
observed=channel-readyadapter-stopped=true borrowed-open=trueOn an older supported tmux, the program reports the control capability refusal:
requires tmux 3.7+- Parameters
-
Guides#
Pages that discuss observe!.