aboutsummarylogtreecommitdiffstats
path: root/writedoc
blob: 659db2fda0e8c9410dd7bed75662af00492a674b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh
# A fast way of creating and editing notes using neovim without defining a file path

# Function to display help message
usage() {
    cat <<EOF
Usage: writedoc [options] FILENAME

  OPTIONS:
  -c          Change the default values of directory and/or file type
              use with either -d or -t to change the defaults
              FILENAME is not required and will be ignored
  -p          Use another folder in the default directory
              e.g. writedoc -p ExampleFolder ExampleFile =
              ~/Default/ExampleFolder/ExampleFile.md
  
  -r          Reset all values back to the default setting

  -d          Directory where the file will be created (default: ./Documents)
  
  -t          File type of the file (default: .md)
  
  FILENAME    Name of the markdown file (without extension)
EOF
    exit 1
}

CHANGE=0
PFLAG=0

# Get directory and file type
DATA_DIR="/usr/share/writedoc"


dirInput="$DATA_DIR/directory.txt"

while IFS= read -r line
do
    DIRECTORY="$line"
done < "$dirInput"

ftInput="$DATA_DIR/ft.txt"
while IFS= read -r line
do
    FILETYPE="$line"
done < "$ftInput"

# Parse flags with getopts
while getopts ":cp:rd:t:" OPTION; do
    case "$OPTION" in
        c) # Change defaults flag
            CHANGE=1
            ;;
        p) # + Folder
            PFLAG=1
            DIRECTORY="$DIRECTORY$OPTARG"
            ;;
        r) # Reset defaults
            echo "./Documents/" > "$dirInput" 
            echo ".md" > "ftInput"
            exit 0
            ;;
        d) # Directory
            DIRECTORY="$OPTARG"
            if [ $CHANGE -eq 1 ]; then
                echo "$DIRECTORY" > "$dirInput"
                exit 0
            fi
            ;;
        t) # File type
            FILETYPE="$OPTARG"
            if [ $CHANGE -eq 1 ]; then
                echo "$FILETYPE" > "$ftInput"
                exit 0
            fi
            ;;
        :)  # Handle missing arguments
            echo "Error: Option -$OPTARG requires an argument." >&2
            usage
            ;;
        \?) # Handle invalid options
            echo "Error: Invalid option -$OPTARG" >&2
            usage
            ;;
    esac
done

# Shift the processed options so that $1 is now the FILENAME
shift $((OPTIND - 1))

# Check if FILENAME is provided
if [ $# -eq 0 ]; then
    echo "Error: FILENAME is required." >&2
    usage
fi

FILENAME="$1"

# Make sure the directory exists - create if not
if [ ! -d "$DIRECTORY" ]; then
    echo "Directory does not exist. Creating it now..."
    mkdir -p "$DIRECTORY"
fi

# Ensure DIRECTORY ends with a slash
DIRECTORY="${DIRECTORY%/}/"

# Construct the file path
FILEPATH="$DIRECTORY$FILENAME$FILETYPE"

# Create the markdown file with a simple title as content
echo "Creating file at: $FILEPATH"
echo "# $FILENAME" > "$FILEPATH"

# Open the file in Neovim
echo "Opening file with Neovim..."
nvim "$FILEPATH"