Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support externally supplying a terminal width when running outside a tty #4186

Closed
2 tasks done
Nemo157 opened this issue Sep 6, 2022 · 4 comments · Fixed by #4208
Closed
2 tasks done

Support externally supplying a terminal width when running outside a tty #4186

Nemo157 opened this issue Sep 6, 2022 · 4 comments · Fixed by #4208
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@Nemo157
Copy link
Contributor

Nemo157 commented Sep 6, 2022

Please complete the following tasks

Clap Version

v3.2.20

Describe your use case

I am attempting to verify my readme contains the latest version of the help text from my utility. To do so I need an appropriately widthed help text to compare against (and auto-update from).

Describe the solution you'd like

I'm not sure what the best interface for this would be. One thought I had was to obey the COLUMNS variable if that is set and the output is not a tty, that would make text="$(COLUMNS=60 cargo run -- --help)" work fine.

Alternatives, if applicable

Locally I found a way to get a sub-tty with a limited width

script -q -c 'sh -c "stty cols 60 && cargo run -q -- --help"' $(mktemp)

but unfortunately that doesn't work in GitHub Actions for some unknown reason.

Additional Context

No response

@Nemo157 Nemo157 added the C-enhancement Category: Raise on the bar on expectations label Sep 6, 2022
@epage
Copy link
Member

epage commented Sep 6, 2022

Already terminal_size doesn't do detection if STDOUT is not a tty:

Another option is to not use If STDOUT is not a tty, returns None

Is that sufficient or something going wrong with it? Or are you wanting a different fallback terminal width?

Also, as a workaround, you can implement this all yourself by calling terminal_size directly and setting term_width under the specific circumstances for your app.

@Nemo157
Copy link
Contributor Author

Nemo157 commented Sep 6, 2022

Or are you wanting a different fallback terminal width?

Yes, this; I want to override the fallback terminal width in my script to have a consistent width no matter what terminal I'm running under.

Also, as a workaround, you can implement this all yourself

Yep, it doesn't seem too hard (just misses out on a lot of the Parser nicities to be able to override the derive). I just feel like it's pretty common to include --help output into a readme, where the default 100 column fallback is far too wide, so surely there must be other people running into this.

@epage
Copy link
Member

epage commented Sep 12, 2022

Yes, this; I want to override the fallback terminal width in my script to have a consistent width no matter what terminal I'm running under.

In my initial research on this, it sounded like COLUMNS isn't actually env-variable but is instead something built directly into some shells that some users forward on to the environment and that application support is very half-hazard. This made me less interested in supporting it.

Then I saw it suggested in https://clig.dev and saw it used in git which makes me feel more comfortable in supporting it.

The question is what should the level of precedence be?

  1. COLUMNS
  2. `terminal_size``
  3. default

or

  1. `terminal_size``
  2. COLUMNS
  3. default

@epage
Copy link
Member

epage commented Sep 12, 2022

Also, I feel safer putting this into clap v4 rather than clap v3 (if we make the release window)

epage added a commit to epage/clap that referenced this issue Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants