Crossterm how to print text without jumping and move cursor without text moving

so i've been trying to implement and simple terminal text editor using rust crossterm crate and i'm be able to create the screen and implement the cursor movements for basic j,k,h,l keys and i'm be able to print text to the screen. however the issue i'm having is when i read some text files and print it to terminal the lines are jumping like stairway and with the cursor movement the text is movement. how can i fix this. what should i do if i want to render the text as it in the file and edit the text with the cursor movement and text insertion.

here is my code

use core::panic;
use crossterm::event::{poll, read, Event, KeyCode, KeyModifiers};
use crossterm::terminal::{self, ClearType};
use crossterm::ExecutableCommand;
use crossterm::QueueableCommand;
use crossterm::{cursor, style};
use std::io::{stdout, Write};
use std::thread;
use std::time::Duration;

mod filehandle;
mod filepicker;

use filehandle::fileopen;

fn main() {
    let contents = fileopen::openfile();

    let mut stdout = stdout();
    let _ = stdout.execute(terminal::EnterAlternateScreen);
    let _ = terminal::enable_raw_mode();
    let (x, y) = terminal::size().unwrap();
    terminal::SetSize(x, y);
    let wsize = terminal::window_size();
    let _ws = match wsize {
        Ok(val) => val,
        Err(err) => panic!("Error in getting window size {:?}", err),
    };

    let mut quit = false;
    let mut column: u16 = 0;
    let mut row: u16 = 0;
    let mut esc = false;

    let cur = stdout.execute(cursor::EnableBlinking);
    stdout.queue(terminal::Clear(ClearType::All)).unwrap();

    stdout.execute(cursor::SetCursorStyle::BlinkingBlock);
    while !quit {
        while poll(Duration::ZERO).unwrap() {
            match read().unwrap() {
                Event::Resize(x, y) => {
                    terminal::SetSize(x, y);
                }
                Event::FocusGained => println!("FocusGained"),
                Event::FocusLost => println!("FocusLost"),
                Event::Key(event) => match event.code {
                    KeyCode::Char(x) => {
                        if x == 'c' && event.modifiers.contains(KeyModifiers::CONTROL) {
                            quit = true
                        } else if x == 'j' && esc {
                            row += 1;
                        } else if x == 'k' && esc {
                            if row != 0 {
                                row -= 1;
                            }
                        } else if x == 'l' && esc {
                            column += 1;
                        } else if x == 'h' && esc {
                            if column != 0 {
                                column -= 1;
                            }
                        }
                    }
                    KeyCode::Esc => {
                        if esc {
                            esc = false;
                        } else {
                            esc = true;
                        }
                    }
                    KeyCode::Enter => println!("Enter pressed\r"),
                    _ => todo!(),
                },
                Event::Mouse(MouseEvent) => todo!(),
                Event::Paste(string) => todo!(),
            }
        }

        stdout.execute(style::Print(contents.clone()));
        stdout.execute(cursor::MoveTo(column, row));
        stdout.flush();
        thread::sleep(Duration::from_millis(16.66666 as u64));
        stdout.execute(terminal::Clear(ClearType::All));
    }

    terminal::disable_raw_mode();
    stdout.execute(terminal::LeaveAlternateScreen);
}

When in raw mode, printing "\n" no longer moves the cursor down and to the beginning of the next line; instead, it only moves the cursor. You must print "\r\n" to get the same effect.

You will have to print the file a line at a time and include the "\r" (carriage return) as you go.

However, this isn't a significant problem in the big picture, because you will also have to account for the file being bigger than the terminal: figure out how many lines and columns you should print to fit, and where the file is scrolled to, and print only that part. When you're doing all that, it'll be trivial to include the "\r".

Not to mention handling either horizontal scrolling or word wrap, escaping control characters, maybe unless they are SGR, etc...

There's a lot!

thank you what can i do for the text moving with cursor?.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.