Cmd.exec Example
Demonstrates running shell commands using Cmd.exec.
Commands in TEA produce messages, not callbacks. When a command completes, the runtime sends a tagged tuple to your update function. Pattern match on the tag to handle success and failure.
Key Concepts
-
Message Tags:
Cmd.exec(command, tag)produces[tag, {stdout:, stderr:, status:}]. -
Non-Blocking: Commands run in background threads. The UI remains responsive.
-
Success Handling: Match on
status: 0to handle successful execution. -
Error Handling: Match on non-zero status to handle failures.
-
Ractor-Safe: No callbacks means no Proc captures. Messages are shareable.
Hotkeys
-
d: Runls -la(directory listing) -
u: Rununame -a(system info) -
f: Run a command that fails (demonstrates error handling) -
q: Quit
Usage
ruby examples/widget_cmd_exec/app.rb
How It Works
The update function handles both key presses and command results:
UPDATE = -> (msg, model) do case msg # Handle command results in [:got_output, {stdout:, status: 0}] [model.with(result: stdout.strip, loading: false), nil] in [:got_output, {stderr:, status:}] [model.with(result: "Error (exit #{status}): #{stderr.strip}", loading: false), nil] # Handle key presses in _ if msg.d? [model.with(loading: true), Cmd.exec("ls -la", :got_output)] else model end end
All logic stays in update. The command just runs and produces a message.
