huynguyen-and-friend-projects / smoldb

Simple database in C
1 stars 3 forks source link

cli handler pr #29

Open BAOELIETRAN opened 1 month ago

BAOELIETRAN commented 1 month ago

Pull request name goes here

Handling the prompt from user

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

nguyenhuy0905 commented 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.

nguyenhuy0905 commented 1 month ago

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.

nguyenhuy0905 commented 4 weeks ago

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:

image

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!

nguyenhuy0905 commented 1 week ago

@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.

Here's the link to my BTree implementation

nguyenhuy0905 commented 1 week ago

@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.