aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-04-06 22:26:12 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-04-06 22:26:12 -0400
commit3406515a44b13d0c290c28ac42bd364ce27560c7 (patch)
tree38000658e651ad19b9c8c2590df8fd6bb6faa4d7 /examples
parentd8afa73368cdff38125fa1f7d17ad5ce54c84def (diff)
Make string escapes more normal: "\n" for newline, etc. Backticks can be
used to put in literal code without escape sequences.
Diffstat (limited to 'examples')
-rw-r--r--examples/colorful/colorful.tm2
-rw-r--r--examples/commands/commands.tm2
-rw-r--r--examples/http-server/http-server.tm12
-rw-r--r--examples/ini/ini.tm18
-rw-r--r--examples/log/log.tm16
-rw-r--r--examples/pthreads/pthreads.tm2
-rw-r--r--examples/tomo-install/tomo-install.tm2
-rw-r--r--examples/tomodeps/tomodeps.tm12
-rw-r--r--examples/wrap/wrap.tm10
9 files changed, 38 insertions, 38 deletions
diff --git a/examples/colorful/colorful.tm b/examples/colorful/colorful.tm
index 30f3fc12..9a8bbbba 100644
--- a/examples/colorful/colorful.tm
+++ b/examples/colorful/colorful.tm
@@ -5,7 +5,7 @@ HELP := "
Usage: colorful [args...] [--by-line] [--files files...]
"
-CSI := "$\033["
+CSI := "\033["
use patterns
diff --git a/examples/commands/commands.tm b/examples/commands/commands.tm
index ace7deb5..d72398b9 100644
--- a/examples/commands/commands.tm
+++ b/examples/commands/commands.tm
@@ -28,7 +28,7 @@ struct ProgramResult(stdout:[Byte], stderr:[Byte], exit_type:ExitType)
if status == 0
if text := Text.from_bytes(r.stdout)
if trim_newline
- text = text.without_suffix(\n)
+ text = text.without_suffix("\n")
return text
else return none
return none
diff --git a/examples/http-server/http-server.tm b/examples/http-server/http-server.tm
index 3d72c1e8..5b73d1af 100644
--- a/examples/http-server/http-server.tm
+++ b/examples/http-server/http-server.tm
@@ -85,14 +85,14 @@ struct HTTPRequest(method:Text, path:Text, version:Text, headers:[Text], body:Te
struct HTTPResponse(body:Text, status=200, content_type="text/plain", headers:{Text=Text}={})
func bytes(r:HTTPResponse -> [Byte])
body_bytes := r.body.bytes()
- extra_headers := (++: "$k: $v$(\r\n)" for k,v in r.headers) or ""
+ extra_headers := (++: "$k: $v\r\n" for k,v in r.headers) or ""
return "
- HTTP/1.1 $(r.status) OK$\r
- Content-Length: $(body_bytes.length + 2)$\r
- Content-Type: $(r.content_type)$\r
- Connection: close$\r
+ HTTP/1.1 $(r.status) OK\r
+ Content-Length: $(body_bytes.length + 2)\r
+ Content-Type: $(r.content_type)\r
+ Connection: close\r
$extra_headers
- $\r$\n
+ \r\n
".bytes() ++ body_bytes
func _content_type(file:Path -> Text)
diff --git a/examples/ini/ini.tm b/examples/ini/ini.tm
index c24cb4b9..4dc27725 100644
--- a/examples/ini/ini.tm
+++ b/examples/ini/ini.tm
@@ -10,23 +10,23 @@ _HELP := "
"
func parse_ini(path:Path -> {Text={Text=Text}})
- text := path.read() or exit("Could not read INI file: $\[31;1]$(path)$\[]")
+ text := path.read() or exit("Could not read INI file: \[31;1]$(path)\[]")
sections : @{Text=@{Text=Text}}
current_section : @{Text=Text}
# Line wraps:
- text = text.replace_pattern($Pat/\{1 nl}{0+space}/, " ")
+ text = text.replace_pattern($Pat/\\{1 nl}{0+space}/, " ")
for line in text.lines()
line = line.trim()
skip if line.starts_with(";") or line.starts_with("#")
if line.matches_pattern($Pat/[?]/)
- section_name := line.replace($Pat/[?]/, "\1").trim().lower()
+ section_name := line.replace($Pat/[?]/, "@1").trim().lower()
current_section = @{}
sections[section_name] = current_section
else if line.matches_pattern($Pat/{..}={..}/)
- key := line.replace_pattern($Pat/{..}={..}/, "\1").trim().lower()
- value := line.replace_pattern($Pat/{..}={..}/, "\2").trim()
+ key := line.replace_pattern($Pat/{..}={..}/, "@1").trim().lower()
+ value := line.replace_pattern($Pat/{..}={..}/, "@2").trim()
current_section[key] = value
return {k=v[] for k,v in sections[]}
@@ -46,8 +46,8 @@ func main(path:Path, key:Text?)
section := keys[1].lower()
section_data := data[section] or exit("
- Invalid section name: $\[31;1]$section$\[]
- Valid names: $\[1]$(", ".join([k.quoted() for k in data.keys]))$\[]
+ Invalid section name: \[31;1]$section\[]
+ Valid names: \[1]$(", ".join([k.quoted() for k in data.keys]))\[]
")
if keys.length < 2 or keys[2] == '*'
say("$section_data")
@@ -55,7 +55,7 @@ func main(path:Path, key:Text?)
section_key := keys[2].lower()
value := section_data[section_key] or exit("
- Invalid key: $\[31;1]$section_key$\[]
- Valid keys: $\[1]$(", ".join([s.quoted() for s in section_data.keys]))$\[]
+ Invalid key: \[31;1]$section_key\[]
+ Valid keys: \[1]$(", ".join([s.quoted() for s in section_data.keys]))\[]
")
say(value)
diff --git a/examples/log/log.tm b/examples/log/log.tm
index 9c3396e7..4b7893fd 100644
--- a/examples/log/log.tm
+++ b/examples/log/log.tm
@@ -16,24 +16,24 @@ func _timestamp(->Text)
return c_str.as_text()
func info(text:Text, newline=yes)
- say("$\[2]⚫ $text$\[]", newline)
+ say("\[2]⚫ $text\[]", newline)
for file in logfiles
- file.append("$(_timestamp()) [info] $text$\n")
+ file.append("$(_timestamp()) [info] $text\n")
func debug(text:Text, newline=yes)
- say("$\[32]🟢 $text$\[]", newline)
+ say("\[32]🟢 $text\[]", newline)
for file in logfiles
- file.append("$(_timestamp()) [debug] $text$\n")
+ file.append("$(_timestamp()) [debug] $text\n")
func warn(text:Text, newline=yes)
- say("$\[33;1]🟡 $text$\[]", newline)
+ say("\[33;1]🟡 $text\[]", newline)
for file in logfiles
- file.append("$(_timestamp()) [warn] $text$\n")
+ file.append("$(_timestamp()) [warn] $text\n")
func error(text:Text, newline=yes)
- say("$\[31;1]🔴 $text$\[]", newline)
+ say("\[31;1]🔴 $text\[]", newline)
for file in logfiles
- file.append("$(_timestamp()) [error] $text$\n")
+ file.append("$(_timestamp()) [error] $text\n")
func add_logfile(file:Path)
logfiles.add(file)
diff --git a/examples/pthreads/pthreads.tm b/examples/pthreads/pthreads.tm
index 7f720d5a..fee7ce5d 100644
--- a/examples/pthreads/pthreads.tm
+++ b/examples/pthreads/pthreads.tm
@@ -91,7 +91,7 @@ func main()
say_mutex := pthread_mutex_t.new()
announce := func(speaker:Text, text:Text)
do say_mutex.lock()
- say("$\033[2m[$speaker]$\033[m $text")
+ say("\[2][$speaker]\[] $text")
say_mutex.unlock()
worker := pthread_t.new(func()
diff --git a/examples/tomo-install/tomo-install.tm b/examples/tomo-install/tomo-install.tm
index c7d752ee..d915b993 100644
--- a/examples/tomo-install/tomo-install.tm
+++ b/examples/tomo-install/tomo-install.tm
@@ -80,5 +80,5 @@ func main(paths:[Path])
fi
).get_output()!)
- say("$\[1]Installed $url!$\[]")
+ say("\[1]Installed $url!\[]")
diff --git a/examples/tomodeps/tomodeps.tm b/examples/tomodeps/tomodeps.tm
index 1e6be615..5513290c 100644
--- a/examples/tomodeps/tomodeps.tm
+++ b/examples/tomodeps/tomodeps.tm
@@ -20,10 +20,10 @@ func _get_file_dependencies(file:Path -> |Dependency|)
if lines := file.by_line()
for line in lines
if line.matches_pattern($Pat/use {..}.tm/)
- file_import := Path.from_text(line.replace_pattern($Pat/use {..}/, "\1")).resolved(relative_to=file)
+ file_import := Path.from_text(line.replace_pattern($Pat/use {..}/, "@1")).resolved(relative_to=file)
deps.add(Dependency.File(file_import))
else if line.matches_pattern($Pat/use {id}/)
- module_name := line.replace_pattern($Pat/use {..}/, "\1")
+ module_name := line.replace_pattern($Pat/use {..}/, "@1")
deps.add(Dependency.Module(module_name))
return deps[]
@@ -64,17 +64,17 @@ func get_dependency_graph(dep:Dependency -> {Dependency=|Dependency|})
func _printable_name(dep:Dependency -> Text)
when dep is Module(module)
- return "$(\x1b)[34;1m$module$(\x1b)[m"
+ return "\[34;1]$module\[]"
is File(f)
f = f.relative_to((.))
if f.exists()
return Text(f)
else
- return "$(\x1b)[31;1m$(f) (not found)$(\x1b)[m"
+ return "\[31;1]$f (not found)\[]"
func _draw_tree(dep:Dependency, dependencies:{Dependency=|Dependency|}, already_printed:@|Dependency|, prefix="", is_last=yes)
if already_printed.has(dep)
- say(prefix ++ (if is_last then "└── " else "├── ") ++ _printable_name(dep) ++ " $\x1b[2m(recursive)$\x1b[m")
+ say(prefix ++ (if is_last then "└── " else "├── ") ++ _printable_name(dep) ++ " \[2](recursive)\[]")
return
say(prefix ++ (if is_last then "└── " else "├── ") ++ _printable_name(dep))
@@ -112,6 +112,6 @@ func main(files:[Text])
dependencies := get_dependency_graph(Module(arg))
draw_tree(Module(arg), dependencies)
else
- say("$\x1b[2mSkipping $arg$\x1b[m")
+ say("\[2]Skipping $arg\[]")
skip
diff --git a/examples/wrap/wrap.tm b/examples/wrap/wrap.tm
index 1a29701f..bae01739 100644
--- a/examples/wrap/wrap.tm
+++ b/examples/wrap/wrap.tm
@@ -11,15 +11,15 @@ HELP := "
--hyphen='-': The text to use for hyphenation
"
-UNICODE_HYPHEN := \{hyphen}
+UNICODE_HYPHEN := "\{hyphen}"
func unwrap(text:Text, preserve_paragraphs=yes, hyphen=UNICODE_HYPHEN -> Text)
if preserve_paragraphs
paragraphs := text.split($/{2+ nl}/)
if paragraphs.length > 1
- return \n\n.join([unwrap(p, hyphen=hyphen, preserve_paragraphs=no) for p in paragraphs])
+ return "\n\n".join([unwrap(p, hyphen=hyphen, preserve_paragraphs=no) for p in paragraphs])
- return text.replace($/$(hyphen)$(\n)/, "")
+ return text.replace("$(hyphen)\n", "")
func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text)
if width <= 0
@@ -69,7 +69,7 @@ func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text)
if line != ""
lines.insert(line)
- return \n.join(lines)
+ return "\n".join(lines)
func _can_fit_word(line:Text, letters:[Text], width:Int -> Bool; inline)
if line == ""
@@ -99,4 +99,4 @@ func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen=UN
wrap(paragraph, width=width, min_split=min_split, hyphen=hyphen)
)
- out.write(\n\n.join(wrapped_paragraphs[]) ++ \n)
+ out.write("\n\n".join(wrapped_paragraphs[]) ++ "\n")