Skip to content

Commit 0f29877

Browse files
committed
add smart-fetch app
1 parent 36d1223 commit 0f29877

4 files changed

Lines changed: 415 additions & 0 deletions

File tree

apps/smart-fetch/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# smart-fetch
2+
3+
Smart-fetch is a command-line system information tool written in c. Smart-fetch displays information about your operating system, software and hardware in an aesthetic and virsually pleasing way.
4+
5+
Smart-fetch is currently an initial version, but it will be more feature-rich in the future.See the help information for instructions.**smart-fetch -h**

apps/smart-fetch/smart_fetch.c

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
/*
2+
* File : smart_fetch.c
3+
* This file is part of RT-Thread RTOS
4+
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
5+
*
6+
* This software is dual-licensed: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation. For the terms of this
9+
* license, see <http://www.gnu.org/licenses/>.
10+
*
11+
* You are free to use this software under the terms of the GNU General
12+
* Public License, but WITHOUT ANY WARRANTY; without even the implied
13+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* Alternatively for commercial application, you can contact us
17+
* by email <business@rt-thread.com> for commercial license.
18+
*
19+
* Change Logs:
20+
* Date Author Notes
21+
* 2023-06-06 Mr'Zhou the first version
22+
*/
23+
24+
#include <stdlib.h>
25+
#include <stdio.h>
26+
#include <time.h>
27+
#include <unistd.h>
28+
#include <getopt.h>
29+
#include <sys/utsname.h>
30+
#include <sys/statfs.h>
31+
#include <sys/sysinfo.h>
32+
#include "smart_fetch.h"
33+
#include <string.h>
34+
35+
static void smart_fetch_print_info()
36+
{
37+
printf("\033[20A \n");
38+
printf(RED " %s time: " NONE BLUE "%d-%d-%d " NONE GREEN "%d:%d:%d\n" NONE, RIGHT_MOVE, (1900 + smart_p->tm_year), ( 1 + smart_p->tm_mon), smart_p->tm_mday, (smart_p->tm_hour + 12), smart_p->tm_min, smart_p->tm_sec);
39+
printf("%s %s sysname: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.sysname);
40+
printf("%s %s nodename: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.nodename);
41+
printf("%s %s release: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.release);
42+
printf("%s %s version: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.version);
43+
printf("%s %s machine: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.machine);
44+
printf("%s %s domainname: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.__domainname);
45+
printf("%s %s f_bsize: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_bsize);
46+
printf("%s %s f_blocks: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_blocks);
47+
printf("%s %s f_bfree: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_bfree);
48+
printf("%s %s f_bavail: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_bavail);
49+
printf("%s \n", RIGHT_MOVE);
50+
printf("%s \n", RIGHT_MOVE);
51+
printf("%s \n", RIGHT_MOVE);
52+
printf("%s \n", RIGHT_MOVE);
53+
printf("%s " RED "github id:" NONE FONT_FLICKER GREEN GITHUB_ID NONE "\n", RIGHT_MOVE);
54+
printf("%s \n", RIGHT_MOVE);
55+
printf(" %s" FONT_FLICKER BG_BLACK " " BG_RED " " BG_GREEN " " BG_YELLOW " " BG_BLUE " " BG_PURPLE " " BG_CYAN " " BG_WHITE " " NONE "\n", RIGHT_MOVE );
56+
printf(" %s" BG_BLACK " " BG_RED " " BG_GREEN " " BG_YELLOW " " BG_BLUE " " BG_PURPLE " " BG_CYAN " " BG_WHITE " " NONE "\n", RIGHT_MOVE);
57+
58+
}
59+
60+
static void smart_fetch_print_logo()
61+
{
62+
printf("\n \
63+
\e[0;31m.oyssssssssssssssssssssssssssy+.\e[0m \n \
64+
\e[0;31m/yossssssss\e[0mrrrr\e[0;31msssss\e[0mrrrr\e[0;31msssssssoy+\e[0m \n \
65+
\e[0;31m:h/sssssssss\e[0mrrrr\e[0;31msss\e[0mrrrr\e[0;31msssssssssh+-\e[0m \n \
66+
\e[0;31mys.ssssssssss\e[0mrrrrrrr\e[0;31msssssssssssssss+-p\e[0m \n \
67+
\e[0;31mh+-sssssssssss\e[0mrrrr\e[0;31msssssssssssssssssss:h/\e[0m \n \
68+
\e[0;31mh+-ssssssssssss\e[0mrrrr\e[0;31mssssssssssssssssssss+a'\e[0m \n \
69+
\e[0;31m/dmsssssssssssss\e[0mrrrr\e[0;31msssssssssssssssssssssz8`\e[0m \n \
70+
\e[0;31mc/ysssssssssssssss\e[0m\e[5m\e[3;32mrt-thread\e[0m\e[0;31mssssssssssssssss-f'\e[0m \n \
71+
\e[0;31mp/osssss\e[0mttttttttttttt\e[0;31mssssss\e[0mtttttttttttt\e[0;31msssss+j;\e[0m \n \
72+
\e[0;31mp/osssss\e[0mttttttttttttt\e[0;31mssssss\e[0mttttttttttttt\e[0;31msssss+j;\e[0m \n \
73+
\e[0;31mc/yssssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31mssssssss-k/\e[0m \n \
74+
\e[0;31m/dmssssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31msssssss*c'\e[0m \n \
75+
\e[0;31mh+-sssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31mssssss+a'\e[0m \n \
76+
\e[0;31mh+-ssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31msssss:h/\e[0m \n \
77+
\e[0;31mys.sssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31mssss+-p\e[0m \n \
78+
\e[0;31m:h/ssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31msssh+-\e[0m \n \
79+
\e[0;31m/yosssssssssssssssssssssssssssssoy+\e[0m \n \
80+
\e[0;31m.oysssssssssssssssssssssssssssy+.\e[0m \n \
81+
");
82+
}
83+
84+
void smart_fetch_get_time()
85+
{
86+
time_t timep;
87+
time(&timep);
88+
smart_p = localtime(&timep);
89+
}
90+
91+
int smart_fetch_get_utsname()
92+
{
93+
memset(&smart_sys_info, '\0', sizeof(smart_sys_info));
94+
if(uname(&smart_sys_info) != -1)
95+
{
96+
return SUCCESS_SYS_INFO;
97+
}
98+
return ERROR_SYS_INFO;
99+
}
100+
101+
int smart_fetch_get_statfs()
102+
{
103+
if(statfs(smart_sys_path,&smart_sys_block) != -1)
104+
{
105+
return SUCCESS_SYS_INFO;
106+
}
107+
return ERROR_SYS_INFO;
108+
}
109+
110+
int smart_fetch_get_memory()
111+
{
112+
char buffer[1024];
113+
FILE *fp;
114+
fp = popen("free", "r");
115+
if(fp < 0)
116+
{
117+
return ;
118+
}
119+
while(fgets(buffer , sizeof(buffer), fp) != NULL)
120+
{
121+
printf("%s", buffer);
122+
}
123+
pclose(fp);
124+
return SUCCESS_SYS_INFO;
125+
}
126+
127+
static void smart_fetch_color_parser(int b_font,char *t_color)
128+
{
129+
char color[16];
130+
if(t_color == NULL)
131+
{
132+
return ;
133+
}
134+
memset(color, '\0', sizeof(color));
135+
if(smart_bold_font != 0)
136+
{
137+
if((strcmp(t_color,"BLACK")) == 0)
138+
{
139+
strcpy(color,L_BLACK);
140+
}
141+
else if((strcmp(t_color,"RED")) == 0)
142+
{
143+
strcpy(color,L_RED);
144+
}
145+
else if((strcmp(t_color,"GREEN")) == 0)
146+
{
147+
strcpy(color,L_GREEN);
148+
}
149+
else if((strcmp(t_color,"YELLOW")) == 0)
150+
{
151+
strcpy(color,YELLOW);
152+
}
153+
else if((strcmp(t_color,"BLUE")) == 0)
154+
{
155+
strcpy(color,L_BLUE);
156+
}
157+
else if((strcmp(t_color,"PURPLE")) == 0)
158+
{
159+
strcpy(color,L_PURPLE);
160+
}
161+
else if((strcmp(t_color,"CYAN")) == 0)
162+
{
163+
strcpy(color,L_CYAN);
164+
}
165+
else if((strcmp(t_color,"WHITE")) == 0)
166+
{
167+
strcpy(color,WHITE);
168+
}
169+
else
170+
{
171+
strcpy(color,L_RED);
172+
}
173+
174+
}
175+
else
176+
{
177+
if((strcmp(t_color,"BLACK")) == 0)
178+
{
179+
strcpy(color,BLACK);
180+
}
181+
else if((strcmp(t_color,"RED")) == 0)
182+
{
183+
strcpy(color,RED);
184+
}
185+
else if((strcmp(t_color,"GREEN")) == 0)
186+
{
187+
strcpy(color,GREEN);
188+
}
189+
else if((strcmp(t_color,"YELLOW")) == 0)
190+
{
191+
strcpy(color,BROWN);
192+
}
193+
else if((strcmp(t_color,"BLUE")) == 0)
194+
{
195+
strcpy(color,BLUE);
196+
}
197+
else if((strcmp(t_color,"PURPLE")) == 0)
198+
{
199+
strcpy(color,PURPLE);
200+
}
201+
else if((strcmp(t_color,"CYAN")) == 0)
202+
{
203+
strcpy(color,CYAN);
204+
}
205+
else if((strcmp(t_color,"WHITE")) == 0)
206+
{
207+
strcpy(color,GRAY);
208+
}
209+
else
210+
{
211+
strcpy(color,GREEN);
212+
}
213+
}
214+
memset(t_color, '\0', sizeof(t_color));
215+
strcpy(t_color, color);
216+
}
217+
218+
static void smart_fetch_color_deal()
219+
{
220+
smart_fetch_color_parser(smart_bold_font, smart_title_color);
221+
smart_fetch_color_parser(smart_bold_font, smart_info_color);
222+
}
223+
224+
static void smart_fetch_help()
225+
{
226+
printf("\t\tsmart_fetch help\n");
227+
printf("\t\t cmd\texplain\n");
228+
printf("\t\t -t \t:update titile color\n");
229+
printf("\t\t -c \t:update information color\n");
230+
printf("\t\t -d \t:disable info\n");
231+
printf("\t\t -b \t:blod font\n");
232+
printf("\t\t example : smart-fetch -b -c YELLOW -t GREEN \n");
233+
}
234+
235+
int smart_fetch_param_parser(int argc, char **argv)
236+
{
237+
char *opt;
238+
int idx;
239+
if (argc < 1)
240+
{
241+
return SUCCESS_SYS_INFO;
242+
}
243+
memset(smart_title_color, '\0', sizeof(smart_title_color));
244+
memset(smart_info_color, '\0', sizeof(smart_info_color));
245+
while((idx = getopt_long(argc, argv, smart_short_option, smart_long_option, NULL)) != EOF)
246+
{
247+
switch(idx)
248+
{
249+
case 't' :
250+
opt = optarg;
251+
strcpy(smart_title_color, opt);
252+
break;
253+
case 'c' :
254+
opt = optarg;
255+
strcpy(smart_info_color, opt);
256+
break;
257+
case 'd' :
258+
opt = optarg;
259+
break;
260+
case 'b' :
261+
smart_bold_font = 1;
262+
break;
263+
case 'h' :
264+
smart_fetch_help();
265+
return ;
266+
default :
267+
break;
268+
}
269+
}
270+
smart_fetch_color_deal();
271+
smart_fetch_get_time();
272+
smart_fetch_get_utsname();
273+
smart_fetch_get_statfs();
274+
smart_fetch_print_logo();
275+
smart_fetch_print_info();
276+
return SUCCESS_SYS_INFO;
277+
}
278+
279+
280+
int main(int argc, char **argv)
281+
{
282+
smart_fetch_param_parser(argc, argv);
283+
return 0;
284+
}

apps/smart-fetch/smart_fetch.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* File : smart_fetch.h
3+
* This file is part of RT-Thread RTOS
4+
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
5+
*
6+
* This software is dual-licensed: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation. For the terms of this
9+
* license, see <http://www.gnu.org/licenses/>.
10+
*
11+
* You are free to use this software under the terms of the GNU General
12+
* Public License, but WITHOUT ANY WARRANTY; without even the implied
13+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* Alternatively for commercial application, you can contact us
17+
* by email <business@rt-thread.com> for commercial license.
18+
*
19+
* You should have received a copy of the GNU General Public License along
20+
* with this program; if not, write to the Free Software Foundation, Inc.,
21+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22+
*
23+
* Change Logs:
24+
* Date Author Notes
25+
* 2023-06-06 Bernard the first version
26+
*/
27+
28+
29+
#ifndef SMART_FETCH_H__
30+
#define SMART_FETCH_H__
31+
32+
#define ERROR_SYS_INFO -1
33+
#define SUCCESS_SYS_INFO 0
34+
35+
36+
#define NONE "\e[0m"
37+
#define BLACK "\e[0;30m"
38+
#define L_BLACK "\e[1;30m"
39+
#define RED "\e[0;31m"
40+
#define L_RED "\e[1;31m"
41+
#define GREEN "\e[0;32m"
42+
#define L_GREEN "\e[1;32m"
43+
#define BROWN "\e[0;33m"
44+
#define YELLOW "\e[1;33m"
45+
#define BLUE "\e[0;34m"
46+
#define L_BLUE "\e[1;34m"
47+
#define PURPLE "\e[0;35m"
48+
#define L_PURPLE "\e[1;35m"
49+
#define CYAN "\e[0;36m"
50+
#define L_CYAN "\e[1;36m"
51+
#define GRAY "\e[0;37m"
52+
#define WHITE "\e[1;37m"
53+
54+
#define BG_BLACK "\e[40m"
55+
#define BG_RED "\e[41m"
56+
#define BG_GREEN "\e[42m"
57+
#define BG_YELLOW "\e[43m"
58+
#define BG_BLUE "\e[44m"
59+
#define BG_PURPLE "\e[45m"
60+
#define BG_CYAN "\e[46m"
61+
#define BG_WHITE "\e[47m"
62+
63+
#define BOLD "\e[1m"
64+
#define UNDERLINE "\e[4m"
65+
#define BLINK "\e[5m"
66+
#define REVERSE "\e[7m"
67+
#define HIDE "\e[8m"
68+
#define CLEAR "\e[2J"
69+
#define CLRLINE "\r\e[K"
70+
71+
#define RIGHT_MOVE "\e[60C"
72+
#define FONT_FLICKER "\e[5m"
73+
74+
#define GITHUB_ID "zmq810150896"
75+
const struct option smart_long_option[] =
76+
{
77+
"title_color", 1, NULL, 't',
78+
"info_color", 1, NULL, 'c',
79+
"disable", 1, NULL, 'd',
80+
"bold", 0, NULL, 'b',
81+
"help", 0, NULL, 'h',
82+
{NULL, 0, NULL, 0}
83+
};
84+
85+
struct statfs smart_sys_block;
86+
struct utsname smart_sys_info;
87+
struct tm *smart_p;
88+
89+
const char const *smart_short_option = "t:c:d:bh";
90+
int smart_bold_font = 0;
91+
char smart_title_color[16]=RED;
92+
char smart_info_color[16]=GREEN;
93+
char smart_sys_path[256] = "/";
94+
95+
#endif /*__SMART_FETCH_H__*/

0 commit comments

Comments
 (0)