diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-15 16:53:42 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-15 16:53:42 -0400 |
| commit | 0060686646561b099e3c8b9908d82cc22eba433a (patch) | |
| tree | af28f64b57252fdbf8262adfcc625c58dedc43fa /examples | |
| parent | 835eb7e89627eea923bfd57bdacba7065c6b1d4c (diff) | |
Update examples
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/ini.tm | 22 | ||||
| -rw-r--r-- | examples/tomodeps.tm | 26 | ||||
| -rw-r--r-- | examples/wrap.tm | 2 |
3 files changed, 25 insertions, 25 deletions
diff --git a/examples/ini.tm b/examples/ini.tm index afd71a30..c0d490cb 100644 --- a/examples/ini.tm +++ b/examples/ini.tm @@ -7,7 +7,7 @@ _HELP := " " func parse_ini(path:Path)->{Text:{Text:Text}}: - text := path:read() + text := path:read():or_exit("Could not read INI file: $\[31;1]$(path.text_content)$\[]") sections := {:Text:@{Text:Text}} current_section := @{:Text:Text} sections:set("", current_section) @@ -37,25 +37,23 @@ func main(path:Path, key:Text): $_USAGE ") - if not path:is_file() or path:is_pipe(): - exit("Could not read file: $(path.text_content)") - data := parse_ini(path) if keys.length < 1 or keys[1] == '*': !! $data return section := keys[1]:lower() - if not data:has(section): - exit("Invalid section name: $section; valid names: $(", ":join([k:quoted() for k in data.keys]))") - - section_data := data:get(section) + section_data := data:get(section):or_exit(" + Invalid section name: $\[31;1]$section$\[] + Valid names: $\[1]$(", ":join([k:quoted() for k in data.keys]))$\[] + ") if keys.length < 2 or keys[2] == '*': !! $section_data return section_key := keys[2]:lower() - if not section_data:has(section_key): - exit("Invalid key: $section_key; valid keys: $(", ":join(section_data.keys))") - - say(section_data:get(section_key)) + value := section_data:get(section_key):or_exit(" + Invalid key: $\[31;1]$section_key$\[] + Valid keys: $\[1]$(", ":join([s:quoted() for s in section_data.keys]))$\[] + ") + say(value) diff --git a/examples/tomodeps.tm b/examples/tomodeps.tm index a427c5f9..e3354136 100644 --- a/examples/tomodeps.tm +++ b/examples/tomodeps.tm @@ -15,13 +15,14 @@ func _get_file_dependencies(file:Path)->{Dependency}: return {:Dependency} deps := {:Dependency} - for line in file:read():lines(): - if line:matches($/use {..}.tm/): - file_import := Path.from_unsafe_text(line:replace($/use {..}/, "\1")):resolved(relative_to=file) - deps:add(Dependency.File(file_import)) - else if line:matches($/use {id}/): - module_name := line:replace($/use {..}/, "\1") - deps:add(Dependency.Module(module_name)) + if lines := file:by_line(): + for line in lines: + if line:matches($/use {..}.tm/): + file_import := Path.from_unsafe_text(line:replace($/use {..}/, "\1")):resolved(relative_to=file) + deps:add(Dependency.File(file_import)) + else if line:matches($/use {id}/): + module_name := line:replace($/use {..}/, "\1") + deps:add(Dependency.Module(module_name)) return deps func _build_dependency_graph(dep:Dependency, dependencies:&{Dependency:{Dependency}}): @@ -38,9 +39,10 @@ func _build_dependency_graph(dep:Dependency, dependencies:&{Dependency:{Dependen return unvisited := {:Path} - for line in files_path:read():lines(): - tm_path := Path.from_unsafe_text(line):resolved(relative_to=(~/.local/src/tomo/$module/)) - unvisited:add(tm_path) + if lines := files_path:by_line(): + for line in lines: + tm_path := Path.from_unsafe_text(line):resolved(relative_to=(~/.local/src/tomo/$module/)) + unvisited:add(tm_path) module_deps := {:Dependency} visited := {:Path} @@ -87,7 +89,7 @@ func _draw_tree(dep:Dependency, dependencies:{Dependency:{Dependency}}, already_ child_prefix := prefix ++ (if is_last: " " else: "│ ") - children := dependencies:get(dep, {:Dependency}) + children := dependencies:get(dep):or_else({:Dependency}) for i,child in children.items: is_child_last := (i == children.length) _draw_tree(child, dependencies, already_printed, child_prefix, is_child_last) @@ -96,7 +98,7 @@ func draw_tree(dep:Dependency, dependencies:{Dependency:{Dependency}}): printed := {:Dependency} say(_printable_name(dep)) printed:add(dep) - deps := dependencies:get(dep, {:Dependency}) + deps := dependencies:get(dep):or_else({:Dependency}) for i,child in deps.items: is_child_last := (i == deps.length) _draw_tree(child, dependencies, already_printed=&printed, is_last=is_child_last) diff --git a/examples/wrap.tm b/examples/wrap.tm index 0c3eac79..54198193 100644 --- a/examples/wrap.tm +++ b/examples/wrap.tm @@ -82,7 +82,7 @@ func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen=UN files = [(/dev/stdin)] for file in files: - text := file:read() + text := file:read():or_exit("Could not read file: $(file.text_content)") if rewrap: text = unwrap(text) |
