<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.jreadline.console;

import org.jboss.jreadline.console.settings.Settings;

import java.util.Arrays;
import java.util.List;

/**
 * A simple buffer to keep track of one line in the console
 * and the cursor position.
 *
 * @author <NAME> <<EMAIL>>
 */
public class Buffer {

    private int cursor = 0;
    private StringBuilder line;
    private String prompt;
    private int delta; //need to keep track of a delta for ansi terminal
    private Character mask;

    private final static int TAB = 4;

    protected Buffer() {
        this(null, null);
    }

    /**
     * Instantiate a Buffer with given prompt
     *
     * @param promptText set prompt
     */
    protected Buffer(String promptText) {
        this(promptText, null);
    }

    protected Buffer(String promptText, Character mask) {
        if(promptText != null)
            prompt = promptText;
        else
            prompt = "";

        line = new StringBuilder();
        delta = 0;
        this.mask = mask;
    }

    /**
     * Reset the buffer
     *
     * @param promptText set prompt
     */
    protected void reset(String promptText) {
        reset(promptText, null);
    }

    protected void reset(String promptText, Character mask) {
        if(promptText != null)
            prompt = promptText;
        else
            prompt = "";
        cursor = 0;
        line = new StringBuilder();
        delta = 0;
        this.mask = mask;
    }

    /**
     * @return length of the line without prompt
     */
    protected int length() {
        return line.length();
    }

    protected int totalLength() {
        return line.length() + prompt.length()+1;
    }

    protected int getCursor() {
        return cursor;
    }

    protected int getCursorWithPrompt() {
        return getCursor() + prompt.length()+1;
    }

    protected String getPrompt() {
        return prompt;
    }

    protected void setCursor(int cursor) {
        this.cursor = cursor ;
    }

    /**
     * Move the cursor left if the param is negative,
     * and right if its positive.
     * Return ansi code to represent the move
     *
     * @param move where to move
     * @param termWidth terminal width
     * @return ansi string that represent the move
     */<commit_msg>Added diablePrompt which disables prompt from calculations when finding cursor position. needed for search.<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.jreadline.console;

import org.jboss.jreadline.console.settings.Settings;

import java.util.Arrays;
import java.util.List;

/**
 * A simple buffer to keep track of one line in the console
 * and the cursor position.
 *
 * @author <NAME> <<EMAIL>>
 */
public class Buffer {

    private int cursor = 0;
    private StringBuilder line;
    private String prompt;
    private int delta; //need to keep track of a delta for ansi terminal
    private Character mask;
    private boolean disablePrompt = false;

    private final static int TAB = 4;

    protected Buffer() {
        this(null, null);
    }

    /**
     * Instantiate a Buffer with given prompt
     *
     * @param promptText set prompt
     */
    protected Buffer(String promptText) {
        this(promptText, null);
    }

    protected Buffer(String promptText, Character mask) {
        if(promptText != null)
            prompt = promptText;
        else
            prompt = "";

        line = new StringBuilder();
        delta = 0;
        this.mask = mask;
    }

    /**
     * Reset the buffer
     *
     * @param promptText set prompt
     */
    protected void reset(String promptText) {
        reset(promptText, null);
    }

    protected void reset(String promptText, Character mask) {
        if(promptText != null)
            prompt = promptText;
        else
            prompt = "";
        cursor = 0;
        line = new StringBuilder();
        delta = 0;
        this.mask = mask;
    }

    /**
     * @return length of the line without prompt
     */
    protected int length() {
        return line.length();
    }

    protected int totalLength() {
        if(disablePrompt)
            return line.length()+1;
        else
            return line.length() + prompt.length()+1;
    }

    protected int getCursor() {
        return cursor;
    }

    protected int getCursorWithPrompt() {
        if(disablePrompt)
            return getCursor()+1;
        else
            return getCursor() + prompt.length()+1;
    }

    protected String getPrompt() {
        return prompt;
    }

    protected void setCursor(int cursor) {
        this.cursor = cursor ;
    }

    /**
     * Need to disable prompt in calculations involving search.
     *
     * @param disable prompt or not
     */
    protected void disablePrompt(boolean disable) {
        disablePrompt = disable;
    }

    /**
     * Move the cursor left if the param is negative,
     * and right if its positive.
     * Return ansi code to represent the move
     *
     * @param move where to move
     * @param termWidth terminal width
     * @return ansi string that represent the move
     */
{'fraction_non_alphanumeric': 0.08373421527360192, 'fraction_numerical': 0.004810583283223091, 'mean_word_length': 2.308304326205868, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2763757', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1708, 'n_words': 686}