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
use crate::row::Row;
use std::collections::HashMap;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

/// Whitespace character array
const WHITESPACE: [char; 2] = [' ', '\t'];

/// String helper macro
#[macro_export]
macro_rules! st {
    ($value:expr) => {
        $value.to_string()
    };
}

/// Lazy regex creation
#[macro_export]
macro_rules! regex {
    ($re:literal $(,)?) => {{
        static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
        RE.get_or_init(|| regex::Regex::new($re).unwrap())
    }};
}

/// A struct that holds positions
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Loc {
    pub x: usize,
    pub y: usize,
}

impl From<(usize, usize)> for Loc {
    fn from(loc: (usize, usize)) -> Loc {
        let (x, y) = loc;
        Loc { x, y }
    }
}

/// A struct that holds size
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Size {
    pub w: usize,
    pub h: usize,
}

impl From<(usize, usize)> for Size {
    fn from(size: (usize, usize)) -> Size {
        let (w, h) = size;
        Size { w, h }
    }
}

pub trait BoundedRange {
    fn first(&self) -> usize;
    fn last(&self) -> usize;
}

impl BoundedRange for std::ops::Range<usize> {
    fn first(&self) -> usize {
        self.start
    }

    fn last(&self) -> usize {
        self.end
    }
}

impl BoundedRange for std::ops::RangeInclusive<usize> {
    fn first(&self) -> usize {
        *self.start()
    }

    fn last(&self) -> usize {
        *self.end() + 1
    }
}

/// Generate a look up table between the raw and display indices
#[must_use]
pub fn raw_indices(s: &str, i: &[usize], tab_width: usize) -> HashMap<usize, usize> {
    let mut raw = 0;
    let mut indices = HashMap::new();
    indices.insert(0, 0);
    for (c, ch) in s.chars().enumerate() {
        if ch == '\t' {
            for i in 1..=tab_width {
                indices.insert(c + i, raw + i);
            }
            raw += 4;
        } else {
            raw += ch.len_utf8();
            indices.insert(i[c + 1], raw);
        }
    }
    indices
}

/// Retrieve the indices of word boundaries
#[must_use]
pub fn words(row: &Row) -> Vec<usize> {
    // Gather information and set up algorithm
    let mut result = vec![];
    let mut chr = 0;
    let mut pad = true;
    // While still inside the row
    while chr < row.text.len() {
        let c = row.text[chr];
        match c {
            // Move forward through all the spaces
            ' ' => (),
            '\t' => {
                // If we haven't encountered text yet
                if pad {
                    // Make this a word boundary
                    result.push(chr);
                }
            }
            _ => {
                // Set the marker to false, as we're encountering text
                pad = false;
                // Set this as a word boundary
                result.push(chr);
                // Skip through text, end when we find whitespace or the end of the row
                while chr < row.text.len() && !WHITESPACE.contains(&row.text[chr]) {
                    chr += 1;
                }
                // Deal with next lot of whitespace or exit if at the end of the row
                continue;
            }
        }
        // Advance and continue
        chr += 1;
    }
    // Add on the last point on the row as a word boundary
    result.push(row.len());
    result
}

/// Determine the display width of a string
#[must_use]
pub fn width(s: &str, tab: usize) -> usize {
    let s = s.replace('\t', &" ".repeat(tab));
    s.width()
}

/// Determine the display width of a character
#[must_use]
pub fn width_char(c: char, tab: usize) -> usize {
    if c == '\t' {
        tab
    } else {
        c.width().unwrap_or(0)
    }
}

/// This will take text, and align it to the middle of the screen
#[must_use]
pub fn align_middle(s: &str, space: usize, tab_width: usize) -> Option<String> {
    let len = width(s, tab_width) / 2;
    let half = space / 2;
    let pad = " ".repeat(half.saturating_sub(len));
    if len * 2 + pad.len() > space {
        None
    } else {
        Some(format!("{}{}{}", pad, s, pad))
    }
}

/// This will take text, and align it to the left and right hand sides
#[must_use]
pub fn align_sides(lhs: &str, rhs: &str, space: usize, tab_width: usize) -> Option<String> {
    let total = width(lhs, tab_width) + width(rhs, tab_width);
    if total > space {
        None
    } else {
        Some(format!(
            "{}{}{}",
            lhs,
            " ".repeat(space.saturating_sub(total)),
            rhs
        ))
    }
}

/// Determine the filetype from the extension
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn filetype(extension: &str) -> Option<String> {
    Some(st!(match extension.to_ascii_lowercase().as_str() {
        "abap" => "ABAP",
        "ada" => "Ada",
        "ahk" | "ahkl" => "AutoHotkey",
        "applescript" | "scpt" => "AppleScript",
        "arc" => "Arc",
        "asp" | "asax" | "ascx" | "ashx" | "asmx" | "aspx" | "axd" => "ASP",
        "as" => "ActionScript",
        "asc" | "ash" => "AGS Script",
        "asm" | "nasm" => "Assembly",
        "awk" | "auk" | "gawk" | "mawk" | "nawk" => "Awk",
        "bat" | "cmd" => "Batch",
        "b" | "bf" => "Brainfuck",
        "c" => "C",
        "cmake" => "CMake",
        "cbl" | "cobol" | "cob" => "Cobol",
        "class" | "java" => "Java",
        "clj" | "cl2" | "cljs" | "cljx" | "cljc" => "Clojure",
        "coffee" => "CoffeeScript",
        "cr" => "Crystal",
        "cu" | "cuh" => "Cuda",
        "cpp" | "cxx" => "C++",
        "cs" | "cshtml" | "csx" => "C#",
        "css" => "CSS",
        "csv" => "CSV",
        "d" | "di" => "D",
        "dart" => "Dart",
        "diff" | "patch" => "Diff",
        "dockerfile" => "Dockerfile",
        "ex" | "exs" => "Elixr",
        "elm" => "Elm",
        "el" => "Emacs Lisp",
        "erb" => "ERB",
        "erl" | "es" => "Erlang",
        "fs" | "fsi" | "fsx" => "F#",
        "f" | "f90" | "fpp" | "for" => "FORTRAN",
        "fish" => "Fish",
        "fth" => "Forth",
        "g4" => "ANTLR",
        "gd" => "GDScript",
        "glsl" | "vert" | "shader" | "geo" | "fshader" | "vrx" | "vsh" | "vshader" | "frag" =>
            "GLSL",
        "gnu" | "gp" | "plot" => "Gnuplot",
        "go" => "Go",
        "groovy" | "gvy" => "Groovy",
        "hlsl" => "HLSL",
        "h" => "C Header",
        "haml" => "Haml",
        "handlebars" | "hbs" => "Handlebars",
        "hs" => "Haskell",
        "hpp" => "C++ Header",
        "html" | "htm" | "xhtml" => "HTML",
        "ini" | "cfg" => "INI",
        "ino" => "Arduino",
        "ijs" => "J",
        "json" => "JSON",
        "jsx" => "JSX",
        "js" => "JavaScript",
        "jl" => "Julia",
        "kt" | "ktm" | "kts" => "Kotlin",
        "ll" => "LLVM",
        "l" | "lex" => "Lex",
        "lua" => "Lua",
        "ls" => "LiveScript",
        "lol" => "LOLCODE",
        "lisp" | "asd" | "lsp" => "Common Lisp",
        "log" => "Log file",
        "m4" => "M4",
        "man" | "roff" => "Groff",
        "matlab" => "Matlab",
        "m" => "Objective-C",
        "ml" => "OCaml",
        "mk" | "mak" => "Makefile",
        "md" | "markdown" => "Markdown",
        "nix" => "Nix",
        "numpy" => "NumPy",
        "opencl" | "cl" => "OpenCL",
        "php" => "PHP",
        "pas" => "Pascal",
        "pl" => "Perl",
        "psl" => "PowerShell",
        "pro" => "Prolog",
        "py" | "pyw" => "Python",
        "pyx" | "pxd" | "pxi" => "Cython",
        "r" => "R",
        "rst" => "reStructuredText",
        "rkt" => "Racket",
        "rb" | "ruby" => "Ruby",
        "rs" => "Rust",
        "sh" => "Shell",
        "scss" => "SCSS",
        "sql" => "SQL",
        "sass" => "Sass",
        "scala" => "Scala",
        "scm" => "Scheme",
        "st" => "Smalltalk",
        "swift" => "Swift",
        "toml" => "TOML",
        "tcl" => "Tcl",
        "tex" => "TeX",
        "ts" | "tsx" => "TypeScript",
        "txt" => "Plain Text",
        "vala" => "Vala",
        "vb" | "vbs" => "Visual Basic",
        "vue" => "Vue",
        "xm" | "x" | "xi" => "Logos",
        "xml" => "XML",
        "y" | "yacc" => "Yacc",
        "yaml" | "yml" => "Yaml",
        "yxx" => "Bison",
        "zsh" => "Zsh",
        _ => return None,
    }))
}