Created
September 30, 2016 12:59
-
-
Save JosephMillsAtWork/7d8c59093b667af86b4955a7c78bf7c7 to your computer and use it in GitHub Desktop.
Bash Syntax Highlighter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "bashhighlighter.h" | |
#include <QColor> | |
BashSyntaxHighlighter::BashSyntaxHighlighter(QObject *parent) | |
: | |
QSyntaxHighlighter(parent) | |
{ | |
} | |
BashSyntaxHighlighter::BashSyntaxHighlighter(QTextDocument *parent) | |
: QSyntaxHighlighter(parent) | |
{ | |
HighlightingRule rule; | |
//keywordFormat.setFontWeight(QFont::Bold); | |
//FIXME make long ass List | |
QStringList commandList; | |
commandList << "lsblk" << "lsb_release"<<"lscpu"<< "alias" << "apropos" << "apt-get" << "aptitude" << "aspell" << "awk" << "basename" | |
<< "bash" << "basename" << "bc" << "bg" << "break" << "builtin" << "bzip2"<< "cal" | |
<< "cat" << "cd"<< "cfdisk"<< "chgrp"<< "chmod"<< "chown"<< "chroot"<< "chkconfig" | |
<< "cksum"<< "clear"<< "cmp"<< "comm"<< "command"<< "continue"<< "cp"<< "cron" | |
<< "crontab"<< "csplit"<< "curl" << "cut" "date"<< "dc"<< "dd"<< "ddrescue"<< "declare" | |
<< "df"<< "diff"<< "diff3"<< "dig"<< "dir"<< "dircolors"<< "dirname"<< "dirs"<< "dmesg" | |
<< "du"<< "echo"<< "egrep"<< "eject"<< "enable"<< "env"<< "ethtool"<< "eval"<< "exec" | |
<< "exit"<< "expect"<< "expand"<< "export"<< "expr"<< "fdformat"<< "fdisk"<< "fg" | |
<< "fgrep"<< "file"<< "find"<< "fmt"<< "fold"<< "format"<< "free"<< "fsck"<< "ftp" | |
<< "function"<< "fuser"<< "gawk"<< "getopts"<< "grep"<< "groupadd"<< "groupdel" | |
<< "groupmod"<< "groups"<< "gzip"<< "hash"<< "head"<< "help"<< "history" | |
<< "hostname"<< "htop"<< "iconv"<< "id"<< "ifconfig"<< "ifdown"<< "ifup" | |
<< "import"<< "install"<< "ip"<< "jobs"<< "join"<< "kill"<< "killall"<< "less" | |
<< "let"<< "link"<< "ln"<< "local"<< "locate"<< "logname"<< "logout"<< "look" | |
<< "lpc"<< "lpr"<< "lprint"<< "lprintd"<< "lprintq"<< "lprm"<< "ls"<< "lsof" | |
<< "make"<< "man"<< "mkdir"<< "mkfifo"<< "mkisofs"<< "mknod"<< "more"<< "most" | |
<< "mount"<< "mtools"<< "mtr"<< "mv"<< "mmv"<< "nc"<< "netstat"<< "nice" | |
<< "nl"<< "nohup"<< "notify-send"<< "nslookup"<< "open"<< "op"<< "passwd" | |
<< "paste"<< "pathchk"<< "ping"<< "pkill"<< "popd"<< "pr"<< "printcap" | |
<< "printenv"<< "printf"<< "ps"<< "pushd"<< "pv"<< "pwd"<< "q"<< "quota" | |
<< "quotacheck"<< "quotactl"<< "r"<< "ram"<< "rar"<< "rcp"<< "read" | |
<< "readarray" << "readonly" << "reboot" << "rename" << "renice" << "remsync" | |
<< "return" << "rev"<< "rm" << "rmdir" << "rsync"<< "screen" << "scp" | |
<< "sdiff"<< "sed"<< "select"<< "seq"<< "set"<< "sftp"<< "shift" | |
<< "shopt" << "shutdown"<< "sleep"<< "slocate"<< "sort"<< "source"<< "split" | |
<< "ssh"<< "stat"<< "strace"<< "su"<< "sudo"<< "sum"<< "suspend"<< "sync"<< "tail" | |
<< "tar"<< "tee"<< "test"<< "time"<< "timeout"<< "times"<< "touch"<< "top" | |
<< "tput"<< "traceroute"<< "trap"<< "tr"<< "true"<< "tsort"<< "tty"<< "type" | |
<< "ulimit"<< "umask"<< "umount"<< "unalias"<< "uname"<< "unexpand" | |
<< "uniq"<< "units"<< "unrar"<< "unset"<< "unshar"<< "until"<< "uptime" | |
<< "useradd"<< "userdel"<< "usermod"<< "users"<< "uuencode"<< "uudecode" | |
<< "v"<< "vdir"<< "vi"<< "vmstat"<< "wait"<< "watch"<< "wc"<< "whereis" | |
<< "which"<< "while"<< "who"<< "whoami"<< "wget"<< "write"<< "xargs" | |
<< "xdg-open" "xz"<< "yes"<< "zip"; | |
commandKeyword.setForeground(QColor("#A25100")); | |
foreach (QString pattern, commandList) { | |
rule.pattern = QRegExp(pattern + "\\ "); | |
rule.format = commandKeyword; | |
highlightingRules.append(rule); | |
} | |
keywordFormat.setForeground(Qt::darkRed); | |
QStringList keywordPatterns; | |
keywordPatterns << "if " << "then" << "elif " << "else" << "fi" << "while " << "do" << "done" << "until " << "for " | |
<< "\\&" << "\\|" << "\\ \\+\\ " << "\\\\" << "\\ /\\ " << "\\ \\*\\ " << "\\ \\-\\ " << ":" << ";" << "<" << ">" | |
<< "\\[" << "\\]" << "\\$\\[" << "\\{" << "\\}" << "\\$\\{" << "!" << "="; | |
foreach (QString pattern, keywordPatterns) { | |
rule.pattern = QRegExp(pattern); | |
rule.format = keywordFormat; | |
highlightingRules.append(rule); | |
} | |
variables.setForeground(QColor("#00FF00")); | |
rule.pattern = QRegExp("[a-zA-Z][a-zA-Z0-9]*="); | |
rule.format = variables; | |
highlightingRules.append(rule); | |
rule.pattern = QRegExp("\\$[a-zA-Z][a-zA-Z0-9]*"); | |
rule.format = variables; | |
highlightingRules.append(rule); | |
path.setForeground(Qt::darkBlue); | |
rule.pattern = QRegExp("/[A-Za-z0-9.\\-_/]*\\ "); | |
rule.format = path; | |
highlightingRules.append(rule); | |
optionsFormat.setForeground(Qt::blue); | |
rule.pattern = QRegExp("\\ \\-[a-zA-Z0-9]*\\ "); | |
rule.format = optionsFormat; | |
highlightingRules.append(rule); | |
rule.pattern = QRegExp("\\ \\-\\-[a-zA-Z0-9]*\\ "); | |
rule.format = optionsFormat; | |
highlightingRules.append(rule); | |
singleLineCommentFormat.setForeground(Qt::gray); | |
singleLineCommentFormat.setFontItalic(true); | |
rule.pattern = QRegExp("#[a-zA-Z0-9,.\\;\\:\\-_\\ ]*"); | |
rule.format = singleLineCommentFormat; | |
highlightingRules.append(rule); | |
quotationFormat.setForeground(Qt::darkGreen); | |
rule.pattern = QRegExp("\".*\""); | |
rule.format = quotationFormat; | |
highlightingRules.append(rule); | |
shebang.setForeground(Qt::darkCyan ); | |
rule.pattern = QRegExp("#!/bin/bash"); | |
rule.format = shebang; | |
highlightingRules.append(rule); | |
} | |
void BashSyntaxHighlighter::highlightBlock(const QString &text) { | |
foreach (HighlightingRule rule, highlightingRules) { | |
QRegExp expression(rule.pattern); | |
int index = text.indexOf(expression); | |
while (index >= 0) { | |
int length = expression.matchedLength(); | |
setFormat(index, length, rule.format); | |
index = text.indexOf(expression, index + length); | |
} | |
} | |
setCurrentBlockState(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QSyntaxHighlighter> | |
#include <QStringList> | |
class BashSyntaxHighlighter : public QSyntaxHighlighter { | |
public: | |
BashSyntaxHighlighter(QObject* parent=new QObject()); | |
BashSyntaxHighlighter(QTextDocument* parent); | |
void highlightBlock(const QString &text); | |
private: | |
struct HighlightingRule { | |
QRegExp pattern; | |
QTextCharFormat format; | |
}; | |
QVector<HighlightingRule> highlightingRules; | |
QRegExp commentStartExpression; | |
QRegExp commentEndExpression; | |
QTextCharFormat keywordFormat; | |
QTextCharFormat commandKeyword; | |
QTextCharFormat operatorKeyword; | |
QTextCharFormat variables; | |
QTextCharFormat shebang; | |
QTextCharFormat path; | |
QTextCharFormat optionsFormat; | |
QTextCharFormat singleLineCommentFormat; | |
QTextCharFormat multiLineCommentFormat; | |
QTextCharFormat quotationFormat; | |
QTextCharFormat functionFormat; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment