Skip to content

Commit

Permalink
[REFACTOR] fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
cerebrux committed Sep 20, 2020
1 parent 785e638 commit 5f5cbf7
Showing 1 changed file with 142 additions and 152 deletions.
294 changes: 142 additions & 152 deletions shelldio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ welcome_screen() {
}

option_detail() {
cat << EOF
cat <<EOF
Το shelldio έχει τις παρακάτω επιλογές
Expand All @@ -65,38 +65,36 @@ cat << EOF
EOF
}


# Δημιουργεί και εμφανίζει σε λίστα τους σταθμούς στο txt file που δέχεται σαν flag
list_stations(){
while IFS='' read -r line || [[ -n "$line" ]]; do
num=$(( num + 1 ))
echo ["$num"] "$line" | cut -d "," -f1
done < "$1"
list_stations() {
while IFS='' read -r line || [[ -n "$line" ]]; do
num=$((num + 1))
echo ["$num"] "$line" | cut -d "," -f1
done <"$1"
}

# Πληροφορίες που εμφανίζονται μετά την επιλογή του σταθμού
info() {
welcome_screen
echo -ne "| Η ώρα είναι $(date +"%T")\n| Ακούτε $stathmos_name\n| Πατήστε Q/q για έξοδο ή R/r για επιστροφή στη λίστα σταθμών"
welcome_screen
echo -ne "| Η ώρα είναι $(date +"%T")\n| Ακούτε $stathmos_name\n| Πατήστε Q/q για έξοδο ή R/r για επιστροφή στη λίστα σταθμών"
}

add_stations() {
echo "Εμφάνιση λίστας σταθμών"
sleep 2
list_stations "$all_stations"
while true
do
read -rp "Επέλεξε αριθμού σταθμού (Q/q για έξοδο): " input_station
while true; do
read -rp "Επέλεξε αριθμού σταθμού (Q/q για έξοδο): " input_station
if [[ $input_station = "q" ]] || [[ $input_station = "Q" ]]; then
echo "Έξοδος..."
exit 0
elif [ "$input_station" -gt 0 ] && [ "$input_station" -le $num ]; then #έλεγχος αν το input είναι μέσα στο εύρος της λίστας των σταθμών
station=$(sed "${input_station}q;d" "$stations")
stathmos_name=$(echo "$station" | cut -d "," -f1)
stathmos_url=$(echo "$station" | cut -d "," -f2)
stathmos_name=$(echo "$station" | cut -d "," -f1)
stathmos_url=$(echo "$station" | cut -d "," -f2)
#stathmos_name=$(< "$all_stations" head -n$(( "$input_station" )) | tail -n1 | cut -d "," -f1)
#stathmos_url=$(< "$all_stations" head -n$(( "$input_station" )) | tail -n1 | cut -d "," -f2)
echo "$stathmos_name,$stathmos_url" >> "$my_stations"
echo "$stathmos_name,$stathmos_url" >>"$my_stations"
echo " Προστέθηκε ο σταθμός $stathmos_name."
else
echo "Αριθμός εκτός λίστας"
Expand All @@ -106,195 +104,187 @@ add_stations() {

}

remove_station(){
remove_station() {
if [ ! -f "$HOME/.shelldio/my_stations.txt" ]; then
echo "Δεν έχει δημιουργηθεί το αρχείο my_stations."
echo "Για πληροφορίες τρέξε την παράμετρο --help."
else
echo "Εμφάνιση λίστας προσωπικών σταθμών"
sleep 2
list_stations "$my_stations"
while true
do
read -rp "Επέλεξε αριθμού σταθμού (Q/q για έξοδο): " remove_station
if [[ $remove_station = "q" ]] || [[ $remove_station = "Q" ]]; then
echo "Έξοδος..."
exit 0
elif [ "$remove_station" -gt 0 ] && [ "$remove_station" -le $num ]; then #έλεγχος αν το input είναι μέσα στο εύρος της λίστας των σταθμών
stathmos_name=$(< "$HOME/.shelldio/my_stations.txt" head -n$(( "$remove_station" )) | tail -n1 | cut -d "," -f1)
stathmos_url=$(< "$HOME/.shelldio/my_stations.txt" head -n$(( "$remove_station" )) | tail -n1 | cut -d "," -f2)
sed -i "$remove_station""d" "$HOME/.shelldio/my_stations.txt"
echo "Διαγράφηκε ο σταθμός $stathmos_name."
else
echo "Αριθμός εκτός λίστας"
fi
while true; do
read -rp "Επέλεξε αριθμού σταθμού (Q/q για έξοδο): " remove_station
if [[ $remove_station = "q" ]] || [[ $remove_station = "Q" ]]; then
echo "Έξοδος..."
exit 0
elif [ "$remove_station" -gt 0 ] && [ "$remove_station" -le $num ]; then #έλεγχος αν το input είναι μέσα στο εύρος της λίστας των σταθμών
stathmos_name=$(head <"$HOME/.shelldio/my_stations.txt" -n$(("$remove_station")) | tail -n1 | cut -d "," -f1)
stathmos_url=$(head <"$HOME/.shelldio/my_stations.txt" -n$(("$remove_station")) | tail -n1 | cut -d "," -f2)
sed -i "$remove_station""d" "$HOME/.shelldio/my_stations.txt"
echo "Διαγράφηκε ο σταθμός $stathmos_name."
else
echo "Αριθμός εκτός λίστας"
fi
done
fi
}

### Λίστα με τις επιλογές σαν 1ο όρισμα shelldio --[option]

while [ "$1" != "" ]; do
case $1 in
-h | --help )
option_detail
exit
;;
-l | --list )
welcome_screen
while true
do
if [ -f "$my_stations" ]; then
case $1 in
-h | --help)
option_detail
exit
;;
-l | --list)
welcome_screen
while true; do
if [ -f "$my_stations" ]; then
read -rp "Θέλετε να εμφανισθούν όλοι οι σταθμοί ή οι αγαπημένοι σας σταθμοί; (a=Όλοι οι σταθμοί | f=Αγαπημένοι):" list_choice
if [ "$list_choice" == "a" ]; then
echo "Εμφάνιση όλων των σταθμών:"
sleep 1
list_stations "$all_stations"
exit 0
elif [ "$list_choice" == "f" ]; then
elif [ "$list_choice" == "f" ]; then
echo "Εμφάνιση αγαπημένων σταθμών:"
sleep 1
list_stations "$my_stations"
sleep 1
list_stations "$my_stations"
exit 0
else
else
echo "Λάθος επιλογή, θα πρέπει να γράψετε a ή f και να πατήσετε enter"
fi
else
list_stations "$all_stations"
exit 0
fi
done
;;
-a | --add )
welcome_screen
add_stations
exit
;;
-r | --remove )
welcome_screen
remove_station
exit
;;
-f | --fresh )
welcome_screen
echo "Γίνεται λήψη του αρχείου των σταθμών από το αποθετήριο."
sleep 1
curl -sL https://raw.githubusercontent.com/CerebruxCode/shelldio/stable/.shelldio/all_stations.txt --output "$HOME/.shelldio/all_stations.txt"
exit
;;
done
;;
-a | --add)
welcome_screen
add_stations
exit
;;
-r | --remove)
welcome_screen
remove_station
exit
;;
-f | --fresh)
welcome_screen
echo "Γίνεται λήψη του αρχείου των σταθμών από το αποθετήριο."
sleep 1
curl -sL https://raw.githubusercontent.com/CerebruxCode/shelldio/stable/.shelldio/all_stations.txt --output "$HOME/.shelldio/all_stations.txt"
exit
;;
esac
done

### Base script

while true; do
terms=0
trap ' [ $terms = 1 ] || { terms=1; kill -TERM -$$; }; exit' EXIT INT HUP TERM QUIT

### Base script

while true
do
terms=0
trap ' [ $terms = 1 ] || { terms=1; kill -TERM -$$; }; exit' EXIT INT HUP TERM QUIT

# Έλεγχος αν υπάρχει ο mpv
if ! command -v mpv &> /dev/null ; then
echo "Δεν βρέθηκε συμβατός player. Συμβατός player είναι ο mpv"
exit
fi
# Έλεγχος αν υπάρχει ο mpv
if ! command -v mpv &>/dev/null; then
echo "Δεν βρέθηκε συμβατός player. Συμβατός player είναι ο mpv"
exit
fi

if [ "$#" -eq "0" ]; then #στην περίπτωση που δε δοθεί όρισμα παίρνει το προκαθορισμένο αρχείο
if [ -d "$HOME/.shelldio/" ]; then
if [ -f "$my_stations" ]; then
if [ -s "$my_stations" ]; then
stations="$my_stations"
if [ "$#" -eq "0" ]; then #στην περίπτωση που δε δοθεί όρισμα παίρνει το προκαθορισμένο αρχείο
if [ -d "$HOME/.shelldio/" ]; then
if [ -f "$my_stations" ]; then
if [ -s "$my_stations" ]; then
stations="$my_stations"
else
stations="$all_stations"
fi
else
if [ ! -f "$all_stations" ]; then
echo "Δεν ήταν δυνατή η εύρεση του αρχείου σταθμών. Γίνεται η λήψη του..."
sleep 2
curl -sL https://raw.githubusercontent.com/CerebruxCode/shelldio/stable/.shelldio/all_stations.txt --output "$HOME/.shelldio/all_stations.txt"
fi
stations="$all_stations"
fi
else
if [ ! -f "$all_stations" ]; then
echo "Δεν ήταν δυνατή η εύρεση του αρχείου σταθμών. Γίνεται η λήψη του..."
sleep 2
curl -sL https://raw.githubusercontent.com/CerebruxCode/shelldio/stable/.shelldio/all_stations.txt --output "$HOME/.shelldio/all_stations.txt"
fi
echo "Δημιουργείτε ο κρυφός φάκελος .shelldio ο οποίος θα περιέχει τα αρχεία των σταθμών."
sleep 2
mkdir -p "$HOME/.shelldio"
echo "Γίνεται η λήψη του αρχείου με όλους τους σταθμούς."
sleep 2
curl -sL https://raw.githubusercontent.com/CerebruxCode/shelldio/stable/.shelldio/all_stations.txt --output "$HOME/.shelldio/all_stations.txt"
stations="$all_stations"
fi
else
echo "Δημιουργείτε ο κρυφός φάκελος .shelldio ο οποίος θα περιέχει τα αρχεία των σταθμών."
sleep 2
mkdir -p "$HOME/.shelldio"
echo "Γίνεται η λήψη του αρχείου με όλους τους σταθμούς."
sleep 2
curl -sL https://raw.githubusercontent.com/CerebruxCode/shelldio/stable/.shelldio/all_stations.txt --output "$HOME/.shelldio/all_stations.txt"
stations="$all_stations"
else
stations="$1"
fi
else
stations="$1"
fi

while true
do
while true; do

welcome_screen
welcome_screen

num=0
num=0

list_stations "$stations"
list_stations "$stations"

if [ ! -f "$my_stations" ]; then
echo "Από προεπιλογή η λίστα σταθμών περιέχει όλους τους σταθμούς."
echo "Μπορείς να δημιουργήσεις ένα αρχείο με τους αγαπημένους σου σταθμούς."
echo "shelldio --help για να δεις πως μπορείς να το κάνεις!"
elif [ ! -s "$my_stations" ]; then
echo "Το αρχείο my_stations.txt υπάρχει αλλά είναι κενό."
echo "Θα φορτώσει η λίστα με όλους τους σταθμούς."
echo "Αν θέλεις να προσθέσεις αγαπημένους σταθμούς δοκίμασε την επιλογή add"
echo "shelldio --add"
fi
echo "---------------------------------------------------------"
read -rp "Διαλέξτε Σταθμό (ή Q/q για έξοδο): " input_play
if [ ! -f "$my_stations" ]; then
echo "Από προεπιλογή η λίστα σταθμών περιέχει όλους τους σταθμούς."
echo "Μπορείς να δημιουργήσεις ένα αρχείο με τους αγαπημένους σου σταθμούς."
echo "shelldio --help για να δεις πως μπορείς να το κάνεις!"
elif [ ! -s "$my_stations" ]; then
echo "Το αρχείο my_stations.txt υπάρχει αλλά είναι κενό."
echo "Θα φορτώσει η λίστα με όλους τους σταθμούς."
echo "Αν θέλεις να προσθέσεις αγαπημένους σταθμούς δοκίμασε την επιλογή add"
echo "shelldio --add"
fi
echo "---------------------------------------------------------"
read -rp "Διαλέξτε Σταθμό (ή Q/q για έξοδο): " input_play

if [[ $input_play = "q" ]] || [[ $input_play = "Q" ]]; then
echo "Έξοδος..."
exit 0
elif [ "$input_play" -gt 0 ] && [ "$input_play" -le $num ]; then #έλεγχος αν το input είναι μέσα στο εύρος της λίστας των σταθμών
stathmos_name=$(< "$stations" head -n$(( "$input_play" )) | tail -n1 | cut -d "," -f1)
stathmos_url=$(< "$stations" head -n$(( "$input_play" )) | tail -n1 | cut -d "," -f2)
break
else
echo "Αριθμός εκτός λίστας"
sleep 2
clear
fi
done
if [[ $input_play = "q" ]] || [[ $input_play = "Q" ]]; then
echo "Έξοδος..."
exit 0
elif [ "$input_play" -gt 0 ] && [ "$input_play" -le $num ]; then #έλεγχος αν το input είναι μέσα στο εύρος της λίστας των σταθμών
stathmos_name=$(head <"$stations" -n$(("$input_play")) | tail -n1 | cut -d "," -f1)
stathmos_url=$(head <"$stations" -n$(("$input_play")) | tail -n1 | cut -d "," -f2)
break
else
echo "Αριθμός εκτός λίστας"
sleep 2
clear
fi
done

mpv "$stathmos_url" &> /dev/null &
mpv "$stathmos_url" &>/dev/null &

while true
do
clear
info
sleep 0
read -r -n1 -t1 input_play # Για μικρότερη αναμονή της read
if [[ $input_play = "q" ]] || [[ $input_play = "Q" ]]; then
while true; do
clear
echo "Έξοδος..."
exit 0
elif [[ $input_play = "r" ]] || [[ $input_play = "R" ]]; then
for pid in $(pgrep '^mpv$')
do
url="$(ps -o command= -p "$pid" | awk '{print $2}')"
if [[ "$url" == "$stathmos_url" ]]; then
info
sleep 0
read -r -n1 -t1 input_play # Για μικρότερη αναμονή της read
if [[ $input_play = "q" ]] || [[ $input_play = "Q" ]]; then
clear
echo "Έξοδος..."
kill "$pid"
else
printf "Απέτυχε ο αυτόματος τερματισμός. \nΠάτα τον συνδυασμό Ctrl+C ή κλείσε το τερματικό \nή τερμάτισε το Shelldio απο τις διεργασίες του συστήματος"
exit 0
elif [[ $input_play = "r" ]] || [[ $input_play = "R" ]]; then
for pid in $(pgrep '^mpv$'); do
url="$(ps -o command= -p "$pid" | awk '{print $2}')"
if [[ "$url" == "$stathmos_url" ]]; then
echo "Έξοδος..."
kill "$pid"
else
printf "Απέτυχε ο αυτόματος τερματισμός. \nΠάτα τον συνδυασμό Ctrl+C ή κλείσε το τερματικό \nή τερμάτισε το Shelldio απο τις διεργασίες του συστήματος"
fi
done
clear
echo "Επιστροφή στη λίστα σταθμών"
sleep 2
clear
break
fi
done
clear
echo "Επιστροφή στη λίστα σταθμών"
sleep 2
clear
break
fi

done
done

done

0 comments on commit 5f5cbf7

Please sign in to comment.