Skip to content

Commit 9d49376

Browse files
committed
Add session commands
- SetWindowTitle(title) - SetForegroundColor(color) - SetBackgroundColor(color) - SetCursorColor(color) - SaveScreen - RestoreScreen
1 parent 1111971 commit 9d49376

File tree

3 files changed

+92
-23
lines changed

3 files changed

+92
-23
lines changed

README.md

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,12 @@ fmt.Println(&buf)
145145
Other available helper functions are: `Faint`, `Italic`, `CrossOut`,
146146
`Underline`, `Overline`, `Reverse`, and `Blink`.
147147

148-
## Screen
148+
## Positioning
149149

150150
```go
151-
// Reset the terminal to its default style, removing any active styles
152-
termenv.Reset()
153-
154-
// Switch to the altscreen. The former view can be restored with ExitAltScreen()
155-
termenv.AltScreen()
156-
157-
// Exit the altscreen and return to the former terminal view
158-
termenv.ExitAltScreen()
159-
160-
// Clear the visible portion of the terminal
161-
termenv.ClearScreen()
162-
163151
// Move the cursor to a given position
164152
termenv.MoveCursor(row, column)
165153

166-
// Hide the cursor
167-
termenv.HideCursor()
168-
169-
// Show the cursor
170-
termenv.ShowCursor()
171-
172154
// Save the cursor position
173155
termenv.SaveCursorPosition()
174156

@@ -194,6 +176,28 @@ termenv.CursorNextLine(n)
194176
// Move the cursor up a given number of lines and place it at the beginning of
195177
// the line
196178
termenv.CursorPrevLine(n)
179+
```
180+
181+
## Screen
182+
183+
```go
184+
// Reset the terminal to its default style, removing any active styles
185+
termenv.Reset()
186+
187+
// RestoreScreen restores a previously saved screen state
188+
termenv.RestoreScreen()
189+
190+
// SaveScreen saves the screen state
191+
termenv.SaveScreen()
192+
193+
// Switch to the altscreen. The former view can be restored with ExitAltScreen()
194+
termenv.AltScreen()
195+
196+
// Exit the altscreen and return to the former terminal view
197+
termenv.ExitAltScreen()
198+
199+
// Clear the visible portion of the terminal
200+
termenv.ClearScreen()
197201

198202
// Clear the current line
199203
termenv.ClearLine()
@@ -213,6 +217,28 @@ termenv.InsertLines(n)
213217
termenv.DeleteLines(n)
214218
```
215219

220+
## Session
221+
222+
```go
223+
// SetWindowTitle sets the terminal window title
224+
termenv.SetWindowTitle(title)
225+
226+
// SetForegroundColor sets the default foreground color
227+
termenv.SetForegroundColor(color)
228+
229+
// SetBackgroundColor sets the default background color
230+
termenv.SetBackgroundColor(color)
231+
232+
// SetCursorColor sets the cursor color
233+
termenv.SetCursorColor(color)
234+
235+
// Hide the cursor
236+
termenv.HideCursor()
237+
238+
// Show the cursor
239+
termenv.ShowCursor()
240+
```
241+
216242
## Mouse
217243

218244
```go

screen.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
const (
9+
// Cursor positioning.
910
CursorUpSeq = "%dA"
1011
CursorDownSeq = "%dB"
1112
CursorForwardSeq = "%dC"
@@ -29,8 +30,7 @@ const (
2930
EraseLineLeftSeq = "1K"
3031
EraseEntireLineSeq = "2K"
3132

32-
ShowCursorSeq = "?25h"
33-
HideCursorSeq = "?25l"
33+
// Mouse.
3434
EnableMousePressSeq = "?9h" // press only (X10)
3535
DisableMousePressSeq = "?9l"
3636
EnableMouseSeq = "?1000h" // press, release, wheel
@@ -41,15 +41,52 @@ const (
4141
DisableMouseCellMotionSeq = "?1002l"
4242
EnableMouseAllMotionSeq = "?1003h" // press, release, move, wheel
4343
DisableMouseAllMotionSeq = "?1003l"
44-
AltScreenSeq = "?1049h"
45-
ExitAltScreenSeq = "?1049l"
44+
45+
// Screen.
46+
RestoreScreenSeq = "?47l"
47+
SaveScreenSeq = "?47h"
48+
AltScreenSeq = "?1049h"
49+
ExitAltScreenSeq = "?1049l"
50+
51+
// Session.
52+
SetWindowTitleSeq = "2;%s\007"
53+
SetForegroundColorSeq = "10;%s\007"
54+
SetBackgroundColorSeq = "11;%s\007"
55+
SetCursorColorSeq = "12;%s\007"
56+
ShowCursorSeq = "?25h"
57+
HideCursorSeq = "?25l"
4658
)
4759

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

65+
// SetForegroundColor sets the default foreground color.
66+
func SetForegroundColor(color Color) {
67+
fmt.Printf(OSC+SetForegroundColorSeq, color)
68+
}
69+
70+
// SetBackgroundColor sets the default background color.
71+
func SetBackgroundColor(color Color) {
72+
fmt.Printf(OSC+SetBackgroundColorSeq, color)
73+
}
74+
75+
// SetCursorColor sets the cursor color.
76+
func SetCursorColor(color Color) {
77+
fmt.Printf(OSC+SetCursorColorSeq, color)
78+
}
79+
80+
// RestoreScreen restores a previously saved screen state.
81+
func RestoreScreen() {
82+
fmt.Print(CSI + RestoreScreenSeq)
83+
}
84+
85+
// SaveScreen saves the screen state.
86+
func SaveScreen() {
87+
fmt.Print(CSI + SaveScreenSeq)
88+
}
89+
5390
// AltScreen switches to the alternate screen buffer. The former view can be
5491
// restored with ExitAltScreen().
5592
func AltScreen() {
@@ -213,3 +250,8 @@ func EnableMouseAllMotion() {
213250
func DisableMouseAllMotion() {
214251
fmt.Print(CSI + DisableMouseAllMotionSeq)
215252
}
253+
254+
// SetWindowTitle sets the terminal window title.
255+
func SetWindowTitle(title string) {
256+
fmt.Printf(OSC+SetWindowTitleSeq, title)
257+
}

termenv.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Profile int
1515

1616
const (
1717
CSI = "\x1b["
18+
OSC = "\x1b]"
1819

1920
Ascii = Profile(iota)
2021
ANSI

0 commit comments

Comments
 (0)