Skip to content

Commit

Permalink
Add session commands
Browse files Browse the repository at this point in the history
- SetWindowTitle(title)
- SetForegroundColor(color)
- SetBackgroundColor(color)
- SetCursorColor(color)
- SaveScreen
- RestoreScreen
  • Loading branch information
muesli committed Jan 31, 2022
1 parent 1111971 commit 9d49376
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 23 deletions.
64 changes: 45 additions & 19 deletions README.md
Expand Up @@ -145,30 +145,12 @@ fmt.Println(&buf)
Other available helper functions are: `Faint`, `Italic`, `CrossOut`,
`Underline`, `Overline`, `Reverse`, and `Blink`.

## Screen
## Positioning

```go
// Reset the terminal to its default style, removing any active styles
termenv.Reset()

// Switch to the altscreen. The former view can be restored with ExitAltScreen()
termenv.AltScreen()

// Exit the altscreen and return to the former terminal view
termenv.ExitAltScreen()

// Clear the visible portion of the terminal
termenv.ClearScreen()

// Move the cursor to a given position
termenv.MoveCursor(row, column)

// Hide the cursor
termenv.HideCursor()

// Show the cursor
termenv.ShowCursor()

// Save the cursor position
termenv.SaveCursorPosition()

Expand All @@ -194,6 +176,28 @@ termenv.CursorNextLine(n)
// Move the cursor up a given number of lines and place it at the beginning of
// the line
termenv.CursorPrevLine(n)
```

## Screen

```go
// Reset the terminal to its default style, removing any active styles
termenv.Reset()

// RestoreScreen restores a previously saved screen state
termenv.RestoreScreen()

// SaveScreen saves the screen state
termenv.SaveScreen()

// Switch to the altscreen. The former view can be restored with ExitAltScreen()
termenv.AltScreen()

// Exit the altscreen and return to the former terminal view
termenv.ExitAltScreen()

// Clear the visible portion of the terminal
termenv.ClearScreen()

// Clear the current line
termenv.ClearLine()
Expand All @@ -213,6 +217,28 @@ termenv.InsertLines(n)
termenv.DeleteLines(n)
```

## Session

```go
// SetWindowTitle sets the terminal window title
termenv.SetWindowTitle(title)

// SetForegroundColor sets the default foreground color
termenv.SetForegroundColor(color)

// SetBackgroundColor sets the default background color
termenv.SetBackgroundColor(color)

// SetCursorColor sets the cursor color
termenv.SetCursorColor(color)

// Hide the cursor
termenv.HideCursor()

// Show the cursor
termenv.ShowCursor()
```

## Mouse

```go
Expand Down
50 changes: 46 additions & 4 deletions screen.go
Expand Up @@ -6,6 +6,7 @@ import (
)

const (
// Cursor positioning.
CursorUpSeq = "%dA"
CursorDownSeq = "%dB"
CursorForwardSeq = "%dC"
Expand All @@ -29,8 +30,7 @@ const (
EraseLineLeftSeq = "1K"
EraseEntireLineSeq = "2K"

ShowCursorSeq = "?25h"
HideCursorSeq = "?25l"
// Mouse.
EnableMousePressSeq = "?9h" // press only (X10)
DisableMousePressSeq = "?9l"
EnableMouseSeq = "?1000h" // press, release, wheel
Expand All @@ -41,15 +41,52 @@ const (
DisableMouseCellMotionSeq = "?1002l"
EnableMouseAllMotionSeq = "?1003h" // press, release, move, wheel
DisableMouseAllMotionSeq = "?1003l"
AltScreenSeq = "?1049h"
ExitAltScreenSeq = "?1049l"

// Screen.
RestoreScreenSeq = "?47l"
SaveScreenSeq = "?47h"
AltScreenSeq = "?1049h"
ExitAltScreenSeq = "?1049l"

// Session.
SetWindowTitleSeq = "2;%s\007"
SetForegroundColorSeq = "10;%s\007"
SetBackgroundColorSeq = "11;%s\007"
SetCursorColorSeq = "12;%s\007"
ShowCursorSeq = "?25h"
HideCursorSeq = "?25l"
)

// Reset the terminal to its default style, removing any active styles.
func Reset() {
fmt.Print(CSI + ResetSeq + "m")
}

// SetForegroundColor sets the default foreground color.
func SetForegroundColor(color Color) {
fmt.Printf(OSC+SetForegroundColorSeq, color)
}

// SetBackgroundColor sets the default background color.
func SetBackgroundColor(color Color) {
fmt.Printf(OSC+SetBackgroundColorSeq, color)
}

// SetCursorColor sets the cursor color.
func SetCursorColor(color Color) {
fmt.Printf(OSC+SetCursorColorSeq, color)
}

// RestoreScreen restores a previously saved screen state.
func RestoreScreen() {
fmt.Print(CSI + RestoreScreenSeq)
}

// SaveScreen saves the screen state.
func SaveScreen() {
fmt.Print(CSI + SaveScreenSeq)
}

// AltScreen switches to the alternate screen buffer. The former view can be
// restored with ExitAltScreen().
func AltScreen() {
Expand Down Expand Up @@ -213,3 +250,8 @@ func EnableMouseAllMotion() {
func DisableMouseAllMotion() {
fmt.Print(CSI + DisableMouseAllMotionSeq)
}

// SetWindowTitle sets the terminal window title.
func SetWindowTitle(title string) {
fmt.Printf(OSC+SetWindowTitleSeq, title)
}
1 change: 1 addition & 0 deletions termenv.go
Expand Up @@ -15,6 +15,7 @@ type Profile int

const (
CSI = "\x1b["
OSC = "\x1b]"

Ascii = Profile(iota)
ANSI
Expand Down

0 comments on commit 9d49376

Please sign in to comment.