Skip to content

Commit

Permalink
Merge pull request #128 from cs50/develop
Browse files Browse the repository at this point in the history
Tidies `code` wrapper
  • Loading branch information
dmalan authored Nov 25, 2023
2 parents d64268f + 39c9cd2 commit b73dd04
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions opt/cs50/bin/code
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# Formatting
bold=$(tput bold)
normal=$(tput sgr0)

# Check whether run with any options
options=false
for arg in "$@"; do
Expand All @@ -14,7 +18,7 @@ if [ "$options" = false ]; then

# If no arguments
if [ $# -eq 0 ]; then
read -p "Are you sure you want to run $(tput bold)code$(tput sgr0) without a filename? It would open a new tab. [y/N] " -r
read -p "Are you sure you want to run ${bold}code${normal} without a filename? It would open a new tab. [y/N] " -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
Expand All @@ -28,7 +32,7 @@ if [ "$options" = false ]; then

# If a directory
if [ -d "$arg" ]; then
read -p "Are you sure you want to open $(tput bold)$arg$(tput sgr0)? It's a folder, not a file, so it would open in a new tab. [y/N] " -r
read -p "Are you sure you want to open ${bold}${arg}${normal}? It's a folder, not a file, so it would open in a new tab. [y/N] " -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
Expand All @@ -39,31 +43,31 @@ if [ "$options" = false ]; then

# If first letter is capitalized but one of these languages
if [[ "$arg" =~ ^[A-Z].*\.(c|css|html|js|py)$ ]]; then
read -p "Are you sure you want to create $(tput bold)$arg$(tput sgr0)? Filenames aren't usually capitalized. [y/N] " -r
read -p "Are you sure you want to create ${bold}${arg}${normal}? Filenames aren't usually capitalized. [y/N] " -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
fi

# If first letter is lowercase but Java
if [[ "$arg" =~ ^[a-z].*\.java$ ]]; then
read -p "Are you sure you want to create $(tput bold)$arg$(tput sgr0)? Filenames are usually capitalized. [y/N] " -r
read -p "Are you sure you want to create ${bold}${arg}${normal}? Filenames are usually capitalized. [y/N] " -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
fi

# If file doesn't have an extension
if [[ ! "$arg" =~ \.[^.]+$ ]]; then
read -p "Are you sure you want to create $(tput bold)$arg$(tput sgr0)? Filenames usually have extensions. [y/N] " -r
read -p "Are you sure you want to create ${bold}${arg}${normal}? Filenames usually have extensions. [y/N] " -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
fi

# If file extension is capitalized
if [[ "$arg" =~ \.[A-Z]+$ ]]; then
read -p "Are you sure you want to create $(tput bold)$arg$(tput sgr0)? File extensions aren't usually capitalized. [y/N] " -r
read -p "Are you sure you want to create ${bold}${arg}${normal}? File extensions aren't usually capitalized. [y/N] " -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
Expand Down

0 comments on commit b73dd04

Please sign in to comment.