Skip to content

Instantly share code, notes, and snippets.

@nyteshade
Last active January 17, 2021 08:31
Show Gist options
  • Save nyteshade/87efd916ba0ba0db27b6e9210f4e940a to your computer and use it in GitHub Desktop.
Save nyteshade/87efd916ba0ba0db27b6e9210f4e940a to your computer and use it in GitHub Desktop.
ZSH Prompt (might require OhMyZsh)
#include <sys/types.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct IPA {
char ip[16];
char name[32];
} IPA;
typedef struct Args {
short showColor;
short hideName;
short maxCount;
char *ipFormat;
char *nameFormat;
} Args;
char *mkLowerCase(char *mixed) {
char *s = mixed;
for (; *s; ++s) *s = tolower(*s);
return mixed;
}
short idxOf(char *substring, char *string) {
char *result = strstr(string, substring);
return result ? result - string : -1;
}
short idxOfL(char *substring, char *string) {
char *sub = strdup(substring), *str = strdup(string), res = 0;
mkLowerCase(sub);mkLowerCase(str); res = idxOf(sub,str);
free(sub); free(str); return (short)res;
}
int main(int argc, char **argv) {
struct ifaddrs *tmp, *addrs;
char *s, format[255];
char ipWColor[] = "\x1b[0m%s\x1b[0;34m%s";
char ipWOColor[] = "%s%s";
char nameWColor[] = "\x1b[1;37m:\x1b[0;33m%s\x1b[0m";
char nameWOColor[] = ":%s";
int first = 1, i, shown = 0, ipCount = 0;
IPA ips[10];
Args args;
memset(&args, 0L, sizeof(Args));
args.maxCount = 10;
args.showColor = 1;
args.hideName = 0;
getifaddrs(&addrs);
tmp = addrs;
if (argc > 1 && (~idxOfL("?", argv[1]) || ~idxOfL("help", argv[1]))) {
printf("Usage: %s <args.maxCount> [--no-color] [--hide-name]\n", basename(argv[0]));
return 0;
}
for (i=1;i<argc;i++)
if (~idxOfL("--hide-name",argv[i])) { args.hideName = 1; break;}
for (i=1;i<argc;i++)
if (~idxOfL("--no-color",argv[i])) { args.showColor = 0; break; }
for (i=1;i<argc;i++)
if (!(args.maxCount = atoi(argv[i]))) { args.maxCount = 10; }
i = 0;
while(tmp) {
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET) {
struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
sprintf(ips[i].ip, "%s", inet_ntoa(pAddr->sin_addr));
sprintf(ips[i].name, "%s", tmp->ifa_name);
/* convert name tolower */
s = ips[i].name; for (; *s; ++s) *s = tolower(*s); s = ips[i].name;
ipCount++;
i++;
}
tmp = tmp->ifa_next;
}
strcpy(format, args.showColor ? ipWColor : ipWOColor);
if (!args.hideName) {
strcat(format, args.showColor ? nameWColor : nameWOColor);
}
for (i = 0, first = 1; i < ipCount; i++) {
if (!~idxOfL("lo", ips[i].name)) {
printf(
format,
first ? "" : ",",
ips[i].ip,
args.hideName ? "" : ips[i].name
);
first = 0;
shown++;
if (shown >= args.maxCount) break;
}
}
printf("\n");
freeifaddrs(addrs);
}
terse_git_prompt_info () {
local ref
if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]
then
ref=$(command git symbolic-ref HEAD 2> /dev/null) || ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
echo "(%{\x1b[31m%}${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
fi
}
export PS1=$'
%{\x1b[1m%}%{$fg[yellow]%}%{$USER%}%{$reset_color%}@%{\x1b[1m%}%{$fg[red]%}%{$HOST%}%{$reset_color%}/$(getcip --hide-name 2) $(terse_git_prompt_info)
%{\x1b[1m%}%{$fg[green]%}$(pwd)%{$reset_color%}
%(?:%{$fg[green]%}➜:%{$fg[red]%}➜) %{$reset_color%}'
@nyteshade
Copy link
Author

nyteshade commented Sep 20, 2019

In addition to everything else, you'll need Oh My Zsh

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

This also requires getcip. Copy the source to a file, such as /tmp/getcip.c and then run gcc -o getcip getcip.c. It should compile directly. Then put it in /usr/local/bin.

sudo mkdir -p /usr/local/bin
mv getcip /usr/local/bin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment