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.top;
019
020import edu.umd.cs.findbugs.annotations.Nullable;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024import java.util.Objects;
025import java.util.function.Function;
026import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
027import org.apache.yetus.audience.InterfaceAudience;
028
029
030/**
031 * The presentation logic for the input mode.
032 */
033@InterfaceAudience.Private
034public class InputModeScreenPresenter {
035  private final InputModeScreenView inputModeScreenView;
036  private final String message;
037  private final List<String> histories;
038  private final Function<String, ScreenView> resultListener;
039
040  private StringBuilder inputString = new StringBuilder();
041  private int cursorPosition;
042  private int historyPosition = -1;
043
044  public InputModeScreenPresenter(InputModeScreenView inputModeScreenView, String message,
045    @Nullable List<String> histories, Function<String, ScreenView> resultListener) {
046    this.inputModeScreenView = Objects.requireNonNull(inputModeScreenView);
047    this.message = Objects.requireNonNull(message);
048
049    if (histories != null) {
050      this.histories = Collections.unmodifiableList(new ArrayList<>(histories));
051    } else {
052      this.histories = Collections.emptyList();
053    }
054
055    this.resultListener = Objects.requireNonNull(resultListener);
056  }
057
058  public void init() {
059    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
060    inputModeScreenView.refreshTerminal();
061  }
062
063  public ScreenView returnToNextScreen() {
064    inputModeScreenView.hideCursor();
065    String result = inputString.toString();
066
067    return resultListener.apply(result);
068  }
069
070  public void character(Character character) {
071    inputString.insert(cursorPosition, character);
072    cursorPosition += 1;
073    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
074    inputModeScreenView.refreshTerminal();
075  }
076
077  public void backspace() {
078    if (cursorPosition == 0) {
079      return;
080    }
081
082    inputString.deleteCharAt(cursorPosition - 1);
083    cursorPosition -= 1;
084    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
085    inputModeScreenView.refreshTerminal();
086  }
087
088  public void delete() {
089    if (inputString.length() == 0 || cursorPosition > inputString.length() - 1) {
090      return;
091    }
092
093    inputString.deleteCharAt(cursorPosition);
094    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
095    inputModeScreenView.refreshTerminal();
096  }
097
098  public void arrowLeft() {
099    if (cursorPosition == 0) {
100      return;
101    }
102
103    cursorPosition -= 1;
104    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
105    inputModeScreenView.refreshTerminal();
106  }
107
108  public void arrowRight() {
109    if (cursorPosition > inputString.length() - 1) {
110      return;
111    }
112
113    cursorPosition += 1;
114    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
115    inputModeScreenView.refreshTerminal();
116  }
117
118  public void home() {
119    cursorPosition = 0;
120    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
121    inputModeScreenView.refreshTerminal();
122  }
123
124  public void end() {
125    cursorPosition = inputString.length();
126    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
127    inputModeScreenView.refreshTerminal();
128  }
129
130  public void arrowUp() {
131    if (historyPosition == 0 || histories.isEmpty()) {
132      return;
133    }
134
135    if (historyPosition == -1) {
136      historyPosition = histories.size() - 1;
137    } else {
138      historyPosition -= 1;
139    }
140
141    inputString = new StringBuilder(histories.get(historyPosition));
142
143    cursorPosition = inputString.length();
144    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
145    inputModeScreenView.refreshTerminal();
146  }
147
148  public void arrowDown() {
149    if (historyPosition == -1 || histories.isEmpty()) {
150      return;
151    }
152
153    if (historyPosition == histories.size() - 1) {
154      historyPosition = -1;
155      inputString = new StringBuilder();
156    } else {
157      historyPosition += 1;
158      inputString = new StringBuilder(histories.get(historyPosition));
159    }
160
161    cursorPosition = inputString.length();
162    inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
163    inputModeScreenView.refreshTerminal();
164  }
165}