davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

Help output is too narrow when run in Emacs on Linux #222

Closed wconrad closed 9 years ago

wconrad commented 9 years ago

When I run a gli program inside Emacs in Linux, the help output is too narrow. To reproduce:

Create a new gli project

wayne@treebeard:/tmp/todo$ bundle exec bin/todo
fatal: Not a git repository (or any of the parent directories): .git
NAME
    todo - Describe your application here

SYNOPSIS
    todo [global options] command [command options] [arguments...]

VERSION
    0.0.1

GLOBAL OPTIONS
    -f, --flagname=The name of the argument - Describe
                                              some
                                              flag
                                              here
                                              (default:
                                              the
                                              default)
    --help                                  - Show
                                              this
                                              message
    -s, --[no-]switch                       - Describe
                                              some
                                              switch
                                              here
    --version                               - Display
                                              the
                                              program
                                              version

COMMANDS
    add      - Describe
               add
               here
    complete - Describe
               complete
               here
    help     - Shows
               a
               list
               of
               commands
               or
               help
               for
               one
               command
    list     - Describe
               list
               here

Versions

Run outside of emacs, the stty command reports its width normally:

wayne@treebeard:~$ stty size
63 237

But inside an Emacs shell, it reports the width like this:

wayne@treebeard:~$ stty size
0 0

I presume that "0 0" means that stty can't determine the terminal size. I haven't found out if this represents a bug, or is just the way it is in an Emacs shell.

A possible workaround

It should be possible to make gli to disregard a zero width report from stty, causing it to fallback to Terminal.default_size. That will result in passable output when a gli program is run in emacs, or any other environment where stty doesn't know the terminal width.

If you like, I'd be glad to submit a pull request with that change.

davetron5000 commented 9 years ago

Hmm. The way it figures out what to do is here: https://github.com/davetron5000/gli/blob/gli-2/lib/gli/terminal.rb#L53 (sorry about that code—I think I had just discovered lambdas and was reading a lot of FP stuff at the time :).

You could have your editor export COLUMNS and LINES, but I would agree that ignoring 0's probably makes sense. You could probably change this line: https://github.com/davetron5000/gli/blob/gli-2/lib/gli/terminal.rb#L81 so that it ignores any 0's and moves on to the next "size determiner".

wconrad commented 9 years ago

The lambdas are fine. I think, though, that having them come in pairs is causing a bit of a problem. To make this change, the first lambda--the predicate--will need to actually run the stty command just as the second lambda does. If there was just one, and it returned nil or some-such to indicate "don't know," that would make things easier for this change.

However, I think calling stty twice is the sure thing: The test coverage in this code isn't complete, and I hesitate to make to much of a change to it

I've got ideas for how the code could be structured differently, but they are more radical surgery (with attendant risks, and taking more time). I would think of moving each determiner into its own little class, tested in isolation. Are you interested in any changes of that sort?