Skip to content

Commit 60ec3bf

Browse files
committed
improved the doc for tables a bit
1 parent 63de5dc commit 60ec3bf

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

content/docs/aesh/table.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,14 @@ This wraps each border character with the ANSI color prefix and `ANSI.RESET` suf
257257

258258
## Using Tables in Commands
259259

260-
A complete command that renders tabular output:
260+
Inside a command's `execute()` method, the `CommandInvocation` gives you access to the `Shell` object via `getShell()`. From the `Shell` you can get the current terminal dimensions with `size()` and write output with `write()` or `writeln()`. This makes it straightforward to render tables that adapt to the user's terminal:
261261

262262
```java
263263
import org.aesh.command.Command;
264264
import org.aesh.command.CommandDefinition;
265265
import org.aesh.command.CommandResult;
266266
import org.aesh.command.invocation.CommandInvocation;
267+
import org.aesh.command.shell.Shell;
267268
import org.aesh.util.table.Table;
268269
import org.aesh.util.table.TableStyle;
269270

@@ -273,23 +274,33 @@ public class ListUsersCommand implements Command<CommandInvocation> {
273274
@Override
274275
public CommandResult execute(CommandInvocation invocation) {
275276
List<User> users = loadUsers();
277+
Shell shell = invocation.getShell();
276278

277279
String table = Table.<User>builder()
278-
.maxWidth(invocation.getShell().size().getWidth())
280+
.maxWidth(shell.size().getWidth())
279281
.style(TableStyle.DUCKDB)
280282
.column("Name", u -> u.getName())
281283
.column("Email", u -> u.getEmail())
282284
.column("Role", u -> u.getRole())
283285
.build()
284286
.render(users);
285287

286-
invocation.getShell().writeln(table);
288+
shell.writeln(table);
287289
return CommandResult.SUCCESS;
288290
}
289291
}
290292
```
291293

292-
Using `invocation.getShell().size().getWidth()` adapts the table to the current terminal width.
294+
The key calls in the chain:
295+
296+
| Call | Returns | Purpose |
297+
|------|---------|---------|
298+
| `invocation.getShell()` | `Shell` | Access the terminal |
299+
| `shell.size()` | `Size` | Get terminal dimensions |
300+
| `size.getWidth()` | `int` | Terminal width in columns |
301+
| `shell.writeln(text)` | `void` | Write output to the terminal |
302+
303+
This ensures the table fits the user's terminal regardless of window size. See the [CommandInvocation API](command-invocation) documentation for the full set of `Shell` methods available.
293304

294305
## Validation Helpers
295306

0 commit comments

Comments
 (0)