Skip to main content

compio_term\command/
multi.rs

1use std::fmt;
2
3use crate::Command;
4
5/// An ordered batch of terminal commands.
6///
7/// Implemented for borrowed collections that yield shared [`Command`]
8/// references and for heterogeneous tuples of up to 20 commands.
9pub trait Commands {
10    /// Writes the ANSI representation of every command in order.
11    ///
12    /// This method is normally called through [`CommandQueue::queue_many`].
13    ///
14    /// [`CommandQueue::queue_many`]: crate::CommandQueue::queue_many
15    fn write_ansi(&self, writer: &mut impl fmt::Write) -> fmt::Result;
16}
17
18impl<'a, C, I: ?Sized> Commands for &'a I
19where
20    C: Command + 'a,
21    &'a I: IntoIterator<Item = &'a C>,
22{
23    fn write_ansi(&self, writer: &mut impl fmt::Write) -> fmt::Result {
24        for command in *self {
25            command.write_ansi(writer)?;
26        }
27        Ok(())
28    }
29}
30
31macro_rules! impl_commands_for_tuples {
32    (($head_type:ident, $head:ident) $(, ($tail_type:ident, $tail:ident))*) => {
33        impl<$head_type: Command $(, $tail_type: Command)*> Commands
34            for ($head_type, $($tail_type,)*)
35        {
36            fn write_ansi(&self, writer: &mut impl fmt::Write) -> fmt::Result {
37                let ($head, $($tail,)*) = self;
38                $head.write_ansi(writer)?;
39                $($tail.write_ansi(writer)?;)*
40                Ok(())
41            }
42        }
43
44        impl_commands_for_tuples!($(($tail_type, $tail)),*);
45    };
46    () => {
47        impl Commands for () {
48            fn write_ansi(&self, _: &mut impl fmt::Write) -> fmt::Result {
49                Ok(())
50            }
51        }
52    };
53}
54
55impl_commands_for_tuples!(
56    (C0, c0),
57    (C1, c1),
58    (C2, c2),
59    (C3, c3),
60    (C4, c4),
61    (C5, c5),
62    (C6, c6),
63    (C7, c7),
64    (C8, c8),
65    (C9, c9),
66    (C10, c10),
67    (C11, c11),
68    (C12, c12),
69    (C13, c13),
70    (C14, c14),
71    (C15, c15),
72    (C16, c16),
73    (C17, c17),
74    (C18, c18),
75    (C19, c19)
76);