From cdb12d7b31ec3355e918cb10ad9c8172cd50e72c Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 10 Dec 2024 18:48:44 -0500 Subject: [PATCH] refactor(ansi): return sequences for both ANSI and DEC modes when multiple modes are set or reset --- ansi/mode.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/ansi/mode.go b/ansi/mode.go index 2e77c738..57f3f0a8 100644 --- a/ansi/mode.go +++ b/ansi/mode.go @@ -51,7 +51,8 @@ type Mode interface { // SetMode (SM) returns a sequence to set a mode. // The mode arguments are a list of modes to set. // -// If one of the modes is a [DECMode], the sequence will use the DEC format. +// If one of the modes is a [DECMode], the function will returns two escape +// sequences. // // ANSI format: // @@ -74,7 +75,8 @@ func SM(modes ...Mode) string { // ResetMode (RM) returns a sequence to reset a mode. // The mode arguments are a list of modes to reset. // -// If one of the modes is a [DECMode], the sequence will use the DEC format. +// If one of the modes is a [DECMode], the function will returns two escape +// sequences. // // ANSI format: // @@ -94,9 +96,9 @@ func RM(modes ...Mode) string { return ResetMode(modes...) } -func setMode(reset bool, modes ...Mode) string { +func setMode(reset bool, modes ...Mode) (s string) { if len(modes) == 0 { - return "" + return } cmd := "h" @@ -113,21 +115,24 @@ func setMode(reset bool, modes ...Mode) string { return seq + strconv.Itoa(modes[0].Mode()) + cmd } - var dec bool - list := make([]string, len(modes)) - for i, m := range modes { - list[i] = strconv.Itoa(m.Mode()) + dec := make([]string, 0, len(modes)/2) + ansi := make([]string, 0, len(modes)/2) + for _, m := range modes { switch m.(type) { case DECMode: - dec = true + dec = append(dec, strconv.Itoa(m.Mode())) + case ANSIMode: + ansi = append(ansi, strconv.Itoa(m.Mode())) } } - if dec { - seq += "?" + if len(ansi) > 0 { + s += seq + strings.Join(ansi, ";") + cmd } - - return seq + strings.Join(list, ";") + cmd + if len(dec) > 0 { + s += seq + "?" + strings.Join(dec, ";") + cmd + } + return } // RequestMode (DECRQM) returns a sequence to request a mode from the terminal.