new: Add proper color support, move ncurses code to term.c

This commit is contained in:
ngn
2024-04-23 02:28:22 +03:00
parent bf056066a9
commit fd6489f13f
9 changed files with 271 additions and 227 deletions

View File

@ -1,36 +1,36 @@
#include <stdio.h>
#include <stdarg.h>
#include "log.h"
#include <stdarg.h>
#include <stdio.h>
void info(const char* msg, ...){
void info(const char *msg, ...) {
va_list args;
va_start(args, msg);
printf(LOG_BOLD LOG_BLUE">>> "LOG_RESET LOG_BOLD);
printf(C_BOLD C_BLUE ">>> " C_RESET C_BOLD);
vprintf(msg, args);
printf(LOG_RESET"\n");
printf(C_RESET "\n");
va_end(args);
}
void error(const char* msg, ...){
void error(const char *msg, ...) {
va_list args;
va_start(args, msg);
printf(LOG_BOLD LOG_RED">>> "LOG_RESET LOG_BOLD);
printf(C_BOLD C_RED ">>> " C_RESET C_BOLD);
vprintf(msg, args);
printf(LOG_RESET"\n");
printf(C_RESET "\n");
va_end(args);
}
void success(const char* msg, ...){
void success(const char *msg, ...) {
va_list args;
va_start(args, msg);
printf(LOG_BOLD LOG_GREEN">>> "LOG_RESET LOG_BOLD);
printf(C_BOLD C_GREEN ">>> " C_RESET C_BOLD);
vprintf(msg, args);
printf(LOG_RESET"\n");
printf(C_RESET "\n");
va_end(args);
}

View File

@ -1,9 +1,11 @@
#define LOG_RED "\x1b[31m"
#define LOG_BOLD "\x1b[1m"
#define LOG_BLUE "\x1b[34m"
#define LOG_GREEN "\x1b[32m"
#define LOG_RESET "\x1b[0m"
#pragma once
void info(const char* msg, ...);
void error(const char* msg, ...);
void success(const char* msg, ...);
#define C_RED "\x1b[31m"
#define C_BOLD "\x1b[1m"
#define C_BLUE "\x1b[34m"
#define C_GREEN "\x1b[32m"
#define C_RESET "\x1b[0m"
void info(const char *msg, ...);
void error(const char *msg, ...);
void success(const char *msg, ...);

View File

@ -18,137 +18,85 @@
*/
#include <sys/wait.h>
#include <stdbool.h>
#include <libintl.h>
#include <locale.h>
#include <limits.h>
#include <curses.h>
#include <libintl.h>
#include <limits.h>
#include <locale.h>
#include <menu.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <menu.h>
#include "util.h"
#include "log.h"
#include "term.h"
#include "util.h"
#define _(x) gettext(x)
int ask(char* text, ITEM** items, int sz) {
attron(COLOR_PAIR(1) | A_BOLD);
mvprintw(1, 1, "%s", text);
char line[strlen(text)+5];
for (int i = 0; i < strlen(text); i++){
line[i] = '#';
line[i+1] = '\0';
}
mvprintw(2, 1, "%s", line);
attroff(COLOR_PAIR(1) | A_BOLD);
refresh();
WINDOW* swin = newwin(LINES-3, COLS-1, 3, 1);
keypad(swin, TRUE);
MENU* menu = new_menu(items);
set_menu_win(menu, swin);
set_menu_sub(menu, swin);
set_menu_mark(menu, ">");
post_menu(menu);
wrefresh(swin);
bool done = false;
int indx = 0, c = 0;
while((c = wgetch(swin))){
switch(c){
case KEY_DOWN:
if (indx != (sz+1))
indx++;
menu_driver(menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
if (indx != 0)
indx--;
menu_driver(menu, REQ_UP_ITEM);
break;
case '\n':
done = true;
break;
}
wrefresh(swin);
if(done)
break;
}
free_menu(menu);
return indx;
}
bool add_startx(){
bool add_startx() {
bool ret = false;
char* profile = ".bash_profile";
char* shellenv = getenv("SHELL");
char *profile = ".bash_profile";
char *shellenv = getenv("SHELL");
if (NULL != shellenv && endswith(shellenv, "zsh"))
profile = ".zshrc";
char profile_file[PATH_MAX];
if(!joinhome(profile_file, profile)){
if (!joinhome(profile_file, profile)) {
error(_("Failed to get the home directory"));
return ret;
}
FILE* pf = fopen(profile_file, "r");
if(NULL==pf){
FILE *pf = fopen(profile_file, "r");
if (NULL == pf) {
error(_("Failed to open %s for reading"), profile_file);
return ret;
}
char* line = NULL, *all = NULL;
char *line = NULL, *all = NULL;
int indx = 0;
size_t sz, len;
while((sz = getline(&line, &len, pf)) != -1) {
if(strcmp(line, "#auto-startx \n")==0){
while ((sz = getline(&line, &len, pf)) != -1) {
if (strcmp(line, "#auto-startx \n") == 0) {
ret = true;
goto END;
}
if (NULL == all){
if (NULL == all) {
all = malloc(sz);
for(int i = 0; i < sz; i++){
for (int i = 0; i < sz; i++) {
all[indx] = line[i];
indx++;
}
}else {
all = realloc(all, indx+sz);
for(int i = 0; i < sz; i++){
} else {
all = realloc(all, indx + sz);
for (int i = 0; i < sz; i++) {
all[indx] = line[i];
indx++;
}
}
}
char* startx = AUTOSTARTX;
all = realloc(all, indx+strlen(startx)+10);
for(int i = 0; i < strlen(startx); i++){
char *startx = AUTOSTARTX;
all = realloc(all, indx + strlen(startx) + 10);
for (int i = 0; i < strlen(startx); i++) {
all[indx] = startx[i];
indx++;
}
fclose(pf);
pf = fopen(profile_file, "w");
if(NULL==pf){
if (NULL == pf) {
error(_("Failed to open %s for writing"), profile_file);
goto END;
goto END;
}
all[indx] = '\0';
if(fwrite(all, 1, indx, pf)<=0)
if (fwrite(all, 1, indx, pf) <= 0)
error(_("Failed to write to %s"), profile_file);
ret = true;
@ -159,8 +107,8 @@ END:
return ret;
}
int main(int argc, char** argv, char** envp){
if(getuid()==0){
int main(int argc, char **argv, char **envp) {
if (getuid() == 0) {
error(_("You should use this script as a regular user, not as root!"));
return EXIT_FAILURE;
}
@ -168,97 +116,78 @@ int main(int argc, char** argv, char** envp){
signal(SIGINT, SIG_IGN);
setlocale(LC_ALL, "");
textdomain("xcfg");
struct Desktop desktops[] = {
{
.name="XFCE4",
.desc=_("Lightweight desktop environment for UNIX-like operating systems"),
.pkg="xfce4",
.cmd="startxfce4\n",
},
{
.name="LXDE",
.desc=_("Free desktop environment with comparatively low resource requirements"),
.pkg="lxde",
.cmd="startlxde\n",
},
{
.name="bspwm",
.desc=_("Tiling window manager based on binary space partitioning"),
.pkg="bspwm",
.cmd="bspwm\n"
},
{
.name="i3",
.desc=_("Improved tiling window manager"),
.pkg="i3",
.cmd="i3\n"
}
};
char* mpi_path = check_path("mp-install");
if(NULL==mpi_path){
struct Desktop desktops[] = {
{
.name = "XFCE4",
.desc = _("Lightweight desktop environment for UNIX-like operating "
"systems"),
.pkg = "xfce4",
.cmd = "startxfce4\n",
},
{
.name = "LXDE",
.desc = _("Free desktop environment with comparatively low resource "
"requirements"),
.pkg = "lxde",
.cmd = "startlxde\n",
},
{.name = "bspwm",
.desc = _("Tiling window manager based on binary space partitioning"),
.pkg = "bspwm",
.cmd = "bspwm\n"},
{.name = "i3",
.desc = _("Improved tiling window manager"),
.pkg = "i3",
.cmd = "i3\n"}};
char *mpi_path = check_path("mp-install");
if (NULL == mpi_path) {
error(_("mp is not installed!"));
return EXIT_FAILURE;
}
free(mpi_path);
char* doas_path = check_path("doas");
if(NULL==doas_path) {
char *doas_path = check_path("doas");
if (NULL == doas_path) {
doas_path = check_path("sudo");
if(NULL==doas_path){
if (NULL == doas_path) {
error(_("Install doas or sudo to use this script"));
free(mpi_path);
return EXIT_FAILURE;
}
}
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_BLUE, COLOR_BLUE);
term_init();
int sz = sizeof(desktops)/sizeof(struct Desktop);
ITEM** items = malloc(sizeof(ITEM *)*(sz+1));
int sz = sizeof(desktops) / sizeof(struct Desktop);
ITEM **items = malloc(sizeof(ITEM *) * (sz + 1));
for(int i = 0; i < sz; i++)
for (int i = 0; i < sz; i++)
items[i] = new_item(desktops[i].name, desktops[i].desc);
items[sz] = NULL;
int indx = ask(_("Choose a desktop enviroment"), items, sz+1);
int indx = term_ask(_("Choose a desktop enviroment"), items, sz + 1);
for (int i = 0; i < sz; i++)
free_item(items[i]);
free(items);
clear();
char* yn[] = {_("Yes"), _("No"), NULL};
sz = sizeof(yn)/sizeof(char*);
items = (ITEM **)malloc(sizeof(ITEM *)*(sz+1));
bool autox = term_yn(_("Add auto-startx to shell configuration?"));
for(int i = 0; i < sz; i++)
items[i] = new_item(yn[i], "");
term_finish();
bool autox = ask(_("Add auto-startx to shell configuration?"), items, sz) == 0;
for (int i = 0; i < sz; i++)
free_item(items[i]);
free(items);
clear();
clear();
endwin();
if(autox && !add_startx())
if (autox && !add_startx())
goto FAIL;
char xinitrc[PATH_MAX];
if(!joinhome(xinitrc, ".xinitrc")){
if (!joinhome(xinitrc, ".xinitrc")) {
error(_("Failed to get the home directory"));
goto FAIL;
}
FILE* xf = fopen(xinitrc, "w");
if(fwrite(desktops[indx].cmd, 1, strlen(desktops[indx].cmd), xf)<0){
FILE *xf = fopen(xinitrc, "w");
if (fwrite(desktops[indx].cmd, 1, strlen(desktops[indx].cmd), xf) < 0) {
error(_("Failed to write to %s"), xinitrc);
fclose(xf);
goto FAIL;
@ -268,11 +197,9 @@ int main(int argc, char** argv, char** envp){
success(_("Configuration has been saved!"));
info(_("Installing %s"), desktops[indx].name, mpi_path);
char* args[] = {
doas_path, "mp-install", desktops[indx].pkg, NULL
};
char *args[] = {doas_path, "mp-install", desktops[indx].pkg, NULL};
if(execvp(doas_path, args) != 0) {
if (execvp(doas_path, args) != 0) {
error(_("Installation failed"));
}

100
src/term.c Normal file
View File

@ -0,0 +1,100 @@
#include <libintl.h>
#include <menu.h>
#include <ncurses.h>
#include <string.h>
#define _(x) gettext(x)
void term_init() {
initscr();
if (has_colors()) {
use_default_colors();
start_color();
}
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_GREEN, -1);
init_pair(2, COLOR_BLUE, -1);
start_color();
}
int term_ask(char *text, ITEM **items, int sz) {
attron(COLOR_PAIR(1) | A_BOLD);
mvprintw(1, 1, "%s", text);
char line[strlen(text) + 5];
for (int i = 0; i < strlen(text); i++) {
line[i] = '=';
line[i + 1] = '\0';
}
mvprintw(2, 1, "%s", line);
attroff(COLOR_PAIR(1) | A_BOLD);
refresh();
WINDOW *swin = newwin(LINES - 3, COLS - 1, 3, 1);
keypad(swin, TRUE);
MENU *menu = new_menu(items);
set_menu_win(menu, swin);
set_menu_sub(menu, swin);
set_menu_mark(menu, ">>> ");
set_menu_fore(menu, COLOR_PAIR(2));
post_menu(menu);
curs_set(0);
wrefresh(swin);
bool done = false;
int indx = 0, c = 0;
while ((c = wgetch(swin))) {
switch (c) {
case KEY_DOWN:
if (indx != (sz + 1))
indx++;
menu_driver(menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
if (indx != 0)
indx--;
menu_driver(menu, REQ_UP_ITEM);
break;
case '\n':
done = true;
break;
}
wrefresh(swin);
if (done)
break;
}
curs_set(1);
free_menu(menu);
return indx;
}
bool term_yn(char *text) {
char *yn[] = {_("Yes"), _("No"), NULL};
int sz = sizeof(yn) / sizeof(char *);
ITEM *items[sz + 1];
for (int i = 0; i < sz; i++)
items[i] = new_item(yn[i], "");
bool res = term_ask(_(text), items, sz) == 0;
for (int i = 0; i < sz; i++)
free_item(items[i]);
clear();
return res;
}
void term_finish() {
clear();
endwin();
}

7
src/term.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <menu.h>
void term_init();
int term_ask(char *, ITEM **, int);
bool term_yn(char *);
void term_finish();

View File

@ -1,49 +1,47 @@
#include <stdbool.h>
#include "util.h"
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "util.h"
bool joinhome(char* res, char* path){
char* homedir = getenv("HOME");
bool joinhome(char *res, char *path) {
char *homedir = getenv("HOME");
if(NULL==homedir)
if (NULL == homedir)
return false;
snprintf(res, PATH_MAX, "%s/%s", homedir, path);
return true;
}
bool exists(char* path){
return access(path, F_OK)==0;
}
bool exists(char *path) { return access(path, F_OK) == 0; }
bool endswith(const char *str, const char *suf){
bool endswith(const char *str, const char *suf) {
int strl = strlen(str);
int sufl = strlen(suf);
if (sufl > strl)
if (sufl > strl)
return false;
return strncmp(str + strl - sufl, suf, sufl) == 0;
}
char* check_path(char* bin) {
char* path = getenv("PATH");
char* res = NULL;
char *check_path(char *bin) {
char *path = getenv("PATH");
char *res = NULL;
if(NULL == path)
if (NULL == path)
return NULL;
path = strdup(path);
char* p = strtok(path, ":");
while(NULL != p) {
char* fp = malloc(PATH_MAX);
char *p = strtok(path, ":");
while (NULL != p) {
char *fp = malloc(PATH_MAX);
snprintf(fp, PATH_MAX, "%s/%s", p, bin);
if(exists(fp)){
if (exists(fp)) {
res = fp;
break;
}

View File

@ -1,13 +1,18 @@
#define AUTOSTARTX "\n#auto-startx \nif [[ \"$(tty)\" == \"/dev/tty1\" ]] && [ -z \"$DISPLAY\" ]; then\nexec startx\nfi\n"
#pragma once
#include <stdbool.h>
struct Desktop {
char* name;
char* desc;
char* pkg;
char* cmd;
};
#define AUTOSTARTX \
"\n#auto-startx \nif [[ \"$(tty)\" == \"/dev/tty1\" ]] && [ -z " \
"\"$DISPLAY\" ]; then\nexec startx\nfi\n"
bool exists(char*);
char* check_path(char*);
bool joinhome(char*, char*);
bool endswith(const char*, const char*);
typedef struct Desktop {
char *name;
char *desc;
char *pkg;
char *cmd;
} desktop_t;
bool exists(char *);
char *check_path(char *);
bool joinhome(char *, char *);
bool endswith(const char *, const char *);