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