commit 18f78db05c0b86b189f5b77206a44082f80b6353 Author: night0721 Date: Fri Oct 11 19:04:55 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed46268 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +nsh +*.o +*.tar.gz diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..10621d3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause License + +Copyright (c) 2024, Night Kaly + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..039823f --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +.POSIX: +.SUFFIXES: + +CC = cc +VERSION = 1.0 +TARGET = nsh +PREFIX ?= /usr/local +BINDIR = $(PREFIX)/bin + +# Flags +CFLAGS = -O3 -march=native -mtune=native -pipe -s -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 + +SRC = $(TARGET).c + +$(TARGET): $(SRC) + $(CC) $(SRC) -o $@ $(CFLAGS) + +dist: + mkdir -p $(TARGET)-$(VERSION) + cp -R README.md $(TARGET) $(TARGET)-$(VERSION) + tar -cf $(TARGET)-$(VERSION).tar $(TARGET)-$(VERSION) + gzip $(TARGET)-$(VERSION).tar + rm -rf $(TARGET)-$(VERSION) + +install: $(TARGET) + mkdir -p $(DESTDIR)$(BINDIR) + cp -p $(TARGET) $(DESTDIR)$(BINDIR)/$(TARGET) + chmod 755 $(DESTDIR)$(BINDIR)/$(TARGET) + +uninstall: + $(RM) $(DESTDIR)$(BINDIR)/$(TARGET) + +clean: + $(RM) $(TARGET) + +all: $(TARGET) + +.PHONY: all dist install uninstall clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7c2400 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# nsh +Neo Syntax Highlighter + +# Usage +```sh +git diff | nsh +diff | nsh +``` + +# Dependencies +None + +# Building +You will need to run these with elevated privilages. +``` +$ make +# make install +``` + +# Contributions +Contributions are welcomed, feel free to open a pull request. + +# License +This project is licensed under the BSD-3-Clause License. See [LICENSE](https://github.com/night0721/nsh/blob/master/LICENSE) for more information. diff --git a/nsh.c b/nsh.c new file mode 100644 index 0000000..1589ab5 --- /dev/null +++ b/nsh.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#define COLOR_RESET "\033[0m" +#define COLOR_TEAL "\033[38;2;148;226;213m" +#define COLOR_GREEN "\033[38;2;166;227;161m" +#define COLOR_RED "\033[38;2;243;139;168m" +#define COLOR_BLUE "\033[38;2;137;180;250m" +#define COLOR_PEACH "\033[38;2;250;179;135m" +#define COLOR_YELLOW "\033[38;2;249;226;175m" +#define COLOR_OVERLAY0 "\033[38;2;108;112;134m" + +#define MAX_LINE_LENGTH 1024 + +int main() +{ + char buffer[MAX_LINE_LENGTH]; + + while (fgets(buffer, sizeof(buffer), stdin) != NULL) { + /* Remove newline from buffer, if present */ + size_t len = strlen(buffer); + if (len > 0 && buffer[len - 1] == '\n') { + buffer[len - 1] = '\0'; + } + + if (strncmp(buffer, "---", 3) == 0) { + printf(COLOR_YELLOW "%s" COLOR_RESET "\n", buffer); + } else if (strncmp(buffer, "+++", 3) == 0) { + printf(COLOR_PEACH "%s" COLOR_RESET "\n", buffer); + } else if (strncmp(buffer, "@@", 2) == 0) { + printf(COLOR_OVERLAY0 "%s" COLOR_RESET "\n", buffer); + } else if (strncmp(buffer, "index", 5) == 0) { + printf(COLOR_TEAL "%s" COLOR_RESET "\n", buffer); + } else if (strncmp(buffer, "diff", 4) == 0) { + printf(COLOR_BLUE "%s" COLOR_RESET "\n", buffer); + } else if (buffer[0] == '+') { + printf(COLOR_GREEN "%s" COLOR_RESET "\n", buffer); + } else if (buffer[0] == '-') { + printf(COLOR_RED "%s" COLOR_RESET "\n", buffer); + } else { + printf("%s\n", buffer); + } + } + return 0; +} +