Skip to content

Commit

Permalink
initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusDierheimer committed Feb 18, 2021
0 parents commit ebb14af
Show file tree
Hide file tree
Showing 28 changed files with 948 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/build/
**/.vscode/
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.0)

project(fastfetch)

include (${CMAKE_ROOT}/Modules/FindX11.cmake)

add_executable(fastfetch
src/fastfetch.c
src/break.c
src/logo.c
src/title.c
src/seperator.c
src/os.c
src/host.c
src/kernel.c
src/uptime.c
src/packages.c
src/shell.c
src/resolution.c
src/desktopenvironment.c
src/theme.c
src/icons.c
src/font.c
src/terminal.c
src/cpu.c
src/gpu.c
src/memory.c
src/disk.c
src/battery.c
src/locale.c
src/colors.c
)

target_link_libraries(fastfetch
${X11_LIBRARIES}
pci
)
5 changes: 5 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mkdir -p build/
cd build/
cmake ..
cmake --build .
./fastfetch
31 changes: 31 additions & 0 deletions src/battery.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "fastfetch.h"

void ffPrintBattery(FFstate* state)
{

FILE* fullFile = fopen("/sys/class/power_supply/BAT0/charge_full", "r");
if(fullFile == NULL)
return;
uint32_t full;
fscanf(fullFile, "%lu", &full);
fclose(fullFile);

FILE* nowFile = fopen("/sys/class/power_supply/BAT0/charge_full", "r");
if(nowFile == NULL)
return;
uint32_t now;
fscanf(nowFile, "%lu", &now);
fclose(nowFile);

FILE* statusFile = fopen("/sys/class/power_supply/BAT0/status", "r");
if(statusFile == NULL)
return;
char status[256];
fscanf(statusFile, "%s", status);
fclose(statusFile);

uint32_t percentage = (now / (double) full) * 100;

ffPrintLogoAndKey(state, "Battery");
printf("%lu%% [%s]\n", percentage, status);
}
7 changes: 7 additions & 0 deletions src/break.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "fastfetch.h"

void ffPrintBreak(FFstate* state)
{
ffPrintLogoLine(state);
putchar('\n');
}
20 changes: 20 additions & 0 deletions src/colors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "fastfetch.h"

void ffPrintColors(FFstate* state)
{
ffPrintLogoLine(state);

for(uint8_t i = 0; i < 8; i++)
printf("\033[4%dm ", i);

puts("\033[0m");

ffPrintLogoLine(state);

for(uint8_t i = 8; i < 16; i++)
printf("\033[48;5;%dm ", i);

puts("\033[0m");

puts("");
}
22 changes: 22 additions & 0 deletions src/cpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "fastfetch.h"

#include <float.h>

void ffPrintCPU(FFstate* state)
{
char name[256];
ffParsePropFile("/proc/cpuinfo", "model name%*s %[^\n]", name);

char frequency[256];
ffReadFile("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", frequency, 256);

char cores[256];

uint32_t hz;
sscanf(frequency, "%ul", &hz); //in KHz
hz /= 1000; //to MHz
double ghz = (double) hz / 1000.0; //to GHz

ffPrintLogoAndKey(state, "CPU");
printf("%s (%i) @ %.9gGHz\n", name, get_nprocs(), ghz);
}
56 changes: 56 additions & 0 deletions src/desktopenvironment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "fastfetch.h"

#include <string.h>
#include <malloc.h>

static void printKDE()
{
char* line = NULL;
char version[256];
size_t len;

FILE* plasma = fopen("/usr/share/xsessions/plasma.desktop", "r");
if(plasma == NULL)
{
fputs("Error opening file: /usr/share/xsessions/plasma.desktop\n", stderr);
exit(35);
}

while (getline(&line, &len, plasma) != -1)
{
if (sscanf(line, "X-KDE-PluginInfo-Version=%[^\n]", version) > 0)
break;
}

fclose(plasma);
free(line);
printf("KDE Plasma %s", version);
}

void ffPrintDesktopEnvironment(FFstate* state)
{
const char* currentDesktop = getenv("XDG_CURRENT_DESKTOP");
if(currentDesktop == NULL)
return;

ffPrintLogoAndKey(state, "DE");

if(strcmp(currentDesktop, "KDE") == 0)
printKDE();
else
printf("%s", currentDesktop);

const char* sessionType = getenv("XDG_SESSION_TYPE");
if(sessionType == NULL)
putchar('\n');
else if(strcmp(sessionType, "wayland") == 0)
puts(" (Wayland)");
else if(strcmp(sessionType, "x11") == 0)
puts(" (X11)");
else if(strcmp(sessionType, "tty") == 0)
puts(" TTY");
else if(strcmp(sessionType, "mir") == 0)
puts(" (Mir)");
else
printf(" (%s)\n", sessionType);
}
24 changes: 24 additions & 0 deletions src/disk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "fastfetch.h"

#include <sys/statvfs.h>

void ffPrintDisk(FFstate* state)
{
struct statvfs fs;
int ret = statvfs("/", &fs);
if(ret != 0)
{
return;
}

const uint32_t GB = (1024 * 1024) * 1024;

const uint32_t total = (fs.f_blocks * fs.f_frsize) / GB;
const uint32_t available = (fs.f_bfree * fs.f_frsize) / GB;
const uint32_t used = total - available;
const uint32_t percentage = (used / (double) total) * 100.0;


ffPrintLogoAndKey(state, "Disk (/)");
printf("%luGB / %luGB (%lu%%)\n", used, total, percentage);
}
166 changes: 166 additions & 0 deletions src/fastfetch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#include "fastfetch.h"

#include <string.h>

void ffPrintKey(FFstate* state, const char* key)
{
printf("%s"FASTFETCH_TEXT_MODIFIER_BOLT"%s"FASTFETCH_TEXT_MODIFIER_RESET": ", state->color, key);
}

void ffPrintLogoAndKey(FFstate* state, const char* key)
{
ffPrintLogoLine(state);
ffPrintKey(state, key);
}

uint32_t ffTruncateLastNewline(char* buffer, uint32_t read)
{
if(read == 0)
return read;

if(buffer[read - 1] == '\n')
{
buffer[read - 1] = '\0';
--read;
}

return read;
}

uint32_t ffReadFile(const char* fileName, char* buffer, uint32_t bufferSize)
{
FILE* file = fopen(fileName, "r");
if(file == NULL)
{
fprintf(stderr, "Failed to open file: %s\n", fileName);
exit(3);
}

uint32_t read = fread(buffer, sizeof(char), bufferSize, file);
read = ffTruncateLastNewline(buffer, read);

fclose(file);

return read;
}

void ffParsePropFile(const char* fileName, const char* regex, char* buffer)
{
char* line = NULL;
size_t len;

FILE* file = fopen(fileName, "r");
if(file == NULL)
{
buffer[0] = '\0';
return;
}

while (getline(&line, &len, file) != -1)
{
if (sscanf(line, regex, buffer) > 0)
break;
}

fclose(file);
free(line);
}

void ffParsePropFileHome(FFstate* state, const char* relativeFile, const char* regex, char* buffer)
{
char absolutePath[512];
strcpy(absolutePath, state->passwd->pw_dir);
strcat(absolutePath, "/");
strcat(absolutePath, relativeFile);

ffParsePropFile(absolutePath, regex, buffer);
}

static inline bool strSet(const char* str)
{
return str != NULL && str[0] != '\0';
}

void ffPrintGtkPretty(const char* gtk2, const char* gtk3, const char* gtk4)
{
if(strSet(gtk2) && strSet(gtk3) && strSet(gtk4))
{
if((strcmp(gtk2, gtk3) == 0) && (strcmp(gtk2, gtk4) == 0))
printf("%s [GTK2/3/4]", gtk2);
else if(strcmp(gtk2, gtk3) == 0)
printf("%s [GTK2/3], %s [GTK4]", gtk2, gtk4);
else if(strcmp(gtk3, gtk4) == 0)
printf("%s [GTK2], %s [GTK3/4]", gtk2, gtk3);
else
printf("%s [GTK2], %s [GTK3], %s [GTK4]", gtk2, gtk3, gtk4);
}
else if(strSet(gtk2) && strSet(gtk3))
{
if(strcmp(gtk2, gtk3) == 0)
printf("%s [GTK2/3]", gtk2);
else
printf("%s [GTK2], %s [GTK3]", gtk2, gtk3);
}
else if(strSet(gtk3) && strSet(gtk4))
{
if(strcmp(gtk3, gtk4) == 0)
printf("%s [GTK3/4]", gtk3);
else
printf("%s [GTK3], %s [GTK4]", gtk3, gtk4);
}
else if(strSet(gtk2))
{
printf("%s [GTK2]", gtk2);
}
else if(strSet(gtk3))
{
printf("%s [GTK3]", gtk3);
}
else if(strSet(gtk4))
{
printf("%s [GTK4]", gtk4);
}
}

int main(int argc, char** argv)
{
FFstate state;

//init
ffLoadLogo(&state); //Use ffLoadLogoSet to specify an image. Note that this also overwrites color
state.current_row = 0;
state.passwd = getpwuid(getuid());
uname(&state.utsname);
sysinfo(&state.sysinfo);


//Configuration
state.logo_seperator = 4;
state.titleLength = 20; // This is overwritten by ffPrintTitle

//Start the printing
ffPrintTitle(&state);
ffPrintSeperator(&state);
ffPrintOS(&state);
ffPrintHost(&state);
ffPrintKernel(&state);
ffPrintUptime(&state);
ffPrintPackages(&state);
ffPrintShell(&state);
ffPrintResolution(&state);
ffPrintDesktopEnvironment(&state);
ffPrintTheme(&state);
ffPrintIcons(&state);
ffPrintFont(&state);
ffPrintTerminal(&state);
ffPrintCPU(&state);
ffPrintGPU(&state);
ffPrintMemory(&state);
ffPrintDisk(&state);
ffPrintBattery(&state);
ffPrintLocale(&state);
ffPrintBreak(&state);
ffPrintColors(&state);

return 0;
}
Loading

0 comments on commit ebb14af

Please sign in to comment.