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.mode;
019
020import java.util.List;
021import java.util.function.Consumer;
022import org.apache.hadoop.hbase.hbtop.mode.Mode;
023import org.apache.hadoop.hbase.hbtop.screen.AbstractScreenView;
024import org.apache.hadoop.hbase.hbtop.screen.Screen;
025import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
026import org.apache.hadoop.hbase.hbtop.terminal.KeyPress;
027import org.apache.hadoop.hbase.hbtop.terminal.Terminal;
028import org.apache.hadoop.hbase.hbtop.terminal.TerminalPrinter;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * The screen where we can choose the {@link Mode} in the top screen.
033 */
034@InterfaceAudience.Private
035public class ModeScreenView extends AbstractScreenView {
036
037  private static final int SCREEN_DESCRIPTION_START_ROW = 0;
038  private static final int MODE_START_ROW = 4;
039
040  private final ModeScreenPresenter modeScreenPresenter;
041
042  public ModeScreenView(Screen screen, Terminal terminal, Mode currentMode,
043    Consumer<Mode> resultListener, ScreenView nextScreenView) {
044    super(screen, terminal);
045    this.modeScreenPresenter =
046      new ModeScreenPresenter(this, currentMode, resultListener, nextScreenView);
047  }
048
049  @Override
050  public void init() {
051    modeScreenPresenter.init();
052  }
053
054  @Override
055  public ScreenView handleKeyPress(KeyPress keyPress) {
056    switch (keyPress.getType()) {
057      case Escape:
058        return modeScreenPresenter.transitionToNextScreen(false);
059
060      case Enter:
061        return modeScreenPresenter.transitionToNextScreen(true);
062
063      case ArrowUp:
064        modeScreenPresenter.arrowUp();
065        return this;
066
067      case ArrowDown:
068        modeScreenPresenter.arrowDown();
069        return this;
070
071      case PageUp:
072      case Home:
073        modeScreenPresenter.pageUp();
074        return this;
075
076      case PageDown:
077      case End:
078        modeScreenPresenter.pageDown();
079        return this;
080
081      default:
082        // Do nothing
083        break;
084    }
085
086    if (keyPress.getType() != KeyPress.Type.Character) {
087      return this;
088    }
089
090    assert keyPress.getCharacter() != null;
091    switch (keyPress.getCharacter()) {
092      case 'q':
093        return modeScreenPresenter.transitionToNextScreen(false);
094
095      default:
096        // Do nothing
097        break;
098    }
099
100    return this;
101  }
102
103  public void showModeScreen(Mode currentMode, List<Mode> modes, int currentPosition,
104    int modeHeaderMaxLength, int modeDescriptionMaxLength) {
105    showScreenDescription(currentMode);
106
107    for (int i = 0; i < modes.size(); i++) {
108      showMode(i, modes.get(i), i == currentPosition, modeHeaderMaxLength,
109        modeDescriptionMaxLength);
110    }
111  }
112
113  private void showScreenDescription(Mode currentMode) {
114    TerminalPrinter printer = getTerminalPrinter(SCREEN_DESCRIPTION_START_ROW);
115    printer.startBold().print("Mode Management").stopBold().endOfLine();
116    printer.print("Current mode: ").startBold().print(currentMode.getHeader()).stopBold()
117      .endOfLine();
118    printer.print("Select mode followed by <Enter>").endOfLine();
119  }
120
121  public void showMode(int pos, Mode mode, boolean selected, int modeHeaderMaxLength,
122    int modeDescriptionMaxLength) {
123
124    String modeHeader = String.format("%-" + modeHeaderMaxLength + "s", mode.getHeader());
125    String modeDescription =
126      String.format("%-" + modeDescriptionMaxLength + "s", mode.getDescription());
127
128    int row = MODE_START_ROW + pos;
129    TerminalPrinter printer = getTerminalPrinter(row);
130    if (selected) {
131      printer.startHighlight().print(modeHeader).stopHighlight()
132        .printFormat(" = %s", modeDescription).endOfLine();
133    } else {
134      printer.printFormat("%s = %s", modeHeader, modeDescription).endOfLine();
135    }
136  }
137}