Skip to content

Commit

Permalink
Scrub function
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Sep 8, 2024
1 parent dc770a5 commit 0772e48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion alire.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ansiada"
description = "ANSI escape sequences"
version = "1.0.0"
version = "1.1.0-dev"

licenses = "MIT"
authors = ["Alejandro R. Mosteo"]
Expand Down
32 changes: 32 additions & 0 deletions src/ansiada.ads
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package AnsiAda with Pure is

type States is (Off, On);

function Scrub (Sequence : String) return String;
-- Remove all ANSI formatting from a string;

function Shorten (Sequence : String) return String is (Sequence);
-- Some consecutive commands can be combined, resulting in a shorter
-- string. Currently does nothing, but included for future optimization.
Expand Down Expand Up @@ -367,4 +370,33 @@ private
function Scroll_Down (Lines : Positive) return String is
(CSI & Img (Lines) & "T");

-----------
-- Scrub --
-----------

function Scrub (Clean : String;
Dirty : String;
Cleaning : Boolean := False)
return String
is (if Dirty = ""
then Clean
else
(if Cleaning and then Dirty (Dirty'First) /= 'm' then
Scrub (Clean, Dirty (Dirty'First + 1 .. Dirty'Last), True)
elsif Cleaning and then Dirty (Dirty'First) = 'm' then
Scrub (Clean, Dirty (Dirty'First + 1 .. Dirty'Last), False)
elsif not Cleaning and then Dirty (Dirty'First) = ESC then
Scrub (Clean, Dirty (Dirty'First + 1 .. Dirty'Last), True)
elsif not Cleaning and then Dirty (Dirty'First) /= ESC then
Scrub (Clean & Dirty (Dirty'First),
Dirty (Dirty'First + 1 .. Dirty'Last), False)
else
raise Program_Error with "Unexpected state while scrubbing with "
& "clean=" & Clean
& " dirty=" & Dirty
& " cleaning=" & Cleaning'Image));

function Scrub (Sequence : String) return String
is (Scrub ("", Sequence));

end AnsiAda;

0 comments on commit 0772e48

Please sign in to comment.