1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-02-09 13:23:51 +02:00

Add support for more complex command prompts (#583)

The bashsession lexer did only recognized command prompts starting with
one of the characters '#$%>'. This commit adds support for a prefixed
block of the form `[.*@.*]`, which is a common layout for command prompts.
This commit is contained in:
Tobias Neitzel 2021-12-20 08:04:05 +01:00 committed by GitHub
parent d38fcfcce0
commit ac2891ff2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 6 deletions

View File

@ -20,7 +20,7 @@ var BashSession = internal.Register(MustNewLazyLexer(
func bashsessionRules() Rules {
return Rules{
"root": {
{`(^[#$%>]\s*)(.*\n?)`, ByGroups(GenericPrompt, Using(Bash)), nil},
{`^((?:\[[^]]+@[^]]+\]\s?)?[#$%>])(\s*)(.*\n?)`, ByGroups(GenericPrompt, Text, Using(Bash)), nil},
{`^.+\n?`, GenericOutput, nil},
},
}

View File

@ -5,3 +5,11 @@ Hello, world!
> make -j build
%ls | wc -l
5
[user@host]$ whoami
user
[user@host] # id
uid=1000(user) gid=1000(user) groups=1000(user)
[super user :D@super host :D] %if [ 1 -eq 1 ]; then uname; fi
Linux
[user@host]%echo $((1+1))
2

View File

@ -1,19 +1,56 @@
[
{"type":"GenericPrompt","value":"$ "},
{"type":"GenericPrompt","value":"$"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"echo"},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"Hello, world!\""},
{"type":"Text","value":"\n"},
{"type":"GenericOutput","value":"Hello, world!\n"},
{"type":"GenericPrompt","value":"%\t"},
{"type":"GenericPrompt","value":"%"},
{"type":"Text","value":"\t"},
{"type":"NameBuiltin","value":"pwd"},
{"type":"Text","value":"\n"},
{"type":"GenericOutput","value":"/User/foo\n"},
{"type":"GenericPrompt","value":"> "},
{"type":"Text","value":"make -j build\n"},
{"type":"GenericPrompt","value":"\u003e"},
{"type":"Text","value":" make -j build\n"},
{"type":"GenericPrompt","value":"%"},
{"type":"Text","value":"ls "},
{"type":"Punctuation","value":"|"},
{"type":"Text","value":" wc -l\n"},
{"type":"GenericOutput","value":"5\n"}
{"type":"GenericOutput","value":"5\n"},
{"type":"GenericPrompt","value":"[user@host]$"},
{"type":"Text","value":" whoami\n"},
{"type":"GenericOutput","value":"user\n"},
{"type":"GenericPrompt","value":"[user@host] #"},
{"type":"Text","value":" id\n"},
{"type":"GenericOutput","value":"uid=1000(user) gid=1000(user) groups=1000(user)\n"},
{"type":"GenericPrompt","value":"[super user :D@super host :D] %"},
{"type":"Keyword","value":"if"},
{"type":"Text","value":" "},
{"type":"Operator","value":"["},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"1"},
{"type":"Text","value":" -eq "},
{"type":"LiteralNumber","value":"1"},
{"type":"Text","value":" "},
{"type":"Operator","value":"]"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"then"},
{"type":"Text","value":" uname"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"fi"},
{"type":"Text","value":"\n"},
{"type":"GenericOutput","value":"Linux\n"},
{"type":"GenericPrompt","value":"[user@host]%"},
{"type":"NameBuiltin","value":"echo"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"$(("},
{"type":"LiteralNumber","value":"1"},
{"type":"Operator","value":"+"},
{"type":"LiteralNumber","value":"1"},
{"type":"Keyword","value":"))"},
{"type":"Text","value":"\n"},
{"type":"GenericOutput","value":"2\n"}
]