util.ha (946B)
1 // SPDX-License-Identifier: GPL-3.0-only 2 // (c) Hare authors <https://harelang.org> 3 4 use fs; 5 use hare::module; 6 use memio; 7 use os; 8 use path; 9 use sort; 10 use sort::cmp; 11 use strings; 12 13 let firstline: bool = true; 14 15 fn trim_comment(s: str) str = { 16 let trimmed = memio::dynamic(); 17 let tok = strings::tokenize(s, "\n"); 18 for (let line => strings::next_token(&tok)) { 19 memio::concat(&trimmed, strings::trimprefix(line, " "), "\n")!; 20 }; 21 return strings::dup(memio::string(&trimmed)!); 22 }; 23 24 export fn submodules(path: str, show_undocumented: bool) ([]str | error) = { 25 let submodules: []str = []; 26 let it = os::iter(path)?; 27 defer fs::finish(it); 28 let pathbuf = path::init(path)!; 29 for (let d => module::next(it)?) { 30 path::set(&pathbuf, path, d.name, "README")!; 31 if (show_undocumented || os::exists(path::string(&pathbuf))) { 32 append(submodules, strings::dup(d.name))!; 33 }; 34 }; 35 sort::sort(submodules, size(str), &cmp::strs); 36 return submodules; 37 };