ruby b - 使用fzf浏览Chrome书签

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby b - 使用fzf浏览Chrome书签相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env bash
# vim: set filetype=ruby:
# b - browse Chrome bookmarks with fzf

[ $(uname) = Darwin ] || exit 1
which fzf > /dev/null 2>&1 || brew reinstall --HEAD fzf || exit 1

/usr/bin/ruby -x "$0"                                          |
  fzf-tmux -u 30% --ansi --multi --no-hscroll --tiebreak=begin |
  awk 'BEGIN { FS = "\t" } { print $2 }'                       |
  xargs open

exit $?

#!ruby
# encoding: utf-8

require 'json'
FILE = '~/Library/Application Support/Google/Chrome/Default/Bookmarks'
CJK  = /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/

def build parent, json
  name = [parent, json['name']].compact.join('/')
  if json['type'] == 'folder'
    json['children'].map { |child| build name, child }
  else
    { name: name, url: json['url'] }
  end
end

def just str, width
  str.ljust(width - str.scan(CJK).length)
end

def trim str, width
  len = 0
  str.each_char.each_with_index do |char, idx|
    len += char =~ CJK ? 2 : 1
    return str[0, idx] if len > width
  end
  str
end

width = `tput cols`.strip.to_i / 2
json  = JSON.load File.read File.expand_path FILE
items = json['roots']
        .values_at(*%w(bookmark_bar synced other))
        .compact
        .map { |e| build nil, e }
        .flatten

items.each do |item|
  name = trim item[:name], width
  puts [just(name, width),
        item[:url]].join("\t\x1b[36m") + "\x1b[m"
end

以上是关于ruby b - 使用fzf浏览Chrome书签的主要内容,如果未能解决你的问题,请参考以下文章

chrome浏览器书签为啥不能同步

chrome谷歌浏览器如何导入或导出书签

如何导出谷歌浏览器Chrome的书签

MacBook上chrome浏览器怎么让书签左键直接新开标签页

002.谷歌浏览器Chrome如何导出或导入书签

我在谷歌chrome浏览器上保存的书签怎么都没了?怎么回事?怎么恢复啊