|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.banner; |
| 7 | + |
| 8 | +import com.typesafe.config.Config; |
| 9 | +import io.jooby.Extension; |
| 10 | +import io.jooby.Jooby; |
| 11 | +import org.slf4j.Logger; |
| 12 | + |
| 13 | +import javax.annotation.Nonnull; |
| 14 | +import javax.inject.Provider; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +import static com.github.lalyos.jfiglet.FigletFont.convertOneLine; |
| 18 | +import static io.jooby.ServiceKey.key; |
| 19 | +import static java.util.Objects.requireNonNull; |
| 20 | + |
| 21 | +/** |
| 22 | + * <h1>banner</h1> |
| 23 | + * <p> |
| 24 | + * Prints out an ASCII art banner on startup using |
| 25 | + * <a href="https://github.com/lalyos/jfiglet">jfiglet</a>. |
| 26 | + * </p> |
| 27 | + * |
| 28 | + * <h2>usage</h2> |
| 29 | + * |
| 30 | + * <pre>{@code |
| 31 | + * package com.myapp; |
| 32 | + * |
| 33 | + * { |
| 34 | + * use(new Banner()); |
| 35 | + * } |
| 36 | + * }</pre> |
| 37 | + * |
| 38 | + * <p> |
| 39 | + * Prints out the value of <code>application.name</code> which here is <code>myapp</code>. Or you |
| 40 | + * can specify the text to prints out: |
| 41 | + * </p> |
| 42 | + * |
| 43 | + * <pre>{@code |
| 44 | + * package com.myapp; |
| 45 | + * |
| 46 | + * { |
| 47 | + * use(new Banner("my awesome app")); |
| 48 | + * } |
| 49 | + * }</pre> |
| 50 | + * |
| 51 | + * <h2>font</h2> |
| 52 | + * <p> |
| 53 | + * You can pick and use the font of your choice via {@link #font(String)} option: |
| 54 | + * </p> |
| 55 | + * |
| 56 | + * <pre>{@code |
| 57 | + * package com.myapp; |
| 58 | + * |
| 59 | + * { |
| 60 | + * use(new Banner("my awesome app").font("slant")); |
| 61 | + * } |
| 62 | + * }</pre> |
| 63 | + * |
| 64 | + * <p> |
| 65 | + * Font are distributed within the library inside the <code>/flf</code> classpath folder. A full |
| 66 | + * list of fonts is available <a href="http://patorjk.com/software/taag">here</a>. |
| 67 | + * </p> |
| 68 | + * |
| 69 | + * @author edgar |
| 70 | + * @since 2.0.0 |
| 71 | + */ |
| 72 | +public class BannerModule implements Extension { |
| 73 | + |
| 74 | + private static final String FONT = "classpath:/flf/%s.flf"; |
| 75 | + |
| 76 | + private String font = "speed"; |
| 77 | + |
| 78 | + private final Optional<String> text; |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates a new {@link BannerModule} with the given text. |
| 82 | + * |
| 83 | + * @param text Text to display. |
| 84 | + */ |
| 85 | + public BannerModule(final String text) { |
| 86 | + this.text = Optional.of(text); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Default banner, defined by <code>application.name</code>. |
| 91 | + */ |
| 92 | + public BannerModule() { |
| 93 | + this.text = Optional.empty(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void install(@Nonnull Jooby application) throws Exception { |
| 98 | + Logger log = application.getLog(); |
| 99 | + Config conf = application.getConfig(); |
| 100 | + String name = conf.getString("application.name"); |
| 101 | + String version = conf.getString("application.version"); |
| 102 | + String text = this.text.orElse(name); |
| 103 | + |
| 104 | + Provider<String> ascii = () -> { |
| 105 | + try { |
| 106 | + return rtrim(convertOneLine(fontPath(font), text)); |
| 107 | + } catch (Throwable t) { |
| 108 | + return text; |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + application.getServices().put(key(String.class, "application.banner"), ascii); |
| 113 | + |
| 114 | + application.onStarting(() -> log.info("\n{} v{}\n", ascii.get(), version)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Set/change default font (speed). |
| 119 | + * |
| 120 | + * @param font A font's name. |
| 121 | + * @return This module. |
| 122 | + */ |
| 123 | + public BannerModule font(final String font) { |
| 124 | + this.font = requireNonNull(font, "Font is required."); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + static String fontPath(String font) { |
| 129 | + return String.format(FONT, font); |
| 130 | + } |
| 131 | + |
| 132 | + static String rtrim(String s) { |
| 133 | + int i = s.length() - 1; |
| 134 | + while (i >= 0 && Character.isWhitespace(s.charAt(i))) { |
| 135 | + i--; |
| 136 | + } |
| 137 | + return s.substring(0, i + 1); |
| 138 | + } |
| 139 | +} |
0 commit comments