Skip to content

Commit

Permalink
Add trim mode
Browse files Browse the repository at this point in the history
  • Loading branch information
newsch committed May 29, 2019
1 parent 6cedd02 commit c3e666d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ int canvas_resize(Canvas **canvas_pointer, int newrows, int newcols) {
* Requires a pointer to a canvas pointer.
*
* Returns 1 if the canvas was truncated, 0 otherwise.
*
* TODO: update docstring
*/
Canvas *canvas_trimc(Canvas *orig, char ignore, bool right, bool bottom,
bool left, bool top) {
Expand Down Expand Up @@ -353,6 +355,7 @@ Canvas *canvas_trimc(Canvas *orig, char ignore, bool right, bool bottom,
(right ? mr : orig->num_cols - 1));
}

// TODO: drop this or fill it out
inline Canvas *canvas_trim(Canvas *orig, int right, int bottom, int left,
int top) {}

Expand Down
37 changes: 37 additions & 0 deletions src/fe_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ editor_mode_t modes[] = {
{"Pan", "Pan around the canvas", mode_pan},
{"Free-Line", "Draw a line with your arrow keys", mode_free_line},
{"Brush", "Paint with arrow keys and mouse", mode_brush},
{"Trim", "Remove empty space", mode_trim},
};

typedef struct {
Expand Down Expand Up @@ -475,3 +476,39 @@ int mode_brush(reason_t reason, State *state) {

return 0;
}

/* mode_trim
*
* Trim edges of the canvas.
*
* Select a direction to trim and
*/
int mode_trim(reason_t reason, State *state) {
if (reason == NEW_KEY) {
int dir = 0; // 0: top, 1: right, 2: down, 3: left
switch (state->ch_in) {
case KEY_UP:
dir = 0;
break;
case KEY_RIGHT:
dir = 1;
break;
case KEY_DOWN:
dir = 2;
break;
case KEY_LEFT:
dir = 3;
break;
default:
return 0;
}
Canvas *old_canvas = state->view->canvas;
// trimc order : right bottom left top
Canvas *new_canvas =
canvas_trimc(old_canvas, ' ', dir == 1, dir == 2, dir == 3, dir == 0);
state->view->canvas = new_canvas;
canvas_free(old_canvas);
redraw_canvas_win();
}
return 0;
}
1 change: 1 addition & 0 deletions src/fe_modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mode_function_t mode_insert;
mode_function_t mode_pan;
mode_function_t mode_free_line;
mode_function_t mode_brush;
mode_function_t mode_trim;

typedef struct {
char *name;
Expand Down
1 change: 1 addition & 0 deletions src/mode_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef enum {
MODE_PAN,
MODE_FREE_LINE,
MODE_BRUSH,
MODE_TRIM,

// ^ add your mode above
LAST, // used to get number of elements
Expand Down

0 comments on commit c3e666d

Please sign in to comment.