Open BAOELIETRAN opened 1 month ago
Here's a random trick to create colorful prompt: use ANSI escape codes. Imma just "steal" my old code and paste it here:
#define RESET_ALL "\033[0m"
/* List of colors */
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGNETA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
#define DEFAULT_COLOR "\033[39m"
/* List of formats */
#define BOLD "\033[1m"
#define DIM "\033[2m"
#define ITALIC "\033[3m"
#define UNDERLINE "\033[4m"
#define STRIKETHROUGH "\033[9m"
How you would use these is like so: Say you want to print "Hello World" in red,
printf("\033[31m" "Hello World\n" "\033[0m");
or, if you paste all the #defines above into your source code:
printf(RED "Hello World\n" RESET_ALL);
The "\033[0m" (aka, RESET_ALL in this case) is important, otherwise, the next printf will still show red even though you didn't specify a color.
EDIT: sometimes you see people use \x1b instead of \033 as the prefix. It will yield the same result, so far as I'm aware, anyways.
Another trick, here's how you would make a progress bar:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
uint progress = 0;
while(progress <= 100){
printf("\r%d%% ", progress);
for(int i = 0; i <= progress; i += 10){
printf("#");
}
progress += 10;
sleep(1);
fflush(stdout);
}
printf("\n");
return EXIT_SUCCESS;
}
The output is something of:
30% ###
On Windows machine, change the unistd include to windows, and change sleep to Sleep. Although, I dunno what we may be using a progress bar for.
It's quite disheartening to say this, and maybe I'm too strict here, but the animation, while cool, doesn't enhance user experience, but kinda hammers it, due to the loading time. :disappointed:
Also, print functions are considered very "expensive" on the processor, due to it being a syscall.
If you're a recruiter, you would prefer more practical tools, right? :smile:
There are some minor issues like memory leaks, which I can fix for you. But the big ones, like animation, is up to your decision. Were it me, I would save the changes on a different branch first, then change this branch's code to be more pragmatic. That way, you won't lose the cool stuff.
Lastly, I think we shall remove the "explanatory" comments? These ones:
I suggest that, the parts of the code you don't understand, you should copy that part of the code into a, say, text file. I prefer Markdown because you can get syntax-highlighting like this:
std::print("Isn't it cool that we get some syntax highlights?");
while(true){
SomeClassToCauseMemLeak* evil = new SomeClassToCauseMemLeak;
}
Then, write a detailed explanation below the code snippet.
Anyways, really nice effort!
@BAOELIETRAN also, check my implementation of BTree. I got it working (halfway) already. Thanks a lot for your motivation to write this hell of a data structure. Your implementation (as well as mine) is of course, not perfect, but still, that gave me the motivation necessary. It took me a few days and 2 rewrites just to get search() and insert() on mine work properly.
I just force-push my BTree implementation onto a separate repository in this organization lol.
@BAOELIETRAN in case you're still working on this PR. You have a merge conflict here, unfortunately :disappointed:. Before you proceed, you should resolve the merge conflicts first.
Pull request name goes here
Handling the prompt from user
Whenever user run the file with a random argument, a prompt will pop up.
Declare new function smoldb_input_buf_read to put the content of the user's input into the buffer of the InputBuf struct, and also set the length of the buffer.
Declare new function prompt_prototype to receive the check the argument, read the user's input, and handle the request.
Why:
Declare new functions for the prototype of the prompt.
Declare new functions, and handle the edge cases of the prompt, such as if user type "mogging", the program will exit.
I think we should discuss how to make the prompt more colorful and interesting, like moving figure or sth?
@huynguyen-and-friend-projects/everyone
Pull request by: Bao Ngo