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

Add session commands #53

Merged
merged 1 commit into from Jan 31, 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
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