aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar BanceDev 2024-09-10 11:29:43 -0400
committerGravatar BanceDev 2024-09-10 11:31:20 -0400
commita0ff5b10f1307c12ffd464850eeec3ec66e2770f (patch)
treecf9347863fe24ca58bdcb9650f5f7bdcbad6e28b
parentadded coloring to help command (diff)
lua api functions for getting terminal width and height
-rw-r--r--.lush/init.lua1
-rw-r--r--.lush/scripts/example.lua5
-rw-r--r--src/lua_api.c25
-rw-r--r--src/lush.c15
4 files changed, 46 insertions, 0 deletions
diff --git a/.lush/init.lua b/.lush/init.lua
index 5446dd0..d3166d7 100644
--- a/.lush/init.lua
+++ b/.lush/init.lua
@@ -24,6 +24,7 @@ lush.setenv("PATH", path)
-- the prompt can be customized here too
-- %u is username, %h is hostname, %w is current working directory
+-- %t is current time in hr:min:sec, %d is date in MM/DD/YYYY
lush.setPrompt("[%u@%h: %w]")
-- aliases can be defined using the alias method by passing the alias name
diff --git a/.lush/scripts/example.lua b/.lush/scripts/example.lua
index c60f99b..0239500 100644
--- a/.lush/scripts/example.lua
+++ b/.lush/scripts/example.lua
@@ -91,3 +91,8 @@ print("Value of EXAMPLE: " .. lush.getenv("EXAMPLE"))
-- you can unset an environment variable with unsetenv
lush.unsetenv("EXAMPLE")
+
+-- you can get the current terminal width(cols) and height(rows)
+-- this function is very useful if you want to make certain kinds of custom prompts in your init.lua
+print("Terminal Columns: " .. lush.termCols())
+print("Terminal Rows: " .. lush.termRows())
diff --git a/src/lua_api.c b/src/lua_api.c
index 21f5d39..93fee31 100644
--- a/src/lua_api.c
+++ b/src/lua_api.c
@@ -26,6 +26,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -332,6 +333,26 @@ static int l_alias(lua_State *L) {
return 0;
}
+static int l_terminal_cols(lua_State *L) {
+ struct winsize w;
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
+ perror("ioctl");
+ return 0;
+ }
+ lua_pushnumber(L, w.ws_col);
+ return 1;
+}
+
+static int l_terminal_rows(lua_State *L) {
+ struct winsize w;
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
+ perror("ioctl");
+ return 0;
+ }
+ lua_pushnumber(L, w.ws_row);
+ return 1;
+}
+
// -- register Lua functions --
void lua_register_api(lua_State *L) {
@@ -370,6 +391,10 @@ void lua_register_api(lua_State *L) {
lua_setfield(L, -2, "setPrompt");
lua_pushcfunction(L, l_alias);
lua_setfield(L, -2, "alias");
+ lua_pushcfunction(L, l_terminal_cols);
+ lua_setfield(L, -2, "termCols");
+ lua_pushcfunction(L, l_terminal_rows);
+ lua_setfield(L, -2, "termRows");
// set the table as global
lua_setglobal(L, "lush");
}
diff --git a/src/lush.c b/src/lush.c
index bd8d4e8..d295e45 100644
--- a/src/lush.c
+++ b/src/lush.c
@@ -195,6 +195,9 @@ static size_t get_prompt_len(const char *format, const char *username,
} else if (strncmp(format, "%w", 2) == 0) {
prompt_len += strlen(cwd);
format += 2;
+ } else if (strncmp(format, "%t", 2) == 0) {
+ prompt_len += 8; // size of time format
+ format += 2;
} else {
prompt_len++;
format++;
@@ -230,6 +233,18 @@ static char *format_prompt_string(const char *input, const char *username,
strcpy(dest, cwd);
dest += strlen(cwd);
input += 2;
+ } else if (strncmp(input, "%t", 2) == 0) {
+ time_t current_time;
+ time(&current_time);
+
+ struct tm *local_time = localtime(&current_time);
+
+ // Format the time as HH:MM:SS
+ char time_string[9]; // HH:MM:SS is 8 characters + null terminator
+ strftime(time_string, sizeof(time_string), "%H:%M:%S", local_time);
+ strcpy(dest, time_string);
+ dest += strlen(time_string);
+ input += 2;
} else {
*dest++ = *input++;
}