Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use consts for sequences #102

Merged
merged 1 commit into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions color.go
Expand Up @@ -117,12 +117,12 @@ func xTermColor(s string) (RGBColor, error) {
}

switch {
case strings.HasSuffix(s, "\a"):
s = strings.TrimSuffix(s, "\a")
case strings.HasSuffix(s, "\033"):
s = strings.TrimSuffix(s, "\033")
case strings.HasSuffix(s, "\033\\"):
s = strings.TrimSuffix(s, "\033\\")
case strings.HasSuffix(s, string(BEL)):
s = strings.TrimSuffix(s, string(BEL))
case strings.HasSuffix(s, string(ESC)):
s = strings.TrimSuffix(s, string(ESC))
case strings.HasSuffix(s, ST):
s = strings.TrimSuffix(s, ST)
default:
return RGBColor(""), ErrInvalidColor
}
Expand Down
6 changes: 1 addition & 5 deletions hyperlink.go
@@ -1,15 +1,11 @@
package termenv

import (
"fmt"
)

// Hyperlink creates a hyperlink using OSC8.
func Hyperlink(link, name string) string {
return output.Hyperlink(link, name)
}

// Hyperlink creates a hyperlink using OSC8.
func (o *Output) Hyperlink(link, name string) string {
return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\", link, name)
return OSC + "8;;" + link + ST + name + OSC + "8;;" + ST
}
4 changes: 1 addition & 3 deletions notification.go
@@ -1,13 +1,11 @@
package termenv

import "fmt"

// Notification triggers a notification using OSC777.
func Notification(title, body string) {
output.Notification(title, body)
}

// Notification triggers a notification using OSC777.
func (o *Output) Notification(title, body string) {
_, _ = o.WriteString(fmt.Sprintf(OSC+"777;notify;%s;%s\x1b\\", title, body))
_, _ = o.WriteString(OSC + "777;notify;" + title + ";" + body + ST)
}
8 changes: 4 additions & 4 deletions screen.go
Expand Up @@ -57,10 +57,10 @@ const (
EndBracketedPasteSeq = "201~"

// Session.
SetWindowTitleSeq = "2;%s\007"
SetForegroundColorSeq = "10;%s\007"
SetBackgroundColorSeq = "11;%s\007"
SetCursorColorSeq = "12;%s\007"
SetWindowTitleSeq = "2;%s" + string(BEL)
SetForegroundColorSeq = "10;%s" + string(BEL)
SetBackgroundColorSeq = "11;%s" + string(BEL)
SetCursorColorSeq = "12;%s" + string(BEL)
ShowCursorSeq = "?25h"
HideCursorSeq = "?25l"
)
Expand Down
10 changes: 8 additions & 2 deletions termenv.go
Expand Up @@ -12,10 +12,16 @@ var (
)

const (
// Escape character
ESC = '\x1b'
// Bell
BEL = '\a'
// Control Sequence Introducer
CSI = "\x1b["
CSI = string(ESC) + "["
// Operating System Command
OSC = "\x1b]"
OSC = string(ESC) + "]"
// String Terminator
ST = string(ESC) + `\`
)

func (o *Output) isTTY() bool {
Expand Down
8 changes: 4 additions & 4 deletions termenv_unix.go
Expand Up @@ -160,7 +160,7 @@ func readNextResponse(fd File) (response string, isOSC bool, err error) {
}

// first byte must be ESC
for start != '\033' {
for start != ESC {
start, err = readNextByte(fd)
if err != nil {
return "", false, err
Expand Down Expand Up @@ -197,7 +197,7 @@ func readNextResponse(fd File) (response string, isOSC bool, err error) {

if oscResponse {
// OSC can be terminated by BEL (\a) or ST (ESC)
if b == '\a' || strings.HasSuffix(response, "\033") {
if b == BEL || strings.HasSuffix(response, string(ESC)) {
return response, true, nil
}
} else {
Expand Down Expand Up @@ -249,10 +249,10 @@ func (o Output) termStatusReport(sequence int) (string, error) {
}

// first, send OSC query, which is ignored by terminal which do not support it
fmt.Fprintf(tty, "\033]%d;?\033\\", sequence)
fmt.Fprintf(tty, OSC+"%d;?"+ST, sequence)

// then, query cursor position, should be supported by all terminals
fmt.Fprintf(tty, "\033[6n")
fmt.Fprintf(tty, CSI+"6n")

// read the next response
res, isOSC, err := readNextResponse(tty)
Expand Down