Skip to content

Commit

Permalink
Added a shuffle effect for the centered info text.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart1978 committed Aug 30, 2024
1 parent 179a5ce commit 9eecf59
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 15 deletions.
17 changes: 5 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "sysinfo.h"
#include "center_text.h"
#include "selfie.h"
#include "shuffle.h"
#include <errno.h>
#include <string.h>

Expand Down Expand Up @@ -43,7 +44,7 @@ int main()
return 1;
}

int result = snprintf(all_text, MAX_BUFFER_SIZE - 1, "%s" BOLD CYAN "Uptime" RESET ":%s\n" BOLD CYAN "Total RAM" RESET ":%.2f GB\n" BOLD CYAN "Free RAM" RESET ":%.2f GB\n" BOLD CYAN "Buffered RAM" RESET ":%.2f GB\n" BOLD CYAN "Shared RAM" RESET ":%.2f GB\n" BOLD CYAN "Total Swap" RESET ":%.2f GB\n" BOLD CYAN "Free Swap" RESET ":%.2f GB\n" BOLD CYAN "Number of processes:" RESET "%lu\n", system_text, uptime, (double)info.totalram / GB,
int result = snprintf(all_text, MAX_BUFFER_SIZE - 1, "%s Uptime: %s\nTotal RAM: %.2f GB\nFree RAM: %.2f GB\nBuffered RAM: %.2f GB\nShared RAM: %.2f GB\nTotal Swap: %.2f GB\nFree Swap: %.2f GB\nNumber of processes: %lu\n", system_text, uptime, (double)info.totalram / GB,
(double)(info.freeram + info.bufferram + info.sharedram) / GB, (double)info.bufferram / GB, (double)info.sharedram / GB,
(double)info.totalswap / GB, (double)info.freeswap / GB, (unsigned long)info.procs);

Expand All @@ -66,17 +67,9 @@ int main()
return 1;
}

printf("\033[34;0H");
if (printf("%s\n", centered_text) < 0)
{
fprintf(stderr, "Failed to print centered text\n");
free(centered_text);
free(all_text);
free(system_text);
free(uptime);
return 1;
}

printf("\033[1m\033[34;1H");
show_shuffled(centered_text, 100, "cyan", HELP);
printf("\033[0m\033[50;1H");
// Don't forget to free the allocated memory !
free(centered_text);
free(all_text);
Expand Down
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
project(
'sysinfo',
'c',
version: '1.0.2',
version: '1.0.3',
meson_version: '>= 0.60.0',
default_options: ['warning_level=3', 'buildtype=release'],
)

executable(
'sysinfo',
['main.c', 'sysinfo.c', 'center_text.c', 'selfie.c'],
['main.c', 'sysinfo.c', 'center_text.c', 'selfie.c', 'shuffle.c'],
include_directories: include_directories('.'),
)
executable(
Expand Down
187 changes: 187 additions & 0 deletions shuffle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#include "shuffle.h"

int width, height;

struct timespec req;

// Swap a and b very fast (inline Assembly test: not necessary, C is really fast enough)
static void swap(int *a, int *b)
{
__asm__(
"movl (%0), %%eax;\n"
"movl (%1), %%ebx;\n"
"movl %%ebx, (%0);\n"
"movl %%eax, (%1);"
:
: "r"(a), "r"(b)
: "%eax", "%ebx");
}

// The Fischer - Yates shuffle algorithm
static void shuffle(int *array, int n)
{
srand((unsigned int)time(NULL));
for (int i = n - 1; i > 0; i--)
{
int j = rand() % (i + 1);
swap(&array[i], &array[j]);
}
}

int show_shuffled(char *ansi_pic, int speed, char *rgb, int is_help)
{
// The wait time between each printed char is: speed * 10000 nanoseconds
req.tv_nsec = speed * NSECONDS;
req.tv_sec = 0;
int shuffle_index, shuffled_row, shuffled_col, row, col, total_pixels, max_length = 0, c;
int flag = 0;

row = col = 0;

// Pointer to the loaded string
char *textfile;
textfile = ansi_pic;

// Get 2D dimension of the text file (x = max_length, y = rows)
for (c = *textfile; c != '\0'; max_length = col > max_length ? col : max_length)
{
c == '\n' ? col = 0, col++, row++ : col++;
c = *textfile++;
}

// Reset pointer
textfile = ansi_pic;

// Create 2D text array
char pic_array[row + 1][max_length];

width = max_length - 1;
height = row;

total_pixels = height * width;

row = col = 0;

/*Fill shorter rows with blank spaces and replace tabs with blank space.
*Store everything in 2D array */
while ((c = *textfile++) != '\0')
{
pic_array[row][col] = (char)c;
if (c == '\n')
{
if (col < max_length - 1)
{
for (c = ' '; col <= max_length; pic_array[row][col++] = (char)c)
;
}
col = -1;
row++;
}
else if (c == '\t')
{
pic_array[row][col] = ' ';
}
col++;
}

// go to position 34, 1 (under the picture)
printf("\033[34;1H");

int shuffle_array[total_pixels];

// Fill the shuffle array with ascending numbers.
for (int i = 0; i < total_pixels; i++)
{
shuffle_array[i] = i;
}

// Shuffle all the numbers in the array
shuffle(shuffle_array, total_pixels);

int r, g, b;

if (!strcmp(rgb, "random")) // Print with random RGB color
{
srand(time(NULL));
r = rand() % 255;
g = rand() % 255;
b = rand() % 255;
}
else if (!strcmp(rgb, "red"))
r = 255, g = 0, b = 0;
else if (!strcmp(rgb, "green"))
r = 0, g = 255, b = 0;
else if (!strcmp(rgb, "yellow"))
r = 255, g = 255, b = 0;
else if (!strcmp(rgb, "blue"))
r = 0, g = 0, b = 255;
else if (!strcmp(rgb, "magenta"))
r = 255, g = 0, b = 255;
else if (!strcmp(rgb, "cyan"))
r = 0, g = 255, b = 255;
else if (!strcmp(rgb, "white"))
r = 255, g = 255, b = 255;
else if (!strcmp(rgb, "black"))
r = 0, g = 0, b = 0;
else if (!strcmp(rgb, "orange"))
r = 255, g = 165, b = 0;
else if (!strcmp(rgb, "grey"))
r = 127, g = 127, b = 127;
else
{
printf("\033[38;2;%sm", rgb); // Print with given RGB values
goto jump;
}

printf("\033[38;2;%d;%d;%dm", r, g, b); // Print with one of the 'standard' colors

jump:

// Hide the cursor
printf(INVISIBLE);

// Show and delete the ASCII picture with amazing shuffle effect
for (int p = 0; p < (is_help ? 1 : 2); p++)
{
for (int i = 0; i < total_pixels; i++)
{
shuffle_index = shuffle_array[i];
shuffled_row = shuffle_index / width;
shuffled_col = shuffle_index % width;

// Move Cursor to shuffled position
printf("\033[%d;%dH", (shuffled_row + 1) + 34, shuffled_col + 1);
// Print char at shuffled position
printf("%c", pic_array[shuffled_row][shuffled_col]);
// Wait some nanoseconds after every char
nanosleep(&req, NULL);
fflush(stdout);
}

// Show the ASCII art for 2 seconds at first glance (with bit flag)
((!(flag & IS_END_FLAG)) && !is_help) ? sleep(2), flag |= IS_END_FLAG : 0;

if (!is_help)
{
// Shuffle the array again for deletion effect
shuffle(shuffle_array, total_pixels);

// Delete the 2D array (Fill with blank spaces)
for (int r = 0; r < height; r++)
{
for (int q = 0; q < width; pic_array[r][q++] = ' ')
;
}
}
}
// Reset text mode
printf(RST);

// Delete screen and go to position 1, 1 (only if not showing help)
!is_help ? printf(CLRJ) : printf("\033[18;1H");

// Show the cursor again
printf(VISIBLE);

return EXIT_SUCCESS;
}
39 changes: 39 additions & 0 deletions shuffle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#define HELP 1
#define NO_HELP 0

// Nanoseconds multiplied with speed
#define NSECONDS 10000

// The last bit as flag
#define IS_END_FLAG 0x01

// Hide the cursor
#define INVISIBLE "\033[?25l"

// Show the cursor
#define VISIBLE "\033[?25h"

// Clear screen and jump to position 1,1 (ANSI position starts at 1)
#define CLRJ "\033[2J\033[1;1H"

// Clear screen
#define CLR "\033[2J"

// Reset text mode
#define RST "\033[0m"

// Dimension of the ASCII picture
extern int width, height;

// Structure for 'nanosleep' function
extern struct timespec req;

extern int show_shuffled(char *ansi_pic, int speed, char *rgb, int is_help);
2 changes: 1 addition & 1 deletion sysinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ char *get_system_info()
perror("getifaddrs");
}

snprintf(systeminfo, 4096, BOLD CYAN "Hostname:" RESET " %s\n" BOLD CYAN "Kernel:" RESET " %s\n" BOLD CYAN "OS:" RESET " %s\n" BOLD CYAN "Desktop Environment:" RESET " %s\n" BOLD CYAN "CPU:" RESET " %s\n" BOLD CYAN "Local IP:" RESET " %s\n",
snprintf(systeminfo, 4096, "Hostname: %s\nKernel: %s\nOS: %s\nDesktop Environment: %s\nCPU: %s\nLocal IP: %s\n",
hostname, kernel, os, desktop_environment, CPU, local_IP);

return systeminfo;
Expand Down

0 comments on commit 9eecf59

Please sign in to comment.