-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
Text.tsx
121 lines (100 loc) 路 2.52 KB
/
Text.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, {type ReactNode} from 'react';
import chalk, {type ForegroundColorName} from 'chalk';
import {type LiteralUnion} from 'type-fest';
import colorize from '../colorize.js';
import {type Styles} from '../styles.js';
export type Props = {
/**
* Change text color. Ink uses chalk under the hood, so all its functionality is supported.
*/
readonly color?: LiteralUnion<ForegroundColorName, string>;
/**
* Same as `color`, but for background.
*/
readonly backgroundColor?: LiteralUnion<ForegroundColorName, string>;
/**
* Dim the color (emit a small amount of light).
*/
readonly dimColor?: boolean;
/**
* Make the text bold.
*/
readonly bold?: boolean;
/**
* Make the text italic.
*/
readonly italic?: boolean;
/**
* Make the text underlined.
*/
readonly underline?: boolean;
/**
* Make the text crossed with a line.
*/
readonly strikethrough?: boolean;
/**
* Inverse background and foreground colors.
*/
readonly inverse?: boolean;
/**
* This property tells Ink to wrap or truncate text if its width is larger than container.
* If `wrap` is passed (by default), Ink will wrap text and split it into multiple lines.
* If `truncate-*` is passed, Ink will truncate text instead, which will result in one line of text with the rest cut off.
*/
readonly wrap?: Styles['textWrap'];
readonly children?: ReactNode;
};
/**
* This component can display text, and change its style to make it colorful, bold, underline, italic or strikethrough.
*/
export default function Text({
color,
backgroundColor,
dimColor = false,
bold = false,
italic = false,
underline = false,
strikethrough = false,
inverse = false,
wrap = 'wrap',
children,
}: Props) {
if (children === undefined || children === null) {
return null;
}
const transform = (children: string): string => {
if (dimColor) {
children = chalk.dim(children);
}
if (color) {
children = colorize(children, color, 'foreground');
}
if (backgroundColor) {
children = colorize(children, backgroundColor, 'background');
}
if (bold) {
children = chalk.bold(children);
}
if (italic) {
children = chalk.italic(children);
}
if (underline) {
children = chalk.underline(children);
}
if (strikethrough) {
children = chalk.strikethrough(children);
}
if (inverse) {
children = chalk.inverse(children);
}
return children;
};
return (
<ink-text
style={{flexGrow: 0, flexShrink: 1, flexDirection: 'row', textWrap: wrap}}
internal_transform={transform}
>
{children}
</ink-text>
);
}