Merge pull request #74 from AnonymusRaccoon/prompt

Prompt
This commit is contained in:
Anonymus Raccoon
2020-05-24 22:22:55 +02:00
committed by GitHub
15 changed files with 286 additions and 34 deletions
+3 -1
View File
@@ -55,7 +55,9 @@ SRC = src/shell.c \
src/builtin/builtin_bindkey.c \
src/key_bindings/other_bindings.c \
src/builtin/builtin_vars.c \
src/key_bindings/signals.c
src/key_bindings/signals.c \
src/prompt/prompt_values.c \
src/prompt/prompt_utilities.c
OBJ = $(SRC:%.c=%.o)
OBJ += src/main.o
+1
View File
@@ -32,6 +32,7 @@ int buffer_get_display_pos(buffer_t *buffer);
int self_insert_command(int key, buffer_t *buffer, env_t *env);
int newline_command(int key, buffer_t *buffer, env_t *env);
int eof_command(int key, buffer_t *buffer, env_t *env);
int history_size(history_t *hist);
int backward_delete_char_command(int key, buffer_t *buffer, env_t *env);
int delete_char_command(int key, buffer_t *buffer, env_t *env);
+11
View File
@@ -64,7 +64,18 @@ my_addstr(window, str))
#define CYAN 36
#define WHITE 37
#define BOLD 1
#define FAINT 2
#define ITALIC 3
#define UNDERLINE 4
#define SLOW_BLINK 5
#define FAST_BLINK 6
#define REVERSE 7
#define DISABLE_ATTR 20
#define my_attron(attr, value) (printf("\x1B[%dm", attr + value))
#define my_attroff(attr, value) (printf("\x1B[%dm", attr + DISABLE_ATTR))
#define my_attrreset() (printf("\x1B[0m"))
+18
View File
@@ -0,0 +1,18 @@
/*
** EPITECH PROJECT, 2020
** ash
** File description:
** prompt
*/
#ifndef PROMPT_H_
#define PROMPT_H_
char *prompt_attr(int attr, bool off);
char *minimal_hostname(char *hostname);
char *date_format(int date);
char *str_tolower(char *str);
char *get_prompt_value(char c, env_t *env);
#endif /* !PROMPT_H_ */
+4 -4
View File
@@ -63,10 +63,10 @@ void init_source_args(char **argv, int len_argv, env_t *env)
char *str = NULL;
env->vars = my_setenv(env->vars, "$", tostr(getpid()));
env->vars = my_setenv(env->vars, "*",
len_argv > 2 ? get_special_arg_at(argv, len_argv) : "");
env->vars = my_setenv(env->vars, "@",
len_argv > 2 ? get_special_arg_star(argv, len_argv) : "");
env->vars = my_setenv(env->vars, "*", \
len_argv > 2 ? get_special_arg_at(argv, len_argv) : "");
env->vars = my_setenv(env->vars, "@", \
len_argv > 2 ? get_special_arg_star(argv, len_argv) : "");
env->vars = my_setenv(env->vars, "#", tostr(len_argv - 2));
for (int i = 1; i < len_argv - 1; i++) {
str = tostr(i);
+7
View File
@@ -17,6 +17,10 @@ void on_sigint(int sig, siginfo_t *info, void *context)
my_addstr(stdwin, "\n");
}
void on_sigtstp(int sig, siginfo_t *info, void *context)
{
}
void setup_sigint(void)
{
struct sigaction sa;
@@ -25,6 +29,9 @@ void setup_sigint(void)
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &sa, NULL);
sa.sa_sigaction = &on_sigtstp;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
sigaction(SIGTSTP, &sa, NULL);
}
int tty_sigintr_command(int key, buffer_t *buffer, env_t *env)
+1
View File
@@ -46,6 +46,7 @@ int main(int argc, char **argv, char **env)
if (!envt)
return (ERROR);
envt->vars = my_setenv(envt->vars, "prompt", "%U%m%u:%B%~%b%# ");
if (argc >= 2)
builtin_source(argv, envt);
else
+25 -1
View File
@@ -6,7 +6,12 @@
*/
#include "my_ncurses.h"
#include <stddef.h>
#include <stdio.h>
#include <unctrl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
void my_npause(my_window *window)
{
@@ -21,4 +26,23 @@ void my_nresume(my_window *window)
if (!window)
return;
tcsetattr(0, TCSANOW, &window->saved_termios);
}
void my_getmaxyx(int *y, int *x)
{
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
*y = size.ws_row;
*x = size.ws_col;
}
void my_getcuryx(int *y, int *x)
{
my_refresh();
printf("\x1B[6n");
fflush(stdout);
scanf("\x1B[%d;%dR", y, x);
*y -= 1;
*x -= 1;
}
+15 -19
View File
@@ -12,11 +12,26 @@
#include <string.h>
#include <sys/ioctl.h>
int get_escape_character_length(const char *str)
{
int i = 0;
if (str[1] != '[')
return (1);
while (str[i] && str[i] != 'm')
i++;
return (i);
}
void my_addstr(my_window *window, const char *str)
{
int tmp;
for (int i = 0; str[i]; i++) {
if (str[i] == KEY_ESCAPE) {
i += get_escape_character_length(&str[i]);
continue;
}
if (str[i] == '\n') {
window->y++;
window->x = 0;
@@ -59,23 +74,4 @@ int my_parsechar(const char *c)
if (c[1])
return (-1);
return (c[0]);
}
void my_getmaxyx(int *y, int *x)
{
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
*y = size.ws_row;
*x = size.ws_col;
}
void my_getcuryx(int *y, int *x)
{
my_refresh();
printf("\x1B[6n");
fflush(stdout);
scanf("\x1B[%d;%dR", y, x);
*y -= 1;
*x -= 1;
}
+32 -6
View File
@@ -5,13 +5,12 @@
** prompt
*/
#include "shell.h"
#include "builtin.h"
#include "redirections.h"
#include "parser.h"
#include "utility.h"
#include "prompt.h"
#include <unistd.h>
#include <malloc.h>
#include <string.h>
@@ -58,12 +57,39 @@ int prompt_run(char *cmd, redirection *inout[2], env_t *env, redirection *cmds)
return (ret);
}
char *parse_prompt(char *prompt, env_t *env)
{
char *value = NULL;
int value_length = 0;
int length = strlen(prompt);
for (int i = 0; prompt[i]; i++) {
if (prompt[i] != '%')
continue;
value = get_prompt_value(prompt[i + 1], env);
if (!value)
continue;
value_length = strlen(value);
prompt = realloc(prompt, sizeof(char) * (value_length + length + 2));
if (!prompt)
return (NULL);
rm_n_char(&prompt[i], 2);
insert_substring(prompt, value, i + 1);
i += value_length - 1;
length += value_length - 2;
}
return (prompt);
}
void prompt_prepare(buffer_t *buffer, env_t *env)
{
char *prompt = my_getenv(env->vars, "PS1");
char *prompt = my_getenv(env->vars, "prompt");
if (!prompt)
prompt = "$ ";
printf("%s", prompt);
buffer->startx = strlen(prompt);
prompt = "";
prompt = strdup(prompt);
prompt = parse_prompt(prompt, env);
my_mvaddstr(env->window, NO_MOVE, 0, prompt);
buffer->startx = env->window->x;
free(prompt);
}
+44
View File
@@ -0,0 +1,44 @@
/*
** EPITECH PROJECT, 2020
** ash
** File description:
** prompt_utilities
*/
#include <stdbool.h>
#include <ctype.h>
#include "my_ncurses.h"
char *minimal_hostname(char *hostname)
{
int i = 0;
while (hostname[i] && hostname[i] != '.')
i++;
if (hostname[i] == '.')
hostname[i] = '\0';
return (hostname);
}
char *prompt_attr(int attr, bool off)
{
static char str[10];
sprintf(str, "\x1B[%dm", attr + (off ? DISABLE_ATTR : 0));
return (str);
}
char *date_format(int date)
{
static char format[3];
sprintf(format, "%02d", date);
return (format);
}
char *str_tolower(char *str)
{
for (int i = 0; str[i]; i++)
str[i] = tolower(str[i]);
return (str);
}
+111
View File
@@ -0,0 +1,111 @@
/*
** EPITECH PROJECT, 2020
** ash
** File description:
** prompt_values
*/
#include "shell.h"
#include "my_ncurses.h"
#include "prompt.h"
#include "key_functions.h"
#include "utility.h"
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
char *get_prompt_value4(char c, env_t *env, struct tm *tm)
{
static char time[11];
if (c == 't' || c == '@') {
strftime(time, sizeof(time), "%I:%M%p", tm);
return (str_tolower(time));
}
if (c == 'T') {
strftime(time, sizeof(time), "%H:%M", tm);
return (time);
}
if (c == 'p') {
strftime(time, sizeof(time), "%I:%M:%S%p", tm);
return (str_tolower(time));
}
if (c == 'P') {
strftime(time, sizeof(time), "%H:%M:%S", tm);
return (time);
}
return (time);
}
char *get_prompt_value3(char c, env_t *env)
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
static char name[10];
if (c == 'd') {
strftime(name, sizeof(name), "%a", tm);
return (name);
}
if (c == 'D')
return (date_format(tm->tm_mday));
if (c == 'w') {
strftime(name, sizeof(name), "%b", tm);
return (name);
}
if (c == 'W')
return (date_format(tm->tm_mon + 1));
if (c == 'y')
return (&tostr(tm->tm_year + 1900)[2]);
if (c == 'Y')
return (tostr(tm->tm_year + 1900));
return (get_prompt_value4(c, env, tm));
}
char *get_prompt_value2(char c, env_t *env)
{
if (c == '?')
return (my_getenv(env->vars, "?"));
if (c == '%')
return ("%");
if (c == 'B')
return (prompt_attr(BOLD, false));
if (c == 'b')
return (prompt_attr(0, false));
if (c == 'U')
return (prompt_attr(UNDERLINE, false));
if (c == 'u')
return (prompt_attr(UNDERLINE, true));
if (c == 'S')
return (prompt_attr(REVERSE, false));
if (c == 's')
return (prompt_attr(REVERSE, true));
if (c == '!' || c == 'h')
return (tostr(history_size(env->history)));
return (get_prompt_value3(c, env));
}
char *get_prompt_value(char c, env_t *env)
{
static char value[PATH_MAX];
if (c == '/' || c == '~')
return (getcwd(value, sizeof(value)));
if (c == 'n' || c == 'N')
return (getlogin());
if (c == 'M' || c == 'm') {
gethostname(value, sizeof(value));
if (c == 'm')
minimal_hostname(value);
return (value);
}
if (c == 'l')
return (ttyname(STDIN_FILENO));
if (c == '#') {
if (!geteuid())
return ("#");
return (">");
}
return (get_prompt_value2(c, env));
}
+6 -1
View File
@@ -23,7 +23,10 @@ int process_key(int key, buffer_t *buffer, env_t *env)
{
if (key <= 0)
return (0);
my_clrtobot();
if (env->window) {
my_move(env->window, NO_MOVE, env->window->w);
my_clrtobot();
}
if (buffer->quoted_insert) {
buffer->quoted_insert = false;
return (self_insert_command(key, buffer, env));
@@ -57,6 +60,8 @@ void shell_refresh(buffer_t *buffer, env_t *env)
static int oldwidth = -1;
if (oldwidth == -1)
oldwidth = env->window->w;
if (buffer->buffer && !buffer->buffer[0])
oldbuffer_pos = 0;
int y = env->window->y - (oldbuffer_pos + buffer->startx) / oldwidth;
int newy = y + (buffer->pos + buffer->startx) / env->window->w;
-1
View File
@@ -62,7 +62,6 @@ char **split_str(char *str, char **delims)
index++;
i = -1;
}
// if (i > 0)
arr[index++] = str;
arr[index] = NULL;
return (arr);
+8 -1
View File
@@ -1,4 +1,11 @@
int main()
/*
** EPITECH PROJECT, 2020
** ash
** File description:
** loop
*/
int main(void)
{
while (1);
return (0);