Skip to content

Commit

Permalink
Make more highlight improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Oct 28, 2024
1 parent 457cefb commit ebcd61c
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 43 deletions.
76 changes: 42 additions & 34 deletions llamafile/high.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,7 @@ static std::string extname(const std::string_view path) {
return std::string(name.substr(dot_pos + 1));
}

int main(int argc, char *argv[]) {

// process flags
int opt;
int infd = 0;
int outfd = 1;
const char *lang = nullptr;
const char *inpath = nullptr;
while ((opt = getopt(argc, argv, "hl:o:")) != -1) {
switch (opt) {
case 'h':
printf("usage: %s [-l LANG] [-o OUTFILE] [INFILE]\n", argv[0]);
exit(0);
case 'l':
lang = optarg;
break;
case 'o':
if ((outfd = creat(optarg, 0644)) == -1) {
perror(optarg);
exit(1);
}
break;
default:
exit(1);
}
}
if (optind < argc) {
inpath = argv[optind];
if ((infd = open(inpath, O_RDONLY)) == -1) {
perror(inpath);
exit(1);
}
}

static void highlight(int infd, int outfd, const char *lang, const char *inpath) {
// create syntax highlighter
Highlight *h;
const char *ext;
Expand Down Expand Up @@ -123,3 +90,44 @@ int main(int argc, char *argv[]) {
H.flush(&res);
write(outfd, res.data(), res.size());
}

int main(int argc, char *argv[]) {

// process flags
int opt;
int outfd = 1;
const char *lang = nullptr;
while ((opt = getopt(argc, argv, "hl:o:")) != -1) {
switch (opt) {
case 'h':
printf("usage: %s [-l LANG] [-o OUTFILE] [INFILE]\n", argv[0]);
exit(0);
case 'l':
lang = optarg;
break;
case 'o':
if ((outfd = creat(optarg, 0644)) == -1) {
perror(optarg);
exit(1);
}
break;
default:
exit(1);
}
}

// process files
if (optind == argc) {
highlight(0, outfd, lang, 0);
} else {
for (int i = optind; i < argc; ++i) {
int infd;
const char *inpath = argv[i];
if ((infd = open(inpath, O_RDONLY)) == -1) {
perror(inpath);
exit(1);
}
highlight(infd, outfd, lang, inpath);
}
}
}
3 changes: 3 additions & 0 deletions llamafile/highlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,11 @@ class HighlightShell : public Highlight {
int u_ = 0;
int t_ = 0;
int i_ = 0;
int curl_ = 0;
int last_ = 0;
bool pending_heredoc_ = false;
bool indented_heredoc_ = false;
bool no_interpolation_ = false;
std::string word_;
std::string heredoc_;
};
Expand Down
Loading

0 comments on commit ebcd61c

Please sign in to comment.