src/jdk.internal.le/share/classes/jdk/internal/jline/console/CursorBuffer.java
branchJDK-8200758-branch
changeset 57072 29604aafa0fc
parent 57071 94e9270166f0
parent 52979 7384e00d5860
child 57076 687505381ca4
equal deleted inserted replaced
57071:94e9270166f0 57072:29604aafa0fc
     1 /*
       
     2  * Copyright (c) 2002-2016, the original author or authors.
       
     3  *
       
     4  * This software is distributable under the BSD license. See the terms of the
       
     5  * BSD license in the documentation provided with this software.
       
     6  *
       
     7  * http://www.opensource.org/licenses/bsd-license.php
       
     8  */
       
     9 package jdk.internal.jline.console;
       
    10 
       
    11 import static jdk.internal.jline.internal.Preconditions.checkNotNull;
       
    12 
       
    13 /**
       
    14  * A holder for a {@link StringBuilder} that also contains the current cursor position.
       
    15  *
       
    16  * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
       
    17  * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
       
    18  * @since 2.0
       
    19  */
       
    20 public class CursorBuffer
       
    21 {
       
    22     private boolean overTyping = false;
       
    23 
       
    24     public int cursor = 0;
       
    25 
       
    26     public final StringBuilder buffer = new StringBuilder();
       
    27 
       
    28     public CursorBuffer copy () {
       
    29         CursorBuffer that = new CursorBuffer();
       
    30         that.overTyping = this.overTyping;
       
    31         that.cursor = this.cursor;
       
    32         that.buffer.append (this.toString());
       
    33 
       
    34         return that;
       
    35     }
       
    36 
       
    37     public boolean isOverTyping() {
       
    38         return overTyping;
       
    39     }
       
    40 
       
    41     public void setOverTyping(final boolean b) {
       
    42         overTyping = b;
       
    43     }
       
    44 
       
    45     public int length() {
       
    46         return buffer.length();
       
    47     }
       
    48 
       
    49     public char nextChar() {
       
    50         if (cursor == buffer.length()) {
       
    51             return 0;
       
    52         } else {
       
    53             return buffer.charAt(cursor);
       
    54         }
       
    55     }
       
    56 
       
    57     public char current() {
       
    58         if (cursor <= 0) {
       
    59             return 0;
       
    60         }
       
    61 
       
    62         return buffer.charAt(cursor - 1);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Write the specific character into the buffer, setting the cursor position
       
    67      * ahead one. The text may overwrite or insert based on the current setting
       
    68      * of {@link #isOverTyping}.
       
    69      *
       
    70      * @param c the character to insert
       
    71      */
       
    72     public void write(final char c) {
       
    73         buffer.insert(cursor++, c);
       
    74         if (isOverTyping() && cursor < buffer.length()) {
       
    75             buffer.deleteCharAt(cursor);
       
    76         }
       
    77     }
       
    78 
       
    79     /**
       
    80      * Insert the specified chars into the buffer, setting the cursor to the end of the insertion point.
       
    81      */
       
    82     public void write(final CharSequence str) {
       
    83         checkNotNull(str);
       
    84 
       
    85         if (buffer.length() == 0) {
       
    86             buffer.append(str);
       
    87         }
       
    88         else {
       
    89             buffer.insert(cursor, str);
       
    90         }
       
    91 
       
    92         cursor += str.length();
       
    93 
       
    94         if (isOverTyping() && cursor < buffer.length()) {
       
    95             buffer.delete(cursor, cursor + str.length());
       
    96         }
       
    97     }
       
    98 
       
    99     public boolean clear() {
       
   100         if (buffer.length() == 0) {
       
   101             return false;
       
   102         }
       
   103 
       
   104         buffer.delete(0, buffer.length());
       
   105         cursor = 0;
       
   106         return true;
       
   107     }
       
   108 
       
   109     public String upToCursor() {
       
   110         if (cursor <= 0) {
       
   111             return "";
       
   112         }
       
   113 
       
   114         return buffer.substring(0, cursor);
       
   115     }
       
   116 
       
   117     @Override
       
   118     public String toString() {
       
   119         return buffer.toString();
       
   120     }
       
   121 }