Skip to content

Commit ede1344

Browse files
ewelspditommasochristopher-hakkaartbentsherman
authored
Support standard environment variables for ANSI terminal output (#6362)
--------- Signed-off-by: Phil Ewels <[email protected]> Signed-off-by: Paolo Di Tommaso <[email protected]> Signed-off-by: Ben Sherman <[email protected]> Co-authored-by: Paolo Di Tommaso <[email protected]> Co-authored-by: Chris Hakkaart <[email protected]> Co-authored-by: Ben Sherman <[email protected]>
1 parent b5b0676 commit ede1344

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

docs/reference/env-vars.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ The following environment variables control the configuration of the Nextflow ru
250250
: Defines the minimum size of the `.command.run` staging script for it to be written to a separate `.command.stage` file (default: `'1 MB'`).
251251
: This setting is useful for executors that impose a size limit on job scripts.
252252

253-
## Proxy settings
253+
## Other settings
254254

255255
`FTP_PROXY`
256256
: :::{versionadded} 21.06.0-edge
@@ -269,5 +269,12 @@ The following environment variables control the configuration of the Nextflow ru
269269
Proxy authentication is supported by providing the credentials in the proxy URL, e.g. `https://user:[email protected]:port`.
270270
:::
271271

272+
`NO_COLOR`
273+
: Disables ANSI color codes in Nextflow log output. When this variable is set, Nextflow prints plain text logs following the [NO_COLOR standard](https://no-color.org/).
274+
: If both `NO_COLOR` and `NXF_ANSI_LOG` are set, `NXF_ANSI_LOG` takes precedence.
275+
272276
`NO_PROXY`
273277
: Defines one or more host names that should not use the proxy server. Separate multiple names using a comma character.
278+
279+
`TERMINAL_WIDTH`
280+
: Forces the terminal width of ANSI-formatted log output. Overrides automatic terminal width detection and uses the specified width for line wrapping when set to an integer value.

modules/nextflow/src/main/groovy/nextflow/cli/CliOptions.groovy

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package nextflow.cli
1818

1919
import com.beust.jcommander.DynamicParameter
2020
import com.beust.jcommander.Parameter
21+
import groovy.transform.CompileStatic
2122
import groovy.util.logging.Slf4j
23+
import nextflow.SysEnv
2224
import nextflow.exception.AbortOperationException
2325
import org.fusesource.jansi.Ansi
2426

@@ -28,6 +30,7 @@ import org.fusesource.jansi.Ansi
2830
* @author Paolo Di Tommaso <[email protected]>
2931
*/
3032
@Slf4j
33+
@CompileStatic
3134
class CliOptions {
3235

3336
/**
@@ -102,18 +105,24 @@ class CliOptions {
102105
if( quiet )
103106
return ansiLog = false
104107

105-
final env = System.getenv('NXF_ANSI_LOG')
108+
final env = SysEnv.get('NXF_ANSI_LOG')
106109
if( env ) try {
107110
return Boolean.parseBoolean(env)
108111
}
109112
catch (Exception e) {
110113
log.warn "Invalid boolean value for variable NXF_ANSI_LOG: $env -- it must be 'true' or 'false'"
111114
}
115+
116+
// Check NO_COLOR environment variable (https://no-color.org/)
117+
final noColor = SysEnv.get('NO_COLOR')
118+
if( noColor ) {
119+
return ansiLog = false
120+
}
112121
return Ansi.isEnabled()
113122
}
114123

115124
boolean hasAnsiLogFlag() {
116-
ansiLog==true || System.getenv('NXF_ANSI_LOG')=='true'
125+
ansiLog==true || SysEnv.get('NXF_ANSI_LOG')=='true'
117126
}
118127

119128
}

modules/nextflow/src/main/groovy/nextflow/trace/AnsiLogObserver.groovy

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@
1616

1717
package nextflow.trace
1818

19+
import static nextflow.util.LoggerHelper.*
20+
import static org.fusesource.jansi.Ansi.*
21+
1922
import java.util.regex.Pattern
2023

2124
import groovy.transform.CompileStatic
2225
import jline.TerminalFactory
2326
import nextflow.Session
27+
import nextflow.SysEnv
2428
import nextflow.trace.event.TaskEvent
2529
import nextflow.util.Duration
2630
import nextflow.util.SysHelper
2731
import nextflow.util.Threads
2832
import org.fusesource.jansi.Ansi
2933
import org.fusesource.jansi.AnsiConsole
30-
31-
import static nextflow.util.LoggerHelper.isHashLogPrefix
32-
import static org.fusesource.jansi.Ansi.Attribute
33-
import static org.fusesource.jansi.Ansi.Color
34-
import static org.fusesource.jansi.Ansi.ansi
3534
/**
3635
* Implements an observer which display workflow
3736
* execution progress and notifications using
@@ -97,12 +96,25 @@ class AnsiLogObserver implements TraceObserverV2 {
9796

9897
private long lastWidthReset
9998

100-
private Boolean enableSummary = System.getenv('NXF_ANSI_SUMMARY') as Boolean
99+
private Boolean enableSummary = SysEnv.get('NXF_ANSI_SUMMARY') as Boolean
101100

102101
private final int WARN_MESSAGE_TIMEOUT = 35_000
103102

104103
private WorkflowStatsObserver statsObserver
105104

105+
private static Integer getEnvTerminalWidth() {
106+
final env = SysEnv.get('TERMINAL_WIDTH')
107+
if( !env )
108+
return null
109+
try {
110+
return Integer.parseInt(env)
111+
}
112+
catch( NumberFormatException e ) {
113+
// do not log error to avoid catch-22 logging event (this class renders logging events)
114+
return null
115+
}
116+
}
117+
106118
private void markModified() {
107119
changeTimestamp = System.currentTimeMillis()
108120
}
@@ -256,7 +268,7 @@ class AnsiLogObserver implements TraceObserverV2 {
256268
return
257269
}
258270

259-
cols = TerminalFactory.get().getWidth()
271+
cols = getEnvTerminalWidth() ?: TerminalFactory.get().getWidth()
260272
rows = TerminalFactory.get().getHeight()
261273

262274
// calc max width

0 commit comments

Comments
 (0)