We define the delete key to delete a character, the carriage return to indicate a line break, and F12 to delete the entire line, and F1 to exit.
All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
Dp @ dp :~ /Cursestest % cat a. c
# Include
# Include
# Include
// Code by myhaspl@myhaspl.com
// Date: 2014/1/17
Int main (int argc, char * argv [])
{
Setlocale (LC_ALL ,"");
Initscr ();
Clear ();
Noecho ();
Cbreak ();
If (has_colors () = FALSE)
{
Endwin ();
Printf ("your terminal does not support color! \ N ");
Return (1 );
}
Start_color ();/* Start the color mechanism */
Mvprintw (5, COLS/2-10, "simple Editor-limited to single screen editing ");
Refresh ();
Init_pair (1, COLOR_GREEN, COLOR_BLACK );
WINDOW * win1;
Int width = COLS-14;
Int height = LINES-14;
Int x, y;
Win1 = newwin (height, width, 7,7); // new window (row, column, begin_y, begin_x)
Keypad (win1, TRUE );
Wattron (win1, COLOR_PAIR (1 ));
Box (win1, ACS_VLINE, ACS_HLINE );
Wrefresh (win1 );
Getyx (win1, y, x );
++ Y; ++ x;
While (1 ){
Int c = mvwgetch (win1, y, x );
Switch (c)
{
Case KEY_RIGHT:
++ X;
If (x> = width-1 ){
++ Y;
X = 1;
}
Break;
Case KEY_LEFT: -- x;
If (x <1 ){
-- Y;
X = width-2;
}
Break;
Case KEY_UP:
-- Y;
If (y <1 ){
Y = height-2;
}
Break;
Case KEY_DOWN:
++ Y;
If (y> = height-1 ){
Y = 1;
}
Break;
Case 10:
++ Y;
If (y> = height-1 ){
Y = 1;
}
Break;
Case KEY_F (1 ):
// Exit
Mvprintw (LINES-3, 2, "exit the editor? ");
Mvprintw (LINES-2, 2 ,"");
Refresh ();
Int ans = getch ();
If (ans = 'y' | ans = 'y ')
{
Mvprintw (LINES-2, 2, "Yes \ n ");
Refresh ();
Return 0;
} Else
Mvprintw (LINES-2, 2, "No \ n ");
Refresh ();
Break;
Case KEY_F (12 ):
// Delete a row
Wdeleteln (win1 );
Winsertln (win1 );
Box (win1, ACS_VLINE, ACS_HLINE );
Case KEY_DC:
// Delete a character
Mvwprintw (win1, y, x ,"");
Break;
Default:
Mvwprintw (win1, y, x, "% c", c );
++ X;
If (x> = width-1 ){
++ Y;
X = 1;
}
If (y> = height-1 ){
Y = 1;
}
Wrefresh (win1 );
}
}
Wattroff (win1, COLOR_PAIR (1 ));
Endwin ();
Return 0;
}
Run
Dp @ dp :~ /Cursestest % gcc-lncursesw a. c-o mytest
Dp @ dp :~ /Cursestest %./mytest
All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/