#How would I hide a specific Character in a ListArray

38 messages · Page 1 of 1 (latest)

proven oriole
#

Basically, i'm making a simple terminal file editor, which has basic syntax highlighting. I'm facing an issue where the character used for the 'cursor' (which is controlled by arrow keys) is fucking up the syntax highlighting. I use 2 variables which store its location (currentLine, and currentColumn), so how would I hide it or something off the syntax highlighting function?

Code:

    static public int currentColumn = 0;
    static public int currentLine = 0;
    static private List<String> fileArray = new ArrayList<>();

    public static String highlightSyntax(String line) { // Highlight syntax
        line = line.replaceAll("\\bdef\\b", RED + "def" + RESET)
            .replaceAll("\\bwhile\\b", RED + "while" + RESET)
            .replaceAll("\\bprint\\b", YELLOW + "print" + RESET)
            .replaceAll("\\bif\\b", RED + "if" + RESET)
            .replaceAll("\\belse\\b", RED + "else" + RESET)
            .replaceAll("\\bint\\b", GREEN + "int" + RESET);
        return line;
    }
sturdy hareBOT
#

This post has been reserved for your question.

Hey @proven oriole! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

proven oriole
#

let me know if u need the rest of the code aswell

twilit charm
proven oriole
# twilit charm can u show the cursor code

this is the code to set the line/col:

    public static void controlKeys(int index) {
        removeCursor(); // Remove the current cursor
    
        switch (index) {
            case 1: // Left arrow key
                currentColumn = Math.max(0, currentColumn - 1);
                break;
            case 2: // Right arrow key
                currentColumn = Math.min(fileArray.get(currentLine).length(), currentColumn + 1);
                break;
            case 3: // Up arrow key
                if (currentLine > 0) {
                    currentLine--;
                    currentColumn = Math.min(currentColumn, fileArray.get(currentLine).length());
                }
                break;
            case 4: // Down arrow key
                if (currentLine < fileArray.size() - 1) {
                    currentLine++;
                    currentColumn = Math.min(currentColumn, fileArray.get(currentLine).length());
                }
                break;
            default:
                System.out.println("Error");
                return;
        }
    
        updateCursor(); 
    }

and this is the code that updates the actual arraylist

    private static void updateCursor() { // Update cursor
        String currentItem = fileArray.get(currentLine);
        String modifiedItem = currentItem.substring(0, currentColumn) + "|" + currentItem.substring(currentColumn);
        fileArray.set(currentLine, modified);

        displayArrayContent();
    }
twilit charm
#
 public static String highlightSyntax(String line) { // Highlight syntax
        line = line.replaceAll("\\bdef\\b", RED + "def" + RESET)
            .replaceAll("\\bwhile\\b", RED + "while" + RESET)
            .replaceAll("\\bprint\\b", YELLOW + "print" + RESET)
            .replaceAll("\\bif\\b", RED + "if" + RESET)
            .replaceAll("\\belse\\b", RED + "else" + RESET)
            .replaceAll("\\bint\\b", GREEN + "int" + RESET);
        return line;
    }
#

so you want this

#

to ignore the

#

cursor right

#

that is just |

proven oriole
#

yes

#

because it can get in the middle of keywords and then instead of for example 'def' (which is colored red), it would be 'd|ef' which wont have any color at all

twilit charm
#

hm

#

what about if you

#

make another variable

#

where you get rid of the cursor

#

then do all the color stuff

#

and then after doing it you add the cursor back

#

that could work

proven oriole
#

so like have a variable for the arraylist with the cursor removed and then apply color?

twilit charm
#

yea

#

and then after you apply the color you add the cursor back

proven oriole
#

quick question tho, how do i access a specific character of a object in a arraylist

#

because i cant really find anyway of how to do this

#

i know how to access an object

#

like if the array list was ["AAA, "BBB', "CCC"]
i know i could do like .delete(0) to delete "AAA", but how would i say delete the middle A of it

twilit charm
#

uh

#

string.find

#

i think

#

sorry

#
line.indexOf("|")
#

and u get the index

#

u gotta remember the position of the cursor to put it back after editing so ye

#

use indexof

proven oriole
#

alright

twilit charm
proven oriole
#

tryin to implement it, will let u know