@@ -19,10 +19,11 @@ nextpage:
1919
2020{% render 'fwe-wip-warning.md', site: site %}
2121
22- In this chapter, you'll explore advanced Dart features that improve the user
23- experience of your command-line application. You'll learn how to use enhanced
24- enums to manage console colors and extensions to add new functionality to
25- existing types, making your application more interactive and visually appealing.
22+ In this chapter, you'll explore advanced Dart features that
23+ improve the user experience of your command-line application.
24+ You'll learn how to use enhanced enums to manage console colors and
25+ extensions to add new functionality to existing types,
26+ making your application more interactive and visually appealing.
2627
2728:::secondary What you'll learn
2829
@@ -36,28 +37,28 @@ existing types, making your application more interactive and visually appealing.
3637
3738Before you begin this chapter, ensure you:
3839
39- * Have completed Chapter 6 and have a working Dart development environment
40- with the ` dartpedia ` project.
41- * Are familiar with basic programming concepts like variables, functions, and
42- control flow.
43- * Understand the concepts of packages and libraries in Dart.
44- * Have a basic understanding of object-oriented programming concepts like
45- classes and enums.
40+ - Have completed Chapter 6 and have a
41+ working Dart development environment with the ` dartpedia ` project.
42+ - Are familiar with basic programming concepts like
43+ variables, functions, and control flow.
44+ - Understand the concepts of packages and libraries in Dart.
45+ - Have a basic understanding of
46+ object-oriented programming concepts like classes and enums.
4647
4748## Tasks
4849
49- You will improve the user experience of your Dartpedia CLI application by adding
50- color to the output and improving text formatting.
50+ You will improve the user experience of your Dartpedia CLI application by
51+ adding color to the output and improving text formatting.
5152
5253### Task 1: Enhance the console color enum
5354
54- First, add color to the console output. The
55- ` ConsoleColor ` enum will include RGB values and methods for applying
56- colors to text.
55+ First, add color to the console output.
56+ The ` ConsoleColor ` enum will include RGB values and
57+ methods for applying colors to text.
5758
58591 . Create the ` command_runner/lib/src/console.dart ` file.
5960
60- 2 . Add the following code to define the ` ConsoleColor ` enum:
61+ 1 . Add the following code to define the ` ConsoleColor ` enum:
6162
6263 ``` dart title="command_runner/lib/src/console.dart"
6364 import 'dart:io';
@@ -113,10 +114,11 @@ colors to text.
113114 }
114115 ```
115116
116- This enum defines a set of console colors with their corresponding RGB
117- values. Each color is a constant instance of the `ConsoleColor` enum.
117+ This enum defines a set of console colors with
118+ their corresponding RGB values.
119+ Each color is a constant instance of the `ConsoleColor` enum.
118120
119- 3 . Add methods to the `ConsoleColor` enum for applying colors to text:
121+ 1 . Add methods to the `ConsoleColor` enum for applying colors to text:
120122
121123 ```dart title="command_runner/lib/src/console.dart"
122124 enum ConsoleColor {
@@ -159,14 +161,17 @@ colors to text.
159161 }
160162 ```
161163
162- These methods use [ANSI escape codes][] to apply foreground and background
163- colors to text. The `applyForeground` and `applyBackground` methods return
164+ These methods use [ANSI escape codes][] to
165+ apply foreground and background colors to text.
166+ The `applyForeground` and `applyBackground` methods return
164167 a string with the ANSI escape codes applied.
165168
169+ [ANSI escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code
170+
166171### Task 2: Create a String extension
167172
168- Next, create an extension on the `String` class to add utility methods
169- for applying console colors and formatting text.
173+ Next, create an extension on the `String` class to
174+ add utility methods for applying console colors and formatting text.
170175
1711761. Add the following code to the `command_runner/lib/src/console.dart` file:
172177
@@ -204,10 +209,12 @@ for applying console colors and formatting text.
204209 }
205210 ```
206211
207- This code defines an extension called `TextRenderUtils` on the `String`
208- class. It adds three getter methods for applying console colors:
209- `errorText`, `instructionText`, and `titleText`. It also adds a method for
210- splitting a string into lines of a specified length called `splitLinesByLength`.
212+ This code defines an extension called
213+ `TextRenderUtils` on the `String` class.
214+ It adds three getter methods for applying console colors:
215+ `errorText`, `instructionText`, and `titleText`.
216+ It also adds a method for splitting a string into
217+ lines of a specified length called `splitLinesByLength`.
211218
212219### Task 3: Update command_runner package
213220
@@ -229,13 +236,13 @@ Update the `command_runner` package to export `console.dart`.
229236
230237### Task 4: Implement colorful echo command
231238
232- Finally, implement an example command to test the print. It's
233- good practice to implement example usage of a package in Dart for
234- developers that will use your package. This example creates a command
235- that makes console output colorful.
239+ Finally, implement an example command to test the print.
240+ It's good practice to implement example usage of a package in Dart for
241+ developers that will use your package.
242+ This example creates a command that makes console output colorful.
236243
2372441. Open the `example/command_runner_example.dart` file.
238- 2 . Replace the contents of the file with the following code:
245+ 1 . Replace the contents of the file with the following code:
239246
240247 ```dart title="command_runner/example/command_runner_example.dart"
241248 import 'dart:async';
@@ -301,20 +308,24 @@ that makes console output colorful.
301308 }
302309 ```
303310
304- This code defines a `PrettyEcho` command that extends the `Command` class.
305- It takes a string as an argument and applies different colors to each word
306- based on its position in the string. The `run` method uses the `titleText`,
307- `instructionText`, and `errorText` getter methods from the `TextRenderUtils`
308- extension to apply the colors.
311+ This code defines a `PrettyEcho` command that
312+ extends the `Command` class.
313+ It takes a string as an argument and
314+ applies different colors to each word
315+ based on its position in the string.
316+ The `run` method uses the
317+ `titleText`, `instructionText`, and `errorText` getter methods from
318+ the `TextRenderUtils` extension to apply the colors.
309319
310- 3 . Navigate to `/dartpedia/command_runner` and run the following command:
320+ 1 . Navigate to `/dartpedia/command_runner` and run the following command:
311321
312322 ```bash
313323 dart run example/command_runner_example.dart echo "hello world goodbye"
314324 ```
315325
316- You should see the following text printed to the console, with the first
317- word appearing in light blue, the second in yellow, and the third in red.
326+ You should see the following text printed to the console,
327+ with the first word appearing in light blue,
328+ the second in yellow, and the third in red.
318329
319330 ```bash
320331 hello world goodbye
@@ -334,9 +345,7 @@ In this lesson, you learned about:
334345
335346## Next lesson
336347
337- In the next lesson, you'll learn how to further improve the
338- `command_runner` package by polishing the `HelpCommand`, completing the
339- `CommandRunner` class, adding the `onOutput` argument, and providing a complete
340- example.
341-
342- [ANSI escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code
348+ In the next lesson, you'll learn how to
349+ further improve the `command_runner` package by
350+ polishing the `HelpCommand`, completing the `CommandRunner` class,
351+ adding the `onOutput` argument, and providing a complete example.
0 commit comments