Skip to content

Commit

Permalink
support multi files opening
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Dec 2, 2015
1 parent 44a4f40 commit 6b9a287
Showing 1 changed file with 76 additions and 72 deletions.
148 changes: 76 additions & 72 deletions rmate
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ port="${RMATE_PORT:-$port}"


# misc initialization
filepath=""
selection=""
displayname=""
filetype=""
filepaths=""
verbose=false
nowait=true
force=false
Expand Down Expand Up @@ -202,57 +199,101 @@ if [[ "$host" = "auto" && "$SSH_CONNECTION" != "" ]]; then
host=${SSH_CONNECTION%% *}
fi

filepath="$1"
filepaths=("$@")

if [ "$filepath" = "" ]; then
if [ "$filepaths" = "" ]; then
if [[ $nowait = false ]]; then
filepath='-'
filepaths='-'
else
case "$-" in
*i*)
showusage
exit 1
;;
*)
filepath='-'
filepaths='-'
;;
esac
fi
fi

shift
if [ ! "${#@}" -eq 0 ]; then
echo "There is more than one file specified. Opening only $filepath and ignoring other."
fi

if [ "$filepath" != "-" ]; then
realpath=`canonicalize "$filepath"`
log $realpath
#------------------------------------------------------------
# main
#------------------------------------------------------------

function open_file {

if [ -d "$filepath" ]; then
echo "$filepath is a directory and rmate is unable to handle directories."
exit 1
fi
filepath="$1"

if [ "$filepath" != "-" ]; then
realpath=`canonicalize "$filepath"`
log $realpath

if [ -f "$realpath" ] && [ ! -w "$realpath" ]; then
if [[ $force = false ]]; then
echo "File $filepath is not writable! Use -f to open anyway."
if [ -d "$filepath" ]; then
echo "$filepath is a directory and rmate is unable to handle directories."
exit 1
elif [[ $verbose = true ]]; then
log "File $filepath is not writable! Opening anyway."
fi

if [ -f "$realpath" ] && [ ! -w "$realpath" ]; then
if [[ $force = false ]]; then
echo "File $filepath is not writable! Use -f to open anyway."
exit 1
elif [[ $verbose = true ]]; then
log "File $filepath is not writable! Opening anyway."
fi
fi

if [ "$displayname" = "" ]; then
displayname="$hostname:$filepath"
fi
else
displayname="$hostname:untitled"
fi

if [ "$displayname" = "" ]; then
displayname="$hostname:$filepath"
echo "open" 1>&3
echo "display-name: $displayname" 1>&3
echo "real-path: $realpath" 1>&3
echo "data-on-save: yes" 1>&3
echo "re-activate: yes" 1>&3
echo "token: $filepath" 1>&3

if [[ $new = true ]]; then
echo "new: yes" 1>&3
fi
else
displayname="$hostname:untitled"
fi

#------------------------------------------------------------
# main
#------------------------------------------------------------
if [ "$selection" != "" ]; then
echo "selection: $selection" 1>&3
fi

if [ "$filetype" != "" ]; then
echo "file-type: $filetype" 1>&3
fi

if [ "$filepath" != "-" ] && [ -f "$filepath" ]; then
filesize=`ls -lLn "$realpath" | awk '{print $5}'`
echo "data: $filesize" 1>&3
cat "$realpath" 1>&3
elif [ "$filepath" = "-" ]; then
if [ -t 0 ]; then
echo "Reading from stdin, press ^D to stop"
else
log "Reading from stdin"
fi

# preserve trailing newlines
data=`cat; echo x`
data=${data%x}
filesize=$(echo -ne "$data" | wc -c)
echo "data: $filesize" 1>&3
echo -n "$data" 1>&3
else
echo "data: 0" 1>&3
fi

echo 1>&3
}

function handle_connection {
local cmd
local name
Expand Down Expand Up @@ -330,47 +371,10 @@ read server_info 0<&3

log $server_info

echo "open" 1>&3
echo "display-name: $displayname" 1>&3
echo "real-path: $realpath" 1>&3
echo "data-on-save: yes" 1>&3
echo "re-activate: yes" 1>&3
echo "token: $filepath" 1>&3

if [[ $new = true ]]; then
echo "new: yes" 1>&3
fi

if [ "$selection" != "" ]; then
echo "selection: $selection" 1>&3
fi

if [ "$filetype" != "" ]; then
echo "file-type: $filetype" 1>&3
fi

if [ "$filepath" != "-" ] && [ -f "$filepath" ]; then
filesize=`ls -lLn "$realpath" | awk '{print $5}'`
echo "data: $filesize" 1>&3
cat "$realpath" 1>&3
elif [ "$filepath" = "-" ]; then
if [ -t 0 ]; then
echo "Reading from stdin, press ^D to stop"
else
log "Reading from stdin"
fi

# preserve trailing newlines
data=`cat; echo x`
data=${data%x}
filesize=$(echo -ne "$data" | wc -c)
echo "data: $filesize" 1>&3
echo -n "$data" 1>&3
else
echo "data: 0" 1>&3
fi
for file in "${filepaths[@]}"; do
open_file "$file"
done

echo 1>&3
echo "." 1>&3

if [[ $nowait = true ]]; then
Expand Down

0 comments on commit 6b9a287

Please sign in to comment.