n-panelize 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copy this file into /usr/share/zsh/site-functions/
  2. # and add 'autoload n-panelize` to .zshrc
  3. #
  4. # This function somewhat reminds the panelize feature from Midnight Commander
  5. # It allows browsing output of arbitrary command. Example usage:
  6. # v-panelize ls /usr/local/bin
  7. #
  8. # Uses n-list
  9. emulate -L zsh
  10. setopt extendedglob
  11. zmodload zsh/curses
  12. local IFS="
  13. "
  14. unset NLIST_COLORING_PATTERN
  15. [ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
  16. [ -f ~/.config/znt/n-panelize.conf ] && builtin source ~/.config/znt/n-panelize.conf
  17. local list
  18. local selected
  19. NLIST_REMEMBER_STATE=0
  20. if [ -t 0 ]; then
  21. # Check if there is proper input
  22. if [ "$#" -lt 1 ]; then
  23. echo "Usage: n-panelize {command} [option|argument] ... or command | n-panelize"
  24. return 1
  25. fi
  26. # This loop makes script faster on some Zsh's (e.g. 5.0.8)
  27. repeat 1; do
  28. list=( `"$@"` )
  29. done
  30. # TODO: $? doesn't reach user
  31. [ "$?" -eq 127 ] && return $?
  32. else
  33. # Check if can reattach to terminal
  34. if [[ ! -c /dev/tty && ! -t 2 ]]; then
  35. echo "No terminal available (no /dev/tty)"
  36. return 1
  37. fi
  38. # This loop makes script faster on some Zsh's (e.g. 5.0.8)
  39. repeat 1; do
  40. list=( "${(@f)"$(<&0)"}" )
  41. done
  42. if [[ ! -c /dev/tty ]]; then
  43. exec <&2
  44. else
  45. exec </dev/tty
  46. fi
  47. fi
  48. n-list "${list[@]}"
  49. if [ "$REPLY" -gt 0 ]; then
  50. selected="$reply[REPLY]"
  51. print -zr "# $selected"
  52. fi
  53. # vim: set filetype=zsh: