Verified Commit 68dc40b1 authored by Hermann Mayer's avatar Hermann Mayer
Browse files

BashRC: Corrected some git-stats glitches. VimRC: Improved JSON/XML formatting helpers.

parent fd9233c2
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
#!/usr/bin/env ruby
require 'tempfile'
require 'pp'

$width = 80
input = ARGF.read
file = Tempfile.new
file.write(input)
file.flush
words = '[\w.!?~\(\)\{\}\[\]-]'
output = `tidy -xml -i --quiet true --indent-attributes true #{file.path}`
output = output.gsub(/>(#{words})/, ">\n\\1").gsub(/(#{words})</, "\\1\n<")
stack = []
text = []

# Wraps the string into lines no longer than width. This method
# breaks on the first whitespace character that does not exceed width
# (which is 80 by default).
#
# @param str [String] the string to word wrap
# @param width [Integer] maximum line width
# @param indent [Integer] the left indent level
# @return [String] the word wrapped variant
def word_wrap(str, width = $width, indent = 0)
  lines = str.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n").lines
  lines.map { |line| ' ' * indent + line }.join
end

lines = output.lines.map do |line|
  if /<\w/.match? line
    if match = line.match(/^(\s+)\S/)
      level = match[1].size
    else
      level = 0
    end
    stack << [line.match(/<([\w-]+) ?/)[1], level]
    text = []
  end

  if /<\/#{stack.last.first}>/.match? line
    elem, level = stack.pop
    line = (' ' * level) + line.lstrip

    unless text.empty?
      indent = level + 2
      text = word_wrap(text.join(' '), $width - indent, indent)
      line = text + line
      text = []
    end

    next line
  end

  unless /<|>|=".*"$/.match? line
    text << line.strip
    next
  end

  line
end

puts lines.join
+6 −3
Original line number Diff line number Diff line
@@ -76,12 +76,15 @@ imap <F5> <ESC><i}<CR>i
" Reformat the buffer
map <F7> mzgg=G`z<CR>
map <leader>jf <ESC>:execute "%!$HOME/.vim/tools/json-pretty.js " . &shiftwidth<CR><CR>
xnoremap <leader>jf c<C-R>=system("$HOME/.vim/tools/json-pretty.js " . &shiftwidth, @")<CR><ESC>
map <leader>xx <ESC>:silent %!$HOME/.vim/tools/xml-format.rb<CR><CR>
xnoremap <leader>xx <ESC>:'<,'>:!$HOME/.vim/tools/xml-format.rb<CR>
map <leader>jm <ESC>:%!$HOME/.vim/tools/json-pretty.js -m<CR><CR>
map <leader>pf <ESC>:%!$HOME/.vim/tools/php-pretty.php<CR><CR>
map <leade>pf <ESC>:%!$HOME/.vim/tools/php-pretty.php<CR><CR>
" map <leader>jf <ESC>:%!python $HOME/.vim/tools/json-tool.py<CR>
" map <leader>jf <ESC>:%!python -mjson.tool<CR>
map <leader>xf <ESC>:silent %!xmllint --encode UTF-8 --format -<CR>
map <leader>hf <ESC>:silent %!tidy -mi -xml -wrap 0 %<CR>
map <leader>xf <ESC>:silent %!tidy -xml -i --quiet true --indent-attributes true<CR>
xnoremap <leader>xf <ESC>:'<,'>:!tidy -xml -i --quiet true --indent-attributes true<CR>
vmap <leader>jsf <ESC>:'<,'>!js-beautify -b expand -B -f -<CR><CR>

" Add UUIDv4
+2 −2
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ function git-user-stats()
            { printf "%s %s %s\n",add,subs,loc }'
    fi

    git log --author="${1}" --pretty=tformat: --numstat | \
    git log --all --author="${1}" --pretty=tformat: --numstat | \
        gawk "${awkCommand}"
}

@@ -32,7 +32,7 @@ function git-stat()
    (
        echo "Commits Additions Deletions Total Email Name"

        git log --format="%aE %aN" | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}' | sort -rn | while read line; do
        git log --all --format="%aE %aN" | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}' | sort -rn | while read line; do

            commits=$(echo $line | cut -d ' ' -f1)
            name=$(echo $line | cut -d ' ' -f2-)