Skip to content

Commit

Permalink
feat: Display progress bars by default when stdout is a TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 13, 2023
1 parent 2924cf8 commit 0a22983
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions assets/chezmoi.io/docs/reference/command-line-flags/global.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ Read and write the persistent state from *filename*. By default, chezmoi stores
its persistent state in `chezmoistate.boltdb` in the same directory as its
configuration file.

## `--progress`
## `--progress` *value*

Show progress when downloading externals.
Show progress when downloading externals. *value* can be `on`, `off`, or `auto`.
The default is `auto` which shows progress bars when stdout is a terminal.

## `-R`, `--refresh-externals` [*value*]

Expand Down
15 changes: 13 additions & 2 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type ConfigFile struct {
Mode chezmoi.Mode `json:"mode" mapstructure:"mode" yaml:"mode"`
Pager string `json:"pager" mapstructure:"pager" yaml:"pager"`
PINEntry pinEntryConfig `json:"pinentry" mapstructure:"pinentry" yaml:"pinentry"`
Progress bool `json:"progress" mapstructure:"progress" yaml:"progress"`
Progress autoBool `json:"progress" mapstructure:"progress" yaml:"progress"`
Safe bool `json:"safe" mapstructure:"safe" yaml:"safe"`
ScriptEnv map[string]string `json:"scriptEnv" mapstructure:"scriptEnv" yaml:"scriptEnv"`
ScriptTempDir chezmoi.AbsPath `json:"scriptTempDir" mapstructure:"scriptTempDir" yaml:"scriptTempDir"` //nolint:lll
Expand Down Expand Up @@ -1373,7 +1373,7 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
persistentFlags.VarP(&c.DestDirAbsPath, "destination", "D", "Set destination directory")
persistentFlags.Var(&c.Mode, "mode", "Mode")
persistentFlags.Var(&c.persistentStateAbsPath, "persistent-state", "Set persistent state file")
persistentFlags.BoolVar(&c.Progress, "progress", c.Progress, "Display progress bars")
persistentFlags.Var(&c.Progress, "progress", "Display progress bars")
persistentFlags.BoolVar(&c.Safe, "safe", c.Safe, "Safely replace files and symlinks")
persistentFlags.VarP(&c.SourceDirAbsPath, "source", "S", "Set source directory")
persistentFlags.Var(&c.UseBuiltinAge, "use-builtin-age", "Use builtin age")
Expand Down Expand Up @@ -1945,6 +1945,14 @@ func (c *Config) persistentStateFile() (chezmoi.AbsPath, error) {
return defaultConfigFileAbsPath.Dir().Join(persistentStateFileRelPath), nil
}

// progressAutoFunc detects whether progress bars should be displayed.
func (c *Config) progressAutoFunc() bool {
if stdout, ok := c.stdout.(*os.File); ok {
return term.IsTerminal(int(stdout.Fd()))
}
return false
}

func (c *Config) newTemplateData() *templateData {
// Determine the user's username and group, if possible.
//
Expand Down Expand Up @@ -2379,6 +2387,9 @@ func newConfigFile(bds *xdg.BaseDirectorySpecification) ConfigFile {
},
Interpreters: defaultInterpreters,
Pager: os.Getenv("PAGER"),
Progress: autoBool{
auto: true,
},
PINEntry: pinEntryConfig{
Options: pinEntryDefaultOptions,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/readhttpresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (m httpSpinnerModel) View() string {

func (c *Config) readHTTPResponse(resp *http.Response) ([]byte, error) {
switch {
case c.noTTY || !c.Progress:
case c.noTTY || !c.Progress.Value(c.progressAutoFunc):
return io.ReadAll(resp.Body)

case resp.ContentLength >= 0:
Expand Down

0 comments on commit 0a22983

Please sign in to comment.