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
//! Reading the hooks tmux is holding.
//!
//! A tmux hook is an array option, so one hook name holds a numbered set of
//! commands rather than a single command. The numbers are not a formality:
//! removing the middle of a set leaves a gap, and tmux keeps it, so
//! [`IndexedHooks`] is a sparse map rather than a list. A caller that only
//! wants "the command" asks for [`SparseValues::first`].
//!
//! Hooks are not the only array option -- `command-alias` and
//! `terminal-overrides` are the same shape -- so the container is
//! [`SparseValues`] and [`IndexedHooks`] names the hook-shaped one.
use BTreeMap;
use ;
use crateTmuxText;
/// The values one array option holds, by the index tmux stores them under.
///
/// Indices are sparse, and tmux means it. Setting a value without an index
/// writes slot `0`, removing one slot of several leaves the rest where they
/// were, and nothing renumbers, so a `SparseValues` with two entries may hold
/// them at `0` and `30`. Iteration is by ascending index.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// let guard = libtmux::test::TestServer::new().await?;
/// let server = guard.server();
///
/// server.set_hook("after-new-window", "display-message built").await?;
/// let hooks = server.hook("after-new-window").await?.expect("the hook is set");
///
/// assert_eq!(hooks.len(), 1);
/// assert_eq!(
/// hooks.first().map(|value| value.to_string_lossy().into_owned()),
/// Some("display-message built".to_owned()),
/// );
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
/// The commands one hook name holds.
///
/// A hook is an array option whose values are tmux commands, so this is the
/// [`SparseValues`] of that shape rather than a type of its own.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::{IndexedHooks, ReplaceMode, TmuxText};
/// use std::collections::BTreeMap;
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let server = guard.server();
///
/// // The indices are tmux's own and are sparse: removing one does not renumber
/// // the rest, so the map is keyed rather than positional.
/// let mut entries = BTreeMap::new();
/// entries.insert(0, TmuxText::from("display-message first"));
/// entries.insert(4, TmuxText::from("display-message fifth"));
/// server
/// .set_hooks("after-new-window", &IndexedHooks::from(entries), ReplaceMode::Replace)
/// .await?;
///
/// let hooks: IndexedHooks = server.hook("after-new-window").await?.expect("just written");
/// assert_eq!(hooks.len(), 2);
/// assert_eq!(hooks.indices().copied().collect::<Vec<_>>(), vec![0, 4]);
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
pub type IndexedHooks = ;
/// Whether writing a hook keeps the entries it does not name.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::{IndexedHooks, ReplaceMode, TmuxText};
/// use std::collections::BTreeMap;
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let server = guard.server();
///
/// let mut first = BTreeMap::new();
/// first.insert(0, TmuxText::from("display-message one"));
/// first.insert(1, TmuxText::from("display-message two"));
/// server
/// .set_hooks("after-new-window", &IndexedHooks::from(first), ReplaceMode::Replace)
/// .await?;
///
/// // `Merge` writes the indices it names and leaves the rest alone.
/// let mut second = BTreeMap::new();
/// second.insert(1, TmuxText::from("display-message replaced"));
/// server
/// .set_hooks("after-new-window", &IndexedHooks::from(second), ReplaceMode::Merge)
/// .await?;
///
/// let hooks = server.hook("after-new-window").await?.expect("just written");
/// assert_eq!(hooks.len(), 2);
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```