View Javadoc

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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.hbtop.screen.help;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.hbtop.screen.AbstractScreenView;
22  import org.apache.hadoop.hbase.hbtop.screen.Screen;
23  import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
24  import org.apache.hadoop.hbase.hbtop.terminal.KeyPress;
25  import org.apache.hadoop.hbase.hbtop.terminal.Terminal;
26  import org.apache.hadoop.hbase.hbtop.terminal.TerminalPrinter;
27  
28  /**
29   * The help screen.
30   */
31  @InterfaceAudience.Private
32  public class HelpScreenView extends AbstractScreenView {
33  
34    private static final int SCREEN_DESCRIPTION_START_ROW = 0;
35    private static final int COMMAND_DESCRIPTION_START_ROW = 3;
36  
37    private final HelpScreenPresenter helpScreenPresenter;
38  
39    public HelpScreenView(Screen screen, Terminal terminal, long refreshDelay,
40      ScreenView nextScreenView) {
41      super(screen, terminal);
42      this.helpScreenPresenter = new HelpScreenPresenter(this, refreshDelay, nextScreenView);
43    }
44  
45    @Override
46    public void init() {
47      helpScreenPresenter.init();
48    }
49  
50    @Override
51    public ScreenView handleKeyPress(KeyPress keyPress) {
52      return helpScreenPresenter.transitionToNextScreen();
53    }
54  
55    public void showHelpScreen(long refreshDelay, CommandDescription[] commandDescriptions) {
56      showScreenDescription(refreshDelay);
57  
58      TerminalPrinter printer = getTerminalPrinter(COMMAND_DESCRIPTION_START_ROW);
59      for (CommandDescription commandDescription : commandDescriptions) {
60        showCommandDescription(printer, commandDescription);
61      }
62  
63      printer.endOfLine();
64      printer.print("Press any key to continue").endOfLine();
65    }
66  
67    private void showScreenDescription(long refreshDelay) {
68      TerminalPrinter printer = getTerminalPrinter(SCREEN_DESCRIPTION_START_ROW);
69      printer.startBold().print("Help for Interactive Commands").stopBold().endOfLine();
70      printer.print("Refresh delay: ").startBold()
71        .print((double) refreshDelay / 1000).stopBold().endOfLine();
72    }
73  
74    private void showCommandDescription(TerminalPrinter terminalPrinter,
75      CommandDescription commandDescription) {
76      terminalPrinter.print("  ");
77      boolean first = true;
78      for (String key : commandDescription.getKeys()) {
79        if (first) {
80          first = false;
81        } else {
82          terminalPrinter.print(",");
83        }
84        terminalPrinter.startBold().print(key).stopBold();
85      }
86  
87      terminalPrinter.printFormat(": %s", commandDescription.getDescription()).endOfLine();
88    }
89  }