diff --git a/.github/workflows/directory.jule b/.github/workflows/directory.jule index ecefa04..18183c4 100644 --- a/.github/workflows/directory.jule +++ b/.github/workflows/directory.jule @@ -1,110 +1,145 @@ use std::process::{exit} -use std::fs::{FsError, OFlag, open, read_dir} +use std::fs::{DirEntry, FsError, OFlag, open, read_dir} static mut TABLE: str = "" static PACKAGES: [...]str = [ - "./math", - "./search", - "./sort", - "./string", + "./math", + "./search", + "./sort", + "./string", ] fn workflow_failed(message: str) { - const FAIL_EXIT_CODE = 1 - outln(message) - exit(FAIL_EXIT_CODE) + const FAIL_EXIT_CODE = 1 + outln(message) + exit(FAIL_EXIT_CODE) } fn write(text: str) { TABLE += text } fn write_line(text: str) { write(text + "\n") } +fn sort_dirents(mut &dirents: []&DirEntry) { + let less = fn(s1: str, s2: str): bool { + let s1r = ([]rune)(s1) + let s2r = ([]rune)(s2) + + let mut n = s1r.len + if n > s2r.len { + n = s2r.len + } + + let mut i = 0 + for i < n; i++ { + let ir = s1r[i] + let jr = s2r[i] + + if ir != jr { + ret ir < jr + } + } + + ret s1r.len < s2r.len + } + + let mut i = 0 + for i < dirents.len-1; i++ { + let mut j = 0 + for j < dirents.len-i-1; j++ { + if less(dirents[j+1].name, dirents[j].name) { + dirents[j+1], dirents[j] = dirents[j], dirents[j+1] + } + } + } +} + fn make_name(mut s: str): str { - // Remove all cutset substrings. - let cutset_subs = [ - ".jule", - ".", - "/", - "\\", - ] - for _, subs in cutset_subs { - s = s.replace(subs, "", -1) - } - - // Replace all underlines with space. - s = s.replace("_", " ", -1) - - // Make uppercase first character of all words. - let mut i = 0 - let mut wait_space = false - for i < s.len; i++ { - let b = s[i] - if wait_space { - if b == ' ' { - wait_space = false - } - continue - } - - s[i] = 'Z' - ('z' - b) - - wait_space = true - } - - ret s + // Remove all cutset substrings. + let cutset_subs = [ + ".jule", + ".", + "/", + "\\", + ] + for _, subs in cutset_subs { + s = s.replace(subs, "", -1) + } + + // Replace all underlines with space. + s = s.replace("_", " ", -1) + + // Make uppercase first character of all words. + let mut i = 0 + let mut wait_space = false + for i < s.len; i++ { + let b = s[i] + if wait_space { + if b == ' ' { + wait_space = false + } + continue + } + + s[i] = 'Z' - ('z' - b) + + wait_space = true + } + + ret s } fn append_package(package: str) { - let (dirents, err) = read_dir(package) - if err != FsError.Ok { - workflow_failed("package did not readed: " + package) - } - - let package_table_name = make_name(package) - - write_line("") - write_line("") - write("## ") - write(package_table_name) - for _, dirent in dirents { - // Ignore sub-diretories. - if dirent.stat.is_dir() { - continue - } - - let file_table_name = make_name(dirent.name) - let file_url = package + "/" + dirent.name - - write_line("") - write(" - [") - write(file_table_name) - write("](") - write(file_url) - write(")") - } + let (mut dirents, err) = read_dir(package) + if err != FsError.Ok { + workflow_failed("package did not readed: " + package) + } + + let package_table_name = make_name(package) + + write_line("") + write_line("") + write("## ") + write(package_table_name) + sort_dirents(dirents) + for _, dirent in dirents { + // Ignore sub-diretories. + if dirent.stat.is_dir() { + continue + } + + let file_table_name = make_name(dirent.name) + let file_url = package + "/" + dirent.name + + write_line("") + write(" - [") + write(file_table_name) + write("](") + write(file_url) + write(")") + } } fn create_directory_md() { - const MD_PATH = "./DIRECTORY.md" - let (mut f, mut err) = open(MD_PATH, OFlag.Rdwr|OFlag.Create|OFlag.Trunc, 0o666) - if err != FsError.Ok { - workflow_failed("a problem occurs when creating " + MD_PATH) - } - - defer { f.close() } - - _, err = f.write(([]byte)(TABLE)) - if err != FsError.Ok { - workflow_failed("a problem occurs when creating " + MD_PATH) - } + const MD_PATH = "./DIRECTORY.md" + let (mut f, mut err) = open(MD_PATH, OFlag.Rdwr|OFlag.Create|OFlag.Trunc, 0o666) + if err != FsError.Ok { + workflow_failed("a problem occurs when creating " + MD_PATH) + } + + defer { f.close() } + + _, err = f.write(([]byte)(TABLE)) + if err != FsError.Ok { + workflow_failed("a problem occurs when creating " + MD_PATH) + } } fn main() { - write_line("# Table of Contents") + write_line("# Table of Contents") - for _, package in PACKAGES { - append_package(package) - } + for _, package in PACKAGES { + append_package(package) + } - create_directory_md() + create_directory_md() } diff --git a/DIRECTORY.md b/DIRECTORY.md index 19f190a..f1ea591 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -2,25 +2,25 @@ ## Math - - [Median](./math/median.jule) - [Abs](./math/abs.jule) + - [Median](./math/median.jule) ## Search - [Linear Search](./search/linear_search.jule) ## Sort - - [Shell Sort](./sort/shell_sort.jule) - [Bubble Sort](./sort/bubble_sort.jule) - - [Selection Sort](./sort/selection_sort.jule) - [Exchange Sort](./sort/exchange_sort.jule) - - [Quick Sort](./sort/quick_sort.jule) - [Insertion Sort](./sort/insertion_sort.jule) + - [Quick Sort](./sort/quick_sort.jule) + - [Selection Sort](./sort/selection_sort.jule) + - [Shell Sort](./sort/shell_sort.jule) - [Simple Sort](./sort/simple_sort.jule) ## String - - [Upper Case](./string/upper_case.jule) - - [Is Digit](./string/is_digit.jule) - [Capitalize](./string/capitalize.jule) - - [Lower Case](./string/lower_case.jule) - [Is Alpha](./string/is_alpha.jule) - - [Reverse](./string/reverse.jule) \ No newline at end of file + - [Is Digit](./string/is_digit.jule) + - [Lower Case](./string/lower_case.jule) + - [Reverse](./string/reverse.jule) + - [Upper Case](./string/upper_case.jule) \ No newline at end of file