Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Baby step: Allow no keywords on the command line, and introduce -h, --help option #109

Merged
merged 1 commit into from
Jun 6, 2016

Conversation

zmwangx
Copy link
Collaborator

@zmwangx zmwangx commented Jun 6, 2016

This is point 4 of #87 (comment).


All the machinery is almost there, just need to flip the switch.

Instead of printing the help text when there's is no argument, we now support the standard -h, --help option, and a command line with no argument leads us into the REPL immediately.

Transcript of a sample session:

> googler -C -w github.com -n 3
Please initiate a query.
googler (? for help): f
Initiate a query first.
googler (? for help): n
Initiate a query first.
googler (? for help): o
Initiate a query first.
googler (? for help): googler

 1 GitHub - jarun/googler: Google from the command-line
https://github.com/jarun/googler
Google from the command-line. Contribute to googler development by
creating an account on GitHub.

 2 googler/googler.1 at master · jarun/googler · GitHub
https://github.com/jarun/googler/blob/master/googler.1
Google from the command-line. Contribute to googler development by
creating an account on GitHub.

 3 googler/README.md at master · jarun/googler · GitHub
https://github.com/jarun/googler/blob/master/README.md
Google from the command-line. Contribute to googler development by
creating an account on GitHub.

googler (? for help): q

Also fixed a bug in GooglerArgumentParser where file is not being defaulted to sys.stderr.

All the machinery is almost there, just need to flip the switch.

Instead of printing the help text when there's is no argument, we now
support the standard -h, --help option, and a command line with no
argument leads us into the REPL immediately.

Transcript of a sample session:

    > googler -C -w github.com -n 3
    Please initiate a query.
    googler (? for help): f
    Initiate a query first.
    googler (? for help): n
    Initiate a query first.
    googler (? for help): o
    Initiate a query first.
    googler (? for help): googler

     1 GitHub - jarun/googler: Google from the command-line
    https://github.com/jarun/googler
    Google from the command-line. Contribute to googler development by
    creating an account on GitHub.

     2 googler/googler.1 at master · jarun/googler · GitHub
    https://github.com/jarun/googler/blob/master/googler.1
    Google from the command-line. Contribute to googler development by
    creating an account on GitHub.

     3 googler/README.md at master · jarun/googler · GitHub
    https://github.com/jarun/googler/blob/master/README.md
    Google from the command-line. Contribute to googler development by
    creating an account on GitHub.

    googler (? for help): q

Also fixed a bug in GooglerArgumentParser where file is not being
defaulted to sys.stderr.
@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

Damn, really hope we have some sort auto-generation mechanism for the man page.

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

Grapse helps to some extent.

@jarun jarun merged commit b41d8ed into jarun:master Jun 6, 2016
@jarun
Copy link
Owner

jarun commented Jun 6, 2016

Thank you!

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

Grapse helps to some extent.

The demo seems to be in mdoc with a quick glance. I suppose it has a groff_man mode?

@zmwangx zmwangx deleted the cmdline-no-keywords branch June 6, 2016 03:21
@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

It doesn't seem to work?

screen shot 2016-06-05 at 8 23 07 pm
screen shot 2016-06-05 at 8 23 10 pm

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

Or should we switch to mdoc? AFAIK mdoc is certainly more pleasant than groff_man, and arguably better documented, IIRC.

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

Actually, although I had no luck with http://www.roperzh.com/grapse/, live preview for man pages is a very good idea. I'll hack up something this weekend. The idea is very simple, and is easily done with a bit of Python/Shell and a bit of JavaScript:

  • groff_man to generate HTML;
  • webbrowser.open to open HTML, except that we append a <script> tag for auto refreshing;
  • Have a daemon watch for source file changes, regen HTML on change.

And https://github.com/joeyespo/grip can be used as an example.
There's also https://github.com/mgedmin/restview which is similar.

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

It doesn't seem to work?

Hmmm... That's a problem. I never tested with googler.

I gifted them their first bug and we'll wait for a while on this :).

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

They don't support groff, that's all.

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

Ahh, just saw their readme.

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

This should work for you (OS X only): http://sveinbjorn.org/mandrake

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

I'm going through mdoc.

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

http://sveinbjorn.org/mandrake

Looks nice, but that also seems to be a mdoc editor. At least it won't render our man page.

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

groff -man -Thtml output is crap, but man's plain text output is very easy to parse, so it's easy to convert it to HTML. I'll do the previewer when I have time.

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

Just to give you a sense of what I could achieve with a pretty trivial parsing script:

screen shot 2016-06-05 at 9 54 38 pm

I'll call it a day, and prepare for my tomorrow's exam.

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

👍

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

I think you should create a new project and share this with the world. ;)
Many devs will be very happy.

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

I think you should create a new project and share this with the world. ;)

Exactly what I had in mind. But in reality it would be something trivial, like a single Python script.

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

Yes, but it's better that one person writes it than everyone writing it.

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

BTW, quick question because I didn't reach there yet (and donno where it is):

If we move to mdoc with the command:

man googler

work? Or we need to invoke something else?

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

man googler work? Or we need to invoke something else?

It should work, but need to double check on Linux. At least the official FreeBSD man pages are in mdoc AFAIK, and OS X being close to FreeBSD also has a lot of mdoc man pages too.

You can check the man page of BSD tar on Linux if you have time: https://gist.github.com/anonymous/ea1f802fa997f9d7fb707ec8f57145fa. I don't want to spin up a Linux box at the moment.

Yes, but it's better that one person writes it than everyone writing it.

Yeah, what I meant to say is that it's somewhat more subtable for a gist...

I'll throw what I have here in case you're interested. Apparently it's not integrated and have rendering problems for now, but I'll polish when I have time.

previewman:

#!/usr/bin/env python3

import sys
import textwrap

def to_html(ms):
    length = len(ms)
    ms += chr(0) * 2
    bs = chr(8)
    hs = textwrap.dedent("""
    <!DOCTYPE html>
    <head>
    <title>man page</title>
    </head>
    <body>
    <div style="width: 846px; margin: auto">
    <pre>
    """)
    inbold = False
    inunderline = False
    i = 0
    while i < length:
        # TODO: Both bold and underline
        ch = ms[i]
        if ms[i+1] == bs:
            bold = (ch == ms[i+2])
            underline = (ch == '_')
        else:
            bold = underline = False

        if inbold and not bold:
            hs += '</b>'
            inbold = False
        if inunderline and not underline:
            hs += '</u>'
            inunderline = False

        if bold:
            if not inbold:
                hs += '<b>'
            inbold = True
            i += 2
        if underline:
            if not inunderline:
                hs += '<u>'
            inunderline = True
            ch = ms[i+2]
            i += 2

        if ch == '<':
            hs += '&lt;'
        elif ch == '>':
            hs += '&gt;'
        else:
            hs += ch
        i += 1
    hs += textwrap.dedent("""
    </pre>
    </div>
    </body>
    """)
    return hs

def main():
    sys.stdout.write(to_html(sys.stdin.read()))

if __name__ == '__main__':
    main()

Then

> COLUMNS=120 PAGER=cat man ~/dev/src/forks/googler/googler.1 </dev/null | ./previewman | browser

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

👍

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

You can check the man page of BSD tar on Linux if you have time

Will confirm in the evening.

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 6, 2016

As you can see, text processing in Python... Such a shame.

But I'm not a Perl wizard, and even worse at Awk, so that's it, a poorly performing piece of crap that somehow does the job (almost).

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

I think in a combat it's better to use familiar weapons than suicide with brand new ones (without proper training).

@jarun
Copy link
Owner

jarun commented Jun 6, 2016

It should work, but need to double check on Linux.

Just checked. Works fine.

@jarun
Copy link
Owner

jarun commented Jun 8, 2016

I found this one as well: ronn

@zmwangx
Copy link
Collaborator Author

zmwangx commented Jun 8, 2016

Knew that one, not impressed. I once tried all Markdown to man (groff) converters on the market and was very disappointed by the quality (not sure if the quality of stuff have since improved). Among lightweight markup languages I only know AsciiDoc (or rather, the implementation a2x(1)) as being reasonably good at man pages: http://www.methods.co.nz/asciidoc/chunked/ch24.html. Plus AsciiDoc is kinda endorsed by Linus.

@jarun
Copy link
Owner

jarun commented Jun 8, 2016

asciidoc looks good too. I can try this out. Anything that makes life easier with man pages.

@knkarthik knkarthik mentioned this pull request Sep 18, 2016
7 tasks
@lock lock bot locked and limited conversation to collaborators Nov 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants