1library(fansi) 2 3unitizer_sect("Strip ansi", { 4 strip_ctl(sprintf("hello %sworld%s", red, end)) 5 strip_ctl(sprintf("he%sllo %sworld", red, end)) 6 strip_ctl(sprintf("%shello %sworld%s", grn.bg, red, end)) 7 strip_ctl(sprintf("%s%shello %sworld%s", grn.bg, inv, red, end)) 8 9 string <- paste("string", format(1:10)) 10 string[c(2,4,6)] <- paste0(red, string[c(2,4,6)], end) 11 12 strip_ctl(string) 13 strip_sgr(string) 14 strip_sgr(1:3) 15}) 16unitizer_sect("Corner cases", { 17 strip_ctl("hello\033") 18 # should this be stripped? Not 100% clear since terminal seems to be waiting 19 # for input after it is cated 20 strip_ctl("hello\033[") 21 22 # illegal sequence 23 24 strip_ctl("hello\033[31##3m illegal") 25 strip_ctl("hello\033[31##m legal") 26 27 # non-char inputs; really should just coerce to char and move on since we know 28 # these can't contain the sequences (actually, only true of numerics; other 29 # objects e.g. Diff can produce chars with sequences) 30 31 strip_ctl(1:3) 32}) 33unitizer_sect("Whitespace", { 34 fansi:::process('hello world') 35 fansi:::process('hello. world') 36 fansi:::process(c('hello world', 'hello. world')) 37 fansi:::process('hello. world? moon! wow.') 38 fansi:::process(' hello') 39 fansi:::process(' hello\n world') 40 fansi:::process(' hello \n world') 41 fansi:::process(' hello world\n ') 42 fansi:::process('hello. ') 43 fansi:::process('hello! ') 44 fansi:::process('hello? ') 45 fansi:::process('hello? ') 46 47 # Tabs / ctrl; newlines remain 48 49 fansi:::process(' \t hello') 50 fansi:::process(' \t\a\r hello') 51 52 # interactiong between punct and ctrl 53 54 fansi:::process('hello. \r world.') 55 56 # CSIs 57 58 fansi:::process('hello. \033[31m world.\033[0m') 59 60 # Make sure we are not inadvertently changing SXPs 61 62 str1 <- c("hello ", " world") 63 fansi:::process(str1) 64 str1 65 66 # Paragraphs and so on 67 68 fansi:::process('hello.\n\nworld') 69 fansi:::process('hello.\n\n\nworld') 70 fansi:::process('hello.\n\n\n\nworld') 71 fansi:::process('hello.\n \nworld') 72 fansi:::process('hello.\n\t\nworld') 73 fansi:::process('hello.\n\t\n\tworld') 74 fansi:::process('hello.\n \t \n \t world') 75 fansi:::process('hello.\n\nworld\n\n') 76 fansi:::process('hello.\n\nworld\n\n ') 77 fansi:::process('\n\nhello.\n\t\n\tworld\n\t\n woohoo\n ') 78 fansi:::process('\n \t\nhello.\n\t\n\tworld\n\t\n woohoo\n ') 79}) 80unitizer_sect("Selective stripping", { 81 string.0 <- "hello\033k\033[45p world\n\033[31mgoodbye\a moon" 82 83 strip_ctl(string.0) 84 strip_ctl(string.0, "sgr") 85 strip_ctl(string.0, c("nl", "c0", "sgr", "csi", "esc")) 86 strip_ctl(string.0, "all") # equivalently 87 strip_ctl(string.0, c("c0", "esc")) 88 strip_ctl(string.0, c("nl")) 89 90 # don't strip anything (null op) 91 92 strip_ctl(string.0, character()) 93 94 # negations 95 96 strip_ctl(string.0, c("all", "c0", "esc")) 97 strip_ctl(string.0, c("all", "sgr")) 98 99 # add some illegal sequences 100 101 string.1 <- "hello\033\033[45p world\n\033[31#3mgoodbye\a moon" 102 103 strip_ctl(string.1, c("nl", "sgr", "esc")) 104 strip_ctl(string.1, c("csi")) 105 strip_ctl(string.1, "all") 106 strip_ctl(string.1, c("c0", "nl")) 107 strip_ctl(string.1, c("all", "sgr")) 108 109 strip_sgr(string.1) 110 111 # longer vec 112 113 strip_ctl(c(string.0, string.1, "hello"), warn=FALSE) 114 115 # possible corner cases 116 117 string.2 <- "\033k\033[45p\a\n\033[31mgoodbye moon" 118 strip_ctl(string.2) 119 strip_ctl(string.2, "sgr") 120 121 string.3 <- "hello world\033k\033[45p\a\n\033[31m" 122 strip_ctl(string.3) 123 strip_ctl(string.3, "sgr") 124 125}) 126unitizer_sect("Bad Inputs", { 127 strip_ctl("hello\033[41mworld", warn=1:3) 128 strip_ctl("hello\033[41mworld", ctl=1:3) 129 strip_ctl("hello\033[41mworld", ctl="bananas") 130 strip_ctl("hello\033[41mworld", strip="sgr") 131 132 strip_sgr("hello\033[41mworld", warn=1:3) 133 134}) 135