├── .idea
└── .idea.unity-rich-debug
│ ├── .idea
│ ├── contentModel.xml
│ ├── encodings.xml
│ ├── indexLayout.xml
│ ├── misc.xml
│ ├── modules.xml
│ ├── vcs.xml
│ └── workspace.xml
│ └── riderModule.iml
├── FormattedStringBuilder.cs
├── FormattedStringBuilder.cs.meta
├── LICENSE
├── LICENSE.meta
├── README.md
├── README.md.meta
├── RichTextExtensions.cs
├── RichTextExtensions.cs.meta
├── Skibitsky.Unity.StringFormatter.asmdef
├── Skibitsky.Unity.StringFormatter.asmdef.meta
├── package.json
└── package.json.meta
/.idea/.idea.unity-rich-debug/.idea/contentModel.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.idea/.idea.unity-rich-debug/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/.idea.unity-rich-debug/.idea/indexLayout.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/.idea.unity-rich-debug/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/.idea.unity-rich-debug/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/.idea.unity-rich-debug/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/.idea.unity-rich-debug/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 | pink
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
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 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 | 1561815728969
175 |
176 |
177 | 1561815728969
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
--------------------------------------------------------------------------------
/.idea/.idea.unity-rich-debug/riderModule.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/FormattedStringBuilder.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Text;
3 |
4 | namespace Skibitsky.Unity.StringFormatter
5 | {
6 | public class FormattedStringBuilder
7 | {
8 | private readonly StringBuilder _openingTagsBuilder;
9 | private readonly Stack _closingTagsStack;
10 |
11 | private string String { get; }
12 |
13 | public FormattedStringBuilder() : this(string.Empty)
14 | {
15 | }
16 |
17 | public FormattedStringBuilder(string value)
18 | {
19 | String = value;
20 |
21 | _openingTagsBuilder = new StringBuilder();
22 | _closingTagsStack = new Stack();
23 | }
24 |
25 | public void Append(string value)
26 | {
27 | _openingTagsBuilder.Append(value);
28 | }
29 |
30 | public void PushToEnd(string value)
31 | {
32 | _closingTagsStack.Push(value);
33 | }
34 |
35 | public string Apply(string value)
36 | {
37 | var builder = new StringBuilder(_openingTagsBuilder.ToString());
38 |
39 | builder.Append(value);
40 |
41 | foreach (var s in _closingTagsStack)
42 | builder.Append(s);
43 |
44 | return builder.ToString();
45 | }
46 |
47 | public override string ToString() => Apply(String);
48 |
49 | public static implicit operator string(FormattedStringBuilder fsb) => fsb.ToString();
50 | public static implicit operator FormattedStringBuilder(string str) => new FormattedStringBuilder(str);
51 | }
52 | }
--------------------------------------------------------------------------------
/FormattedStringBuilder.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: e62a0fd5b3a72440e931e189b9d5a3f3
3 | timeCreated: 1616843668
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 skibitsky
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/LICENSE.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 4f652e2c4810e479ba92ea28462aa4ac
3 | DefaultImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # String Formatter [](https://openupm.com/packages/com.skibitsky.string-formatter/)
2 | > Format strings with HTML-like tags (Unity's Rich Text) in LINQ fashion
3 |
4 | ## Usage
5 |
6 | ```csharp
7 | // Without String Formatter
8 | Debug.Log($"Player's name: {name}");
9 |
10 | // With String Formatter
11 | Debug.Log($"Player's name: {name.Bold().Italic().Size(16).Red()});
12 |
13 | // Reuse
14 | var nameFormat = new FormattedStringBuilder().Bold().Italic().Size(16).Red();
15 | Debug.Log($"Player One name: {nameFormat.Appy(name1)});
16 | Debug.Log($"Player Two name: {nameFormat.Appy(name2)});
17 |
18 | // Extend
19 | public static FormattedStringBuilder Dead(this FormattedStringBuilder source)
20 | {
21 | source.Append("Player "); // Add before string. Used for opening tags
22 | source.PushToEnd(" is dead!"); // Add after string. Used for closing tags
23 |
24 | return source.Bold().Italic(); // Also make string bold and italic
25 | }
26 | public static FormattedStringBuilder Dead(this string source) => new FormattedStringBuilder(source).Dead();
27 |
28 | ```
29 |
30 | ## Installation
31 |
32 | ### Install via OpenUPM
33 |
34 | The package is available on the [openupm registry](https://openupm.com). It's recommended to install it via [openupm-cli](https://github.com/openupm/openupm-cli).
35 |
36 | ```
37 | openupm add com.skibitsky.string-formatter
38 | ```
39 |
40 | ### Install via Git URL
41 |
42 | Open *Packages/manifest.json* with your favorite text editor. Add the following line to the dependencies block.
43 |
44 | {
45 | "dependencies": {
46 | "com.skibitsky.string-formatter": "https://github.com/skibitsky/string-formatter.git"
47 | }
48 | }
49 |
50 | Notice: Unity Package Manager records the current commit to a lock entry of the *manifest.json*. To update to the latest version, change the hash value manually or remove the lock entry to resolve the package.
51 |
52 | "lock": {
53 | "com.skibitsky.string-formatter": {
54 | "revision": "master",
55 | "hash": "..."
56 | }
57 | }
58 |
59 |
60 |
--------------------------------------------------------------------------------
/README.md.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 575a29ec0da534a49b03fdfacf134edf
3 | TextScriptImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/RichTextExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace Skibitsky.Unity.StringFormatter
2 | {
3 | public static class RichTextExtensions
4 | {
5 | public static FormattedStringBuilder Size(this string source, int size, bool skip = false) => new FormattedStringBuilder(source).Size(size, skip);
6 | public static FormattedStringBuilder Bold(this string source, bool skip = false) => new FormattedStringBuilder(source).Bold(skip);
7 | public static FormattedStringBuilder Italic(this string source, bool skip = false) => new FormattedStringBuilder(source).Italic(skip);
8 | public static FormattedStringBuilder Color(this string source, string color, bool skip = false) => new FormattedStringBuilder(source).Color(color, skip);
9 |
10 | public static FormattedStringBuilder Aqua(this string source, bool skip = false) => new FormattedStringBuilder(source).Aqua(skip);
11 | public static FormattedStringBuilder Black(this string source, bool skip = false) => new FormattedStringBuilder(source).Black(skip);
12 | public static FormattedStringBuilder Blue(this string source, bool skip = false) => new FormattedStringBuilder(source).Blue(skip);
13 | public static FormattedStringBuilder Brown(this string source, bool skip = false) => new FormattedStringBuilder(source).Brown(skip);
14 | public static FormattedStringBuilder Cyan(this string source, bool skip = false) => new FormattedStringBuilder(source).Cyan(skip);
15 | public static FormattedStringBuilder DarkBlue(this string source, bool skip = false) => new FormattedStringBuilder(source).DarkBlue(skip);
16 | public static FormattedStringBuilder Fuchsia(this string source, bool skip = false) => new FormattedStringBuilder(source).Fuchsia(skip);
17 | public static FormattedStringBuilder Green(this string source, bool skip = false) => new FormattedStringBuilder(source).Green(skip);
18 | public static FormattedStringBuilder Grey(this string source, bool skip = false) => new FormattedStringBuilder(source).Grey(skip);
19 | public static FormattedStringBuilder LightBlue(this string source, bool skip = false) => new FormattedStringBuilder(source).LightBlue(skip);
20 | public static FormattedStringBuilder Lime(this string source, bool skip = false) => new FormattedStringBuilder(source).Lime(skip);
21 | public static FormattedStringBuilder Magenta(this string source, bool skip = false) => new FormattedStringBuilder(source).Magenta(skip);
22 | public static FormattedStringBuilder Maroon(this string source, bool skip = false) => new FormattedStringBuilder(source).Maroon(skip);
23 | public static FormattedStringBuilder Navy(this string source, bool skip = false) => new FormattedStringBuilder(source).Navy(skip);
24 | public static FormattedStringBuilder Olive(this string source, bool skip = false) => new FormattedStringBuilder(source).Olive(skip);
25 | public static FormattedStringBuilder Orange(this string source, bool skip = false) => new FormattedStringBuilder(source).Orange(skip);
26 | public static FormattedStringBuilder Purple(this string source, bool skip = false) => new FormattedStringBuilder(source).Purple(skip);
27 | public static FormattedStringBuilder Red(this string source, bool skip = false) => new FormattedStringBuilder(source).Red(skip);
28 | public static FormattedStringBuilder Silver(this string source, bool skip = false) => new FormattedStringBuilder(source).Silver(skip);
29 | public static FormattedStringBuilder Teal(this string source, bool skip = false) => new FormattedStringBuilder(source).Teal(skip);
30 | public static FormattedStringBuilder White(this string source, bool skip = false) => new FormattedStringBuilder(source).White(skip);
31 | public static FormattedStringBuilder Yellow(this string source, bool skip = false) => new FormattedStringBuilder(source).Yellow(skip);
32 |
33 |
34 | public static FormattedStringBuilder Size(this FormattedStringBuilder source, int size, bool skip = false)
35 | {
36 | if (skip)
37 | return source;
38 |
39 | source.Append("");
42 |
43 | source.PushToEnd("");
44 |
45 | return source;
46 | }
47 |
48 | public static FormattedStringBuilder Bold(this FormattedStringBuilder source, bool skip = false)
49 | {
50 | if (skip)
51 | return source;
52 |
53 | source.Append("");
54 | source.PushToEnd("");
55 |
56 | return source;
57 | }
58 |
59 | public static FormattedStringBuilder Italic(this FormattedStringBuilder source, bool skip = false)
60 | {
61 | if (skip)
62 | return source;
63 |
64 | source.Append("");
65 | source.PushToEnd("");
66 |
67 | return source;
68 | }
69 |
70 | public static FormattedStringBuilder Color(this FormattedStringBuilder source, string color, bool skip = false)
71 | {
72 | if (skip)
73 | return source;
74 |
75 | source.Append("");
78 |
79 | source.PushToEnd("");
80 |
81 | return source;
82 | }
83 |
84 | public static FormattedStringBuilder Aqua(this FormattedStringBuilder source, bool skip = false) => source.Color("aqua", skip);
85 | public static FormattedStringBuilder Black(this FormattedStringBuilder source, bool skip = false) => source.Color("black", skip);
86 | public static FormattedStringBuilder Blue(this FormattedStringBuilder source, bool skip = false) => source.Color("blue", skip);
87 | public static FormattedStringBuilder Brown(this FormattedStringBuilder source, bool skip = false) => source.Color("brown", skip);
88 | public static FormattedStringBuilder Cyan(this FormattedStringBuilder source, bool skip = false) => source.Color("cyan", skip);
89 | public static FormattedStringBuilder DarkBlue(this FormattedStringBuilder source, bool skip = false) => source.Color("darkblue", skip);
90 | public static FormattedStringBuilder Fuchsia(this FormattedStringBuilder source, bool skip = false) => source.Color("fuchsia", skip);
91 | public static FormattedStringBuilder Green(this FormattedStringBuilder source, bool skip = false) => source.Color("green", skip);
92 | public static FormattedStringBuilder Grey(this FormattedStringBuilder source, bool skip = false) => source.Color("grey", skip);
93 | public static FormattedStringBuilder LightBlue(this FormattedStringBuilder source, bool skip = false) => source.Color("lightblue", skip);
94 | public static FormattedStringBuilder Lime(this FormattedStringBuilder source, bool skip = false) => source.Color("lime", skip);
95 | public static FormattedStringBuilder Magenta(this FormattedStringBuilder source, bool skip = false) => source.Color("magenta", skip);
96 | public static FormattedStringBuilder Maroon(this FormattedStringBuilder source, bool skip = false) => source.Color("maroon", skip);
97 | public static FormattedStringBuilder Navy(this FormattedStringBuilder source, bool skip = false) => source.Color("navy", skip);
98 | public static FormattedStringBuilder Olive(this FormattedStringBuilder source, bool skip = false) => source.Color("olive", skip);
99 | public static FormattedStringBuilder Orange(this FormattedStringBuilder source, bool skip = false) => source.Color("orange", skip);
100 | public static FormattedStringBuilder Purple(this FormattedStringBuilder source, bool skip = false) => source.Color("purple", skip);
101 | public static FormattedStringBuilder Red(this FormattedStringBuilder source, bool skip = false) => source.Color("red", skip);
102 | public static FormattedStringBuilder Silver(this FormattedStringBuilder source, bool skip = false) => source.Color("silver", skip);
103 | public static FormattedStringBuilder Teal(this FormattedStringBuilder source, bool skip = false) => source.Color("teal", skip);
104 | public static FormattedStringBuilder White(this FormattedStringBuilder source, bool skip = false) => source.Color("white", skip);
105 | public static FormattedStringBuilder Yellow(this FormattedStringBuilder source, bool skip = false) => source.Color("yellow", skip);
106 | }
107 | }
--------------------------------------------------------------------------------
/RichTextExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: cde1b2a3a3bb4715b556f134bfdda658
3 | timeCreated: 1616931726
--------------------------------------------------------------------------------
/Skibitsky.Unity.StringFormatter.asmdef:
--------------------------------------------------------------------------------
1 | {
2 | "name": "Skibitsky.Unity.StringFormatter"
3 | }
4 |
--------------------------------------------------------------------------------
/Skibitsky.Unity.StringFormatter.asmdef.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 5ed40a3dd64f049eb862bb4c8fa18f8e
3 | AssemblyDefinitionImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "com.skibitsky.string-formatter",
3 | "version": "1.0.3",
4 | "displayName": "String Formatter",
5 | "description": "Format strings with HTML-like tags (Unity's Rich Text) in LINQ fashion",
6 | "repository": "skibitsky/string-formatter",
7 | "author": {
8 | "name": "Gleb Skibitsky",
9 | "email": "gleb@skibitsky.com",
10 | "url": "https://skibitsky.com"
11 | },
12 | "unity": "2019.3",
13 | "license": "MIT",
14 | "dependencies": {}
15 | }
--------------------------------------------------------------------------------
/package.json.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 4988b0caa7efa4fb2933e4225dbd58fc
3 | PackageManifestImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------