Skip to content

Commit

Permalink
Finalize v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapral67 committed Dec 15, 2023
1 parent e6468ec commit d570017
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ void main (final String[] args) {
int ordinal = -1;
final Commands command;
while (ordinal < 0 || ordinal >= Commands.values().length) {
Logger.custom("Please Choose A Command:", Ansi.BOLD, Ansi.BLUE);
Logger.customLine("Please Choose A Command:", Ansi.BOLD, Ansi.BLUE);
for (final Commands cmd : Commands.values()) {
Logger.custom("%d) %s".formatted(cmd.ordinal(), cmd.name()));
Logger.customLine("%d) %s".formatted(cmd.ordinal(), cmd.name()));
}
final String token = scanner.nextLine()
.trim();
try {
ordinal = Integer.parseInt(token);
} catch (final NumberFormatException e) {
} catch (final NumberFormatException ignored) {
ordinal = -1;
}
if (ordinal < 0 || ordinal >= Commands.values().length) {
Expand All @@ -58,14 +58,14 @@ void main (final String[] args) {
ordinal = -1;
while (ordinal < 0 || ordinal >= command.options()
.size()) {
Logger.custom("Please Choose An Option:", Ansi.BOLD, Ansi.BLUE);
Logger.customLine("Please Choose An Option:", Ansi.BOLD, Ansi.BLUE);
command.options()
.forEach(e -> Logger.custom("%d) %s".formatted(e.ordinal(), e.name())));
.forEach(e -> Logger.customLine("%d) %s".formatted(e.ordinal(), e.name())));
final String token = scanner.nextLine()
.trim();
try {
ordinal = Integer.parseInt(token);
} catch (final NumberFormatException e) {
} catch (final NumberFormatException ignored) {
ordinal = -1;
}
if (ordinal < 0 || ordinal >= command.options()
Expand Down Expand Up @@ -95,5 +95,4 @@ void main (final String[] args) {
Logger.error(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
Logger.warn("%s may contain A-Z a-z - _ or '".formatted(param.jsonFieldName()));
Logger.info("_ & - characters result in the immediate succeeding character being capitalized");
Logger.info("_ characters are removed, useful for names like McDonald (input: mc_donald)");
Logger.custom("[Required] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Required] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
memberBuilder.firstName(this.scanner()
.nextLine());
System.out.println();
Expand All @@ -109,7 +109,7 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
Logger.warn("%s may contain A-Z a-z - _ or '".formatted(param.jsonFieldName()));
Logger.info("_ & - characters result in the immediate succeeding character being capitalized");
Logger.info("_ characters are removed, useful for names like McDonald (input: mc_donald)");
Logger.custom("[Optional] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Optional] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
final String middleName = this.scanner()
.nextLine()
.trim();
Expand All @@ -122,7 +122,7 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
Logger.warn("%s may contain A-Z a-z - _ or '".formatted(param.jsonFieldName()));
Logger.info("_ & - characters result in the immediate succeeding character being capitalized");
Logger.info("_ characters are removed, useful for names like McDonald (input: mc_donald)");
Logger.custom("[Required] Please Enter %s:%n".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Required] Please Enter %s:%n".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
memberBuilder.lastName(this.scanner()
.nextLine());
System.out.println();
Expand All @@ -131,16 +131,16 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
int ordinal = -1;
final SuffixType suffix;
while (ordinal < 0 || ordinal >= SuffixType.values().length) {
Logger.custom("[Optional] Please Choose A Suffix:", Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Optional] Please Choose A Suffix:", Ansi.BOLD, Ansi.BLUE);
for (final SuffixType sfx : SuffixType.values()) {
Logger.custom("%d) %s".formatted(sfx.ordinal(), sfx.name()));
Logger.customLine("%d) %s".formatted(sfx.ordinal(), sfx.name()));
}
final String token = this.scanner()
.nextLine()
.trim();
try {
ordinal = Integer.parseInt(token);
} catch (final NumberFormatException e) {
} catch (final NumberFormatException ignored) {
if (!token.isEmpty()) {
ordinal = -1;
} else {
Expand All @@ -159,15 +159,15 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
}
case BIRTHDAY -> {
Logger.warn("%s must be formatted like yyyy-MM-dd (e.g. 1970-12-31 -> Dec. 31, 1970)".formatted(param.jsonFieldName()));
Logger.custom("[Required] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Required] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
memberBuilder.birthday(Member.convertStringToDate(this.scanner()
.nextLine()
.trim()));
System.out.println();
}
case DEATHDAY -> {
Logger.warn("%s must be formatted like yyyy-MM-dd (e.g. 1970-12-31 -> Dec. 31, 1970)".formatted(param.jsonFieldName()));
Logger.custom("[Optional] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Optional] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
final String deathDay = this.scanner()
.nextLine()
.trim();
Expand All @@ -178,7 +178,7 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
}
}
case EMAIL -> {
Logger.custom("[Optional] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Optional] Please Enter %s:".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
final String email = this.scanner()
.nextLine()
.trim();
Expand All @@ -188,7 +188,7 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
}
}
case PHONES -> {
Logger.custom("[Optional] Add %s to this Member? (y/N)".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Optional] Add %s to this Member? (y/N)".formatted(param.jsonFieldName()), Ansi.BOLD, Ansi.BLUE);
final String addPhone = this.scanner()
.nextLine()
.trim();
Expand All @@ -198,7 +198,7 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
Logger.warn("For International numbers: + and Country Code are required");
final Map<PhoneType, String> phones = new HashMap<>();
for (final PhoneType phoneType : PhoneType.values()) {
Logger.custom("[Optional] Please Enter %s Phone Number:".formatted(phoneType.name()), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Optional] Please Enter %s Phone Number:".formatted(phoneType.name()), Ansi.BOLD, Ansi.BLUE);
final String phone = this.scanner()
.nextLine()
.trim();
Expand All @@ -216,9 +216,9 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
final List<String> addressLines = new ArrayList<>();
for (int i = 1; i <= Member.REQ_NON_NULL_ADDRESS_SIZE; ++i) {
if (i == 1) {
Logger.custom("[Optional] Please Enter %s line %d:".formatted(param.jsonFieldName(), i), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Optional] Please Enter %s line %d:".formatted(param.jsonFieldName(), i), Ansi.BOLD, Ansi.BLUE);
} else {
Logger.custom("[Required] Please Enter %s line %d:".formatted(param.jsonFieldName(), i), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("[Required] Please Enter %s line %d:".formatted(param.jsonFieldName(), i), Ansi.BOLD, Ansi.BLUE);
}
final String addressLineText = this.scanner()
.nextLine()
Expand Down Expand Up @@ -249,18 +249,18 @@ MemberRecord getExistingMember (final @NotNull String message) {
final List<MemberRecord> records = MemberPicker.getEntries();
int index = -1;
while (index < 0 || index >= records.size()) {
Logger.custom(requireNonNull(message), Ansi.BOLD, Ansi.BLUE);
Logger.customLine(requireNonNull(message), Ansi.BOLD, Ansi.BLUE);
for (int i = 0; i < records.size(); ++i) {
Logger.custom("%d) %s".formatted(i, records.get(i)
.member()
.getFullName()));
Logger.customLine("%d) %s".formatted(i, records.get(i)
.member()
.getFullName()));
}
final String token = this.scanner()
.nextLine()
.trim();
try {
index = Integer.parseInt(token);
} catch (final NumberFormatException e) {
} catch (final NumberFormatException ignored) {
index = -1;
}
if (index < 0 || index >= records.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import software.amazon.awssdk.services.lambda.model.FunctionConfiguration;
import software.amazon.awssdk.services.lambda.model.ListEventSourceMappingsRequest;
import software.amazon.awssdk.services.lambda.model.UpdateEventSourceMappingRequest;
import static java.lang.System.currentTimeMillis;
import static java.lang.Thread.sleep;
import static java.util.Objects.requireNonNull;

public final
class TogglePdfGeneratorEvent implements EventHelper {
private static final long MILLIS_IN_SEC = 1000L;
private final @NotNull LambdaClient lambdaClient = LambdaClient.create();
private final @NotNull Scanner scanner;

Expand Down Expand Up @@ -63,44 +63,58 @@ void execute () {
displayCurrentState(isPdfGeneratorEnabled);
System.out.println();

Logger.custom("SWITCH %s? (y/N)".formatted(isPdfGeneratorEnabled
? "OFF"
: "ON"), Ansi.BOLD, Ansi.BLUE);
Logger.customLine("SWITCH %s? (y/N)".formatted(isPdfGeneratorEnabled
? "OFF"
: "ON"), Ansi.BOLD, Ansi.BLUE);

final String input = this.scanner.nextLine()
.trim();
if (input.equalsIgnoreCase("y")) {
final long startTime = currentTimeMillis();
{
this.lambdaClient.updateEventSourceMapping(UpdateEventSourceMappingRequest.builder()
.uuid(eventSourceMapping.uuid())
.enabled(!isPdfGeneratorEnabled)
.build());

displayCurrentState(!isPdfGeneratorEnabled);
}
final long sleepForMillis = (DdbUtils.DDB_STREAM_MAX_RECORD_AGE.toSeconds()
.longValue() * 1000L) - (currentTimeMillis() - startTime);
if (sleepForMillis > 0L) {
Logger.info("Please Wait For %d Seconds.".formatted(sleepForMillis / 1000L));
System.out.println();

int sleepSec = DdbUtils.DDB_STREAM_MAX_RECORD_AGE_SECONDS.intValue();
final int maxSleepSecLength = digitCount(sleepSec);
int maxCounterLength = 0;
// Only Wait When Switching PdfGenerator From OFF -> ON
while (!isPdfGeneratorEnabled && sleepSec > 0) {
final String counter = "Please Wait For %d Seconds.%s".formatted(sleepSec, " ".repeat(maxSleepSecLength - digitCount(sleepSec)));
if (maxCounterLength == 0 && digitCount(sleepSec) == maxSleepSecLength) {
maxCounterLength = counter.length();
}
Logger.custom("\r", counter, Ansi.BOLD, Ansi.CYAN);
try {
sleep(sleepForMillis);
} catch (final InterruptedException ignored) {
// Allow Signals to Interrupt Sleep
sleep(MILLIS_IN_SEC);
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
if (--sleepSec <= 0L) {
System.out.printf("\r%s\r", " ".repeat(maxCounterLength));
}
}

this.lambdaClient.updateEventSourceMapping(UpdateEventSourceMappingRequest.builder()
.uuid(eventSourceMapping.uuid())
.enabled(!isPdfGeneratorEnabled)
.build());

displayCurrentState(!isPdfGeneratorEnabled);
}
if (!input.isEmpty()) {
if (!input.isBlank()) {
System.out.println();
}
}

private static
int digitCount (final int n) {
return (int) (Math.log10(n) + 1.0);
}

private static
void displayCurrentState (final boolean isOn) {
if (isOn) {
Logger.custom("SWITCHED ON", Ansi.BOLD, Ansi.GREEN);
Logger.customLine("SWITCHED ON", Ansi.BOLD, Ansi.GREEN);
} else {
Logger.custom("SWITCHED OFF", Ansi.BOLD, Ansi.RED);
Logger.customLine("SWITCHED OFF", Ansi.BOLD, Ansi.RED);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class Logger implements LambdaLogger {
}

public static
void custom (final @Nullable String message, final Ansi... styles) {
custom(LogLevel.UNDEFINED, message, styles);
void customLine (final @Nullable String message, final Ansi... styles) {
customLine(LogLevel.UNDEFINED, message, styles);
}

public static
void custom (final @NotNull LogLevel logLevel, final @Nullable String message, final Ansi... styles)
void customLine (final @NotNull LogLevel logLevel, final @Nullable String message, final Ansi... styles)
{
final StringBuilder sb = new StringBuilder();
if (!logLevel.equals(LogLevel.UNDEFINED)) {
Expand All @@ -38,16 +38,36 @@ void custom (final @NotNull LogLevel logLevel, final @Nullable String message, f
}
}

public static
void custom (final @NotNull String prepended, final @Nullable String message, final Ansi... styles) {
custom(LogLevel.UNDEFINED, prepended, message, styles);
}

public static
void custom (final @NotNull LogLevel logLevel, final @NotNull String prepended, final @Nullable String message, final Ansi... styles)
{
final StringBuilder sb = new StringBuilder();
if (!logLevel.equals(LogLevel.UNDEFINED)) {
sb.append("[%s] ".formatted(logLevel.name()));
}
if (nonNull(message)) {
sb.append("%s".formatted(message));
}
if (!sb.isEmpty()) {
System.out.printf("%s%s", prepended, BananaUtils.bananansi(sb.toString(), Font.TERM, styles));
}
}

@Override
public
void log (final String message) {
custom(LogLevel.UNDEFINED, message);
customLine(LogLevel.UNDEFINED, message);
}

@Override
public
void log (final byte[] message) {
custom(LogLevel.UNDEFINED, Arrays.toString(message));
customLine(LogLevel.UNDEFINED, Arrays.toString(message));
}

@Override
Expand All @@ -73,32 +93,32 @@ void log (final @Nullable String message, final @Nullable LogLevel logLevel) {

public static
void fatal (final @Nullable String fatal) {
custom(LogLevel.FATAL, fatal, Ansi.BLACK, Ansi.BG_RED);
customLine(LogLevel.FATAL, fatal, Ansi.BLACK, Ansi.BG_RED);
}

public static
void error (final @Nullable String error) {
custom(LogLevel.ERROR, error, Ansi.RED);
customLine(LogLevel.ERROR, error, Ansi.RED);
}

public static
void warn (final @Nullable String warn) {
custom(LogLevel.WARN, warn, Ansi.YELLOW);
customLine(LogLevel.WARN, warn, Ansi.YELLOW);
}

public static
void info (final @Nullable String info) {
custom(LogLevel.INFO, info, Ansi.CYAN);
customLine(LogLevel.INFO, info, Ansi.CYAN);
}

public static
void debug (final @Nullable String debug) {
custom(LogLevel.DEBUG, debug, Ansi.GREEN);
customLine(LogLevel.DEBUG, debug, Ansi.GREEN);
}

public static
void trace (final @Nullable String trace) {
custom(LogLevel.TRACE, trace, Ansi.FAINT);
customLine(LogLevel.TRACE, trace, Ansi.FAINT);
}

@Override
Expand Down
Loading

0 comments on commit d570017

Please sign in to comment.