Change prompt cursor

This commit is contained in:
savalet
2025-05-04 22:41:08 +02:00
parent 40d2f6d7f6
commit 4ceec261b5
8 changed files with 61 additions and 8 deletions

View File

@@ -11,6 +11,7 @@
#include <string.h>
#include <unistd.h>
#include "builtins_handler.h"
#include "common.h"
#include "u_mem.h"
#include "u_str.h"

View File

@@ -7,7 +7,7 @@
#ifndef COMMON_H
#define COMMON_H
#include "exec.h"
#include <stdint.h>
#define PROMPT_HEADER "┌─["
#define IF_PROMPT "if? "
@@ -17,5 +17,9 @@ enum {
RETURN_FAILURE = 1
};
void free_everything(exec_ctx_t *exec_ctx);
typedef struct {
uint8_t flags;
char *script_file;
char *cmd;
} opt_t;
#endif /* COMMON_H */

View File

@@ -5,12 +5,52 @@
** _
*/
#include <getopt.h>
#include <stdlib.h>
#include "common.h"
#include "debug.h"
#include "shell.h"
int main(int ac __attribute__((unused)), char **av __attribute__((unused)),
char **env)
const char OPT_FLAGS[] = "hc:";
static
void print_usages(FILE *file, char *bin)
{
U_DEBUG_MSG("Debug mode activated.\n");
return shell(env);
fprintf(file, "Usage: %s [-c command] [-dixv]"
" [file]\n", bin);
}
static
bool parse_args(opt_t *opt, int ac, char **av)
{
for (int o = getopt(ac, av, OPT_FLAGS); o != -1;
o = getopt(ac, av, OPT_FLAGS)) {
switch (o) {
case 'h':
exit((print_usages(stdout, av[0]), RETURN_SUCCESS));
case 'c':
opt->cmd = optarg;
break;
default:
return print_usages(stderr, av[0]), false;
}
}
if (optind < ac) {
opt->script_file = av[optind];
U_DEBUG("Script file [%s]\n", opt->script_file);
} else {
U_DEBUG_MSG("No script file provided.\n");
}
return true;
}
int main(int ac, char **av, char **env)
{
opt_t opt = { 0, .script_file = nullptr, .cmd = nullptr };
U_DEBUG_MSG("Debug mode activated.\n");
if (!parse_args(&opt, ac, av))
return RETURN_FAILURE;
return shell(&opt, env);
}

View File

@@ -45,6 +45,7 @@ void init_shell_repl(exec_ctx_t *exec_ctx)
signal(SIGINT, SIG_IGN);
exec_ctx->is_running = true;
if (isatty(STDIN_FILENO)) {
WRITE_CONST(STDOUT_FILENO, BLINKING_VERTICAL_CURSOR);
tcgetattr(STDIN_FILENO, &repl_settings);
exec_ctx->saved_term_settings = repl_settings;
repl_settings.c_iflag = IXON;

View File

@@ -20,6 +20,7 @@
#include "repl.h"
#include "shell.h"
#include "u_str.h"
#include "utils.h"
__attribute__((unused))
static
@@ -119,7 +120,7 @@ bool error_in_init(exec_ctx_t *exec_ctx)
return false;
}
int shell(char **env_ptr)
int shell(opt_t *opt, char **env_ptr)
{
alias_t alias = init_alias();
env_t env = parse_env(env_ptr);

View File

@@ -7,6 +7,7 @@
#ifndef SHELL_H
#define SHELL_H
#include "common.h"
typedef struct {
char **cmd_history;
@@ -14,5 +15,5 @@ typedef struct {
char *last_chdir;
} history_t;
int shell(char **env);
int shell(opt_t *opt, char **env);
#endif /* SHELL_H */

View File

@@ -9,10 +9,12 @@
#ifndef UTILS_H
#define UTILS_H
#include "history.h"
#include "exec.h"
char *strn_to_ndup(int start, int size, char *str);
bool is_a_token(char *str, int index_str);
char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy);
int len_array(char **array);
char *insert_str(const char *base, const char *insert, size_t pos);
void free_everything(exec_ctx_t *exec_ctx);
#endif /* UTILS_H */

View File

@@ -11,6 +11,9 @@
#define ESC "\033"
#define CFMT(n) ESC "[" #n "m"
// special
#define BLINKING_VERTICAL_CURSOR ESC "[5 q"
// move
#define MOVE_RIGHT(n) ESC "[" #n "C"