Move cursor to beginning of a line

I believe Erlang/Elixir can fit to any purpose.

I faced with an issue to move my cursor to the beginning of the line. I can’t find any suitable solution except using pread(IO, Location, N) with N window where

  1. read N bytes
  2. check if there is a \n and repeat until reach \n increasing pread(file, offset - [i * N], N)

And how big should be N in that case?

1 Like

What exactly do you mean by “cursor” and “beginning of line”?

Are you really writing in a file, or are you trying to manipulate the screen in a tty?
Are you writing a file editor?

It’s really hard to answer this question w/o the larger context of what you’re really trying to do.

3 Likes

If your terminal supports ANSI, you can use it to control the cursor very exact: http://ascii-table.com/ansi-escape-sequences.php

2 Likes

I am reading from the file on the file system.
By cursor I mean seeking/offset/caret/pointer/position/location in the opened file when you move your position by N bytes and read starting from this point.
I am writing a parser to parse some custom file format.

1 Like

Then I’d suggest to either keep track of the offsets of the lines in the files or to write the parser in a way that it does not need to go backwards.

I’d prefer the latter.

1 Like