Compare commits
	
		
			2 Commits
		
	
	
		
			5e5e0ee732
			...
			694b4c2cdb
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					694b4c2cdb | ||
| 
						 | 
					d08834e39e | 
							
								
								
									
										153
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										153
									
								
								main.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")
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("\n\n0: List menu again\n")
 | 
			
		||||
	fmt.Printf("4: Quit\n\n> ")
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Todos struct {
 | 
			
		||||
	Items []Todo
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 (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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ListTodosSmall() {
 | 
			
		||||
	fmt.Println("ListTodosSmall func. not implemented")
 | 
			
		||||
	//List all the contents
 | 
			
		||||
}
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println("TODO APP")
 | 
			
		||||
 | 
			
		||||
	fmt.Print("\033[H\033[2J") //clear
 | 
			
		||||
	PrintMenu()
 | 
			
		||||
	list := Todos{}
 | 
			
		||||
	//todos := []Todo{}
 | 
			
		||||
	//list := Todos{todos}
 | 
			
		||||
	fmt.Println("Empty list: ")
 | 
			
		||||
	list.ListTodosFull()
 | 
			
		||||
	for {
 | 
			
		||||
		var input int
 | 
			
		||||
		n, err := fmt.Scanln(&input)
 | 
			
		||||
		if n < 1 || err != nil {
 | 
			
		||||
			fmt.Print("[ERR] Invalid input\n")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	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",
 | 
			
		||||
		},
 | 
			
		||||
		switch input {
 | 
			
		||||
		case 0:
 | 
			
		||||
			PrintMenu()
 | 
			
		||||
		case 1:
 | 
			
		||||
			list.ListTodo()
 | 
			
		||||
			PrintMenu()
 | 
			
		||||
		case 2:
 | 
			
		||||
			in := bufio.NewReader(os.Stdin)
 | 
			
		||||
 | 
			
		||||
			fmt.Print("Add Title:\n> ")
 | 
			
		||||
			title, _ := in.ReadString('\n')
 | 
			
		||||
 | 
			
		||||
			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> ")
 | 
			
		||||
 | 
			
		||||
			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()
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//Add the items for testing
 | 
			
		||||
	for item := range items {
 | 
			
		||||
		list.AddTodo(items[item])
 | 
			
		||||
	}
 | 
			
		||||
	/*
 | 
			
		||||
		list := Todos{}
 | 
			
		||||
		//todos := []Todo{}
 | 
			
		||||
		//list := Todos{todos}
 | 
			
		||||
		fmt.Println("Empty list: ")
 | 
			
		||||
		list.ListTodo()
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Listing...")
 | 
			
		||||
	list.ListTodosFull()
 | 
			
		||||
	fmt.Println("Listing again...")
 | 
			
		||||
	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.Println("Removing item2 via index 1")
 | 
			
		||||
	list.RemoveTodo(1)
 | 
			
		||||
	fmt.Println("Listing again...")
 | 
			
		||||
	list.ListTodosFull()
 | 
			
		||||
		//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.ListTodo()
 | 
			
		||||
	*/
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								todo.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								todo.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user