Files
zenith-cli/main.cpp
T

118 lines
2.8 KiB
C++

#include <iostream>
#include <string>
#include <filesystem>
#include <cstdlib>
#include "curl/curl.h"
extern "C" {
#include "libs/argparse.h"
}
namespace fs = std::filesystem;
struct Scheme{
char fg_color[8];
char sub_fg_color[8];
char bg_color[8];
char sub_bg_color[8];
char accent[8];
char sub_accent[8];
};
void write_pallete(const struct Scheme *s){
//s->bg_color
}
int download_wall(const char* link, const char* path){
CURL* curl;
FILE* fp;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
fp = fopen(path, "wb");
if(fp == NULL) {
fprintf(stderr, \
"ошибка: не удалось создать или открыть файл %s\n", path);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, link);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "ошибка скачивания: %s\n", \
curl_easy_strerror(res));
}
fclose(fp);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
int main(int argc, const char **argv) {
const char* link = nullptr;
const char* raw_scheme_path = nullptr;
const char* raw_wall_path = nullptr;
int is_pastel = 0;
struct argparse_option options[] = {
OPT_HELP(),
OPT_STRING(0,"link", &link, \
"ссылка на изображение с сайта wallhave.cc", NULL, 0, 0),
OPT_BOOLEAN('p', "pastel", &is_pastel, \
"создать пастельные палитры", NULL, 0, 0),
OPT_STRING(0,"scheme-output", &raw_scheme_path,\
"директория для сохранения палитр", NULL, 0, 0),
OPT_STRING(0,"wall-output", &raw_wall_path, \
"директория для сохранения обоев", NULL, 0, 0),
OPT_END(),
};
struct argparse argparse;
argparse_init(&argparse, options, nullptr, 0);
argc = argparse_parse(&argparse, argc, argv);
fs::path target_dir;
if (raw_wall_path == nullptr || std::string(raw_wall_path).empty()){
target_dir = fs::current_path();
}
else{
std::string path_str(raw_wall_path);
if (path_str[0] == '~'){
const char* user_home = std::getenv("HOME");
if (user_home){
path_str.replace(0, 1, user_home);
}
}
target_dir = fs::path(path_str);
}
std::string full_path_str(target_dir / "wallpaper.jpg");
const char* full_path = full_path_str.c_str();
download_wall(link, full_path);
return 0;
}