Ex 099 format instructions to 65 columns, wording, examples

This commit is contained in:
Dave Gauer 2023-04-23 18:54:43 -04:00
parent 99891b53fb
commit 42c430a0c0
2 changed files with 90 additions and 66 deletions

View file

@ -1,115 +1,140 @@
// //
// The output on the console looks a bit rudimentary at first glance. // Terminals have come a long way over the years. Starting with
// However, if you look at the development of modern computers, you can // monochrome lines on flickering CRT monitors and continuously
// see the enormous progress that has been made over the years. // improving to today's modern terminal emulators with sharp
// Starting with monochrome lines on flickering CRT monitors, modern // images, true color, fonts, ligatures, and characters in every
// terminal emulators offer a razor-sharp image with true color and // known language.
// nearly infinite font size thanks to modern hardware.
// //
// In addition, they have mastered ligatures and can represent almost // Formatting our results to be appealing and allow quick visual
// any character in any language. This also makes the output of programs // comprehension of the information is what users desire. <3
// on the console more atractive than ever in recent years.
// //
// This makes it all the more important to format the presentation of // C set string formatting standards over the years, and Zig is
// results in an appealing way, because that is what users appreciate, // following suit and growing daily. Due to this growth, there is
// quick visual comprehension of the information. // no official documentation for standard library features such
// as string formatting.
// //
// C has set standards here over the years, and Zig is preparing to // Therefore, the comments for the format() function are the only
// follow suit. Currently, however, it still lags a bit behind the model, // way to definitively learn how to format strings in Zig:
// but the Zig community is working diligently behind the scenes on
// further options.
//
// Nevertheless, it is time to take a closer look at the possibilities
// that already exist. And of course we will continue this series loosely,
// because Zig continues to grow almost daily.
//
// Since there is no proper documentation on the formatting yet, the most
// important source here is the source code:
// //
// https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L29 // https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L29
// //
// Zig already has a very nice selection of formatting options.
// These can be used in different ways, but typically to convert
// numerical values into various text representations. The
// results can be used for direct output to a terminal or stored
// for later use or written to file. The latter is useful when
// large amounts of data are to be processed by other programs.
// //
// And in fact, you already discover quite a lot of useful formatting. // In Ziglings, we are concerned with the output to the console.
// These can be used in different ways, e.g. to convert numerical values // But since the formatting instructions for files are the same,
// into text and for direct output to the console or to a file. The latter // what you learn applies universally.
// is useful when large amounts of data are to be processed by other programs.
// //
// However, we are concerned here exclusively with the output to the console. // Since we write to "debug" output in Ziglings, our answers
// But since the formatting instructions for files are the same, what you // usually look something like this:
// learn applies universally.
// //
// Since we basically write to debug output in Ziglings, our output usually // print("Text {placeholder} another text \n", .{foo});
// looks like this:
// //
// print("Text {placeholder} another text \n", .{variable}); // In addition to being replaced with foo in this example, the
// {placeholder} in the string can also have formatting applied.
// How does that work?
// //
// But how is the statement just shown formatted? // This actually happens in several stages. In one stage, escape
// // sequences are evaluated. The one we've seen the most
// This actually happens in several stages. On the one hand, escape // (including the example above) is "\n" which means "line feed".
// sequences are evaluated, there is the "\n" which means "line feed" // Whenever this statement is found, a new line is started in the
// in the example. Whenever this statement is found, a new line is started // output. Escape sequences can also be written one after the
// in the output. Escpape sequences can also be written one after the
// other, e.g. "\n\n" will cause two line feeds. // other, e.g. "\n\n" will cause two line feeds.
// //
// By the way, these formattings are passed directly to the terminal // By the way, the result of these escape sequences are passed
// program, i.e. escape sequences have nothing to do with Zig in this // directly to the terminal program. Other than translating them
// respect. The formatting that Zig actually performs is found in the // into control codes, escape sequences have nothing to do with
// curly bracket, the "placeholder", and affects the coresponding variable. // Zig. Zig knows nothing about "line feeds" or "tabs" or
// "bells".
// //
// And this is where it gets exciting, because numbers can have different // The formatting that Zig *does* perform itself is found in the
// sizes, be positive or negative, with a decimal point or without, // curly brackets: "{placeholder}". Formatting instructions in
// and so on. // the placeholder will determine how the corresponding value,
// e.g. foo, is displayed.
// //
// In order to bring these then into a uniform format for the output, // And this is where it gets exciting, because format() accepts a
// instructions can be given to the placeholder: // variety of formatting instructions. It's basically a tiny
// language of its own. Here's a numeric example:
// //
// print("=> {x:0>4}", .{var}); // print("Catch-{x:0>4}.", .{twenty_two});
// //
// This instruction outputs a hexadecimal number with leading zeros. // This formatting instruction outputs a hexadecimal number with
// leading zeros:
// //
// => 0x0017 // Catch-0x0016.
// //
// Let's move on to our exercise: we want to create a table that shows us // Or you can center-align a string like so:
// the multiplication of all numbers together from 1-15. So if you search
// for the number '5' in the row and '4' in the column (or vice versa),
// the result of '5 x 4 = 20' should be displayed there.
// //
// print("{s:*^20}\n", .{"Hello!"});
//
// Output:
//
// *******Hello!*******
//
// Let's try making use of some formatting. We've decided that
// the one thing missing from our lives is a multiplication table
// for all numbers from 1-15. We want the table to be nice and
// neat, with numbers in straight columns like so:
//
// X | 1 2 3 4 5 ...
// ---+---+---+---+---+---+
// 1 | 1 2 3 4 5
//
// 2 | 2 4 6 8 10
//
// 3 | 3 6 9 12 15
//
// 4 | 4 8 12 16 20
//
// 5 | 5 10 15 20 25
//
// ...
//
// Without string formatting, this would be a more challenging
// assignment because the number of digits in the numbers vary
// from 1 to 3. But formatting can help us with that.
// //
const std = @import("std"); const std = @import("std");
const print = std.debug.print; const print = std.debug.print;
pub fn main() !void { pub fn main() !void {
// the max. size of the table // Max number to multiply
const size = 15; const size = 15;
// print the header: // Print the header:
// //
// we start with a single 'X' for the diagonal, // We start with a single 'X' for the diagonal.
// that means there is no result
print("\n X |", .{}); print("\n X |", .{});
// header row with all numbers from 1 to size // Header row with all numbers from 1 to size.
for (0..size) |n| { for (0..size) |n| {
print("{d:>3} ", .{n + 1}); print("{d:>3} ", .{n + 1});
} }
print("\n", .{}); print("\n", .{});
// row line // Header column rule line.
var n: u8 = 0; var n: u8 = 0;
while (n <= size) : (n += 1) { while (n <= size) : (n += 1) {
print("---+", .{}); print("---+", .{});
} }
print("\n", .{}); print("\n", .{});
// now the actual table // Now the actual table. (Is there anything more beautiful
// than a well-formatted table?)
for (0..size) |a| { for (0..size) |a| {
print("{d:>2} |", .{a + 1}); print("{d:>2} |", .{a + 1});
for (0..size) |b| { for (0..size) |b| {
// what formatting is needed here? // What formatting is needed here to make our columns
// nice and straight?
print("{???} ", .{(a + 1) * (b + 1)}); print("{???} ", .{(a + 1) * (b + 1)});
} }
// after each row we use double line feed // After each row we use double line feed:
print("\n\n", .{}); print("\n\n", .{});
} }
} }

View file

@ -1,5 +1,4 @@
108,109c108 134c134
< // what formatting is needed here?
< print("{???} ", .{(a + 1) * (b + 1)}); < print("{???} ", .{(a + 1) * (b + 1)});
--- ---
> print("{d:>3} ", .{(a + 1) * (b + 1)}); > print("{d:>3} ", .{(a + 1) * (b + 1)});