new: Add proper color support, move ncurses code to term.c
This commit is contained in:
parent
bf056066a9
commit
fd6489f13f
9
Makefile
9
Makefile
@ -1,9 +1,11 @@
|
||||
prefix = /usr
|
||||
SRCS = $(wildcard src/*.c)
|
||||
HEADERS = $(wildcard src/*.h)
|
||||
|
||||
PO_DIRS = $(wildcard locale/*/*)
|
||||
PO_FILES = $(wildcard locale/*/*/*.po)
|
||||
|
||||
prefix = /usr
|
||||
|
||||
dist/xcfg: $(SRCS) $(HEADERS) $(PO_FILES)
|
||||
mkdir -p dist
|
||||
gcc $(CFLAGS) $(SRCS) -o $@ -lncurses -lmenu -lm
|
||||
@ -35,4 +37,7 @@ install:
|
||||
uninstall:
|
||||
rm $(DESTDIR)$(prefix)/bin/xcfg
|
||||
|
||||
.PHONY: install uninstall
|
||||
format:
|
||||
clang-format -i -style=file src/*.c src/*.h
|
||||
|
||||
.PHONY: install uninstall format
|
||||
|
@ -7,7 +7,7 @@ msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2024-03-24 03:18+0300\n"
|
||||
"POT-Creation-Date: 2024-04-23 02:27+0300\n"
|
||||
"PO-Revision-Date: 2024-02-20 20:36+0300\n"
|
||||
"Last-Translator: <ngn@ngn.tf>\n"
|
||||
"Language-Team: Turkish <gnome-turk@gnome.org>\n"
|
||||
@ -17,83 +17,83 @@ msgstr ""
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
||||
|
||||
#: src/main.c:101 src/main.c:256
|
||||
#: src/main.c:49 src/main.c:185
|
||||
#, fuzzy
|
||||
msgid "Failed to get the home directory"
|
||||
msgstr "Ev dizini bulmak başarısız oldu"
|
||||
|
||||
#: src/main.c:107
|
||||
#: src/main.c:55
|
||||
#, c-format
|
||||
msgid "Failed to open %s for reading"
|
||||
msgstr "%s dosyasını okumak için açmak başarısız oldu"
|
||||
|
||||
#: src/main.c:146
|
||||
#: src/main.c:94
|
||||
#, c-format
|
||||
msgid "Failed to open %s for writing"
|
||||
msgstr "%s dosyasını yazmak için açmak başarısız oldu"
|
||||
|
||||
#: src/main.c:152 src/main.c:262
|
||||
#: src/main.c:100 src/main.c:191
|
||||
#, c-format
|
||||
msgid "Failed to write to %s"
|
||||
msgstr "%s dosyasına yazmak başarısız oldu"
|
||||
|
||||
#: src/main.c:164
|
||||
#: src/main.c:112
|
||||
msgid "You should use this script as a regular user, not as root!"
|
||||
msgstr ""
|
||||
"Bu betiği normal bir kullanıcı olarak kullanmalısınız, kök kullanıcı olarak "
|
||||
"değil!"
|
||||
|
||||
#: src/main.c:175
|
||||
#: src/main.c:123
|
||||
msgid "Lightweight desktop environment for UNIX-like operating systems"
|
||||
msgstr "UNIX-gibi işletim sistemleri için hafif bir masaütü"
|
||||
|
||||
#: src/main.c:181
|
||||
#: src/main.c:130
|
||||
msgid "Free desktop environment with comparatively low resource requirements"
|
||||
msgstr "Nispeten düşük sistem gereksinimleri olan özgür bir masaüstü ortamı"
|
||||
|
||||
#: src/main.c:187
|
||||
#: src/main.c:136
|
||||
msgid "Tiling window manager based on binary space partitioning"
|
||||
msgstr "İkili alan bölme tabanlı döşeme pencere yöneticisi"
|
||||
|
||||
#: src/main.c:193
|
||||
#: src/main.c:140
|
||||
msgid "Improved tiling window manager"
|
||||
msgstr "Geliştirilmiş döşeme pencere yöneticisi"
|
||||
|
||||
#: src/main.c:201
|
||||
#: src/main.c:146
|
||||
#, fuzzy
|
||||
msgid "mp is not installed!"
|
||||
msgstr "mp kurulu değil"
|
||||
|
||||
#: src/main.c:210
|
||||
#: src/main.c:155
|
||||
msgid "Install doas or sudo to use this script"
|
||||
msgstr "Bu betiği kullanmak için doas ya da sudo kurun"
|
||||
|
||||
#: src/main.c:229
|
||||
#: src/main.c:170
|
||||
msgid "Choose a desktop enviroment"
|
||||
msgstr "Bir masaüstü ortamı seçin"
|
||||
|
||||
#: src/main.c:235
|
||||
msgid "Yes"
|
||||
msgstr "Evet"
|
||||
|
||||
#: src/main.c:235
|
||||
msgid "No"
|
||||
msgstr "Hayır"
|
||||
|
||||
#: src/main.c:242
|
||||
#: src/main.c:176
|
||||
msgid "Add auto-startx to shell configuration?"
|
||||
msgstr "Otomatik-startx kabuk konfigürasyonu ekle?"
|
||||
|
||||
#: src/main.c:268
|
||||
#: src/main.c:197
|
||||
msgid "Configuration has been saved!"
|
||||
msgstr "Konfigürasyon kaydedildi!"
|
||||
|
||||
#: src/main.c:270
|
||||
#: src/main.c:199
|
||||
#, fuzzy, c-format
|
||||
msgid "Installing %s"
|
||||
msgstr "%s kuruluyor"
|
||||
|
||||
#: src/main.c:276
|
||||
#: src/main.c:203
|
||||
#, fuzzy
|
||||
msgid "Installation failed"
|
||||
msgstr "Kurulum başarısız oldu"
|
||||
|
||||
#: src/term.c:82
|
||||
msgid "Yes"
|
||||
msgstr "Evet"
|
||||
|
||||
#: src/term.c:82
|
||||
msgid "No"
|
||||
msgstr "Hayır"
|
||||
|
16
src/log.c
16
src/log.c
@ -1,14 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include "log.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
||||
@ -17,9 +17,9 @@ 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);
|
||||
}
|
||||
@ -28,9 +28,9 @@ 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);
|
||||
}
|
||||
|
12
src/log.h
12
src/log.h
@ -1,8 +1,10 @@
|
||||
#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
|
||||
|
||||
#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, ...);
|
||||
|
119
src/main.c
119
src/main.c
@ -18,76 +18,24 @@
|
||||
|
||||
*/
|
||||
|
||||
#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 ret = false;
|
||||
|
||||
@ -172,29 +120,26 @@ int main(int argc, char** argv, char** envp){
|
||||
struct Desktop desktops[] = {
|
||||
{
|
||||
.name = "XFCE4",
|
||||
.desc=_("Lightweight desktop environment for UNIX-like operating systems"),
|
||||
.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"),
|
||||
.desc = _("Free desktop environment with comparatively low resource "
|
||||
"requirements"),
|
||||
.pkg = "lxde",
|
||||
.cmd = "startlxde\n",
|
||||
},
|
||||
{
|
||||
.name="bspwm",
|
||||
{.name = "bspwm",
|
||||
.desc = _("Tiling window manager based on binary space partitioning"),
|
||||
.pkg = "bspwm",
|
||||
.cmd="bspwm\n"
|
||||
},
|
||||
{
|
||||
.name="i3",
|
||||
.cmd = "bspwm\n"},
|
||||
{.name = "i3",
|
||||
.desc = _("Improved tiling window manager"),
|
||||
.pkg = "i3",
|
||||
.cmd="i3\n"
|
||||
}
|
||||
};
|
||||
.cmd = "i3\n"}};
|
||||
|
||||
char *mpi_path = check_path("mp-install");
|
||||
if (NULL == mpi_path) {
|
||||
@ -213,11 +158,7 @@ int main(int argc, char** argv, char** envp){
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
@ -226,27 +167,15 @@ int main(int argc, char** argv, char** envp){
|
||||
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], "");
|
||||
|
||||
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();
|
||||
term_finish();
|
||||
|
||||
if (autox && !add_startx())
|
||||
goto FAIL;
|
||||
@ -268,9 +197,7 @@ 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) {
|
||||
error(_("Installation failed"));
|
||||
|
100
src/term.c
Normal file
100
src/term.c
Normal 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
7
src/term.h
Normal 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();
|
10
src/util.c
10
src/util.c
@ -1,11 +1,11 @@
|
||||
#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");
|
||||
@ -17,9 +17,7 @@ bool joinhome(char* res, char* 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) {
|
||||
int strl = strlen(str);
|
||||
|
11
src/util.h
11
src/util.h
@ -1,11 +1,16 @@
|
||||
#define AUTOSTARTX "\n#auto-startx \nif [[ \"$(tty)\" == \"/dev/tty1\" ]] && [ -z \"$DISPLAY\" ]; then\nexec startx\nfi\n"
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
|
||||
struct Desktop {
|
||||
#define AUTOSTARTX \
|
||||
"\n#auto-startx \nif [[ \"$(tty)\" == \"/dev/tty1\" ]] && [ -z " \
|
||||
"\"$DISPLAY\" ]; then\nexec startx\nfi\n"
|
||||
|
||||
typedef struct Desktop {
|
||||
char *name;
|
||||
char *desc;
|
||||
char *pkg;
|
||||
char *cmd;
|
||||
};
|
||||
} desktop_t;
|
||||
|
||||
bool exists(char *);
|
||||
char *check_path(char *);
|
||||
|
Loading…
Reference in New Issue
Block a user