In this assessment, you will create a basic terminal shell-style program allowing users to manage (virtual) files and folders.
Paetae/A: Creating a computer program involves:
Kaiaka/M: Creating a well-structured computer program involves:
Kairangi/E: Creating a flexible and robust computer program involves:
// This code is based on a sample found on https://stackoverflow.com/questions/58051099/how-do-i-format-a-string-from-a-string-with-in-swift
let greeting: String = String(format: "Hello! Your name is ‘%@’", name)
A terminal emulator is a computer program that simulates a text-based computer terminal.
In the early days of networked computing, computers were used by connecting to them with a teleprinter; you typed in text-based commands and their output would be printed on paper.
In the modern day, terminal emulators allow you to send commands to a computer without the need for a teleprinter.
Modern terminal emulators include Windows Terminal on Windows 11, Terminal and iTerm 2 on macOS, and so on. Visual Studio Code includes a built-in terminal emulator.
Terminals typically allow you to use a shell, a program that runs other programs. Shell programs let you type in text-based commands followed by arguments — this is similar to calling functions in a programming language.
A shell is the software that interprets the commands sent by a terminal emulator/teleprinter. In general, shells understand a series of built-in commands or defer to programs stored on the computer.
Examples of shells include PowerShell used on Windows, zsh
used on macOS, and bash
used on most Linux distributions.
In our program, all of the commands will be built-in. You will create the commands to run in the shell.
Make sure that you have completed the file system activity from Trees and Nodes. You should have:
FilesystemNode
protocol, describing what a filesystem node contains (i.e. a name, an optional parent)FilesystemNode
so that it conforms to CustomStringConvertible
Folder
struct conforming to FilesystemNode
, which additionally contains child nodes (folders
) and can add items to itself (add(_:)
)File
struct conforming to FilesystemNode
Folder
and File
objectsIf you don't have this structure, you are free to create your own structure to complete the task (i.e. using arrays and/or dictionaries). You will need to add the savejson
and/or loadjson
command to compensate for the absence of a complex data structure or object-oriented programming.
ls
to show the names and sizes of folders and files in the current folder
touch <filename> <filesize>
to create a new file that is 1 KB by default, or an optionally specified size
touch myfile.txt
(creates a 1 KB file)touch backup.dat 4096
(creates a 4096 KB file)mkdir <foldername>
to create a new folder in the current folder
mkdir myfolder
mkdir /home/myname/myfolder
(explicit full path)rm <filename>
to delete a file in the current folder. If no such file exists, tell the user
rm
can only delete files, not foldersrm myfile.txt
rm /home/myname/myfile.txt
(explicit full path)rmdir <foldername>
to delete a folder in the current folder. If no such folder exists, tell the user
rmdir
also deletes all the files and subfolders contained within the specified folderrmdir myfolder
rmdir /home/myname/myfolder
(explicit full path)cd <foldername>
to change to a different folder
..
move to the parent foldercd myfolder
cd ..
cd /home/myname/myfolder
(explicit full path)savejson
to save the entire virtual hard drive's folder structure as a JSON file, ORloadjson
to load the virtual hard drive from a JSON fileThere are also the following requirements to ensure the program is robust:
Your program starts with the following directories by default:
home
— the root folder
documents
- contains two (2) files (cv.pdf
, data.dat
)downloads
- contains no filesmusic
- contains ten (10) files (1.mp3
through to 10.mp3
)photos
- contains a folder and two (2) files (passport.jpg
, photoid.png
)
japan2026
- contains five (3) files (tokyo.png
, kyoto.jpg
, miyajima.gif
)File and folder names must contain at least one (1) character, up to a maximum of twelve (12)
File names must have a dot (.
) followed by a three (3) character file extension
File and folder names must only contain lower-case alphanumeric characters (no punctuation or spaces other than the dot for the extension)
You may not:
savejson
/loadjson
command, if you choose to include it/them/home/myname> ls
documents/ (dir - 2 KB)
downloads/ (dir - 0 KB)
music/ (dir - 10 KB)
photos/ (dir - 7 KB)
TOTAL: 19 KB
/home/myname> cd photos
/home/myname/photos> ls
japan2026/ (dir - 3 KB)
passport.jpg 1 KB
photoid.png 1 KB
TOTAL: 5 KB
/home/myname/photos> rm passport.jpg
/home/myname/photos> touch japan2026/banana.jpg
/home/myname/photos> touch japan2026/apple.jpg
/home/myname/photos> ls
japan2026/ (dir - 5 KB)
photoid.png 1 KB
TOTAL: 6 KB
/home/myname/photos> cd ..
/home/myname> rmdir photos
/home/myname> ls
documents/ (dir - 2 KB)
downloads/ (dir - 0 KB)
music/ (dir - 10 KB)
TOTAL: 12 KB
/home/myname> rm documents
documents is a folder
/home/myname> rmdir banana
Bad command or filename
/home/myname> purplemonkeydishwasher
Bad command or filename