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.terminal.impl;
19  
20  import java.util.Objects;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.hbtop.terminal.AbstractTerminalPrinter;
24  import org.apache.hadoop.hbase.hbtop.terminal.Attributes;
25  import org.apache.hadoop.hbase.hbtop.terminal.Color;
26  import org.apache.hadoop.hbase.hbtop.terminal.TerminalPrinter;
27  
28  /**
29   * An implementation of the {@link TerminalPrinter} interface for normal display mode.
30   */
31  @InterfaceAudience.Private
32  public class TerminalPrinterImpl extends AbstractTerminalPrinter {
33    private final ScreenBuffer screenBuffer;
34    private int row;
35    private int column;
36  
37    private final Attributes attributes = new Attributes();
38  
39    TerminalPrinterImpl(ScreenBuffer screenBuffer, int startRow) {
40      this.screenBuffer = Objects.requireNonNull(screenBuffer);
41      this.row = startRow;
42    }
43  
44    @Override
45    public TerminalPrinter print(String value) {
46      screenBuffer.putString(column, row, value, attributes);
47      column += value.length();
48      return this;
49    }
50  
51    @Override
52    public TerminalPrinter startHighlight() {
53      attributes.setForegroundColor(Color.BLACK);
54      attributes.setBackgroundColor(Color.WHITE);
55      return this;
56    }
57  
58    @Override
59    public TerminalPrinter stopHighlight() {
60      attributes.setForegroundColor(Color.WHITE);
61      attributes.setBackgroundColor(Color.BLACK);
62      return this;
63    }
64  
65    @Override
66    public TerminalPrinter startBold() {
67      attributes.setBold(true);
68      return this;
69    }
70  
71    @Override
72    public TerminalPrinter stopBold() {
73      attributes.setBold(false);
74      return this;
75    }
76  
77    @Override
78    public void endOfLine() {
79      screenBuffer.endOfLine(column, row);
80      row += 1;
81      column = 0;
82    }
83  }