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

Move "cursor snap" logic from cursor_jump() into a utility function #19

Open
Kiyoshika opened this issue Jan 22, 2023 · 0 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@Kiyoshika
Copy link
Owner

I feel like I've implented this logic three different ways throughout this library, and I think the cursor_jump() implementation is probably the cleanest. Just move this into a utility maybe called cursor_snap() which will move the cursor to a specific row/column position and adjust scrolling if needed.

For reference, below is a paste of the implementation from cursor_jump()

NOTE: this was pasted directly from vim so I can't really format it...

NOTE 2: row is the new proposed row (could be less than or greater than cursor->row, or the same) and column is the proposed column.

// TODO: move the cursor scrolling logic to a utility function.
3             // this can be used in other places since I feel like I've implemented
2             // this in three different ways by now...
1             if (screen->lines[row][column] == search_char)
352             {
1                 // NOTE: the +2 is to account for "lines" being 1-indexed and
2                 // the bottom line is inaccessible (used to display edit mode)
3                 if (row > screen->end_idx)
4                 {
5                     screen->end_idx = row + 2;
6                     screen->start_idx = screen->end_idx - screen->max_rows;
7                     screen->current_line = screen->max_rows - 2;
8                 }
9                 else if (row < screen->start_idx)
10                 {
11                     screen->start_idx = row;
12                     screen->end_idx = screen->start_idx + screen->max_rows;
13                     screen->current_line = 0;
14                 }
15                 else
16                 {
17                     if (cursor->row > row)
18                         screen->current_line -= (cursor->row - row);
19                     else
20                         screen->current_line += (row - cursor->row);
21                 }
22
23                 cursor->row = row;
24                 cursor->column = column;
25
26                 move(screen->current_line, cursor->column + cursor->line_num_size + 1);
27                 screen_draw(screen, cursor);
28                 return;
29             }
@Kiyoshika Kiyoshika added the enhancement New feature or request label Jan 22, 2023
@Kiyoshika Kiyoshika self-assigned this Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant