Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LScreen.cpp Source File

LScreen.cpp

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation 
00015  **************************************************************************************/
00016 
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <string>
00021 #include <sys/ioctl.h>
00022 #include <unistd.h>
00023 #include <termios.h>
00024 #include <unistd.h>
00025 #include <fcntl.h>
00026 #include <stdarg.h>
00027 
00028 #include "LScreen.h"
00029 
00030 using namespace std;
00031 using namespace linuxAsyncClient;
00032 
00033 LScreen::LScreen()
00034 {
00035     _hight = 0;
00036     _width = 0;
00037     fcntl(0, F_SETFL, O_NONBLOCK );
00038 }
00039 
00040 LScreen::~LScreen()
00041 {
00042 
00043 }
00044 
00045 void LScreen::getSize(void)
00046 {
00047     struct winsize wsize ;
00048     ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsize);
00049     _width = wsize.ws_col ? wsize.ws_col : 132;
00050     _hight = wsize.ws_row ? wsize.ws_row : 10;
00051 }
00052 
00053 void LScreen::clear(void)
00054 {
00055     getSize();
00056     printf("\033[2J");
00057     reprompt();
00058 
00059 }
00060 
00061 void  LScreen::display(const char* format, ...)
00062 {
00063     va_list args;
00064     va_start(args, format);
00065     vdisplay(format, args);
00066     va_end(args);
00067     reprompt();
00068 }
00069 
00070 void  LScreen::vdisplay(const char* format, va_list args)
00071 {
00072     vsprintf(_buffer, format, args);
00073     fprintf(stdout, "\033[%d;%dH\033[2K", _hight, 1);
00074     fprintf(stdout,"%s", _buffer);
00075     reprompt();
00076 }
00077 
00078 void  LScreen::prompt(const char* format, ...)
00079 {
00080     va_list args;
00081     va_start(args, format);
00082     vprompt(format, args);
00083     va_end(args);
00084 }
00085 
00086 void  LScreen::vprompt(const char* format, va_list args)
00087 {
00088     getSize();
00089     int pos = 0;
00090     string fmt = format;
00091 
00092     if ( ( pos = fmt.find("\n")) > 0 )
00093     {
00094         fmt.replace(pos, 1, " ");
00095     }
00096 
00097     vsprintf(_buffer, format, args);
00098     _prompt = _buffer;
00099     reprompt();
00100 }
00101 
00102 void  LScreen::reprompt(void)
00103 {
00104     int len = 0;
00105     if ( (len =_prompt.size()) >= _width )
00106     {
00107         len = _width - 1;
00108     }
00109     fprintf(stdout,"\033[%d;%dH", _hight, 1);
00110     fprintf(stdout,"\033[0;33m%s\033[0K\033[0;37m", _prompt.substr(0, len).c_str());
00111     fflush(stdout);
00112 }
00113 
00114 
00115 bool LScreen::checkKeyIn(char* val)
00116 {
00117     int c = 0;
00118     int cprev = 0;
00119 
00120     while  ( read(0, &c, 1) == 1 )
00121     {
00122         if ( c == '\n' )
00123         {
00124             *val = cprev;
00125             fprintf(stdout, "\033[1T");
00126             fflush(stdout);
00127             reprompt();
00128             return true;
00129         }
00130         cprev = c;
00131     }
00132     return false;
00133 }
00134