summaryrefslogtreecommitdiff
path: root/libterm
diff options
context:
space:
mode:
Diffstat (limited to 'libterm')
-rw-r--r--libterm/examples/Makefile3
-rw-r--r--libterm/examples/readline.c40
-rw-r--r--libterm/term.c22
-rw-r--r--libterm/term.h26
-rw-r--r--libterm/term_io.c107
-rw-r--r--libterm/term_io.h10
6 files changed, 184 insertions, 24 deletions
diff --git a/libterm/examples/Makefile b/libterm/examples/Makefile
index 00583f8..ffc3d1d 100644
--- a/libterm/examples/Makefile
+++ b/libterm/examples/Makefile
@@ -1,6 +1,7 @@
CC=gcc
CFLAGS=-I../ ../libterm.o
-SOURCES=detect_resize.c detect_screen_size.c filtered_input.c invisible_input.c restore_screen.c print_8025.c
+SOURCES=detect_resize.c detect_screen_size.c filtered_input.c invisible_input.c \
+readline.c restore_screen.c print_8025.c
EXE=$(SOURCES:.c=)
BUILD_DIR=.
diff --git a/libterm/examples/readline.c b/libterm/examples/readline.c
new file mode 100644
index 0000000..cd4467d
--- /dev/null
+++ b/libterm/examples/readline.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <term.h>
+#include <term_io.h>
+
+int main()
+{
+
+ const int sz = 31;
+ char name[sz];
+ char passsword[sz];
+
+ struct term_screen ts; memset( &ts, 0, sizeof(ts) );
+
+ if ( term_init( &ts ) == -1 )
+ printf("Some err when init\n");
+
+ term_set_raw_mode( &ts );
+
+ term_clr_scr( &ts );
+
+ term_cur_set_c( &ts, 0);
+ term_cur_set_r( &ts, 1 );
+ term_readline( &ts, name, sz, READLINE_TEXT );
+
+ term_cur_set_c( &ts, 0 );
+ term_cur_set_r( &ts, 2 );
+ term_readline( &ts, passsword, sz, READLINE_HIDDEN );
+
+
+ term_clr_scr( &ts );
+
+ term_set_orig_mode( &ts );
+
+ printf("Name:%s\n", name);
+ printf("passsword:%s\n", passsword );
+
+ return 0;
+} \ No newline at end of file
diff --git a/libterm/term.c b/libterm/term.c
index e279e81..412253b 100644
--- a/libterm/term.c
+++ b/libterm/term.c
@@ -2,28 +2,6 @@
#include "debug.h"
-enum KEY_ACTION{
- KEY_NULL = 0, /* NULL */
- CTRL_A = 1, /* Ctrl+a */
- CTRL_B = 2, /* Ctrl-b */
- CTRL_C = 3, /* Ctrl-c */
- CTRL_D = 4, /* Ctrl-d */
- CTRL_E = 5, /* Ctrl-e */
- CTRL_F = 6, /* Ctrl-f */
- CTRL_H = 8, /* Ctrl-h */
- TAB = 9, /* Tab */
- CTRL_K = 11, /* Ctrl+k */
- CTRL_L = 12, /* Ctrl+l */
- ENTER = 13, /* Enter */
- CTRL_N = 14, /* Ctrl-n */
- CTRL_P = 16, /* Ctrl-p */
- CTRL_T = 20, /* Ctrl-t */
- CTRL_U = 21, /* Ctrl+u */
- CTRL_W = 23, /* Ctrl+w */
- ESC = 27, /* Escape */
- BACKSPACE = 127 /* Backspace */
-};
-
#define T_ESC "\x1b"
//setup initial terminal stuff
diff --git a/libterm/term.h b/libterm/term.h
index 0be7391..b399cd6 100644
--- a/libterm/term.h
+++ b/libterm/term.h
@@ -16,6 +16,32 @@
#include "screen_modes.h"
#include "debug.h"
+enum TERM_KEY_ACTION {
+ KEY_NULL = 0, /* NULL */
+ CTRL_A = 1, /* Ctrl+a */
+ CTRL_B = 2, /* Ctrl-b */
+ CTRL_C = 3, /* Ctrl-c */
+ CTRL_D = 4, /* Ctrl-d */
+ CTRL_E = 5, /* Ctrl-e */
+ CTRL_F = 6, /* Ctrl-f */
+ CTRL_H = 8, /* Ctrl-h */
+ TAB = 9, /* Tab */
+ CTRL_K = 11, /* Ctrl+k */
+ CTRL_L = 12, /* Ctrl+l */
+ ENTER = 13, /* Enter */
+ CTRL_N = 14, /* Ctrl-n */
+ CTRL_P = 16, /* Ctrl-p */
+ CTRL_T = 20, /* Ctrl-t */
+ CTRL_U = 21, /* Ctrl+u */
+ CTRL_W = 23, /* Ctrl+w */
+ ESC = 27, /* Escape */
+ BACKSPACE = 127 /* Backspace */
+};
+
+
+#define TK_ENTER 13
+#define TK_ESC 27
+#define TK_BACKSPACE 127
typedef struct term_screen
{
diff --git a/libterm/term_io.c b/libterm/term_io.c
index 5ef682b..cd1b3c1 100644
--- a/libterm/term_io.c
+++ b/libterm/term_io.c
@@ -102,4 +102,111 @@ int term_getc( term_screen *ts )
return ret;
}
+int term_putc( term_screen *ts, char c )
+{
+ int ret = 0;
+ int fret = -1;
+ fret = write( ts->ofd, &c, 1 );
+ if ( fret != 1 )
+ {
+ ret = -1;
+ }
+
+ return ret;
+}
+
+//return amoutn fo characters readed
+//on error or if nothing is imputed 0 returned
+int term_readline( term_screen *ts, char *str, size_t str_size, int flag )
+{
+ int ret = 0;
+ int max_row, max_column;
+ char *buf = NULL;
+ int buf_size = str_size-1;
+ int buf_curent = 0;
+ int menu_input=0;
+ char menu_cmd = 0;
+ int orig_row;
+ int orig_col;
+
+ buf = malloc( buf_size );
+ memset( buf, 0, buf_size );
+
+ max_row = term_get_maxrow( ts );
+ max_column = term_get_maxcol( ts );
+ orig_row = term_cur_get_r( ts );
+ orig_col = term_cur_get_c( ts );
+
+ menu_cmd = 0;
+ buf_curent = 0;
+ while ( menu_cmd != TK_ESC )
+ {
+ menu_input = term_getc( ts );
+ if ( menu_input != -1 )
+ {
+ menu_cmd = (char)menu_input;
+ //add to buffer any pritable char
+ //if ( (isgraph( menu_cmd ) && flag == READLINE_TEXT ) ||
+ // (isalpha( menu_cmd ) && flag == READLINE_ALPHA ) )
+ if ( isalpha( menu_cmd ) )
+ {
+ if ( buf_curent < buf_size )
+ {
+ buf[ buf_curent ] = menu_cmd;
+ buf_curent += 1;
+ }
+ //deleteone char from buffer
+ } else if ( menu_cmd == TK_BACKSPACE )
+ {
+ if ( buf_curent > 0)
+ {
+ buf[ buf_curent ] = 0x0;
+ buf_curent -= 1;
+ }
+ //input ready
+ } else if ( menu_cmd == TK_ENTER )
+ {
+ ret = buf_curent;
+ memcpy( str, buf, buf_size );
+ str[ str_size-1 ] = 0x0;
+ break;
+ } else if ( menu_cmd == TK_ESC )
+ {
+ ret = -1;
+ }
+
+ {
+ int i;
+ term_cur_set_r( ts, orig_row );
+ term_cur_set_c( ts, orig_col );
+ for (i=0;i<buf_size;i++)
+ {
+ if ( i < buf_curent )
+ {
+ if ( (flag == READLINE_TEXT) || (flag == READLINE_ALPHA) )
+ {
+ term_putc( ts, buf[i] );
+ } else if ( flag == READLINE_HIDDEN )
+ {
+ term_putc( ts, '*');
+ }
+ } else
+ {
+ term_putc( ts, ' ' );
+ }
+ }
+ term_cur_set_c( ts, orig_col+buf_curent );
+ term_cur_set_r( ts, orig_row );
+ }
+ }
+ }
+
+ if ( buf != NULL)
+ {
+ free( buf );
+ buf = NULL;
+ }
+
+ return ret;
+}
diff --git a/libterm/term_io.h b/libterm/term_io.h
index 8c57282..3c84972 100644
--- a/libterm/term_io.h
+++ b/libterm/term_io.h
@@ -7,11 +7,19 @@
#include "screen_modes.h"
#include "term.h"
+#define READLINE_NONE 0
+#define READLINE_ALPHA 1
+#define READLINE_TEXT 2
+#define READLINE_HIDDEN 3
+
+
int term_fprint( screen_mode_e mode, FILE *f );
int term_print( term_screen *ts, const char *s, size_t n );
int term_print_xy( term_screen *ts, const char *buf, size_t size,
- int init_column, int init_row);
+ int init_column, int init_row);
int term_draw_hline( term_screen *ts, int pc, int pr, int sz, char ch );
int term_getc( term_screen *ts );
+int term_putc( term_screen *ts, char c );
+int term_readline( term_screen *ts, char *str, size_t str_size, int flag );
#endif