Skip to content

Commit

Permalink
update common task on removing ESC Codes
Browse files Browse the repository at this point in the history
Lessons learned from the cumuls model (PR #3332)
- Adds an optional "\r" at the end of the regexp
- Tests the change on the garderos model unit test
- Updates the garderos model unit test so that it can tests prompts with
ESC Codes
  • Loading branch information
robertcheramy committed Dec 10, 2024
1 parent e0cf8eb commit 065f676
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
13 changes: 7 additions & 6 deletions docs/Creating-Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ need to enable privileged mode, either without providing a password (by setting
Note: remove `:telnet, ` if your device does not support telnet.

### Common Task: remove ANSI escape codes
> :warning: This common task is still experimental.
> :warning: This common task is experimental.
> If it does not work for you, please open an issue so that we can adapt the
> code snippet.
Expand All @@ -89,15 +89,16 @@ You can remove most [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escap
code in your model:
```
# Remove ANSI escape codes
expect /\e\[[0-?]*[ -\/]*[@-~]/ do |data, re|
expect /\e\[[0-?]*[ -\/]*[@-~]\r?/ do |data, re|
data.gsub re, ''
end
```
Explanation of the Regular Expression:
- `\e\[` : Control Sequence Introducer (CSI), which starts with "ESC [".
- `[0-?]*`: "parameter" bytes (range 0x30–0x3F, corresponding to ASCII `0–9:;<=>?`)
- `[ -/]*`: "intermediate" bytes (range 0x20–0x2F, corresponding to ASCII ` !"#$%&'()*+,-./`)
- `[@-~]` : the "final" byte (range 0x40–0x7E, corresponding to ASCII ``@A–Z[\]^_`a–z{|}~).[``)
- `\e\[` : Control Sequence Introducer (CSI), which starts with "ESC [".
- `[0-?]*` : "Parameter" bytes (range 0x30–0x3F, corresponding to ASCII `0–9:;<=>?`).
- `[ -\/]*`: "Intermediate" bytes (range 0x20–0x2F, corresponding to ASCII ` !"#$%&'()*+,-./`).
- `[@-~]` : The "final" byte (range 0x40–0x7E, corresponding to ASCII ``@A–Z[\]^_`a–z{|}~).[``).
- `\r?` : Some ESC codes include a carriage return, which we do not want in the resulting config.

## Extending an existing model with a new command

Expand Down
4 changes: 2 additions & 2 deletions lib/oxidized/model/garderos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class Garderos < Oxidized::Model
# Routers for harsh environments
# grs = Garderos Router Software

# remove ANSI escape codes
expect /\e\[[0-?]*[ -\/]*[@-~]/ do |data, re|
# Remove ANSI escape codes
expect /\e\[[0-?]*[ -\/]*[@-~]\r?/ do |data, re|
data.gsub re, ''
end

Expand Down
10 changes: 6 additions & 4 deletions spec/model/garderos_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
end

it 'matches different prompts' do
# Pretty prompt
# Note that the real prompt looks like "\e[4m\rLAB-R1234_Garderos#\e[m\x20"
# The ANSI escape sequences are cleaned by the model (expect),
# this is tested in the test 'runs on R7709 with OS 003_006_068'
# Prompt without ANSI ESC Codes
_('LAB-R1234_Garderos# ').must_match Garderos.prompt

# Same prompt with ANSI ESC Codes, cleaned by the model
prompt = "\e[4m\rLAB-R1234_Garderos#\e[m "
prompt = @node.model.expects prompt
_(prompt).must_match Garderos.prompt
end

it 'runs on R7709 with OS 003_006_068' do
Expand Down

0 comments on commit 065f676

Please sign in to comment.