Advertisement
LACabeza

Notes.sh

Sep 14th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.89 KB | None | 0 0
  1. #! /bin/bash
  2. #
  3. # Simple notes
  4. # Author: Victor Ananjevsky <ananasik@gmail.com>, 2010
  5. #         http://code.google.com/p/yad/wiki/Notes
  6. #
  7. # Meus comandos a 2 cliques ;-)
  8. # Author: rai3mb
  9. #         http://www.vivaolinux.com.br/artigo/Yad-016-Eu-quero-sempre-mais-de-ti?pagina=10
  10. #
  11. # Secret notes
  12. # Author: Alysson Gonçalves <agalysson@gmail.com>, 2012
  13. #
  14.  
  15.  
  16. # Cria uma pasta chamada notes no dir de cache do usuário
  17. export dir_notes="${XDG_CACHE_HOME:-$HOME/.cache}/notes"
  18. [ -e $dir_notes ] || mkdir -p $dir_notes
  19.  
  20. export notes_hist=$dir_notes/notes.hist
  21. [ -e $notes_hist ] || touch $notes_hist
  22.  
  23. export temp_pipe=/tmp/notes.$$
  24. [ -p $temp_pipe ] || mkfifo $temp_pipe
  25. trap "rm -f $temp_pipe" EXIT
  26. exec 3<> $temp_pipe
  27.  
  28. # Função responsável por abrir a nota
  29. function _abrir() {
  30.  
  31.     file_note=$1
  32.     [ -e $file_note ] || return
  33.    
  34.     # Cria arquivo temporário para fins diversos
  35.     tmpfile=$(mktemp --tmpdir notes_new-$$.XXX)
  36.    
  37.     # Salva as alterações no arquivo temporário e depois salva no arquivo original
  38.     yad --text-info --title=${file_note##*/} --show-uri --geometry=400x200-0-0 \
  39.        --window-icon="text-editor" --filename="$file_note" --editable \
  40.         --button="gtk-save:0" --button="gtk-close:1" > $tmpfile
  41.        
  42.     if [[ $? -eq 0 ]]; then
  43.         mv $tmpfile $file_note
  44.     fi
  45.    
  46.     # Salva o histórico de notas
  47.     hist="${file_note##*/}!bash -c '_abrir \"$file_note\"'"
  48.     echo -e "$hist\n`grep -v "$hist" $notes_hist`" | head -5 > $tmpfile
  49.     mv $tmpfile $notes_hist
  50.    
  51.     _menu_atualizar
  52. }
  53. export -f _abrir
  54.  
  55.  
  56. function _menu_criar() {
  57.     file_name=$(yad --image=document-new --title="Nova anotação" --text="Digite o nome da anotação" --entry --editable)
  58.     [[ -z "$file_name" ]] && return
  59.    
  60.     note_new="$dir_notes/$file_name.note"
  61.     # Verifica se existe algum arquivo com o mesmo nome digitado e se sim, pergunta se deseja abri-lo
  62.     if [[ -e "$note_new" ]]; then
  63.         yad --image=dialog-question --title="Confirmação" \
  64.             --text="Existe esse arquivo. Deseja abri-lo?" \
  65.             --button="gtk-yes:0" --button="gtk-no:1"
  66.         [[ $? -eq 1 ]] && return
  67.     else
  68.         touch $note_new
  69.     fi
  70.     _abrir "$note_new"
  71. }
  72. export -f _menu_criar
  73.  
  74.  
  75. function _menu_abrir() {
  76.  
  77.     file_name=$(yad --file --title="Abrir anotação" --image=document-open --geometry=700x500 --filename="$dir_notes/" --file-filter="Notes files|*.note")
  78.     [[ -z "$file_name" ]] && return
  79.    
  80.     _abrir "$file_name"
  81.    
  82. }
  83. export -f _menu_abrir
  84.  
  85. function _menu_atualizar() {
  86.     exec 3<> $temp_pipe
  87.  
  88.     menu="menu:Novo!bash -c '_menu_criar'|Abrir!bash -c '_menu_abrir'||";
  89.     menu="$menu`egrep -v '^#' $notes_hist | tr '\n' '|'`"
  90.     menu="$menu|Fechar!quit"
  91.     echo $menu >&3
  92.    
  93. }
  94. export -f _menu_atualizar
  95.  
  96.  
  97. _menu_atualizar
  98. yad --notification --image='tag-new' --text="Secret notes" --listen <&3 2>- &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement