added saving to a file and cleaned up magic numbers
This commit is contained in:
		
							parent
							
								
									a66f30b663
								
							
						
					
					
						commit
						0b27242338
					
				
							
								
								
									
										7
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								go.mod
									
									
									
									
									
								
							| @ -1,3 +1,10 @@ | |||||||
| module todo-app | module todo-app | ||||||
| 
 | 
 | ||||||
| go 1.20 | go 1.20 | ||||||
|  | 
 | ||||||
|  | require ( | ||||||
|  | 	github.com/fatih/color v1.15.0 // indirect | ||||||
|  | 	github.com/mattn/go-colorable v0.1.13 // indirect | ||||||
|  | 	github.com/mattn/go-isatty v0.0.17 // indirect | ||||||
|  | 	golang.org/x/sys v0.6.0 // indirect | ||||||
|  | ) | ||||||
|  | |||||||
							
								
								
									
										44
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								main.go
									
									
									
									
									
								
							| @ -4,6 +4,7 @@ import ( | |||||||
| 	"bufio" | 	"bufio" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"strings" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func PrintMenu() { | func PrintMenu() { | ||||||
| @ -11,17 +12,26 @@ func PrintMenu() { | |||||||
| 	fmt.Printf("1: List Todo\n") | 	fmt.Printf("1: List Todo\n") | ||||||
| 	fmt.Printf("2: Add Todo\n") | 	fmt.Printf("2: Add Todo\n") | ||||||
| 	fmt.Printf("3: Remove Todo (via index)\n") | 	fmt.Printf("3: Remove Todo (via index)\n") | ||||||
|  | 	fmt.Printf("4: Save to file\n") | ||||||
| 
 | 
 | ||||||
| 	fmt.Printf("\n\n0: List menu again\n") | 	fmt.Printf("\n\n0: Quit\n> ") | ||||||
| 	fmt.Printf("4: Quit\n\n> ") |  | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | const ( | ||||||
|  | 	QUIT   = 0 | ||||||
|  | 	LIST   = 1 | ||||||
|  | 	ADD    = 2 | ||||||
|  | 	REMOVE = 3 | ||||||
|  | 	SAVE   = 4 | ||||||
|  | ) | ||||||
|  | 
 | ||||||
| func main() { | func main() { | ||||||
| 	fmt.Print("\033[H\033[2J") //clear | 	fmt.Print("\033[H\033[2J") //clear | ||||||
| 	PrintMenu() | 	PrintMenu() | ||||||
| 	list := Todos{} | 	list := Todos{} | ||||||
| 	for { | 	for { | ||||||
|  | 
 | ||||||
| 		var input int | 		var input int | ||||||
| 		n, err := fmt.Scanln(&input) | 		n, err := fmt.Scanln(&input) | ||||||
| 		if n < 1 || err != nil { | 		if n < 1 || err != nil { | ||||||
| @ -29,12 +39,15 @@ func main() { | |||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		switch input { | 		switch input { | ||||||
| 		case 0: | 		case QUIT: | ||||||
|  | 			fmt.Print("Quitting, goodbye!\n") | ||||||
|  | 			os.Exit(0) | ||||||
|  | 		case LIST: | ||||||
|  | 			if err := list.ListTodo(); err != nil { | ||||||
|  | 				fmt.Print(err) | ||||||
|  | 			} | ||||||
| 			PrintMenu() | 			PrintMenu() | ||||||
| 		case 1: | 		case ADD: | ||||||
| 			list.ListTodo() |  | ||||||
| 			PrintMenu() |  | ||||||
| 		case 2: |  | ||||||
| 			in := bufio.NewReader(os.Stdin) | 			in := bufio.NewReader(os.Stdin) | ||||||
| 
 | 
 | ||||||
| 			fmt.Print("Add Title:\n> ") | 			fmt.Print("Add Title:\n> ") | ||||||
| @ -46,7 +59,7 @@ func main() { | |||||||
| 			list.AddTodo(Todo{Title: title, Content: content}) | 			list.AddTodo(Todo{Title: title, Content: content}) | ||||||
| 			fmt.Printf("Added %s to the list!\n", title) | 			fmt.Printf("Added %s to the list!\n", title) | ||||||
| 			PrintMenu() | 			PrintMenu() | ||||||
| 		case 3: | 		case REMOVE: | ||||||
| 			var index int | 			var index int | ||||||
| 			fmt.Printf("Which TODO to remove?\n> ") | 			fmt.Printf("Which TODO to remove?\n> ") | ||||||
| 
 | 
 | ||||||
| @ -58,9 +71,18 @@ func main() { | |||||||
| 				fmt.Print(err) | 				fmt.Print(err) | ||||||
| 			} | 			} | ||||||
| 			PrintMenu() | 			PrintMenu() | ||||||
| 		case 4: | 		case SAVE: | ||||||
| 			fmt.Print("Quitting, goodbye!\n") | 			in := bufio.NewReader(os.Stdin) | ||||||
| 			os.Exit(0) | 			fmt.Print("Save to file:\n> ") | ||||||
|  | 			filename, _ := in.ReadString('\n') | ||||||
|  | 			filename = strings.TrimSuffix(filename, "\n") | ||||||
|  | 
 | ||||||
|  | 			if err := list.SaveToFile(filename); err != nil { | ||||||
|  | 				fmt.Print(err) | ||||||
|  | 			} else { | ||||||
|  | 				fmt.Printf("%s saved!\n", filename) | ||||||
|  | 			} | ||||||
|  | 			PrintMenu() | ||||||
| 		default: | 		default: | ||||||
| 			PrintMenu() | 			PrintMenu() | ||||||
| 		} | 		} | ||||||
|  | |||||||
							
								
								
									
										63
									
								
								todo.go
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								todo.go
									
									
									
									
									
								
							| @ -3,8 +3,19 @@ package main | |||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  | 
 | ||||||
|  | 	"github.com/fatih/color" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | func check(err error) { | ||||||
|  | 	if err != nil { | ||||||
|  | 		fmt.Fprintf(os.Stderr, "[ERR] %s", err) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| type Todo struct { | type Todo struct { | ||||||
| 	Title   string | 	Title   string | ||||||
| 	Content string | 	Content string | ||||||
| @ -32,12 +43,56 @@ func (todo *Todos) RemoveTodo(index int) (err error) { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (todo *Todos) ListTodo() int { | func (todo *Todos) ListTodo() (err error) { | ||||||
|  | 	// Create a custom print function for convenience | ||||||
|  | 	id := color.New(color.Bold, color.FgGreen).PrintfFunc() | ||||||
|  | 	title := color.New(color.Bold, color.FgBlue).PrintfFunc() | ||||||
|  | 	content := color.New(color.Bold, color.FgWhite).PrintfFunc() | ||||||
|  | 
 | ||||||
| 	if len(todo.Items) == 0 { | 	if len(todo.Items) == 0 { | ||||||
| 		return 0 | 		return errors.New("[ERR] Todo list is empty!\n") | ||||||
| 	} | 	} | ||||||
| 	for k, v := range todo.Items { | 	for k, v := range todo.Items { | ||||||
| 		fmt.Printf("%d:%s\n", k, v) | 		id("[%d]", k) | ||||||
|  | 		title("%s", v.Title) | ||||||
|  | 		content("%s\n", v.Content) | ||||||
| 	} | 	} | ||||||
| 	return 1 | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (todo *Todos) SaveToFile(filename string) error { | ||||||
|  | 	var sb strings.Builder | ||||||
|  | 	var FILE *os.File | ||||||
|  | 
 | ||||||
|  | 	filename = filename + ".txt" | ||||||
|  | 	for k, v := range todo.Items { | ||||||
|  | 		fmt.Printf("#%d,%s,%s;\n", k, strings.ReplaceAll(v.Title, "\n", ""), strings.ReplaceAll(v.Content, "\n", "")) | ||||||
|  | 		sb.WriteString(strconv.Itoa(k)) | ||||||
|  | 		sb.WriteString(",") | ||||||
|  | 		sb.WriteString(strings.ReplaceAll(v.Title, "\n", "")) | ||||||
|  | 		sb.WriteString(",") | ||||||
|  | 		sb.WriteString(strings.ReplaceAll(v.Content, "\n", "")) | ||||||
|  | 		sb.WriteString(";\n") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	content := sb.String() | ||||||
|  | 
 | ||||||
|  | 	fmt.Println(content) | ||||||
|  | 
 | ||||||
|  | 	if _, err := os.Stat(filename); err != nil { | ||||||
|  | 		if FILE, err = os.Create(filename); err != nil { | ||||||
|  | 			return errors.New("[ERR] could not create file \n") | ||||||
|  | 		} | ||||||
|  | 		return errors.New("[ERR] file does not exist, creating one...\n") | ||||||
|  | 	} | ||||||
|  | 	FILE, _ = os.Create(filename) //errors already handled | ||||||
|  | 
 | ||||||
|  | 	if _, err := FILE.WriteString(content); err != nil { | ||||||
|  | 		return err | ||||||
|  | 		//return errors.New("[ERR] could not write to file\n") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	sb.Reset()   //reset the string builder | ||||||
|  | 	FILE.Close() //close the file | ||||||
|  | 	return nil | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user