zsh で Volta のオートコンプリートを有効にする方法

結論

1.

まず補完機能用のスクリプトを出力します (_volta) 。

$ volta completions zsh -o _volta
   note:  does not exist, creating it
success: installed completions to _volta

2.

出力したスクリプトを /usr/local/share/zsh/site-functions に移動します。

$ sudo mv _volta /usr/local/share/zsh/site-functions

3.

シェルを再起動します*1.

4.

補完機能が効いていることを確かめます。

$ volta [TAB]
completions  -- Generates Volta completions
fetch        -- Fetches a tool to the local machine
help         -- Prints this message or the help of the given subcommand(s)
install      -- Installs a tool in your toolchain
list         -- Displays the current toolchain
pin          -- Pins your project's runtime or package manager
run          -- Run a command with custom Node, npm, and/or Yarn versions
setup        -- Enables Volta for the current user / shell
uninstall    -- Uninstalls a tool from your toolchain
use          --
which        -- Locates the actual binary that will be called by Volta

*1:sourceコマンド等でもよいです

Twitter の API で取得できる media(画像)の URL は原寸大画像を示してはいない

結論

たとえば、API で取得できる media_url_httpshttps://pbs.twimg.com/media/FAlJ_JsUUAAgOSq.jpg であったとします。この URL に wget をしても原寸大画像は得られません*1

常に原寸大画像を取得するためには https://pbs.twimg.com/media/FAlJ_JsUUAAgOSq?format=jpg&name=orig のようにします。つまり media_url_https に対して以下の処理を施します。

  • 拡張子を取り除く(ドットを含む)
  • パラメータとして以下の2つを付与する
    • format=拡張子(ドット不要)
      • たとえば format=jpg
    • name=orig

Ruby でこの処理を行うスクリプト例は以下のとおりです。

url = 'https://pbs.twimg.com/media/FAlJ_JsUUAAgOSq.jpg'
extension_including_dot = File.extname(url)
extension_without_dot = extension_including_dot[1..]
url_without_extension = url.gsub(extension_including_dot, '')

"#{url_without_extension}?format=#{extension_without_dot}&name=orig" #=> "https://pbs.twimg.com/media/FAlJ_JsUUAAgOSq?format=jpg&name=orig"

*1:ただし、元画像のサイズが一定以下ならば原寸大画像となる

\u 始まりの Unicode でエンコードされたテキストが含まれている JSON は jq を通せば UTF-8 にデコードされる

実例

たとえば以下のような JSON (sample.json) があったとします。

{
  "keyword": "\u751f\u5f92\u4f1a\u9577"
}

上記の keyword の値である \u751f\u5f92\u4f1a\u9577 の部分を UTF-8 にデコードした JSON を出力するには jq を通せばよいです。

$ cat sample.json | jq
{
  "keyword": "生徒会長"
}

補足

\x はじまりのエンコードテキストの場合には Parse error になります。

Powered by はてなブログ