Skip to content

Commit 82302f1

Browse files
committed
new PadText method
1 parent e68e3ce commit 82302f1

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using JetBrains.Annotations;
2+
using SER.Code.ArgumentSystem.Arguments;
3+
using SER.Code.ArgumentSystem.BaseArguments;
4+
using SER.Code.Helpers.Exceptions;
5+
using SER.Code.Helpers.Extensions;
6+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
7+
using SER.Code.MethodSystem.MethodDescriptors;
8+
using SER.Code.ValueSystem;
9+
10+
namespace SER.Code.MethodSystem.Methods.TextMethods;
11+
12+
[UsedImplicitly]
13+
public class PadTextMethod : ReturningMethod<TextValue>, ICanError, IAdditionalDescription
14+
{
15+
public override string Description => "Fills the text from the left or right with the given character " +
16+
"until the specified length is met";
17+
18+
public string AdditionalDescription => "The \"character\" argument must have EXACTLY 1 character in it.";
19+
20+
public string[] ErrorReasons =>
21+
[
22+
"The \"character\" argument doesn't have EXACTLY 1 character in it."
23+
];
24+
25+
public override Argument[] ExpectedArguments { get; } =
26+
[
27+
new TextArgument("text to pad"),
28+
new OptionsArgument("pad direction",
29+
"left",
30+
"right"),
31+
new IntArgument("length"),
32+
new TextArgument("character")
33+
];
34+
35+
public override void Execute()
36+
{
37+
var text = Args.GetText("text to pad");
38+
var direction = Args.GetOption("pad direction");
39+
var length = Args.GetInt("length");
40+
var character = Args.GetText("character");
41+
42+
if (character.Length != 1)
43+
throw new ScriptRuntimeError(this, ErrorReasons[0]);
44+
45+
ReturnValue = (direction switch
46+
{
47+
"left" => text.PadLeft(length, character[0]),
48+
"right" => text.PadRight(length, character[0]),
49+
_ => throw new TosoksFuckedUpException("out of order")
50+
}).ToDynamicTextValue(Script);
51+
}
52+
}

0 commit comments

Comments
 (0)