|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.jooby.banner; |
| 20 | + |
| 21 | +import static com.github.lalyos.jfiglet.FigletFont.convertOneLine; |
| 22 | +import static java.util.Objects.requireNonNull; |
| 23 | + |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import org.jooby.Env; |
| 27 | +import org.jooby.Jooby.Module; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import com.google.inject.Binder; |
| 32 | +import com.typesafe.config.Config; |
| 33 | + |
| 34 | +/** |
| 35 | + * <h1>banner</h1> |
| 36 | + * <p> |
| 37 | + * Prints out an ASCII art banner on startup using |
| 38 | + * <a href="https://github.com/lalyos/jfiglet">jfiglet</a>. |
| 39 | + * </p> |
| 40 | + * |
| 41 | + * <h2>usage</h2> |
| 42 | + * |
| 43 | + * <pre>{@code |
| 44 | + * package com.myapp; |
| 45 | + * |
| 46 | + * { |
| 47 | + * use(new Banner()); |
| 48 | + * } |
| 49 | + * }</pre> |
| 50 | + * |
| 51 | + * <p> |
| 52 | + * Prints out the value of <code>application.name</code> which here is <code>myapp</code>. Or you |
| 53 | + * can specify the text to prints out: |
| 54 | + * </p> |
| 55 | + * |
| 56 | + * <pre>{@code |
| 57 | + * package com.myapp; |
| 58 | + * |
| 59 | + * { |
| 60 | + * use(new Banner("my awesome app")); |
| 61 | + * } |
| 62 | + * }</pre> |
| 63 | + * |
| 64 | + * <h2>font</h2> |
| 65 | + * <p> |
| 66 | + * You can pick and use the font of your choice via {@link #font(String)} option: |
| 67 | + * </p> |
| 68 | + * |
| 69 | + * <pre>{@code |
| 70 | + * package com.myapp; |
| 71 | + * |
| 72 | + * { |
| 73 | + * use(new Banner("my awesome app").font("slant")); |
| 74 | + * } |
| 75 | + * }</pre> |
| 76 | + * |
| 77 | + * <p> |
| 78 | + * Font are distributed within the library inside the <code>/flf</code> classpath folder. A full |
| 79 | + * list of fonts is available <a href="http://patorjk.com/software/taag">here</a>. |
| 80 | + * </p> |
| 81 | + * |
| 82 | + * @author edgar |
| 83 | + * @since 1.0.0.CR3 |
| 84 | + */ |
| 85 | +public class Banner implements Module { |
| 86 | + |
| 87 | + private static final String FONT = "classpath:/flf/%s.flf"; |
| 88 | + |
| 89 | + private String font = "speed"; |
| 90 | + |
| 91 | + private final Optional<String> text; |
| 92 | + |
| 93 | + public Banner(final String text) { |
| 94 | + this.text = Optional.of(text); |
| 95 | + } |
| 96 | + |
| 97 | + public Banner() { |
| 98 | + this.text = Optional.empty(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void configure(final Env env, final Config conf, final Binder binder) { |
| 103 | + String name = conf.getString("application.name"); |
| 104 | + Logger log = LoggerFactory.getLogger(name); |
| 105 | + String v = conf.getString("application.version"); |
| 106 | + String text = this.text.orElse(name); |
| 107 | + |
| 108 | + env.onStart( |
| 109 | + () -> log.info("\n{} v{}\n", convertOneLine(String.format(FONT, font), text).trim(), v)); |
| 110 | + } |
| 111 | + |
| 112 | + public Banner font(final String font) { |
| 113 | + this.font = requireNonNull(font, "Font is required."); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments