Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create print method #223

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .documentation/features/ansi_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ AnsiX.ensureSupportsAnsi(force: true);

### Enable/Disable AnsiX

We can enable ANSI formatting simply by running the following:
We can enable or disable ANSI formatting simply by running:

```dart
AnsiX.enable();
AnsiX.disable();
```

If ANSI formatting is disabled, no styles nor colors will be applied and
all output printed by AnsiX will be unformatted.
39 changes: 36 additions & 3 deletions .documentation/features/print.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
## Print


The **AnsiX** class library provides static methods that allow us to print Ansi Widgets in the console fast and easy.
The **AnsiX** class library provides static methods that allow us to
print Ansi Widgets in the console fast and easy.

If ANSI formatting is not supported or disabled manually,
then all output will be printed unformatted.

> Note that the property **AnsiX.allowPrint** needs to be enabled in order to use the print methods.


### Table of Contents

- [print](#print)
- [printStyled](#printStyled)
- [printJson](#printJson)
- [printTreeView](#printTreeView)
Expand All @@ -17,9 +24,34 @@ The **AnsiX** class library provides static methods that allow us to print Ansi
---


- #### print

Prints a string representation of the object to console
with the given styles and ANSI colors.

```dart
static void print(final Object? object)
```

**Usage:**

```dart
import 'package:ansix/ansix.dart';

void main(){
AnsiX.print(
'This is a text with bold style.'.bold(),
);
}
```




- #### printStyled

Prints a string representation of the object to console with the given styles and ANSI colors.
Prints a string representation of the object to console
with the given styles and ANSI colors.

```dart
static void printStyled(
Expand All @@ -46,7 +78,8 @@ void main(){

- #### printJson

Prints an indented string representation of the JSON to console with the given styles and ANSI colors.
Prints an indented string representation of the JSON to console
with the given styles and ANSI colors.

```dart
static void printJson(
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ AnsiX makes it easy to add ANSI styling to your output with minimal effort and m
- [Usage](./.documentation/features/widgets.md#usage)
- [Default theme](./.documentation/features/widgets.md#default-theme)
- [Print](./.documentation/features/print.md#print)
- [print](./.documentation/features/print.md#print)
- [printStyled](./.documentation/features/print.md#printStyled)
- [printJson](./.documentation/features/print.md#printJson)
- [printTreeView](./.documentation/features/print.md#printTreeView)
Expand Down
69 changes: 34 additions & 35 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,42 @@ void main() {
AnsiX.printTreeView(json, theme: AnsiTreeViewTheme.$default());

print('Data Grid'.italic().colored(foreground: AnsiColor.deepSkyBlue5));
print(
AnsiGrid.fromColumns(
<List<Object?>>[
<Object?>[
'Name',
AnsiColor.red.name,
AnsiColor.green.name,
AnsiColor.blue.name,
],
<Object?>[
'Hex',
AnsiColor.red.hex,
AnsiColor.green.hex,
AnsiColor.blue.hex,
],
<Object?>[
'RGB',
AnsiColor.red.rgb,
AnsiColor.green.rgb,
AnsiColor.blue.rgb,
],
AnsiX.printDataGrid(
<List<Object?>>[
<Object?>[
'Name',
AnsiColor.red.name,
AnsiColor.green.name,
AnsiColor.blue.name,
],
theme: const AnsiGridTheme(
border: AnsiBorder(
style: AnsiBorderStyle.square,
type: AnsiBorderType.all,
),
headerTextTheme: AnsiTextTheme(
alignment: AnsiTextAlignment.center,
style: AnsiTextStyle(bold: true),
foregroundColor: AnsiColor.cyan3,
),
cellTextTheme: AnsiTextTheme(
fixedWidth: 15,
alignment: AnsiTextAlignment.center,
),
<Object?>[
'Hex',
AnsiColor.red.hex,
AnsiColor.green.hex,
AnsiColor.blue.hex,
],
<Object?>[
'RGB',
AnsiColor.red.rgb,
AnsiColor.green.rgb,
AnsiColor.blue.rgb,
],
],
theme: const AnsiGridTheme(
border: AnsiBorder(
style: AnsiBorderStyle.square,
type: AnsiBorderType.all,
),
headerTextTheme: AnsiTextTheme(
alignment: AnsiTextAlignment.center,
style: AnsiTextStyle(bold: true),
foregroundColor: AnsiColor.cyan3,
),
cellTextTheme: AnsiTextTheme(
fixedWidth: 15,
alignment: AnsiTextAlignment.center,
),
),
type: AnsiGridType.fromColumns,
);
}
12 changes: 9 additions & 3 deletions lib/src/ansix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ class AnsiX {
TextFormatter _formatter = AnsiTextFormatter();

/// The active text writer.
static Writer writer = const AnsiWriter();
static Writer get writer => _ansix._writer;
Writer _writer = const AnsiWriter();

/// Enables ANSI formatting (if supported by the system).
static void enable() {
_ansix._isEnabled = true;
_ansix._formatter = AnsiTextFormatter();
writer = const AnsiWriter();
_ansix._writer = const AnsiWriter();
}

/// Disables ANSI formatting.
static void disable() {
_ansix._isEnabled = false;
_ansix._formatter = StandardTextFormatter();
writer = const NoAnsiWriter();
_ansix._writer = const NoAnsiWriter();
}

/// Returns true if ANSI escape characters are supported in the attached terminal.
Expand Down Expand Up @@ -113,6 +114,11 @@ class AnsiX {
}
}

/// Prints a string representation of the given object to console
static void print(final Object? object) {
writer.write(object);
}

/// Prints a string representation of the object to console
/// with the given styles and ANSI colors.
static void printStyled(
Expand Down
26 changes: 26 additions & 0 deletions test/print/print_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:ansix/ansix.dart';
import 'package:test/test.dart';

import 'mocks.dart';
import 'utils.dart';

void main() {
group('print', () {
test('print styled text', () {
testPrint(
testMessage.bold(),
allowPrint: true,
expected: mockStyledText,
);
});

test('print unformatted text', () {
testPrint(
testMessage.bold(),
allowPrint: true,
allowAnsi: false,
expected: testMessage,
);
});
});
}
30 changes: 25 additions & 5 deletions test/print/utils.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:async';

import 'package:ansix/ansix.dart';
import 'package:ansix/src/formatter/ansi.dart';
import 'package:ansix/src/writer/ansi.dart';
import 'package:test/test.dart';

import 'mocks.dart';

testPrint({
_testPrint({
required final bool allowPrint,
required final bool allowAnsi,
required Function printCallback,
Expand All @@ -31,14 +33,32 @@ testPrint({
);

expect(printLine, expected);

expect(AnsiX.isEnabled, allowAnsi);
expect(AnsiX.formatter is AnsiTextFormatter, allowAnsi);
expect(AnsiX.writer is AnsiWriter, allowAnsi);
}

testPrint(
final Object? object, {
required final String expected,
required final bool allowPrint,
final bool allowAnsi = true,
}) {
_testPrint(
allowPrint: allowPrint,
allowAnsi: allowAnsi,
expected: expected,
printCallback: () => AnsiX.print(object),
);
}

testPrintStyledText({
required final String expected,
required final bool allowPrint,
final bool allowAnsi = true,
}) {
testPrint(
_testPrint(
allowPrint: allowPrint,
allowAnsi: allowAnsi,
expected: expected,
Expand All @@ -55,7 +75,7 @@ testPrintJson({
required final bool allowPrint,
final bool allowAnsi = true,
}) {
testPrint(
_testPrint(
allowPrint: allowPrint,
allowAnsi: allowAnsi,
expected: expected,
Expand All @@ -72,7 +92,7 @@ testPrintTreeView({
required final bool allowPrint,
final bool allowAnsi = true,
}) {
testPrint(
_testPrint(
allowPrint: allowPrint,
allowAnsi: allowAnsi,
expected: expected,
Expand Down Expand Up @@ -101,7 +121,7 @@ testPrintDataGrid({
required final bool allowPrint,
final bool allowAnsi = true,
}) {
testPrint(
_testPrint(
allowPrint: allowPrint,
allowAnsi: allowAnsi,
expected: expected,
Expand Down