Skip to content

Commit

Permalink
refactor(ansi): return sequences for both ANSI and DEC modes when
Browse files Browse the repository at this point in the history
multiple modes are set or reset
  • Loading branch information
aymanbagabas committed Dec 10, 2024
1 parent cc86585 commit cdb12d7
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions ansi/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand All @@ -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:
//
Expand All @@ -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"
Expand All @@ -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.
Expand Down

0 comments on commit cdb12d7

Please sign in to comment.