18-application-libraries/05-text-encoding-and-style.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
488697d0941b5cad7aa4db6ba618f8ded8a234bd- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// Text encodings, Base64url and styled text.
//
// Strings are sequences of Unicode scalars. Bytes appear only when text is
// encoded explicitly. Styled text is pure data; rendering ANSI sequences is an
// explicit call whose enabling decision belongs to the caller.
UTF8: import("std:text/utf8")
UTF16LE: import("std:text/utf16le")
Base64url: import("std:text/base64url")
Style: import("std:text/style")
StyledText: import("std:text/styled_text")
ColorMode: import("std:text/color_mode")
Ansi: import("std:text/ansi")
text: "café \u{1F600}"
print(text.size()) // 6 Unicode scalars
utf8: UTF8.encode(text)
print(utf8.size()) // 10 bytes: é is 2, the emoji is 4
utf16: UTF16LE.encode(text)
print(utf16.size()) // 14 bytes: the emoji is a surrogate pair
print(UTF8.decode(utf8) == text) // true
print(UTF16LE.decode(utf16) == text) // true
// Invalid input is rejected rather than replaced.
broken: UTF8.encode("a")
broken.add(255)
print(Error.handle(() => { UTF8.decode(broken); "decoded" }, (error) => { "invalid UTF-8 rejected" }))
// Base64url is a canonical, unpadded codec over Bytes.
token: Base64url.encode(UTF8.encode("hi?"))
print(token) // aGk_
print(UTF8.decode(Base64url.decode(token))) // hi?
// Styled text is data; ANSI rendering is explicit.
message: StyledText.concat("status: ", StyledText("ok", Style("green")))
print(Ansi.render(message, false)) // status: ok
// Color policy is a pure decision: AUTO uses the caller-supplied Boolean.
print(ColorMode.resolve(ColorMode.AUTO, false)) // false
print(ColorMode.resolve(ColorMode.ALWAYS, false)) // true
enabled: Ansi.render(message, ColorMode.resolve(ColorMode.ALWAYS, false))
print(enabled.size() > "status: ok".size()) // true: escape sequences were generated