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
use {
    crate::col::*,
    std::{
        str::FromStr,
    },
};

/// Sequence of columns, ordered
#[derive(Debug, Clone, PartialEq)]
pub struct Cols(pub Vec<Col>);

impl Default for Cols {
    fn default() -> Self {
        Self(DEFAULT_COLS.to_vec())
    }
}

impl Cols {
    #[cfg(test)]
    pub fn new<V: Into<Vec<Col>>>(v: V) -> Self {
        Self(v.into())
    }
    pub fn empty() -> Self {
        Self(Vec::new())
    }
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    pub fn contains(&self, tbl: Col) -> bool {
        self.0.contains(&tbl)
    }
    pub fn remove(&mut self, removed: Col) {
        self.0.retain(|&f| f!=removed);
    }
    /// Add a col, preventing duplicates
    /// (may be used when the col is present to reorder)
    pub fn add(&mut self, added: Col) {
        self.remove(added);
        self.0.push(added);
    }
    /// Add the columns of the set, except when they're
    /// already present
    ///
    /// This makes it possible to add a set while keeping
    /// the order of the previous columns, for example
    /// `dysk -c disk+`
    pub fn add_set(&mut self, col_set: &[Col]) {
        if self.0 == ALL_COLS {
            for &col in col_set {
                self.add(col);
            }
        } else {
            for &col in col_set {
                if !self.contains(col) {
                    self.add(col);
                }
            }
        }
    }
    pub fn remove_set(&mut self, col_set: &[Col]) {
        for &col in col_set {
            self.remove(col);
        }
    }
    pub fn cols(&self) -> &[Col] {
        &self.0
    }
}

impl FromStr for Cols {
    type Err = ParseColError;
    fn from_str(value: &str) -> Result<Self, ParseColError> {
        let value = value.trim();
        let mut tokens: Vec<String> = Vec::new();
        let mut must_create = true;
        for c in value.chars() {
            if c.is_alphabetic() || c == '_' {
                if must_create {
                    tokens.push(c.into());
                    must_create = false;
                } else {
                    let len = tokens.len();
                    tokens[len-1].push(c);
                }
            } else {
                tokens.push(c.into());
                must_create = true;
            }
        }
        let mut cols = if let Some(first_token) = tokens.get(0) {
            if first_token == "+" || first_token == "-" {
                // if it starts with an addition or removal, the
                // default set is implied
                Cols::default()
            } else {
                Cols::empty()
            }
        } else {
            return Ok(Self::default());
        };
        let mut negative = false;
        for token in &tokens {
            match token.as_ref() {
                "-" =>  {
                    negative = true;
                }
                "+" | "," | " " => {}
                "all" => {
                    if negative {
                        cols = Cols::empty();
                        negative = false;
                    } else {
                        // if we add all to something, it means the already
                        // present one are meant to be first
                        for &col in ALL_COLS {
                            if !cols.contains(col) {
                                cols.add(col);
                            }
                        }
                    }
                }
                "default" => {
                    if negative {
                        cols.remove_set(DEFAULT_COLS);
                        negative = false;
                    } else {
                        cols.add_set(DEFAULT_COLS);
                    }
                }
                _ => {
                    let col: Col = token.parse()?;
                    if negative {
                        cols.remove(col);
                        negative = false;
                    } else {
                        cols.add(col);
                    }
                }
            }
        }
        match tokens.last().map(|s| s.as_ref()) {
            Some("-") => {
                cols.remove_set(DEFAULT_COLS);
            }
            Some("+") => {
                cols.add_set(DEFAULT_COLS);
            }
            _ => {}
        }
        Ok(cols)
    }
}

#[cfg(test)]
mod cols_parsing {
    use {
        super::*,
        super::Col::*,
    };

    fn check<V: Into<Vec<Col>>>(s: &str, v: V) {
        println!("cols definition: {s:?}");
        let from_str: Cols = s.parse().unwrap();
        let from_vec: Cols = Cols::new(v);
        assert_eq!(from_str, from_vec);
    }

    #[test]
    fn bad_cols(){
        assert_eq!(
            "nothing".parse::<Cols>().unwrap_err().to_string(),
            r#""nothing" can't be parsed as a column; use 'dysk --list-cols' to see all column names"#,
        );
    }

    #[test]
    fn explicit_cols() {
        check(
            "dev",
            vec![Dev],
        );
        check(
            "dev,free,used",
            vec![Dev, Free, Used],
        );
        check(
            "dev+free + used",
            vec![Dev, Free, Used],
        );
        check(
            "  dev   free used ",
            vec![Dev, Free, Used],
        );
        check(
            "all",
            ALL_COLS,
        );
    }

    #[test]
    fn algebraic_cols() {
        check(
            "all - dev -inodes + label",
            vec![Id, Filesystem, Type, Remote, Disk, Used, Use, UsePercent, Free, Size, InodesUsed, InodesUsePercent, InodesFree, InodesCount, MountPoint, Label],
        );
        check(
            "dev + dev +disk - use + size",
            vec![Dev, Disk, Size],
        );
        check(
            "all-default+use",
            vec![Id, Dev, Label, Remote, UsePercent, InodesUsed, InodesUse, InodesUsePercent, InodesFree, InodesCount, Use],
        );
        check(
            "all+default", // special: all but default at the end
            vec![Id, Dev, Label, Remote, UsePercent, InodesUsed, InodesUse, InodesUsePercent, InodesFree, InodesCount, Filesystem, Type, Disk, Used, Use, Free, Size, MountPoint]
        );
        check(
            "fs dev all", // we want all column but fs and dev at the start
            vec![Filesystem, Dev, Id, Label, Type, Remote, Disk, Used, Use, UsePercent, Free, Size, InodesUsed, InodesUse, InodesUsePercent, InodesFree, InodesCount, MountPoint],
        );
        check(
            "fs dev all -id-disk",
            vec![Filesystem, Dev, Label, Type, Remote, Used, Use, UsePercent, Free, Size, InodesUsed, InodesUse, InodesUsePercent, InodesFree, InodesCount, MountPoint],
        );
    }

    #[test]
    fn cols_from_default() {
        check(
            "",
            DEFAULT_COLS,
        );
        check(
            "-dev", // no impact as dev isn't in defaults
            DEFAULT_COLS,
        );
        check(
            "default",
            DEFAULT_COLS,
        );
        check(
            "-default", // not really useful
            vec![],
        );
        check(
            "default-dev", // no impact as dev isn't in defaults
            DEFAULT_COLS,
        );
        check(
            "+dev",
            vec![Filesystem, Type, Disk, Used, Use, Free, Size, MountPoint, Dev]
        );
        check(
            "dev+",
            vec![Dev, Filesystem, Type, Disk, Used, Use, Free, Size, MountPoint]
        );
        check(
            "all-",
            vec![Id, Dev, Label, Remote, UsePercent, InodesUsed, InodesUse, InodesUsePercent, InodesFree, InodesCount],
        );
        check(
            "-size+inodes_free+",
            vec![Filesystem, Type, Disk, Used, Use, Free, MountPoint, InodesFree, Size]
        );
        check(
            "+dev-size+inodes_use",
            vec![Filesystem, Type, Disk, Used, Use, Free, MountPoint, Dev, InodesUse]
        );
        check(
            "-use-type",
            vec![Filesystem, Disk, Used, Free, Size, MountPoint]
        );
        check(
            "default+dev",
            vec![Filesystem, Type, Disk, Used, Use, Free, Size, MountPoint, Dev]
        );
        check(
            "default,size+use", // just reordering
            vec![Filesystem, Type, Disk, Used, Free, MountPoint, Size, Use]
        );
        check(
            "dev default",
            vec![Dev, Filesystem, Type, Disk, Used, Use, Free, Size, MountPoint]
        );
        check(
            "size dev default -disk",
            vec![Size, Dev, Filesystem, Type, Used, Use, Free, MountPoint]
        );
        check(
            "default-fs+inodes",
            vec![Type, Disk, Used, Use, Free, Size, MountPoint, InodesUse]
        );
        check(
            "+inodes_used+inodes_free",
            vec![Filesystem, Type, Disk, Used, Use, Free, Size, MountPoint, InodesUsed, InodesFree]
        );
    }

}