Compare commits

...

2 Commits

Author SHA1 Message Date
simonkellet 694b4c2cdb more features! 9 months ago
simonkellet d08834e39e split the todo into a sep file 9 months ago
  1. 149
      main.go
  2. BIN
      todo
  3. 43
      todo.go

@ -1,78 +1,107 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"os"
) )
type Todo struct { func PrintMenu() {
Title string fmt.Printf("### TODO APP ###\n")
Content string fmt.Printf("1: List Todo\n")
} fmt.Printf("2: Add Todo\n")
fmt.Printf("3: Remove Todo (via index)\n")
type Todos struct { fmt.Printf("\n\n0: List menu again\n")
Items []Todo fmt.Printf("4: Quit\n\n> ")
}
func (todo *Todos) AddTodo(newTodo Todo) {
todo.Items = append(todo.Items, newTodo)
} }
func (todo *Todos) RemoveTodo(index int) { func main() {
//remove todo via index fmt.Print("\033[H\033[2J") //clear
todo.Items = append(todo.Items[:index], todo.Items[index+1:]...) PrintMenu()
} list := Todos{}
for {
var input int
n, err := fmt.Scanln(&input)
if n < 1 || err != nil {
fmt.Print("[ERR] Invalid input\n")
}
func (todo *Todos) ListTodosFull() { switch input {
//List all the contents case 0:
if len(todo.Items) == 0 { PrintMenu()
fmt.Printf("empty\n") case 1:
} list.ListTodo()
for k, v := range todo.Items { PrintMenu()
fmt.Printf("%d:%s\n", k, v) case 2:
} in := bufio.NewReader(os.Stdin)
}
func ListTodosSmall() { fmt.Print("Add Title:\n> ")
fmt.Println("ListTodosSmall func. not implemented") title, _ := in.ReadString('\n')
//List all the contents
}
func main() {
fmt.Println("TODO APP")
list := Todos{} fmt.Print("Add Content:\n> ")
//todos := []Todo{} content, _ := in.ReadString('\n')
//list := Todos{todos}
fmt.Println("Empty list: ") list.AddTodo(Todo{Title: title, Content: content})
list.ListTodosFull() fmt.Printf("Added %s to the list!\n", title)
PrintMenu()
fmt.Println("Adding entries...") case 3:
items := []Todo{ var index int
{ fmt.Printf("Which TODO to remove?\n> ")
Title: "test",
Content: "Here is some content",
},
{
Title: "test1",
Content: "Here is some more content",
},
{
Title: "test2",
Content: "Here is even MORE content",
},
}
//Add the items for testing if _, err := fmt.Scanf("%d", &index); err != nil {
for item := range items { fmt.Printf("Invalid input\n> ")
list.AddTodo(items[item]) }
if err := list.RemoveTodo(index); err != nil {
fmt.Print(err)
}
PrintMenu()
case 4:
fmt.Print("Quitting, goodbye!\n")
os.Exit(0)
default:
PrintMenu()
}
} }
fmt.Println("Listing...") /*
list.ListTodosFull() list := Todos{}
fmt.Println("Listing again...") //todos := []Todo{}
list.ListTodosFull() //list := Todos{todos}
fmt.Println("Empty list: ")
list.ListTodo()
fmt.Println("Adding entries...")
items := []Todo{
{
Title: "test",
Content: "Here is some content",
},
{
Title: "test1",
Content: "Here is some more content",
},
{
Title: "test2",
Content: "Here is even MORE content",
},
}
//Add the items for testing
for item := range items {
list.AddTodo(items[item])
}
fmt.Println("Listing...")
list.ListTodo()
fmt.Println("Listing again...")
list.ListTodo()
fmt.Println("Removing item2 via index 1") fmt.Println("Removing item2 via index 1")
list.RemoveTodo(1) list.RemoveTodo(1)
fmt.Println("Listing again...") fmt.Println("Listing again...")
list.ListTodosFull() list.ListTodo()
*/
} }

BIN
todo

Binary file not shown.

@ -0,0 +1,43 @@
package main
import (
"errors"
"fmt"
)
type Todo struct {
Title string
Content string
}
type Todos struct {
Items []Todo
}
func (todo *Todos) AddTodo(newTodo Todo) {
todo.Items = append(todo.Items, newTodo)
}
func (todo *Todos) RemoveTodo(index int) (err error) {
//remove todo via index
if len(todo.Items) == 0 {
return errors.New("[ERR] Todo list is empty!\n")
}
if index > len(todo.Items) {
return errors.New("[ERR] Index out of bounds")
}
todo.Items = append(todo.Items[:index], todo.Items[index+1:]...)
return nil
}
func (todo *Todos) ListTodo() int {
if len(todo.Items) == 0 {
return 0
}
for k, v := range todo.Items {
fmt.Printf("%d:%s\n", k, v)
}
return 1
}