Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

topic: text editor #8

Open
lukewilliamboswell opened this issue Mar 7, 2024 · 7 comments
Open

topic: text editor #8

lukewilliamboswell opened this issue Mar 7, 2024 · 7 comments
Assignees
Labels
assigned Topic has been assigned in-content Is the issue in lesson content?
Milestone

Comments

@lukewilliamboswell
Copy link
Collaborator

I would like to work on the test editor chapter. I have a package for handling ANSI escape sequences which are supported in most modern terminals, and is most useful when working with the basic-cli platform.

Here is an illustration of an app using this package, and the code for the tui-menu example.

I think I can expand this example to include opening, editing, and saving files. The main limitation I can think of is limiting this to ASCII and not supporting full unicode.

@gvwilson gvwilson added discuss An issue or PR currently being discussed in-content Is the issue in lesson content? propose-addition A suggestion for an addition to content or infrastructure labels Mar 8, 2024
@gvwilson gvwilson added this to the topic-outline milestone Mar 8, 2024
@gvwilson
Copy link
Collaborator

gvwilson commented Mar 8, 2024

👍 to a simple text editor - I found with the Python version that mapping various coordinate systems to each other and handling basic keyboard input was one chapter, while adding undo/redo was another. Don't know if it would be the same here?

@gvwilson gvwilson changed the title File Editor / Terminal UI topic proposal: text editor Mar 8, 2024
@lukewilliamboswell
Copy link
Collaborator Author

Got something minimal to share and seeking some direction - opening a file from arguments, rendering the text contents to a window.

I've included a super simple implementation of a Piece table which looks to be the easiest way to build a text editor.

I've also used the text segmentation from roc-lang/unicode to split the input file into graphemes so in theory this should support unicode out of the box 🤞

So far the demo only renders text, the editing and saving functionality hasn't been included yet.

I'm pretty sure the undo/redo will be essentially free. The plan is to just keep a new copy of the Piece Table (sans buffers) on each significant user action (edits) in a list. This is just duplicating the indexes into the buffers and so shouldn't grow too significantly unless the user has very long editing session. We can always just keep the latest N edits or something if it becomes a problem.

WIP code for this demo (I haven't removed any of the code from the menu example, just added the parts for editor)

Questions

  1. Do you think it is acceptable to render newlines just using "¶", this dramatically simplifies the code code. Though if we want to include line numbers then we will need to handle CLRF breaks anyway?
  2. Do we want multiple "pages", like a menu or something, or keep it simple an just open straight into the editing window?
  3. Do we want multiple "modes", like vim, command and insert and support different controls in each? This should be pretty easy to support, maybe just a couple of the basics.

Demo

roc-editor-1

@gvwilson
Copy link
Collaborator

gvwilson commented Mar 9, 2024

This is a great start - well done! I based the editor in the Python book on the first few parts of Wasim Lorgat's tutorial https://wasimlorgat.com/posts/editor.html - I think we definitely want the line breaks, but modes and windows and what-not won't fit into the one-hour limit.

@gvwilson gvwilson removed the discuss An issue or PR currently being discussed label Mar 9, 2024
@lukewilliamboswell
Copy link
Collaborator Author

Quick update on progress this weekend.

WIP code branch

  • handle line breaks
  • remove unused code from previous example
  • implement a ViewPort to draw the content (was helpful for testing edge cases, may be useful in future for e.g. editing multiple files)
  • enable saving of changes using Ctrl-S keypress
  • add text insertion (and implementation for the PieceTable data structure)

I've had to make a few changes to the roc-ansi package, so this has been good to flush those issues out and resolve them.

I've also been adding a lot of inline comments and thinking about the dot points for the chapter content.

Demo

roc-editor-2

@gvwilson
Copy link
Collaborator

this is great work - thank you very much. I think you may already have an hour's worth of teachable material - if you'd like to grab some time with me ([email protected]) I'd be happy to talk you through next steps.

@gvwilson gvwilson changed the title topic proposal: text editor topic: text editor Mar 10, 2024
@lukewilliamboswell
Copy link
Collaborator Author

Sounds good. I sent you a meeting request via email. Just took a guess at a suitable time re timezones. Apologies if it's too short notice, I could find a time next week.

I'd like to implement text deletion to close out the Piece table basics. Also I'll need to make a release for the unicode and ansi dependencies before this will be usable more generally.

If you don't mind I'd love to write something up about this for my blog and share in the online meetup this weekend, it won't be the same content as the book more my experience.

@gvwilson
Copy link
Collaborator

Blogging about this would be great, and no worries about overlap with whatever you're going to write for the book: you own the rights to your blog posts, so you can decide to recycle them.

@gvwilson gvwilson added assigned Topic has been assigned and removed propose-addition A suggestion for an addition to content or infrastructure labels Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assigned Topic has been assigned in-content Is the issue in lesson content?
Projects
None yet
Development

No branches or pull requests

2 participants