Skip to content

Commit 4116456

Browse files
authored
Merge pull request #28 from RT-Thread/fetch
add smart-fetch app
2 parents 36d1223 + 40f2a7e commit 4116456

4 files changed

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

apps/smart-fetch/smart_fetch.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2023, Real-Thread Technology Co., Ltd
3+
*
4+
* SPDX-License-Identifier: GPL-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2023-06-06 Bernard the first version
9+
*/
10+
11+
#ifndef SMART_FETCH_H__
12+
#define SMART_FETCH_H__
13+
14+
#define ERROR_SYS_INFO -1
15+
#define SUCCESS_SYS_INFO 0
16+
17+
#define NONE "\e[0m"
18+
#define BLACK "\e[0;30m"
19+
#define L_BLACK "\e[1;30m"
20+
#define RED "\e[0;31m"
21+
#define L_RED "\e[1;31m"
22+
#define GREEN "\e[0;32m"
23+
#define L_GREEN "\e[1;32m"
24+
#define BROWN "\e[0;33m"
25+
#define YELLOW "\e[1;33m"
26+
#define BLUE "\e[0;34m"
27+
#define L_BLUE "\e[1;34m"
28+
#define PURPLE "\e[0;35m"
29+
#define L_PURPLE "\e[1;35m"
30+
#define CYAN "\e[0;36m"
31+
#define L_CYAN "\e[1;36m"
32+
#define GRAY "\e[0;37m"
33+
#define WHITE "\e[1;37m"
34+
35+
#define BG_BLACK "\e[40m"
36+
#define BG_RED "\e[41m"
37+
#define BG_GREEN "\e[42m"
38+
#define BG_YELLOW "\e[43m"
39+
#define BG_BLUE "\e[44m"
40+
#define BG_PURPLE "\e[45m"
41+
#define BG_CYAN "\e[46m"
42+
#define BG_WHITE "\e[47m"
43+
44+
#define BOLD "\e[1m"
45+
#define UNDERLINE "\e[4m"
46+
#define BLINK "\e[5m"
47+
#define REVERSE "\e[7m"
48+
#define HIDE "\e[8m"
49+
#define CLEAR "\e[2J"
50+
#define CLRLINE "\r\e[K"
51+
52+
#define RIGHT_MOVE "\e[60C"
53+
#define FONT_FLICKER "\e[5m"
54+
55+
#define GITHUB_ID "zmq810150896"
56+
const struct option smart_long_option[] =
57+
{
58+
"title_color", 1, NULL, 't',
59+
"info_color", 1, NULL, 'c',
60+
"disable", 1, NULL, 'd',
61+
"bold", 0, NULL, 'b',
62+
"help", 0, NULL, 'h',
63+
{NULL, 0, NULL, 0}
64+
};
65+
66+
struct statfs smart_sys_block;
67+
struct utsname smart_sys_info;
68+
struct tm *smart_p;
69+
70+
const char const *smart_short_option = "t:c:d:bh";
71+
int smart_bold_font = 0;
72+
char smart_title_color[16]=RED;
73+
char smart_info_color[16]=GREEN;
74+
char smart_sys_path[256] = "/";
75+
76+
#endif /*__SMART_FETCH_H__*/

apps/smart-fetch/xmake.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- Licensed under the Apache License, Version 2.0 (the "License");
2+
-- You may not use this file except in compliance with the License.
3+
-- You may obtain a copy of the License at
4+
--
5+
-- http://www.apache.org/licenses/LICENSE-2.0
6+
--
7+
-- Unless required by applicable law or agreed to in writing, software
8+
-- distributed under the License is distributed on an "AS IS" BASIS,
9+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
-- See the License for the specific language governing permissions and
11+
-- limitations under the License.
12+
--
13+
-- Copyright (C) 2022-2023 RT-Thread Development Team
14+
--
15+
-- @author zhouquan
16+
-- @file xmake.lua
17+
--
18+
-- Change Logs:
19+
-- Date Author Notes
20+
-- ------------ ---------- -----------------------------------------------
21+
-- 2023-03-10 zhouquan initial version
22+
--
23+
add_rules("mode.debug", "mode.release")
24+
25+
target("smart-fetch")
26+
do
27+
add_includedirs("./")
28+
add_files("*.c")
29+
end
30+
target_end()
31+

0 commit comments

Comments
 (0)