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