@@ -102,16 +102,21 @@ func NewLogger(config LogConfig) *Logger {
102102
103103// Scope returns a new logger with the given scope.
104104func (l * Logger ) Scope (scope string ) * Logger {
105+ return & Logger {scope : scope , level : l .level , size : l .size }
106+ }
107+
108+ func (l * Logger ) getScope () string {
105109 // Margin is 20% of terminal.
106110 size := l .size .Load ()
107111 margin := int (size .margin )
112+ scope := l .scope
108113 if len (scope ) > margin {
109114 scope = "…" + scope [len (scope )- margin + 1 :]
110115 } else {
111116 scope += strings .Repeat (" " , margin - len (scope ))
112117 }
113118 scope = strings .ReplaceAll (scope , "%" , "%%" )
114- return & Logger { scope : scope , level : l . level , size : l . size }
119+ return scope
115120}
116121
117122var ansiTable = func () map [LogLevel ]string {
@@ -140,8 +145,9 @@ func (l *Logger) getPrefix(level LogLevel) string {
140145 return ""
141146 }
142147 prefix := ansiTable [level ]
143- if l .scope != "" {
144- prefix = targetColour (l .scope ) + l .scope + "\033 [0m" + "| " + prefix
148+ scope := l .getScope ()
149+ if scope != "" {
150+ prefix = targetColour (scope ) + scope + "\033 [0m" + "| " + prefix
145151 }
146152 return prefix
147153}
@@ -203,9 +209,8 @@ func (l *Logger) Exec(dir, command string) error {
203209 defer l .size .Unsubscribe (changes )
204210
205211 // Resize the PTY to exclude the margin.
206- if w , h , err := term .GetSize (int (os .Stdin .Fd ())); err == nil {
207- _ = pty .Setsize (p , & pty.Winsize {Rows : uint16 (h ), Cols : uint16 (w ) - (l .size .Load ().margin + 1 )})
208- }
212+ size := l .size .Load ()
213+ _ = pty .Setsize (p , & pty.Winsize {Rows : size .height , Cols : size .width - (l .size .Load ().margin + 1 )})
209214
210215 go func () {
211216 for size := range changes {
@@ -286,7 +291,7 @@ func (l *Logger) writerScanner(wg *sync.WaitGroup, r *io.PipeReader, level LogLe
286291 switch cs .Final {
287292 case 'G' : // G is cursor horizontal absolute.
288293 // We have a CHA sequence, so add the margin width.
289- col := params [0 ] + 18
294+ col := params [0 ] + int ( l . size . Load (). margin ) + 2
290295 segment = csi .Text (fmt .Sprintf ("\033 [%dG" , col ))
291296
292297 case 'K' : // K is erase in line. We want to intercept 1 (clear to start of line) and 2 (clear entire line).
@@ -345,8 +350,8 @@ func (l *Logger) syncTermSize() {
345350 // Watch WINCH for changes.
346351 winch := make (chan os.Signal , 1 )
347352 signal .Notify (winch , syscall .SIGWINCH )
348- defer signal .Stop (winch )
349353 go func () {
354+ defer signal .Stop (winch )
350355 for range winch {
351356 if w , h , err := term .GetSize (int (os .Stdin .Fd ())); err == nil {
352357 margin := uint16 (max (16 , w / 5 ))
0 commit comments