Wednesday, August 19, 2009

Building a VI clone with Groovy pt 2

Ok so, here's the beef. My main issue at the moment with my vi clone is that I think my assumptions about the way I was using a StringBuilder* were initially incorrect. I actually think I now need to store each line separately in a list. This way, I can keep track of the caret, and know exactly which line I am on as well:

def buffers = new File(argstr).getText().tokenize('\n').collect{ new StringBuilder(it) }

With that sorted, I find myself with a number of requirements:
  1. Moving the cursor should be done with the arrow keys.
  2. Deleting stuff (x) should remove the character at the right x, y position.
  3. Do not pass the screen boundary, scroll the file down.
  4. When at the end of the file, do not move the cursor further.
  5. When I am in append mode, text should be added to the end of the line no matter where in the line I am. The cursor should be moved as well.
  6. When I hit escape, a number of things can happen: If I am in edit mode, I drop back to movement mode; If I am in movement mode, I leave the editor (for now, saving the file will be automatic, rather than explicit for simplicity).
  7. If I am in Append Mode or in Edit Mode I should be able to type text in to the correct buffer, and have it appear on my screen. Also, pressing enter should move on to a new line inbetween the current line and the next.
  8. Do not wrap text on the screen for the time being.

Requirement eight I am a little reticent about. I am not sure if it would be harder to work out whether we have wrapped the text on a line (could be stored as a boolean on an object wrapping the StringBuilder) or whether it is better to scroll the whole file with the cursor at the screen boundary. I guess both are valid solutions, and in the future the user could be given an option to do either.

Encapsulating the StringBuilder in a decorator class of it's own does make sense though, because I might be able to add extra information to the line (like, last know caret position) as well as whether it has wrapped or not. I realise that the string length could be calculated every time to work this out, but encapsulating this functionality would be better I feel.


* I have switched to StringBuilder because I forgot I was using Java 5 :O



*

1 comment:

  1. Matt, will you continue this series?

    Please, say "yes".

    ReplyDelete