OXYGENHUB
# acadb5ed28a1 oxygen / crates / oxy-core / src / repo / prompt.rs
22.9 KiB · Rust · last changed Checkout through the state index; two indexes kept; racy files hashed in parallel (refs #41) 1 month ago source blame history raw
1
//! Fast, side-effect-free status for shell prompts.
2
//!
3
//! Every snapshot and checkout writes a manifest of the working copy as it
4
//! was last seen: `size`, `mtime`, and BLAKE3 of each file. `quick_status`
5
//! walks the working copy and compares against that manifest, reading a
6
//! file's content only when its size or mtime changed, and records no
7
//! operation. Ahead/behind counts come from walking the store between the
8
//! local bookmark and its `origin` tracking state.
9
10
use super::*;
11
use std::time::UNIX_EPOCH;
12
13
#[derive(Debug, Clone, Default, PartialEq, Eq)]
14
pub struct PromptInfo {
15
    pub bookmark: String,
16
    /// Working-copy ChangeId prefix.
17
    pub change_id: String,
18
    pub heads: usize,
19
    pub dirty: bool,
20
    pub conflicted: bool,
21
    /// Changes local `main` has that `origin/main` lacks, when tracking exists.
22
    pub ahead: Option<usize>,
23
    pub behind: Option<usize>,
24
}
25
26
impl PromptInfo {
27
    /// The glyph for the working copy's state: `‼` conflicted, `✎` dirty,
28
    /// `✔` clean.
29
    pub fn state_glyph(&self) -> &'static str {
30
        if self.conflicted {
31
            "‼"
32
        } else if self.dirty {
33
            "✎"
34
        } else {
35
            "✔"
36
        }
37
    }
38
39
    /// The ANSI colour (SGR parameter) for the state: red, yellow, or cyan.
40
    pub fn state_color(&self) -> u8 {
41
        if self.conflicted {
42
            31
43
        } else if self.dirty {
44
            33
45
        } else {
46
            36
47
        }
48
    }
49
50
    /// `format` wrapped in the state colour. `guard` is `("\\[", "\\]")` for
51
    /// bash prompts, `("%{", "%}")` for zsh, or empty for raw output.
52
    pub fn format_colored(&self, fmt: &str, guard: (&str, &str)) -> String {
53
        format!(
54
            "{}\x1b[{}m{}{}{}\x1b[0m{}",
55
            guard.0,
56
            self.state_color(),
57
            guard.1,
58
            self.format(fmt),
59
            guard.0,
60
            guard.1
61
        )
62
    }
63
64
    /// Expand `%b` bookmark, `%c` change id, `%h` heads, `%d` dirty marker
65
    /// (`*`), `%x` conflict marker (`!`), `%a`/`%B` ahead/behind counts
66
    /// (empty when unknown), `%A`/`%D` `+n`/`-n` when non-zero, `%s` the
67
    /// state glyph, `%U`/`%O` `↑n`/`↓n` when non-zero, `%i` the repository
68
    /// icon, `%%` percent.
69
    pub fn format(&self, fmt: &str) -> String {
70
        let mut out = String::new();
71
        let mut chars = fmt.chars();
72
        while let Some(c) = chars.next() {
73
            if c != '%' {
74
                out.push(c);
75
                continue;
76
            }
77
            match chars.next() {
78
                Some('b') => out.push_str(&self.bookmark),
79
                Some('c') => out.push_str(&self.change_id),
80
                Some('h') => out.push_str(&self.heads.to_string()),
81
                Some('d') => {
82
                    if self.dirty {
83
                        out.push('*');
84
                    }
85
                }
86
                Some('x') => {
87
                    if self.conflicted {
88
                        out.push('!');
89
                    }
90
                }
91
                Some('a') => out.push_str(&self.ahead.map(|n| n.to_string()).unwrap_or_default()),
92
                Some('B') => out.push_str(&self.behind.map(|n| n.to_string()).unwrap_or_default()),
93
                Some('A') => {
94
                    if let Some(n) = self.ahead.filter(|n| *n > 0) {
95
                        out.push_str(&format!("+{n}"));
96
                    }
97
                }
98
                Some('D') => {
99
                    if let Some(n) = self.behind.filter(|n| *n > 0) {
100
                        out.push_str(&format!("-{n}"));
101
                    }
102
                }
103
                Some('s') => out.push_str(self.state_glyph()),
104
                Some('U') => {
105
                    if let Some(n) = self.ahead.filter(|n| *n > 0) {
106
                        out.push_str(&format!("↑{n}"));
107
                    }
108
                }
109
                Some('O') => {
110
                    if let Some(n) = self.behind.filter(|n| *n > 0) {
111
                        out.push_str(&format!("↓{n}"));
112
                    }
113
                }
114
                Some('i') => out.push_str(ICON),
115
                Some('%') => out.push('%'),
116
                Some(other) => {
117
                    out.push('%');
118
                    out.push(other);
119
                }
120
                None => out.push('%'),
121
            }
122
        }
123
        out
124
    }
125
}
126
127
/// Path → (size, mtime in nanoseconds, BLAKE3 hex) as last seen.
128
/// One manifest entry: the file's stat, its content hash, and the stored
129
/// blob for an opaque file.
130
#[derive(Clone, Debug)]
131
pub(crate) struct ManifestEntry {
132
    pub size: u64,
133
    pub mtime_ns: u128,
134
    pub hash: [u8; 32],
135
    pub blob: Option<ObjectRef>,
136
    /// The graph key (`State::graph_key`) of the base file this text file
137
    /// was found equal to, rendered, byte for byte.
138
    pub graph: Option<[u8; 32]>,
139
    /// Whether the file was executable: a mode change alters neither size
140
    /// nor time.
141
    pub exec: bool,
142
}
143
144
#[derive(Clone, Debug, Default)]
145
pub(crate) struct Manifest {
146
    /// When the manifest was written, for the racy-stat guard.
147
    pub written_ns: u128,
148
    pub entries: BTreeMap<String, ManifestEntry>,
149
}
150
151
/// A parsed manifest and the identity (size, time, inode) of the file it
152
/// came from: reused while the file is the same one.
153
pub(crate) type CachedManifest = ((u64, u128, u64), (bool, bool, std::sync::Arc<Manifest>));
154
155
pub(crate) fn mtime_ns(m: &fs::Metadata) -> u128 {
156
    m.modified()
157
        .ok()
158
        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
159
        .map(|d| d.as_nanos())
160
        .unwrap_or(0)
161
}
162
163
pub const DEFAULT_PROMPT_FORMAT: &str = "%b %c%d%x%A%D";
164
/// The repository icon for `%i` (an atom, for oxygen).
165
pub const ICON: &str = "⚛";
166
/// A format with glyphs: icon, bookmark, change id, state, ahead/behind.
167
pub const GLYPH_PROMPT_FORMAT: &str = "%i %b %c %s%U%O";
168
169
/// The manifest file (oxygen#41): a flat binary layout read in one pass.
170
/// A magic number; the conflicted and uncommitted flags; when it was
171
/// written, at a fixed offset so an otherwise equal manifest can be
172
/// recognised; BLAKE3 of everything else; then each entry in path order:
173
/// path, size, mtime in nanoseconds, BLAKE3 of the content, the stored
174
/// blob of an opaque file, the graph key it was found equal to, and
175
/// whether it is executable. A file that does not check, or in any other
176
/// layout (an older manifest), reads as no manifest, and the next snapshot
177
/// writes it again: it is a cache, written without waiting for the disk.
178
const MANIFEST_MAGIC: &[u8; 8] = b"oxyman2\0";
179
/// Where the written time sits: after the magic and the two flags.
180
const MANIFEST_WRITTEN: std::ops::Range<usize> = 10..26;
181
/// Where the checksum sits, after the written time.
182
const MANIFEST_SUM: std::ops::Range<usize> = 26..58;
183
184
fn manifest_sum(buf: &[u8]) -> [u8; 32] {
185
    let mut h = blake3::Hasher::new();
186
    h.update(&buf[..MANIFEST_SUM.start]);
187
    h.update_rayon(&buf[MANIFEST_SUM.end..]);
188
    *h.finalize().as_bytes()
189
}
190
191
fn encode_manifest(
192
    conflicted: bool,
193
    uncommitted: bool,
194
    written_ns: u128,
195
    entries: &mut dyn Iterator<Item = (&str, ManifestEntry)>,
196
) -> Vec<u8> {
197
    let mut w = crate::flat::Writer::default();
198
    w.fixed(MANIFEST_MAGIC);
199
    w.u8(conflicted as u8);
200
    w.u8(uncommitted as u8);
201
    w.fixed(&written_ns.to_le_bytes());
202
    w.fixed(&[0; 32]);
203
    for (path, e) in entries {
204
        w.bytes(path.as_bytes());
205
        w.u64(e.size);
206
        w.fixed(&e.mtime_ns.to_le_bytes());
207
        w.fixed(&e.hash);
208
        w.opt_object(&e.blob);
209
        match e.graph {
210
            None => w.u8(0),
211
            Some(k) => {
212
                w.u8(1);
213
                w.fixed(&k);
214
            }
215
        }
216
        w.u8(e.exec as u8);
217
    }
218
    let sum = manifest_sum(&w.0);
219
    w.0[MANIFEST_SUM].copy_from_slice(&sum);
220
    w.0
221
}
222
223
fn decode_manifest(buf: &[u8]) -> Option<(bool, bool, Manifest)> {
224
    let mut r = crate::flat::Reader::new(buf);
225
    if &r.fixed::<8>().ok()? != MANIFEST_MAGIC {
226
        return None;
227
    }
228
    let conflicted = r.u8().ok()? == 1;
229
    let uncommitted = r.u8().ok()? == 1;
230
    let written_ns = u128::from_le_bytes(r.fixed().ok()?);
231
    if r.fixed::<32>().ok()? != manifest_sum(buf) {
232
        return None;
233
    }
234
    let mut entries = Vec::new();
235
    while !r.at_end() {
236
        let path = std::str::from_utf8(r.bytes().ok()?).ok()?.to_string();
237
        let e = ManifestEntry {
238
            size: r.u64().ok()?,
239
            mtime_ns: u128::from_le_bytes(r.fixed().ok()?),
240
            hash: r.fixed().ok()?,
241
            blob: r.opt_object().ok()?,
242
            graph: match r.u8().ok()? {
243
                0 => None,
244
                _ => Some(r.fixed().ok()?),
245
            },
246
            exec: r.u8().ok()? == 1,
247
        };
248
        entries.push((path, e));
249
    }
250
    // Written in path order, so this builds the map without searching.
251
    let entries = entries.into_iter().collect();
252
    Some((
253
        conflicted,
254
        uncommitted,
255
        Manifest {
256
            written_ns,
257
            entries,
258
        },
259
    ))
260
}
261
262
impl Repo {
263
    pub(crate) fn manifest_path(&self) -> PathBuf {
264
        self.oxy
265
            .join("workspaces")
266
            .join(self.workspace.to_hex())
267
            .join("manifest")
268
    }
269
270
    /// Record the working copy as it is now (after a snapshot, commit, or
271
    /// checkout), with header lines saying whether the base state is
272
    /// conflicted and whether the working-copy change holds uncommitted work.
273
    pub(crate) fn write_manifest(&self, conflicted: bool, uncommitted: bool) -> Result<()> {
274
        let entries = self.walk_working_copy()?;
275
        self.write_manifest_entries(conflicted, uncommitted, &entries, &HashMap::new())
276
    }
277
278
    /// Write the manifest from entries already walked; `blobs` names the
279
    /// stored blob of each opaque path so the next walk can skip reading it.
280
    pub(crate) fn write_manifest_entries(
281
        &self,
282
        conflicted: bool,
283
        uncommitted: bool,
284
        entries: &BTreeMap<String, FsEntry>,
285
        blobs: &HashMap<String, ObjectRef>,
286
    ) -> Result<()> {
287
        self.manifest_cache.take();
288
        self.listing_cache.take();
289
        let written = std::time::SystemTime::now()
290
            .duration_since(UNIX_EPOCH)
291
            .map(|d| d.as_nanos())
292
            .unwrap_or(0);
293
        let mut newest_ns: u128 = 0;
294
        let mut rows = entries.iter().map(|(path, e)| {
295
            let (size, mtime_ns) = match e.stat {
296
                Some(known) => known,
297
                None => match fs::symlink_metadata(self.root.join(path)) {
298
                    Ok(m) => (m.len(), mtime_ns(&m)),
299
                    Err(_) => (e.content.len() as u64, 0),
300
                },
301
            };
302
            newest_ns = newest_ns.max(mtime_ns);
303
            let entry = ManifestEntry {
304
                size,
305
                mtime_ns,
306
                hash: e
307
                    .content_hash
308
                    .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes()),
309
                blob: blobs.get(path).copied().or(e.blob),
310
                graph: e.graph_key,
311
                exec: e.exec,
312
            };
313
            (path.as_str(), entry)
314
        });
315
        let new = encode_manifest(conflicted, uncommitted, written, &mut rows);
316
        drop(rows);
317
        let p = self.manifest_path();
318
        // Leave an unchanged manifest alone when its stamp already trusts
319
        // every entry (none is within the racy window): rewriting it would
320
        // change nothing but the file, which editors watching the tree
321
        // (and subrepos inside it) take as a change.
322
        if let Ok(old) = fs::read(&p) {
323
            let (w, sum) = (MANIFEST_WRITTEN, MANIFEST_SUM);
324
            if old.len() == new.len()
325
                && old[..w.start] == new[..w.start]
326
                && old[sum.end..] == new[sum.end..]
327
            {
328
                let old_written = u128::from_le_bytes(old[w.clone()].try_into().unwrap());
329
                if newest_ns + 2_000_000_000 < old_written {
330
                    return Ok(());
331
                }
332
            }
333
        }
334
        fs::create_dir_all(p.parent().unwrap())?;
335
        write_cache(&p, &new)
336
    }
337
338
    /// Change the header flags, and set the graph key of each path in
339
    /// `keys` (after a commit, the new base's keys of the files it holds as
340
    /// they are on disk).
341
    pub(crate) fn rewrite_manifest(
342
        &self,
343
        conflicted: bool,
344
        uncommitted: bool,
345
        keys: &HashMap<String, [u8; 32]>,
346
    ) -> Result<()> {
347
        self.manifest_cache.take();
348
        self.listing_cache.take();
349
        let p = self.manifest_path();
350
        let Some((_, _, m)) = fs::read(&p).ok().and_then(|b| decode_manifest(&b)) else {
351
            return self.write_manifest(conflicted, uncommitted);
352
        };
353
        let mut rows = m.entries.iter().map(|(path, e)| {
354
            let mut e = e.clone();
355
            if let Some(k) = keys.get(path) {
356
                e.graph = Some(*k);
357
            }
358
            (path.as_str(), e)
359
        });
360
        write_cache(
361
            &p,
362
            &encode_manifest(conflicted, uncommitted, m.written_ns, &mut rows),
363
        )
364
    }
365
366
    /// Forget the manifest after an operation that changed the working-copy
367
    /// change without a snapshot (undo, rewrites); the prompt shows blank
368
    /// markers until the next snapshot.
369
    pub(crate) fn drop_manifest(&self) {
370
        self.manifest_cache.take();
371
        self.listing_cache.take();
372
        let _ = fs::remove_file(self.manifest_path());
373
    }
374
375
    /// The manifest, with whether it records a conflict and uncommitted
376
    /// work; parsed once while the file stays the same one.
377
    pub(crate) fn read_manifest(&self) -> Result<Option<(bool, bool, std::sync::Arc<Manifest>)>> {
378
        let path = self.manifest_path();
379
        let id = match fs::metadata(&path) {
380
            Ok(m) => {
381
                #[cfg(unix)]
382
                let ino = std::os::unix::fs::MetadataExt::ino(&m);
383
                #[cfg(not(unix))]
384
                let ino = 0;
385
                (m.len(), mtime_ns(&m), ino)
386
            }
387
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
388
            Err(e) => return Err(e.into()),
389
        };
390
        if let Some((cached_id, m)) = &*self.manifest_cache.borrow() {
391
            if *cached_id == id {
392
                return Ok(Some(m.clone()));
393
            }
394
        }
395
        let Some((c, u, m)) = self.parse_manifest(&path)? else {
396
            return Ok(None);
397
        };
398
        let m = (c, u, std::sync::Arc::new(m));
399
        *self.manifest_cache.borrow_mut() = Some((id, m.clone()));
400
        Ok(Some(m))
401
    }
402
403
    fn parse_manifest(&self, path: &std::path::Path) -> Result<Option<(bool, bool, Manifest)>> {
404
        let buf = match fs::read(path) {
405
            Ok(b) => b,
406
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
407
            Err(e) => return Err(e.into()),
408
        };
409
        Ok(decode_manifest(&buf))
410
    }
411
412
    /// Whether the working copy is certainly clean, known from the manifest
413
    /// alone, without loading any history (spec §12.4): the last snapshot
414
    /// or commit found no uncommitted work and no conflict, there are no
415
    /// subrepos (whose state the manifest does not hold), and no file was
416
    /// added, removed or changed since. An entry inside the racy window,
417
    /// where an equal size and time do not prove equal content, is checked
418
    /// by hashing the file. `false` means only that a snapshot must decide.
419
    pub fn clean_by_manifest(&self) -> Result<bool> {
420
        let _t = crate::timing::phase("status: check the manifest");
421
        // Why a snapshot must decide, with timings on.
422
        let not = |why: &str| {
423
            if crate::timing::enabled() {
424
                eprintln!("timing status: not clean by the manifest: {why}");
425
            }
426
            Ok(false)
427
        };
428
        let Some((conflicted, uncommitted, m)) = self.read_manifest()? else {
429
            return not("no manifest");
430
        };
431
        if conflicted || uncommitted || self.root.join(".oxymodules").exists() {
432
            return not("conflicted, uncommitted or subrepos");
433
        }
434
        // Entries in the racy window are hashed, in parallel (a checkout
435
        // just wrote them all).
436
        use rayon::prelude::*;
437
        let racy: Vec<(&String, &ManifestEntry)> = m
438
            .entries
439
            .iter()
440
            .filter(|(_, e)| e.mtime_ns + 2_000_000_000 >= m.written_ns)
441
            .collect();
442
        let root = &self.root;
443
        let changed = racy.par_iter().find_any(|(rel, e)| {
444
            let path = root.join(rel.as_str());
445
            let same = match fs::symlink_metadata(&path) {
446
                Ok(md) if md.file_type().is_symlink() => fs::read_link(&path).is_ok_and(|t| {
447
                    *blake3::hash(t.to_string_lossy().as_bytes()).as_bytes() == e.hash
448
                }),
449
                Ok(md) if md.is_file() => {
450
                    md.len() == e.size
451
                        && fs::read(&path).is_ok_and(|b| *blake3::hash(&b).as_bytes() == e.hash)
452
                }
453
                _ => false,
454
            };
455
            !same
456
        });
457
        if let Some((rel, _)) = changed {
458
            return not(&format!("{rel} changed within the racy window"));
459
        }
460
        if self.quick_dirty_keeping(true)? != Some(false) {
461
            return not("a file differs");
462
        }
463
        Ok(true)
464
    }
465
466
    /// Whether the working copy differs from the last manifest. Without a
467
    /// manifest the answer is `None`.
468
    pub fn quick_dirty(&self) -> Result<Option<bool>> {
469
        self.quick_dirty_keeping(false)
470
    }
471
472
    /// `quick_dirty`; with `keep`, the listing it made is left for the
473
    /// next walk of the working copy.
474
    fn quick_dirty_keeping(&self, keep: bool) -> Result<Option<bool>> {
475
        let Some((_, uncommitted, manifest)) = self.read_manifest()? else {
476
            return Ok(None);
477
        };
478
        if uncommitted {
479
            return Ok(Some(true));
480
        }
481
        use rayon::prelude::*;
482
        let sparse = &self.config.sparse_paths;
483
        let in_scope = |p: &str| {
484
            sparse.is_empty()
485
                || sparse
486
                    .iter()
487
                    .any(|s| p == s || p.starts_with(&format!("{s}/")))
488
        };
489
        // A working copy that cannot be listed counts as changed.
490
        let Ok(listing) = super::walk::list(&self.root, &in_scope) else {
491
            return Ok(Some(true));
492
        };
493
        let differs = || -> Result<bool> {
494
            let links_differ = listing.symlinks.iter().any(|(rel, target)| {
495
                manifest
496
                    .entries
497
                    .get(rel)
498
                    .is_none_or(|me| *blake3::hash(target).as_bytes() != me.hash)
499
            });
500
            if links_differ {
501
                return Ok(true);
502
            }
503
            // Each file is checked on its own, in parallel, stopping at the
504
            // first difference; an unreadable one counts as different.
505
            let files_differ = listing.files.par_iter().any(|(rel, path, m)| {
506
                manifest.entries.get(rel).is_none_or(|me| {
507
                    m.len() != me.size
508
                        || super::is_executable(m) != me.exec
509
                        || (mtime_ns(m) != me.mtime_ns
510
                            && !fs::read(path)
511
                                .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash))
512
                })
513
            });
514
            if files_differ {
515
                return Ok(true);
516
            }
517
            let seen = listing.files.len() + listing.symlinks.len();
518
            if seen == manifest.entries.len() {
519
                return Ok(false);
520
            }
521
            // Manifest entries the walk skipped: tracked files that ignore rules
522
            // hide. They count as present when still on disk and unchanged.
523
            for (rel, me) in &manifest.entries {
524
                let path = self.root.join(rel);
525
                let Ok(m) = fs::symlink_metadata(&path) else {
526
                    return Ok(true);
527
                };
528
                if m.file_type().is_symlink() {
529
                    continue;
530
                }
531
                if m.len() != me.size
532
                    || super::is_executable(&m) != me.exec
533
                    || (mtime_ns(&m) != me.mtime_ns
534
                        && *blake3::hash(&fs::read(&path)?).as_bytes() != me.hash)
535
                {
536
                    return Ok(true);
537
                }
538
            }
539
            Ok(false)
540
        };
541
        let dirty = differs()?;
542
        if dirty && keep {
543
            *self.listing_cache.borrow_mut() = Some(listing);
544
        }
545
        Ok(Some(dirty))
546
    }
547
548
    /// Prompt information without recording anything.
549
    pub fn quick_status(&self) -> Result<PromptInfo> {
550
        let view = self.view()?;
551
        let bookmark = self.config.bookmark.clone();
552
        let heads = self.heads_of(&view, &bookmark)?;
553
        let wc_hash = view.workspaces.get(&self.workspace).copied();
554
        let change_id = match wc_hash {
555
            Some(h) => self.store.get_change_meta(&h)?.change_id.short(8),
556
            None => String::new(),
557
        };
558
        // Conflictedness was recorded by the last snapshot; without a
559
        // manifest (never snapshotted) both flags are unknown and shown clear.
560
        let manifest = self.read_manifest()?;
561
        let conflicted = manifest.as_ref().map(|(c, _, _)| *c).unwrap_or(false);
562
        let dirty = self.quick_dirty()?.unwrap_or(false);
563
        let (ahead, behind) = match view.remotes.get("origin").and_then(|b| b.get(&bookmark)) {
564
            Some(st) => {
565
                let remote_heads = self.store.get_state(st)?.heads;
566
                let ahead = self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok();
567
                let behind = self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok();
568
                (ahead, behind)
569
            }
570
            None => (None, None),
571
        };
572
        Ok(PromptInfo {
573
            bookmark,
574
            change_id,
575
            heads: heads.len(),
576
            dirty,
577
            conflicted,
578
            ahead,
579
            behind,
580
        })
581
    }
582
}
583
584
#[cfg(test)]
585
mod tests {
586
    use super::*;
587
588
    #[test]
589
    fn placeholders_and_glyphs() {
590
        let clean = PromptInfo {
591
            bookmark: "main".into(),
592
            change_id: "658990aa".into(),
593
            heads: 1,
594
            dirty: false,
595
            conflicted: false,
596
            ahead: Some(0),
597
            behind: None,
598
        };
599
        assert_eq!(clean.format(DEFAULT_PROMPT_FORMAT), "main 658990aa");
600
        assert_eq!(clean.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✔");
601
        let busy = PromptInfo {
602
            dirty: true,
603
            ahead: Some(2),
604
            behind: Some(3),
605
            ..clean.clone()
606
        };
607
        assert_eq!(busy.format(DEFAULT_PROMPT_FORMAT), "main 658990aa*+2-3");
608
        assert_eq!(busy.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✎↑2↓3");
609
        let bad = PromptInfo {
610
            conflicted: true,
611
            ..busy.clone()
612
        };
613
        assert_eq!(bad.format("%s%x%d %h %a/%B %%"), "‼!* 1 2/3 %");
614
        assert_eq!(bad.state_color(), 31);
615
        assert_eq!(
616
            busy.format_colored("%b", ("\\[", "\\]")),
617
            "\\[\x1b[33m\\]main\\[\x1b[0m\\]"
618
        );
619
        assert_eq!(clean.format_colored("%b", ("", "")), "\x1b[36mmain\x1b[0m");
620
        assert_eq!(clean.format("%q"), "%q");
621
    }
622
}