Skip to content

Commit

Permalink
Add support for more complex command prompts (#583)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
qtc-de committed Dec 20, 2021
1 parent d38fcfc commit ac2891f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lexers/b/bashsession.go
Expand Up @@ -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},
},
}
Expand Down
8 changes: 8 additions & 0 deletions lexers/testdata/bashsession.actual
Expand Up @@ -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
47 changes: 42 additions & 5 deletions lexers/testdata/bashsession.expected
@@ -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"}
]

0 comments on commit ac2891f

Please sign in to comment.