001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop.screen.help;
019
020import org.apache.hadoop.hbase.hbtop.screen.AbstractScreenView;
021import org.apache.hadoop.hbase.hbtop.screen.Screen;
022import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
023import org.apache.hadoop.hbase.hbtop.terminal.KeyPress;
024import org.apache.hadoop.hbase.hbtop.terminal.Terminal;
025import org.apache.hadoop.hbase.hbtop.terminal.TerminalPrinter;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * The help screen.
030 */
031@InterfaceAudience.Private
032public class HelpScreenView extends AbstractScreenView {
033
034  private static final int SCREEN_DESCRIPTION_START_ROW = 0;
035  private static final int COMMAND_DESCRIPTION_START_ROW = 3;
036
037  private final HelpScreenPresenter helpScreenPresenter;
038
039  public HelpScreenView(Screen screen, Terminal terminal, long refreshDelay,
040    ScreenView nextScreenView) {
041    super(screen, terminal);
042    this.helpScreenPresenter = new HelpScreenPresenter(this, refreshDelay, nextScreenView);
043  }
044
045  @Override
046  public void init() {
047    helpScreenPresenter.init();
048  }
049
050  @Override
051  public ScreenView handleKeyPress(KeyPress keyPress) {
052    return helpScreenPresenter.transitionToNextScreen();
053  }
054
055  public void showHelpScreen(long refreshDelay, CommandDescription[] commandDescriptions) {
056    showScreenDescription(refreshDelay);
057
058    TerminalPrinter printer = getTerminalPrinter(COMMAND_DESCRIPTION_START_ROW);
059    for (CommandDescription commandDescription : commandDescriptions) {
060      showCommandDescription(printer, commandDescription);
061    }
062
063    printer.endOfLine();
064    printer.print("Press any key to continue").endOfLine();
065  }
066
067  private void showScreenDescription(long refreshDelay) {
068    TerminalPrinter printer = getTerminalPrinter(SCREEN_DESCRIPTION_START_ROW);
069    printer.startBold().print("Help for Interactive Commands").stopBold().endOfLine();
070    printer.print("Refresh delay: ").startBold().print((double) refreshDelay / 1000).stopBold()
071      .endOfLine();
072  }
073
074  private void showCommandDescription(TerminalPrinter terminalPrinter,
075    CommandDescription commandDescription) {
076    terminalPrinter.print("  ");
077    boolean first = true;
078    for (String key : commandDescription.getKeys()) {
079      if (first) {
080        first = false;
081      } else {
082        terminalPrinter.print(",");
083      }
084      terminalPrinter.startBold().print(key).stopBold();
085    }
086
087    terminalPrinter.printFormat(": %s", commandDescription.getDescription()).endOfLine();
088  }
089}