~willdurand/public-inbox

Use the hut cli tool for pastes if installed and webapi is missing v1 PROPOSED

Chris DeLuca: 1
 Use the hut cli tool for pastes if installed and webapi is missing

 2 files changed, 49 insertions(+), 40 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~willdurand/public-inbox/patches/69897/mbox | git am -3
Learn more about email & git

[PATCH] Use the hut cli tool for pastes if installed and webapi is missing Export this patch

---
 README.md         |  3 +-
 autoload/srht.vim | 86 ++++++++++++++++++++++++++---------------------
 2 files changed, 49 insertions(+), 40 deletions(-)

diff --git a/README.md b/README.md
index 58939a8..64dcfe4 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,8 @@ This plugin provides the following features per service:
    installed, this plugin will allow `:GBrowse` to open sourcehut URLs
- `paste.sr.ht`:
  - create and retrieve pastes with `:SrhtPaste [sha]` (requires `curl` and the
    [webapi-vim](https://github.com/mattn/webapi-vim) plugin)
    [webapi-vim](https://github.com/mattn/webapi-vim) plugin _or_ the
    [hut](https://sr.ht/~xenrox/hut/) cli tool)

### Configuration

diff --git a/autoload/srht.vim b/autoload/srht.vim
index b8c0676..68ddb10 100644
--- a/autoload/srht.vim
+++ b/autoload/srht.vim
@@ -84,48 +84,55 @@ endfunction

function! s:srht_post_paste(visibility) abort
  " See: https://man.sr.ht/paste.sr.ht/api.md#post-apipastes
  let paste = { 'visibility': a:visibility, 'files' : [] }

  " TODO: add support for multiple buffers
  if executable('hut')
    echon 'Publishing paste... '
    let output = system('hut paste create --visibility ' . a:visibility . ' ' . expand('%:p') . ' 2>&1')
    let url = split(output)[-1]
    echon output
    return url
  else
    let paste = { 'visibility': a:visibility, 'files' : [] }

    echo 'Creating paste content... '
    let contents = join(getline(1, line('$')), "\n")
    let paste['files'] = [
    \ { 'contents': contents, 'filename': s:get_current_filename() }
    \]

    let token = s:get_srht_token()
    if len(token) == 0
      redraw
      call s:print_error(v:errmsg)
      return
    endif

  echo 'Creating paste content... '
  let contents = join(getline(1, line('$')), "\n")
  let paste['files'] = [
  \ { 'contents': contents, 'filename': s:get_current_filename() }
  \]

  let token = s:get_srht_token()
  if len(token) == 0
    redraw
    call s:print_error(v:errmsg)
    return
  endif
    echon 'Publishing paste... '

  redraw
  echon 'Publishing paste... '
    let headers = {
    \ 'Content-Type': 'application/json',
    \ 'Authorization': 'token '.token,
    \}
    let post_url = s:srht_paste_url.'api/pastes'
    let res = webapi#http#post(post_url, webapi#json#encode(paste), headers)
    let data = webapi#json#decode(res.content)

  let headers = {
  \ 'Content-Type': 'application/json',
  \ 'Authorization': 'token '.token,
  \}
  let post_url = s:srht_paste_url.'api/pastes'
  let res = webapi#http#post(post_url, webapi#json#encode(paste), headers)
  let data = webapi#json#decode(res.content)
    redraw
    let url = ''

  redraw
  let url = ''
    if res.status =~# '^2'
      " Build URL
      let url = s:srht_paste_url.data.user.canonical_name.'/'.data.sha
      echomsg 'Done: '.url
    else
      for error in data.errors
        call s:print_error('Error: '.error.reason)
      endfor
    endif

  if res.status =~# '^2'
    " Build URL
    let url = s:srht_paste_url.data.user.canonical_name.'/'.data.sha
    echomsg 'Done: '.url
  else
    for error in data.errors
      call s:print_error('Error: '.error.reason)
    endfor
    return url
  endif

  return url
endfunction

function! s:srht_get_paste(sha) abort
@@ -186,14 +193,15 @@ endfunction
function! srht#Paste(...) abort
  redraw

  " Ensure webapi plugin is installed
  if globpath(&rtp, 'autoload/webapi/http.vim') ==# ''
  " Ensure either hut or webapi plugin is installed
  if globpath(&rtp, 'autoload/webapi/http.vim') ==# '' && !executable('hut')
    echohl ErrorMsg
    echomsg 'srht.vim requires the ''webapi'' plugin, '
    echomsg 'visit https://github.com/mattn/webapi-vim to install it.'
    echomsg 'srht.vim requires the ''webapi'' vim plugin, or the ''hut'' cli tool, '
    echomsg 'visit https://github.com/mattn/webapi-vim to install the plugin, '
    echomsg 'or https://sr.ht/~xenrox/hut/ to install the cli tool.'
    echohl None
    finish
  else
  elseif globpath(&rtp, 'autoload/webapi/http.vim') !=# ''
    " Make sure the webapi plugin works.
    call webapi#json#true()
  endif
-- 
2.54.0
Ack, I’m seeing now the ask to use [PATCH srht.vim] instead of [PATCH]. Sorry about that!