Started picking up D recently. I wrote an application to create journal entries (text files) based on user input.
//this program will write to files. It should be able to read them.
//implement a command interface?
import std.stdio;
import std.string;
import std.datetime;
import fileio = std.file;
import std.conv;
enum Commands {exit = "EXIT", create = "CREATE"};
string menuText(){
return "\nType a command:\n\t- create\n\t- exit\n>";
}
void checkJournalEntriesFolder(){
string path = "journalEntries/";
if(fileio.exists(path) == 0){
writeln("Creating a journalEntries folder for you.");
fileio.mkdir(path);
}
}
string getFileName(){
return "entry_" ~ std.conv.to!string(stdTimeToUnixTime(Clock.currStdTime())) ~ ".txt";
}
int main(string[] argv)
{
writeln("\nWelcome to myJournal.");
checkJournalEntriesFolder();
string mode = "COMMAND";
char[] buf;
Commands command;
while (mode != "EXIT"){
while (mode == "COMMAND"){
string journalContent;
write(menuText);
stdin.readln(buf);
switch(toUpper(buf[0..$-1])) // cut off the /n at the end of the buffer.
{
case command.exit:
{
mode = "EXIT";
writeln("Oh, I'll exit.");
break;
}
case command.create:
{
writeln("create journal entry selected");
mode = "CREATE";
//create file
writeln("You are now typing in your journal.");
writeln("To exit this file, type #EOF");
writeln("----------------------------");
break;
}
default:
{
writeln("\nunrecognized command");
}
}
while (mode == "CREATE") {
stdin.readln(buf);
if(buf[0..$-1] == "#EOF"){
writeln("----------------------------");
mode = "COMMAND";
checkJournalEntriesFolder(); // double check, in case the user deleted it.
string filename = "journalEntries/" ~ getFileName;
fileio.write(filename, journalContent[0..$-1]); //Dont include the blank line.
writeln("File Saved.");
} else {
journalContent ~= buf;
}
}
}
}
return 0;
}
Possible Improvements:
- Maybe be able to move throughout the document once it is open (right now if you hit enter, you cannot move to the previous line).
- Make command / insert mode more like vi.
- Edit existing files.
I am very new to D, so I expect this code will be tore up. I've done some C++, but it has been a while.