vimgolf icon indicating copy to clipboard operation
vimgolf copied to clipboard

List challenges from CLI

Open windo opened this issue 8 years ago • 5 comments

It would be convenient to be able to list (and search?) challenges from the CLI.

Maybe once the web interface is improved, it will not be that much of a problem.

windo avatar Apr 27 '17 22:04 windo

Hi,

Basically you've got them from main page:

$ curl http://vimgolf.com/ | sed -ne '/challenges/p'

Listing challenges a user submitted would be great.

mcrapet avatar Jul 14 '17 19:07 mcrapet

Sure, they can be picked out from the html. An API call would be nice though.

windo avatar Jul 19 '17 16:07 windo

perhaps this will work nicely as a wrapper:

$ cat golf

#!/bin/bash

function golf {
    local page shouldexit

    page=1
    shouldexit=0

    while [ $shouldexit != 1 ]; do
        curl -s http://www.vimgolf.com/?page=$page \
                | grep challenges/ \
                | sed 's#.*challenges/\(.*\)">\(.*\)<.*#\2 :: \1#' \
                | awk 'BEGIN { FS = "::" }; { printf "%50s %s\n", $1, $2 }'
        echo
        echo "Action:"
        echo "[N]ext, [P]revious, Page [N], [Q]uit, [challenge ID]"

        while true; do
            read -p '>> ' prompt

            if [[ $prompt =~ [Nn] ]]; then
                page=$((page + 1))
                break
            elif [[ $prompt =~ [Pp] ]]; then
                page=$((page - 1))
                break
            elif [[ $prompt =~ [0-9] ]]; then
                page=$prompt
                break
            elif [[ $prompt =~ [Qq]  ]]; then
                shouldexit=1
                break
            elif [[ $prompt =~ [a-z0-9]{24} ]]; then
                shouldexit=1
                echo starting vimgolf!!
                vimgolf put "$prompt"
                break
            fi
        done
    done
}

golf

rebelot avatar Jul 05 '18 16:07 rebelot

That s a good start : before upgrading the gem we can simply fetch it with bash.

It s easy to add a new route something like /api/challenges

And this route can return all the challenge (same data has homepage) without pagination. And a simple grep -i can do the search.

The question is : Which format is best suited for bash ? Csv ? Json ? Something custom ? I would say csv but any charactere can be present in the description

Hettomei avatar Jul 05 '18 21:07 Hettomei

I’d say first things first, either adopt a custom separator or disallow special characters in the description (this might also prevent the risk of command injections in some custom implementations maybe?)

rebelot avatar Jul 06 '18 00:07 rebelot