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
use std::ops::RangeInclusive;

/// If and how to wrap lines at the end of the screen.
#[derive(Default, Debug, Copy, Clone)]
pub enum WrapMethod {
    Width,
    Capped(usize),
    Word,
    #[default]
    NoWrap,
}

impl WrapMethod {
    /// Returns `true` if the wrap method is [`NoWrap`].
    ///
    /// [`NoWrap`]: WrapMethod::NoWrap
    #[must_use]
    pub fn is_no_wrap(&self) -> bool {
        matches!(self, Self::NoWrap)
    }

    pub fn wrapping_cap(&self, width: usize) -> usize {
        match self {
            WrapMethod::Capped(cap) => *cap,
            _ => width,
        }
    }
}

/// Where the tabs are placed on screen, can be regular or varied.
#[derive(Debug, Clone, Copy)]
pub struct TabStops(pub usize);

impl TabStops {
    #[inline]
    pub fn spaces_at(&self, x: usize) -> usize {
        self.0 - (x % self.0)
    }
}

impl Default for TabStops {
    fn default() -> Self {
        TabStops(4)
    }
}

/// Wheter to show the new line or not.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum NewLine {
    /// Show the given character on every new line.
    AlwaysAs(char),
    /// Show the given character only when there is whitespace at end
    /// of the line.
    AfterSpaceAs(char),
    /// Don't print anything for a new line character.
    #[default]
    Hidden,
}

impl NewLine {
    #[inline]
    pub fn char(&self, last_char: Option<char>) -> Option<char> {
        match self {
            NewLine::AlwaysAs(char) => Some(*char),
            NewLine::AfterSpaceAs(char) => {
                if last_char.is_some_and(|char| char.is_whitespace() && char != '\n') {
                    Some(*char)
                } else {
                    Some(' ')
                }
            }
            NewLine::Hidden => None,
        }
    }
}

// Pretty much only exists because i wanted one of these with
// usize as its builtin type.
#[derive(Debug, Copy, Clone)]
pub struct ScrollOff {
    pub x: usize,
    pub y: usize,
}

impl Default for ScrollOff {
    fn default() -> Self {
        ScrollOff { y: 3, x: 3 }
    }
}

#[derive(Debug, Clone)]
pub struct WordChars(Vec<std::ops::RangeInclusive<char>>);

impl WordChars {
    pub fn new(ranges: Vec<std::ops::RangeInclusive<char>>) -> Self {
        let word_chars = WordChars(ranges);

        assert!(
            ![' ', '\t', '\n']
                .into_iter()
                .any(|char| word_chars.contains(char)),
            "WordChars cannot contain ' ', '\\n' or '\\t'."
        );

        word_chars
    }

    #[inline]
    pub fn contains(&self, char: char) -> bool {
        self.0.iter().any(|chars| chars.contains(&char))
    }
}

/// Configuration options for printing.
#[derive(Debug, Clone)]
pub struct PrintCfg {
    /// How to wrap the file.
    pub wrap_method: WrapMethod,
    /// Wether to indent wrapped lines or not.
    pub indent_wrap: bool,
    /// Which places are considered a "tab stop".
    pub tab_stops: TabStops,
    /// Wether (and how) to show new lines.
    pub new_line: NewLine,
    /// The horizontal and vertical gaps between the main
    /// cursor and the edges of a [`Label`][crate::ui::Label].
    pub scrolloff: ScrollOff,
    // NOTE: This is relevant for printing with `WrapMethod::Word`.
    /// Characters that are considered to be part of a word.
    pub word_chars: WordChars,
}

impl PrintCfg {
    pub fn new() -> Self {
        Self {
            wrap_method: WrapMethod::default(),
            indent_wrap: true,
            tab_stops: TabStops(4),
            new_line: NewLine::default(),
            scrolloff: ScrollOff::default(),
            word_chars: WordChars::new(vec!['A'..='Z', 'a'..='z', '0'..='9', '_'..='_']),
        }
    }

    pub fn with_no_wrapping(self) -> Self {
        Self {
            wrap_method: WrapMethod::NoWrap,
            ..self
        }
    }

    pub fn width_wrapped(self) -> Self {
        Self {
            wrap_method: WrapMethod::Width,
            ..self
        }
    }

    pub fn word_wrapped(self) -> Self {
        Self {
            wrap_method: WrapMethod::Word,
            ..self
        }
    }

    pub fn wrapped_on_cap(self, cap: usize) -> Self {
        Self {
            wrap_method: WrapMethod::Capped(cap),
            ..self
        }
    }

    pub fn indenting_wrap(self) -> Self {
        Self {
            indent_wrap: true,
            ..self
        }
    }

    pub fn with_tabs_size(self, tab_size: usize) -> Self {
        Self {
            tab_stops: TabStops(tab_size),
            ..self
        }
    }

    pub fn with_new_line_as(self, char: char) -> Self {
        Self {
            new_line: NewLine::AlwaysAs(char),
            ..self
        }
    }

    pub fn with_trailing_new_line_as(self, char: char) -> Self {
        Self {
            new_line: NewLine::AfterSpaceAs(char),
            ..self
        }
    }

    pub fn with_scrolloff(self, x: usize, y: usize) -> Self {
        Self {
            scrolloff: ScrollOff { x, y },
            ..self
        }
    }

    pub fn with_x_scrolloff(self, x_gap: usize) -> Self {
        Self {
            scrolloff: ScrollOff {
                y: self.scrolloff.y,
                x: x_gap,
            },
            ..self
        }
    }

    pub fn with_y_scrolloff(self, y_gap: usize) -> Self {
        Self {
            scrolloff: ScrollOff {
                x: self.scrolloff.x,
                y: y_gap,
            },
            ..self
        }
    }

    pub fn with_word_chars(self, word_chars: impl Iterator<Item = RangeInclusive<char>>) -> Self {
        let word_chars = WordChars::new(word_chars.collect());
        Self { word_chars, ..self }
    }

    /// Same as [`default`], but with a hidden new line.
    ///
    /// [`default`]: PrintCfg::default
    pub(crate) fn default_for_files() -> Self {
        Self {
            wrap_method: WrapMethod::default(),
            indent_wrap: true,
            tab_stops: TabStops(4),
            new_line: NewLine::AlwaysAs(' '),
            scrolloff: ScrollOff::default(),
            word_chars: WordChars::new(vec!['a'..='z', 'A'..='Z', '0'..='9', '_'..='_']),
        }
    }
}

impl Default for PrintCfg {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone, Copy)]
pub struct IterCfg<'a> {
    cfg: &'a PrintCfg,
    iter_lfs: bool,
    force_wrap: Option<WrapMethod>,
    no_indent_wrap: bool,
}

impl<'a> IterCfg<'a> {
    pub fn new(cfg: &'a PrintCfg) -> Self {
        Self {
            cfg,
            iter_lfs: true,
            force_wrap: None,
            no_indent_wrap: false,
        }
    }

    pub fn outsource_lfs(self) -> Self {
        Self {
            iter_lfs: false,
            ..self
        }
    }

    pub fn dont_wrap(self) -> Self {
        Self {
            force_wrap: Some(WrapMethod::NoWrap),
            ..self
        }
    }

    pub fn no_word_wrap(self) -> Self {
        match self.cfg.wrap_method {
            WrapMethod::Word if matches!(self.force_wrap, Some(WrapMethod::NoWrap)) => self,
            WrapMethod::Word => Self {
                force_wrap: Some(WrapMethod::Width),
                ..self
            },
            WrapMethod::Width | WrapMethod::Capped(_) | WrapMethod::NoWrap => self,
        }
    }

    pub fn no_indent_wrap(self) -> Self {
        Self {
            no_indent_wrap: true,
            ..self
        }
    }

    #[inline]
    pub fn show_lf(&self) -> bool {
        self.iter_lfs
    }

    #[inline]
    pub fn wrap_method(&self) -> WrapMethod {
        self.force_wrap.unwrap_or(self.cfg.wrap_method)
    }

    #[inline]
    pub fn indent_wrap(&self) -> bool {
        !self.no_indent_wrap && self.cfg.indent_wrap
    }

    #[inline]
    pub fn tab_stops(&self) -> TabStops {
        self.cfg.tab_stops
    }

    #[inline]
    pub fn new_line(&self) -> NewLine {
        if self.iter_lfs {
            NewLine::Hidden
        } else {
            self.cfg.new_line
        }
    }

    #[inline]
    pub fn scrolloff(&self) -> ScrollOff {
        self.cfg.scrolloff
    }

    #[inline]
    pub fn word_chars(&self) -> &WordChars {
        &self.cfg.word_chars
    }
}