-3
\$\begingroup\$

I am implementing a minimal command line in serial terminal using TivaC board from texas instruments. using Putty

cursor detection is already enabled by application so I don't have to delete the escape sequences generated by the cursors on the terminal like "esc[A", "esc[B".. etc

However in my code where I am storing the char input in a string[] array of size 80, I am not able to implement left and right cursor edits. if I press left I should be able to move my cursor to the required position and should be able to edit the texts.

Behavior: when I press the left from the keyboard the cursor should travel left and allow the user to enter the character where ever the user wants to. However in my case it is able to travel left (already enabled in PUTYY) but not able to add the required character.

For example

$>echo hello

$>echo hellmo (char entered at 5th position)

ideal output:-

hellmo

my output:-

hellom

See the code commented as //left

getcUart0(); 

gets characters from the from user.

putsUart(char *string);

puts a string into the serial

void command_line(void)
{
    char char_input = 0;
    int char_count =0;

    // Get Input from User
    putsUart0("\r\n");
    putsUart0("\033[1;32m$>\033[0m");          //Print Input to let user know to start typing, foreground color:Yellow

    while(1)
    {
        //take input from user
        char_input = getcUart0();           

        //Exit on carriage return
        if (char_input == 13 || carriage_return)
        {
            string[char_count] = '\0';
            carriage_return = 0;
            char_count = 0;
            break;
        }

        // Cursor processing
        if (char_input == 27)
        {
            char next_1 = getcUart0();
            char next_2 = getcUart0();

             //nullify the behavior of up key
            if(next_1 == 91 && next_2 == 65)      //up
            {
                putsUart0("\033[B");
                char_input = '\0';
                continue;
            }
            //nullify the behavior of down key
            else if(next_1 == 91 && next_2 == 66) //down
            {
                putsUart0("\033[A");
                char_input = '\0';
                continue;
            }
            //left key 
            else if(next_1 == 91 && next_2 == 68) //left
            {
                int temp_count = char_count;
                char temp_inp = char_input;
                char_count--;
                string[temp_count] = temp_inp;
                putcUart0(string[temp_count]);
                continue;

            }
        }

        //Process backspace key
        if (char_input == 8 || char_input == 127)
        {
            //putsUart0("\r\nBackspace detect");

            if(char_count <= 0)
            {
                putsUart0("\033[C");

                continue;
            }
            else
            {
                putcUart0(' ');
                putsUart0("\033[D");
                char_count--;
                continue;
            }
        }

        //convert uppercase to lower before storing in string
        if (char_input >= 65 && char_input <= 90)
            string[char_count++] = char_input + 32;

        else
            string[char_count++] = char_input;

        //check for buffer overflow
        if (char_count == MAX_SIZE)
        {
            putsUart0("\r\nCan't exceed more than 80 chars");   

            reset_buffer();                                      
            *string = 0;

            break;
        }

    }

    putsUart0("\r\n");

}
New contributor
EEguy is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

closed as off-topic by πάντα ῥεῖ, 200_success, pacmaninbw, vnp, Jamal Feb 11 at 6:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, 200_success, pacmaninbw, vnp, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.