Skip to content

Commit 6adafbb

Browse files
committed
term: add a separate term.set_tab_title/1 API for controling the current tab title in emulators like Konsole, that support many tabs
1 parent ca00029 commit 6adafbb

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

vlib/term/term_nix.c.v

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,22 @@ pub fn get_cursor_position() !Coord {
8080
return Coord{x, y}
8181
}
8282

83-
// set_terminal_title change the terminal title
83+
// set_terminal_title change the terminal title (usually that is the containing terminal window title)
8484
pub fn set_terminal_title(title string) bool {
8585
if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
86-
return true
86+
return false
87+
}
88+
print('\033]0;')
89+
print(title)
90+
print('\007')
91+
flush_stdout()
92+
return true
93+
}
94+
95+
// set_tab_title changes the terminal *tab title*, for terminal emulators like Konsole, that support several tabs
96+
pub fn set_tab_title(title string) bool {
97+
if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
98+
return false
8799
}
88100
print('\033]30;')
89101
print(title)
@@ -93,8 +105,12 @@ pub fn set_terminal_title(title string) bool {
93105
}
94106

95107
// clear clears current terminal screen.
96-
pub fn clear() {
108+
pub fn clear() bool {
109+
if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
110+
return false
111+
}
97112
print('\x1b[2J')
98113
print('\x1b[H')
99114
flush_stdout()
115+
return true
100116
}

vlib/term/term_test.v

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,19 @@ fn test_set_terminal_title() {
9797
if os.getenv('CI') != 'true' {
9898
return
9999
}
100-
title_change := term.set_terminal_title('v is awesome!')
101-
assert title_change == true
100+
term.set_terminal_title('v is awesome!')
101+
dump(@FILE)
102+
assert true
103+
}
104+
105+
fn test_set_tab_title() {
106+
// do not change the current terminal tab title outside of CI:
107+
if os.getenv('CI') != 'true' {
108+
return
109+
}
110+
term.set_tab_title('v is awesome!')
111+
dump(@FILE)
112+
assert true
102113
}
103114

104115
fn test_strip_ansi() {

0 commit comments

Comments
 (0)