1- import OS ._OS_NAME_
2- import com .colofabrix .scala .figlet4s .unsafe .{FIGureOps , Figlet4s , OptionsBuilderOps }
1+ // import Api.nextID
2+
3+ import Api ._
4+ import DB .{commit , inMemory }
5+ import commons ._
36
4- import java .time .LocalDateTime
57import scala .annotation .tailrec
68import scala .io .StdIn ._
79import scala .language .postfixOps
8- import scala .sys .process ._
9- import scala .util .{Failure , Random , Success , Try }
10+ import scala .util .{Failure , Success , Try }
1011
1112object Main extends App {
1213
13- case class TodoItem (id : Int , name : String , desc : String , dueBy : Option [LocalDateTime ], remind : Boolean )
14-
15- val asciiStyle = fansi.Bold .On ++ Random .shuffle(fansi.Color .all).head
16- val captionStyle = fansi.Bold .On ++ Random .shuffle(fansi.Color .all).head
17- val helpStyle = fansi.Underlined .On ++ fansi.Bold .On ++ Random .shuffle(fansi.Color .all).head
18- val promptText = " > "
19- val listHeading =
20- " \t ID | NAME | DESCRIPTION | DUE BY | REMINDER SET" +
21- " \n\t ------------------------------------------------------------"
22-
23- val helpText =
24- """ add: Add a new todo list item
25- | list: Lists all available todo list items
26- | view [x]: View todo list item with id x
27- | edit [x]: Edit todo list item with id x
28- | del [x]: Delete todo list item with id x
29- | help: Prints this message
30- """ stripMargin
31-
32- def todoList : List [TodoItem ] =
33- List (
34- TodoItem (1 , " Dishes" , " Remember to wash the dishes" , None , remind = false ),
35- TodoItem (2 , " Sweep" , " Sweep the living room and kitchen" , None , remind = false ),
36- TodoItem (3 , " Wash" , " Wash all the dirty clothes available" , None , remind = true ),
37- TodoItem (4 , " Homework" , " Do PHY101 homework with the internet" , Some (LocalDateTime .now()), remind = true ),
38- TodoItem (5 , " Submit" , " Submit PHY101 homework" , Some (LocalDateTime .now()), remind = true )
39- )
40-
41- def parse (string : String , limit : Int = 10 ): String =
42- s " $string... ${" " * ((limit - 3 ) - string.length)}"
43-
44- def listTodoItem (x : TodoItem ): Unit =
45- println(s " \t ${x.id} | ${parse(x.name.take(7 ))} | ${parse(x.desc.take(10 ))} | ${parse(x.dueBy.getOrElse(" N/A" ).toString.take(5 ))} | ${if (x.remind) " Yes" else " No" }" )
46-
47- def viewTodoItem (x : TodoItem ): Unit =
48- println(
49- s """ ID: ${x.id}
50- | Name: ${x.name}
51- | Description: ${x.desc}
52- | Due By: ${x.dueBy.getOrElse(" N/A" )}
53- | Remind Me: ${if (x.remind) " Yes" else " No" } """ .stripMargin
54- )
55-
56- def cls (): Unit =
57- _OS_NAME_ match {
58- case OS .LINUX => " clear" .!
59- case OS .MAC_OS_X => " clear" .!
60- case OS .WINDOWS => " cmd.exe -c cls" .!
61- case OS .SUN_OS => " clear" .!
62- case OS .FREEBSD => " clear" .!
63- case OS .UNKNOWN =>
64- println(" Unrecognized OS\n Exiting..." )
65- System .exit(0 )
66- }
67-
68-
69- def handle (input : String ): Unit = {
14+ def run (input : String ): Unit = {
7015 cls()
7116
72- println(
73- asciiStyle(Figlet4s
74- .builder(" AList 1 . 0 !" )
75- .render
76- .asSeq
77- .mkString(" \n " ))
78- ++ captionStyle(" \n Get your life organized!" )
79- ++ helpStyle(" \n Type help to view options\n " )
80- )
17+ println(header)
8118
8219 input match {
8320 case h if h take 4 equalsIgnoreCase " help" => println(helpText)
8421
85- case v if v take 4 equalsIgnoreCase " add" => ???
22+ case v if v take 4 equalsIgnoreCase " add" => add()
8623
87- case x if x take 4 equalsIgnoreCase " list" =>
88- println(listHeading)
89- todoList foreach listTodoItem
24+ case x if x take 4 equalsIgnoreCase " list" => list()
9025
9126 case v if v take 4 equalsIgnoreCase " view" =>
9227 Try (
93- v.last.toString .toInt
28+ v.split( " " ).last .toInt
9429 ) match {
9530 case Failure (err) => println(s " View process failed with exception: ${err.getCause}" )
9631 case Success (id) =>
97- todoList filter (_.id == id) foreach viewTodoItem
32+ inMemory filter (_.id == id) match {
33+ case x if x.isEmpty =>
34+ println(" No todo list item matches id " + id)
35+ case x =>
36+ x foreach viewTodoItem
37+ }
9838 }
9939
100- case v if v take 4 equalsIgnoreCase " edit" => ???
40+ case v if v take 4 equalsIgnoreCase " edit" =>
41+ Try (
42+ v.split(" " ).last.toInt
43+ ) match {
44+ case Failure (err) => println(s " Edit process failed with exception: ${err.getCause}" )
45+ case Success (id) =>
46+ edit(id)
47+ }
10148
102- case v if v take 4 equalsIgnoreCase " del" => ???
49+ case v if v take 3 equalsIgnoreCase " drop" =>
50+ Try (
51+ v.split(" " ).last.toInt
52+ ) match {
53+ case Failure (err) => println(s " View process failed with exception: ${err.getCause}" )
54+ case Success (id) =>
55+ if (inMemory.isEmpty) {
56+ println(" No todo list item matches id " + id)
57+ } else {
58+ inMemory = inMemory.filterNot(_.id == id)
59+ }
60+ }
61+ list()
62+
63+ case q if q take 4 equalsIgnoreCase " quit" =>
64+ println(" Goodbye!" )
65+ System .exit(0 )
10366
10467 case _ => println(" Unrecognized Input!" )
10568
10669 }
70+ commit()
10771 }
10872
10973 @ tailrec
11074 def prompt (): Unit = {
11175 print(promptText)
11276
113- handle (readLine)
77+ run (readLine)
11478
11579 prompt()
11680 }
11781
11882 cls()
83+
11984 println(
120- asciiStyle(Figlet4s
121- .builder(" AList 1 . 0 !" )
122- .render
123- .asSeq
124- .mkString(" \n " ))
125- ++ captionStyle(" \n Get your life organized!" )
126- ++ helpStyle(" \n Type help to view options\n " )
85+ header
12786 )
12887 prompt()
12988}
0 commit comments