fsk119 commented on code in PR #21786: URL: https://github.com/apache/flink/pull/21786#discussion_r1092736383
########## flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/Printer.java: ########## @@ -0,0 +1,252 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.client.cli; + +import org.apache.flink.api.common.JobID; +import org.apache.flink.configuration.ReadableConfig; +import org.apache.flink.table.api.ResultKind; +import org.apache.flink.table.client.gateway.ClientResult; +import org.apache.flink.table.client.gateway.ResultDescriptor; +import org.apache.flink.table.utils.print.PrintStyle; + +import org.jline.terminal.Terminal; +import org.jline.utils.InfoCmp; + +import java.io.Closeable; + +import static org.apache.flink.table.api.config.TableConfigOptions.TABLE_DML_SYNC; +import static org.apache.flink.table.client.cli.CliStrings.MESSAGE_EXECUTE_STATEMENT; +import static org.apache.flink.table.client.cli.CliStrings.MESSAGE_FINISH_STATEMENT; +import static org.apache.flink.table.client.cli.CliStrings.MESSAGE_STATEMENT_SUBMITTED; +import static org.apache.flink.table.client.cli.CliStrings.MESSAGE_SUBMITTING_STATEMENT; + +/** Printer to print the results to the terminal. */ +public interface Printer extends Closeable { + + /** Flag to determine whether to stop the execution. */ + boolean isQuitCommand(); + + /** Print the results to the terminal. */ + void print(Terminal terminal); + + /** Close the resource of the {@link Printer}. */ + void close(); + + // -------------------------------------------------------------------------------------------- + + static ClearCommandPrinter createClearCommandPrinter() { + return ClearCommandPrinter.INSTANCE; + } + + static QuitCommandPrinter createQuitCommandPrinter() { + return QuitCommandPrinter.INSTANCE; + } + + static HelpCommandPrinter createHelpCommandPrinter() { + return HelpCommandPrinter.INSTANCE; + } + + static ClientResultPrinter createClientResultPrinter( + ClientResult result, ReadableConfig sessionConfig) { + return new ClientResultPrinter(result, sessionConfig); + } + + static InitializationResultPrinter createInitializationResultPrinter() { + return InitializationResultPrinter.INSTANCE; + } + + // -------------------------------------------------------------------------------------------- + // Printers + // -------------------------------------------------------------------------------------------- + + /** Printer to print the HELP results. */ + class HelpCommandPrinter implements Printer { + + private static final HelpCommandPrinter INSTANCE = new HelpCommandPrinter(); + + @Override + public boolean isQuitCommand() { + return false; + } + + @Override + public void print(Terminal terminal) { + terminal.writer().println(CliStrings.MESSAGE_HELP); + terminal.flush(); + } + + @Override + public void close() {} + } + + /** Printer to print the QUIT messages. */ + class QuitCommandPrinter implements Printer { + + private static final QuitCommandPrinter INSTANCE = new QuitCommandPrinter(); + + @Override + public boolean isQuitCommand() { + return true; + } + + @Override + public void print(Terminal terminal) { + printInfo(terminal, CliStrings.MESSAGE_QUIT); + } + + @Override + public void close() {} + } + + /** Printer to clear the terminal. */ + class ClearCommandPrinter implements Printer { + + private static final ClearCommandPrinter INSTANCE = new ClearCommandPrinter(); + + @Override + public boolean isQuitCommand() { + return false; + } + + @Override + public void print(Terminal terminal) { + if (TerminalUtils.isPlainTerminal(terminal)) { + for (int i = 0; i < 200; i++) { // large number of empty lines + terminal.writer().println(); + } + } else { + terminal.puts(InfoCmp.Capability.clear_screen); + } + } + + @Override + public void close() {} + } + + /** Printer prints the initialization results. */ + class InitializationResultPrinter implements Printer { + + static final InitializationResultPrinter INSTANCE = new InitializationResultPrinter(); + + private InitializationResultPrinter() {} + + @Override + public boolean isQuitCommand() { + return false; + } + + @Override + public void print(Terminal terminal) { + printInfo(terminal, MESSAGE_EXECUTE_STATEMENT); + } + + @Override + public void close() {} + } + + /** Printer prints the statement results. */ + class ClientResultPrinter implements Printer { Review Comment: I rename to `StatementCommandPrinter` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org