Commit ea1218f1c1c628a2c51d2e67ec0aa0dfaf7be230

Authored by henyxia
1 parent ae25085d

UI finished

Showing 5 changed files with 117 additions and 6 deletions   Show diff stats
1 CC=gcc 1 CC=gcc
2 -CC_FLAGS=-c -Wall -Werror 2 +CC_FLAGS=-c -Wall -Werror -std=c99 -g
3 CC_LIBS= 3 CC_LIBS=
4 INCLUDES= 4 INCLUDES=
5 5
6 -SOURCES=main.c printx.c 6 +SOURCES=main.c printx.c ui.c
7 OBJECTS=$(SOURCES:.c=.o) 7 OBJECTS=$(SOURCES:.c=.o)
8 OUTPUT=tweekd 8 OUTPUT=tweekd
9 9
1 #include <stdio.h> 1 #include <stdio.h>
2 #include "printx.h" 2 #include "printx.h"
  3 +#include "ui.h"
3 4
4 int main(void) 5 int main(void)
5 { 6 {
6 - initLog(); 7 + initUILog();
  8 + if(!initLog())
  9 + {
  10 + printf("Log creation failed\n");
  11 + return 1;
  12 + }
7 printx(INFO, "Tweekd starting\n"); 13 printx(INFO, "Tweekd starting\n");
  14 + displayUI();
8 closeLog(); 15 closeLog();
9 return 0; 16 return 0;
10 } 17 }
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 #include <string.h> 5 #include <string.h>
6 #include <time.h> 6 #include <time.h>
7 #include "printx.h" 7 #include "printx.h"
  8 +#include "ui.h"
8 9
9 #define FILENAME_LENGTH 24 10 #define FILENAME_LENGTH 24
10 #define S_RESET "\33[0m" 11 #define S_RESET "\33[0m"
@@ -13,11 +14,22 @@ @@ -13,11 +14,22 @@
13 FILE* logfile = NULL; 14 FILE* logfile = NULL;
14 char s_color[4][12] = {"\x1b[01;31m", "\x1b[01;33m", "\x1b[01;32m", "\x1b[01;36m"}; 15 char s_color[4][12] = {"\x1b[01;31m", "\x1b[01;33m", "\x1b[01;32m", "\x1b[01;36m"};
15 16
  17 +void removeCharFromString(char c, char *str)
  18 +{
  19 + int len = strlen(str)+1;
  20 +
  21 + for(int i=0; i<len; i++)
  22 + if(str[i] == c)
  23 + strncpy(&str[i],&str[i+1],len-i);
  24 +}
  25 +
16 bool initLog() 26 bool initLog()
17 { 27 {
18 char filename[FILENAME_LENGTH]; 28 char filename[FILENAME_LENGTH];
19 time_t now = time(NULL); 29 time_t now = time(NULL);
20 30
  31 + strftime(filename, FILENAME_LENGTH, "%F-%T:%d", localtime(&now));
  32 + setStartTime(filename);
21 strftime(filename, FILENAME_LENGTH, "log/%F-%T:%d", localtime(&now)); 33 strftime(filename, FILENAME_LENGTH, "log/%F-%T:%d", localtime(&now));
22 strcat(filename, ".log"); 34 strcat(filename, ".log");
23 35
@@ -39,12 +51,13 @@ void closeLog() @@ -39,12 +51,13 @@ void closeLog()
39 void printx(severity s, char* str, ...) 51 void printx(severity s, char* str, ...)
40 { 52 {
41 char buffer[MAX_BUFFER]; 53 char buffer[MAX_BUFFER];
  54 + char buffer2[MAX_BUFFER];
42 va_list arglist; 55 va_list arglist;
43 va_start(arglist, str); 56 va_start(arglist, str);
44 vsprintf(buffer, str, arglist); 57 vsprintf(buffer, str, arglist);
45 fprintf(logfile, buffer); 58 fprintf(logfile, buffer);
46 - printf(s_color[s]);  
47 - printf(buffer);  
48 - printf(S_RESET); 59 + sprintf(buffer2, "%s%s%s", s_color[s], buffer, S_RESET);
  60 + removeCharFromString('\n', buffer2);
  61 + addToLog(buffer2);
49 va_end(arglist); 62 va_end(arglist);
50 } 63 }
@@ -0,0 +1,82 @@ @@ -0,0 +1,82 @@
  1 +#include <stdio.h>
  2 +#include <sys/types.h>
  3 +#include <unistd.h>
  4 +#include <string.h>
  5 +#include <time.h>
  6 +#include "ui.h"
  7 +
  8 +#define TIME_LENGTH 24
  9 +#define HEADER_TEXT_LENGTH 24
  10 +#define LOG_LINES 25
  11 +#define LOG_LENGTH 69
  12 +
  13 +char tmpretheader[HEADER_TEXT_LENGTH+1];
  14 +char tmpretlog[HEADER_TEXT_LENGTH+1];
  15 +char uilog[LOG_LINES][LOG_LENGTH];
  16 +char started[TIME_LENGTH];
  17 +char uid[HEADER_TEXT_LENGTH];
  18 +char uidDate[HEADER_TEXT_LENGTH];
  19 +
  20 +void initUILog()
  21 +{
  22 + uid[0]='\0';
  23 + uidDate[0]='\0';
  24 + for(int i=0; i<LOG_LINES; i++)
  25 + uilog[i][0] = '\0';
  26 +}
  27 +
  28 +char* fillHeaderWithSpaces(char* text)
  29 +{
  30 + strcpy(tmpretheader, text);
  31 + while(strlen(tmpretheader) < HEADER_TEXT_LENGTH)
  32 + strcat(tmpretheader, " ");
  33 +
  34 + return tmpretheader;
  35 +}
  36 +
  37 +void addToLog(char* newStr)
  38 +{
  39 + for(int i=0; i<(LOG_LINES-1); i++)
  40 + strcpy(uilog[i+1], uilog[i]);
  41 + strcpy(uilog[0], newStr);
  42 +}
  43 +
  44 +char* fillLogWithSpaces(char* text)
  45 +{
  46 + strcpy(tmpretlog, text);
  47 + if(strlen(text) > 0)
  48 + while((strlen(tmpretlog)-12) < LOG_LENGTH)
  49 + strcat(tmpretlog, " ");
  50 + else
  51 + while(strlen(tmpretlog) < LOG_LENGTH)
  52 + strcat(tmpretlog, " ");
  53 +
  54 + return tmpretlog;
  55 +}
  56 +
  57 +
  58 +void setStartTime(char* sT)
  59 +{
  60 + strcpy(started, sT);
  61 +}
  62 +
  63 +void displayUI()
  64 +{
  65 + char buffer[HEADER_TEXT_LENGTH];
  66 + //header
  67 + printf("\u250F\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n");
  68 + printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces("Started since"));
  69 + printf(" %s \u2503\n", fillHeaderWithSpaces("No Tag Detected"));
  70 + printf("\u2503 %s \u2503 T W E E K \u2503 ", fillHeaderWithSpaces(started));
  71 + sprintf(buffer, "%s", strlen(uidDate) > 0 ? uidDate : "Waiting for a tag");
  72 + printf("%s \u2503\n", fillHeaderWithSpaces(buffer));
  73 + sprintf(buffer, "PID %d", getpid());
  74 + printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer));
  75 + sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope");
  76 + printf("%s \u2503\n", fillHeaderWithSpaces(buffer));
  77 + printf("\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n");
  78 + //body
  79 + for(int i=0; i<LOG_LINES; i++)
  80 + printf("\u2503 %s \u2503\n", fillLogWithSpaces(uilog[i]));
  81 + printf("\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n");
  82 +}
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +#ifndef __UI_H__
  2 +#define __UI_H__
  3 +
  4 +void initUILog();
  5 +void addToLog(char*);
  6 +void displayUI();
  7 +void setStartTime(char*);
  8 +
  9 +#endif