Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env bash
  2. declare -r esc=$'\033'
  3. declare -r c_reset="${esc}[0m"
  4. declare -r c_red="${esc}[31m"
  5. declare OPTIND
  6. declare -a opts=( -\# )
  7. declare id
  8. usage() {
  9. LESS=-FEXR less <<'HELP'
  10. ix [OPTIONS]
  11. -l list all pastes, uses fzf for interactive use
  12. -d [id] delete the paste at [id]
  13. -i [id] replaces the paste with stdin
  14. -h print this help
  15. HELP
  16. }
  17. err() {
  18. printf "${c_red}%s${c_reset}\n" "$@" >&2
  19. }
  20. die() {
  21. err "$@"
  22. exit 1
  23. }
  24. has() {
  25. local verbose=0
  26. if [[ $1 == '-v' ]]; then
  27. verbose=1
  28. shift
  29. fi
  30. for c; do c="${c%% *}"
  31. if ! command -v "$c" &> /dev/null; then
  32. (( verbose > 0 )) && err "$c not found"
  33. return 1
  34. fi
  35. done
  36. }
  37. select_from() {
  38. local cmd='command -v'
  39. for a; do
  40. case "$a" in
  41. -c) cmd="$2"; shift 2 ;;
  42. esac
  43. done
  44. for c; do
  45. if $cmd "${c%% *}" &> /dev/null; then
  46. echo "$c"
  47. return 0
  48. fi
  49. done
  50. return 1
  51. }
  52. has_account() {
  53. [[ -r ~/.netrc ]] && grep -qF 'ix.io' ~/.netrc
  54. }
  55. create_account() {
  56. echo "It seems you don't have a ~/.netrc with ix.io in it. Let's make one!"
  57. read -r -p 'enter a username: ' username
  58. read -rs -p 'enter a password (this will be hashed with sha256sum): ' password
  59. echo ''
  60. password=$(sha256sum <<< "$password")
  61. password="${password/% -}"
  62. printf "machine ix.io login %s password %s" "$username" "$password" >> ~/.netrc
  63. chmod 600 ~/.netrc
  64. if has_account; then
  65. echo 'success!'
  66. else
  67. die 'could not create account!'
  68. fi
  69. }
  70. get_user_name() {
  71. awk '"ix.io"==$2{print $4}' ~/.netrc
  72. }
  73. get_pastes() {
  74. curl -s "http://ix.io/user/$1" |
  75. grep -A1 -P '\<a href="\/[a-zA-Z0-9]+"\>' |
  76. awk -F'--' '
  77. BEGIN {FS="<a href=\""}
  78. /a href/{ sub(/\">\[r\]<\/a>/, "\t", $2); printf "http://ix.io" $2 }
  79. /@/{sub(/^\s+/, "", $0); print}
  80. '
  81. }
  82. list_pastes() {
  83. local highlighter
  84. highlighter=$(select_from 'highlight -q --force -O ansi' pygmentize cat)
  85. url=$(get_pastes "$(get_user_name)" | fzf \
  86. --inline-info --cycle \
  87. --header='Ctrl-E = edit; Ctrl-V = view; Ctrl-D = delete' \
  88. --preview="curl -s '{1}' | ${highlighter}" \
  89. --bind 'space:jump' \
  90. --bind 'q:abort' \
  91. --bind "Ctrl-V:execute:less -R < <(curl -s '{1}' ${highlighter:+ | $highlighter}) > /dev/tty" \
  92. --bind 'Ctrl-E:execute:p={1}; edit=$(curl -s "$p" | vipe); ix -i "${p##*/}" <<< "$edit"' \
  93. --expect='Ctrl-D')
  94. if [[ -n "$url" ]]; then
  95. mapfile -t res <<< "$url"
  96. if [[ "${res[0]}" = 'Ctrl-D' ]]; then
  97. ix -d "${res[1]}"
  98. list_pastes
  99. exit
  100. fi
  101. fi
  102. }
  103. has -v curl || die
  104. has_account || create_account
  105. [[ -e ~/.netrc ]] && opts+=( '-n' )
  106. while getopts ":hld:i:n:" x; do
  107. case "$x" in
  108. h) usage; exit ;;
  109. d) curl -s "${opts[@]}" -X DELETE "$OPTARG"; exit ;;
  110. l)
  111. if [[ -e ~/.netrc ]]; then
  112. list_pastes | cut -d' ' -f1 | xclip
  113. else
  114. die 'no netrc found'
  115. fi
  116. exit ;;
  117. i) opts+=( -X PUT ); id="$OPTARG" ;;
  118. n) opts+=( -F "read:1=$OPTARG" ) ;;
  119. esac
  120. done
  121. shift $(( OPTIND - 1 ))
  122. if [[ -t 0 ]]; then
  123. if [[ -n "$1" ]]; then
  124. filename="$1"
  125. shift
  126. response=$(curl "${opts[@]}" -F f:1=@"$filename" "$@" "ix.io/$id")
  127. clipboard=$(select_from 'xclip -r ' 'xsel')
  128. if [[ -n "$clipboard" ]]; then
  129. tee /dev/tty <<< "$response" | $clipboard
  130. else
  131. echo "$response"
  132. fi
  133. exit
  134. fi
  135. echo "^C to cancel, ^D to send."
  136. fi
  137. curl "${opts[@]}" -F f:1='<-' "$@" "ix.io/$id"