1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//!
//! ## Query iterators
//!
//! Listings hand back an ordered `Vec<T>` that the caller owns. Borrow it with
//! `.iter()`, use [`Iterator::filter`] for an inline closure, and
//! [`query::QueryIteratorExt::matching`] for a portable expression or a named
//! [`query::Matcher`]. Exact cardinality inspects at most two items.
//!
//! `matching` and `matching_owned` are this trait's own; `exactly_one` and
//! `one_or_none` deliberately overlap with `itertools::Itertools`, which has
//! `exactly_one` and `at_most_one`. The overlap is kept rather than renamed
//! around: the names are the obvious ones, and the shapes differ where it
//! matters. [`query::ExactlyOneError`] is a plain `NoItems`/`MultipleItems`
//! enum that is `Eq` and cheap to match, where itertools' error owns the
//! iterator so it can replay it. With both traits imported a call is
//! ambiguous, which is a compile error naming both candidates rather than a
//! silent choice; universal function call syntax picks one:
//!
//! ```
//! use libtmux::query::QueryIteratorExt;
//!
//! let values = vec![1];
//! let item = QueryIteratorExt::exactly_one(values.iter());
//! assert_eq!(item, Ok(&1));
//! ```
//!
//! ## Finding what is already there
//!
//! Naming an object is cheaper than listing and scanning for it:
//!
//! ```no_run
//! # async fn walk() -> Result<(), libtmux::Error> {
//! let server = libtmux::Server::new()?;
//!
//! // Find one object rather than listing and scanning.
//! if let Some(session) = server.session("work").await? {
//! if let Some(window) = session.window("editor").await? {
//! if let Some(pane) = window.active_pane().await? {
//! pane.send_line("cargo test").await?;
//! }
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! A listing keeps the reason it failed. A caller that would rather show
//! nothing than an error says so at the call site, where it reads as the
//! choice it is:
//!
//! ```no_run
//! # async fn both(server: &libtmux::Server) -> Result<(), libtmux::Error> {
//! let loud = server.sessions().await?; // Err on failure
//! let quiet = server.sessions().await.unwrap_or_default(); // empty on failure
//! # let _ = (quiet, loud);
//! # Ok(())
//! # }
//! ```
//!
//! ## Building something and cleaning up
//!
//! Once polled, a scoped operation owns creation and cleanup. Cancellation can
//! let an in-flight creation finish, but an object whose creation yields a
//! handle is killed while the Tokio runtime remains active. Ordinary handle
//! `Drop` is deliberately non-destructive.
//!
//! ```no_run
//! # async fn scoped(server: &libtmux::Server) -> Result<(), libtmux::ScopeError<String, libtmux::Error>> {
//! let id = server
//! .with_session("throwaway", async |session| {
//! session.new_window("build").await?;
//! Ok::<_, libtmux::Error>(session.id().to_string())
//! })
//! .await?;
//! # let _ = id;
//! # Ok(())
//! # }
//! ```
//!
//! [`ScopeError`] distinguishes creation, operation and cleanup failures.
//! When both operation and cleanup fail, it retains both errors without
//! converting the operation's error type. Cleanup errors carry
//! [`Error::AfterEffect`] because creation already succeeded.
//!
//! ## Options carry types
//!
//! tmux reports no type over the command line, so the crate generates the
//! schema from tmux's own table. That matters more than it sounds: `status`
//! holds `"on"` but is a choice, because tmux also accepts `2` through `5`.
//! A typed write is checked against the same table before it is sent;
//! [`OptionValue`] says how reads and writes fit together.
//!
//! ```no_run
//! # async fn options(server: &libtmux::Server) -> Result<(), libtmux::Error> {
//! use libtmux::{OptionValue, option_names};
//!
//! // Names are constants, so a typo does not compile.
//! let mouse = server.typed_global_option(option_names::MOUSE).await?;
//! assert!(matches!(mouse, Some(OptionValue::Flag(_))));
//!
//! // A write takes the type a read returns, and a value outside what the
//! // table declares is refused before tmux sees it.
//! server.set_typed_global_option(option_names::HISTORY_LIMIT, 50_000).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## A name reaches tmux as text
//!
//! tmux expands a name through its format machinery before it checks it, so
//! `#{session_id}` in a name would become the id and `#(command)` would run
//! `command` in a shell. That holds for a session and window name, a pane
//! title, an option name, and the `-c` start directory alike. tmux is
//! consistent here: whoever can run `tmux new-session` can already run
//! commands, so a name given on a command line is trusted by construction.
//!
//! A library moves that boundary. tmux's caller is a person at a shell; this
//! crate's caller is a program, and the name it passes may have come from an
//! argument, a request field, or a configuration file. So every argument tmux
//! would expand is typed [`TmuxArg`], and every conversion into it escapes:
//! what a caller passes is what tmux stores.
//!
//! ```no_run
//! # async fn names(server: &libtmux::Server) -> Result<(), libtmux::Error> {
//! use libtmux::{NewSessionOptions, TmuxArg};
//!
//! // Text is text, whatever is in it.
//! server.new_session("release#1").await?;
//!
//! // Expansion is opt-in, and reads as such. Only for a template the
//! // program itself wrote.
//! server
//! .new_session(NewSessionOptions::new("build").start_directory(
//! TmuxArg::format("#{pane_current_path}"),
//! ))
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! Expansion is not the only way the name you asked for is not the name you
//! get. tmux releases through 3.6b rewrite `:` and `.` in a session name to
//! `_`, because a target is split on those, and they do it silently: 3.7
//! refuses such a name outright, and 3.7a keeps it. So `new_session("a:b")`
//! succeeds on every supported release except 3.7 and hands back a session
//! called `a_b` on most of them. The handle reports what tmux stored, so
//! [`Session::name`] is always the truth; the request is what may differ from
//! it. Compare the two when the name has to round-trip.
//!
//! ## Examples
//!
//! Runnable programs live in `examples/`. `inspect` reports what a server is
//! running and `find` selects panes with a typed expression, both of which
//! only read. `scratch` builds a throwaway session on its own socket and
//! cleans it up, which is the shortest complete tour: a window, a split, keys
//! sent, output waited for rather than slept on, and a scope that kills the
//! session whether the body succeeded or not. `watch` reacts to what a server
//! does over one control-mode connection while driving it down the same one.
//! `matrix` runs one workload six ways, so the cost of each execution mode is
//! visible side by side. `sweep` reaps servers that abandoned fixtures left
//! behind, which is maintenance rather than orchestration.
//!
//! `just examples` runs every one of them against a server it owns and fails
//! if any leaves a socket behind.
//!
//! ## Filtering the hierarchy
//!
//! [`Session`], [`Window`], [`Pane`], and [`Client`] carry generated field
//! handles, so an expression names the same type a listing returns:
//!
//! ```
//! use libtmux::query::Filterable as _;
//!
//! let fields = libtmux::Session::filter_fields();
//! let expression = fields.session_name.starts_with("build");
//! let sessions: Vec<libtmux::Session> = Vec::new();
//! assert_eq!(sessions.iter().count(), 0);
//! # let _ = expression;
//! ```
//!
//! Field types decide which operations exist, so a mismatched comparison is a
//! compile error rather than a predicate that is always false:
//!
//! ```compile_fail
//! use libtmux::query::Filterable as _;
//!
//! let fields = libtmux::Session::filter_fields();
//! // `session_name` is text, so it has no integer comparison.
//! let _ = fields.session_name.eq(3_u32);
//! ```
//!
//! ```compile_fail
//! use libtmux::query::Filterable as _;
//!
//! let fields = libtmux::Session::filter_fields();
//! // `session_windows` is an integer, so it has no substring operation.
//! let _ = fields.session_windows.contains("3");
//! ```
//!
//! A question about what a session *contains* needs a value that holds its
//! windows. [`Server::hierarchy`] returns one, and [`SessionTree`] and
//! [`WindowTree`] carry relations for it:
//!
//! ```no_run
//! # async fn contained(server: &libtmux::Server) -> Result<(), libtmux::Error> {
//! use libtmux::query::{Filterable as _, QueryIteratorExt as _};
//! use libtmux::{SessionTree, WindowTree};
//!
//! let sessions = SessionTree::filter_fields();
//! let windows = WindowTree::filter_fields();
//!
//! // The session's own fields sit beside the relation, not behind it.
//! let building = sessions
//! .session
//! .session_name
//! .starts_with("build")
//! .and(sessions.windows.any(windows.window.window_name.eq("editor")));
//!
//! for branch in server.hierarchy().await?.iter().matching(&building) {
//! println!("{}", branch.session);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! Use `matching_owned` to move selected items out of their collection:
//!
//! ```
//! use libtmux::query::QueryIteratorExt;
//!
//! let values = vec![1, 2, 3];
//! let selected = values
//! .into_iter()
//! .matching_owned(|candidate: &i32| *candidate > 1)
//! .collect::<Vec<_>>();
//! assert_eq!(selected, [2, 3]);
//! ```
//!
//! `matching` still needs a borrowed iterator; `into_iter()` does not satisfy
//! it:
//!
//! ```compile_fail,E0271
//! use libtmux::query::QueryIteratorExt;
//!
//! let values = vec![1, 2, 3];
//! let _ = values.into_iter().matching(|candidate: &i32| *candidate > 1);
//! ```
//!
//! Borrowed results cannot outlive their collection:
//!
//! ```compile_fail,E0597
//! use libtmux::query::QueryIteratorExt;
//!
//! let selected = {
//! let values = vec![String::from("only")];
//! values.iter().exactly_one().unwrap()
//! };
//! println!("{selected}");
//! ```
//!
//! Consuming a collection transfers ownership:
//!
//! ```compile_fail,E0382
//! use libtmux::query::QueryIteratorExt;
//!
//! let values = vec![String::from("only")];
//! let selected = values.into_iter().one_or_none().unwrap();
//! println!("{values:?} {selected:?}");
//! ```
// docs.rs builds with this cfg set, so every gated item there carries the
// feature that unlocks it. Nightly-only, and a no-op everywhere else.
compile_error!;
// The derive names this crate `::libtmux` wherever it expands inside the
// `libtmux` package; this alias makes that path resolve here as well as in the
// package's tests and doctests.
extern crate self as libtmux;
/// Exercise layout validation for fuzzing; not a supported API.
pub use EngineCapabilities;
pub use Client;
pub use ;
pub use ControlModeErrorKind;
pub use ;
pub use __fuzz_format_rows;
pub use TmuxText;
pub use ;
pub use __fuzz_environment_listing;
pub use ;
pub use ;
pub use ;
pub use ;
pub use __fuzz_parse_key_bindings;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// The design notes, compiled.
///
/// `design.md` explains why this crate is shaped as it is, and its Rust blocks
/// had drifted out of the crate they describe: one named an `Error` variant
/// nobody wrote. Compiling them is what keeps a rationale honest about the
/// thing it is rationalising.
///
/// Every block in that file is one, an indented block included: rustdoc reads
/// indentation as a fence and a fence with no language as Rust. A block
/// quoting tmux source or terminal output needs a `text` tag, or the gate
/// reports the crate as one that does not compile.
;
/// Derive a stable typed filter schema for a named struct.
///
/// The generated companion exposes typed field handles through
/// [`query::Filterable::filter_fields`].
///
/// # Examples
///
/// ```
/// use libtmux::query::{Filterable as _, QueryIteratorExt as _};
///
/// #[derive(libtmux::Filterable)]
/// #[filterable(target = "task")]
/// struct Task {
/// name: String,
/// done: bool,
/// }
///
/// let values = vec![
/// Task { name: "build".into(), done: false },
/// Task { name: "test".into(), done: true },
/// ];
/// let fields = Task::filter_fields();
/// let expression = fields.name.contains("ui").and(fields.done.eq(false));
/// let selected = values.iter().matching(&expression).collect::<Vec<_>>();
/// assert_eq!(selected.len(), 1);
/// ```
pub use Filterable;
/// Compiles the workspace README's examples, and nothing else.
///
/// The crate README is the crate documentation, so rustdoc already runs its
/// examples. The one at the repository root is the page most readers see
/// first and had no such check, which is how an example that never compiled
/// sat there. `cfg(doctest)` means this exists only while doctests run, so it
/// costs a normal build nothing and appears in no documentation.
;
;