-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat [Golang] [Package] Interactivity (#27)
* Feat [Golang] [Package] Interactivity - [+] feat(interactivity): add interactivity package for handling interactive command-line user interfaces - [+] feat(interactivity): add ConfirmOverwrite function for checking if a file exists and prompting the user for confirmation to overwrite - [+] feat(interactivity): add promptForInput function for reading user input from bufio.Reader and handling cancellation with context * Feat & Fix [Golang] [Package] Real File System & File System Mock - [+] feat(file_system.go): add error handling to FileExists method in the FileSystem interface - [+] fix(file_system.go): change return type of FileExists method in the FileSystem interface to (bool, error) - [+] fix(file_system.go): handle file existence check in the FileExists method of RealFileSystem - [+] fix(file_system_mock.go): change return type of FileExists method in the MockFileSystem to (bool, error) - [+] fix(file_system_mock.go): handle file existence check in the FileExists method of MockFileSystem * Feat [Golang] [Package] Interactivity - [+] feat(interactivity.go): add determineFileName function to determine the file name based on the fileType * Fix & Feat [Golang] [Module] Main Command - [+] fix(main.go): change variable name from fs to rfs in processCSVOption, processDatasetOption, executeCSVConversion, createSeparateCSVFiles, convertToSingleCSV, and writeContentToFile functions - [+] feat(main.go): add import statement for interactivity package
- Loading branch information
1 parent
b096f88
commit 351654e
Showing
4 changed files
with
203 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Package interactivity provides functions to handle interactive command-line | ||
// user interfaces. It includes utilities for prompting the user with questions | ||
// and processing their responses. This package is specifically designed to work | ||
// with the filesystem provided by the github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/filesystem | ||
// package to confirm potential file overwrites and to collect user input in a | ||
// context-aware manner, allowing for graceful cancellation of input requests. | ||
package interactivity | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/filesystem" | ||
) | ||
|
||
// result is a helper struct used internally within the interactivity package | ||
// to encapsulate the user input along with any error that might have occurred | ||
// during the input reading process. It is used to communicate between | ||
// goroutines in the promptForInput function. | ||
type result struct { | ||
input string | ||
err error | ||
} | ||
|
||
// ConfirmOverwrite checks if a file with the given fileName exists in the provided filesystem. | ||
// If the file does exist, it prompts the user for confirmation to overwrite the file. | ||
// The function reads the user's input via the provided bufio.Reader and expects a 'yes' or 'no' response. | ||
// A context.Context is used to handle cancellation of the input request. | ||
// It returns a boolean indicating whether the file should be overwritten and any error encountered. | ||
func ConfirmOverwrite(rfs filesystem.FileSystem, ctx context.Context, reader *bufio.Reader, fileName string) (bool, error) { | ||
exists, err := rfs.FileExists(fileName) | ||
if err != nil { | ||
// Handle the error properly, perhaps by returning it. | ||
return false, err | ||
} | ||
if !exists { | ||
// If the file doesn't exist, no need to confirm overwrite. | ||
return true, nil | ||
} | ||
|
||
// If the file exists, ask the user for confirmation. | ||
fmt.Printf("File '%s' already exists. Overwrite? (yes/no): ", fileName) | ||
|
||
// Call promptForInput without the extra string argument. | ||
overwrite, err := promptForInput(ctx, reader) | ||
if err != nil { | ||
return false, err | ||
} | ||
return strings.ToLower(overwrite) == "yes", nil | ||
} | ||
|
||
// promptForInput waits for a line of user input read from the provided bufio.Reader. | ||
// It takes a context.Context to support cancellation. | ||
// The function trims the newline character from the input and returns the resulting string. | ||
// If the context is cancelled before the user inputs a line, the context's error is returned. | ||
func promptForInput(ctx context.Context, reader *bufio.Reader) (string, error) { | ||
resultChan := make(chan result) | ||
|
||
go func() { | ||
input, err := reader.ReadString('\n') | ||
resultChan <- result{input: input, err: err} | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return "", ctx.Err() | ||
case res := <-resultChan: | ||
return strings.TrimSpace(res.input), res.err | ||
} | ||
} | ||
|
||
// determineFileName should be a function that determines the file name based on the fileType or other logic. | ||
// Note: Currently, unimplemented. | ||
func determineFileName(fileType string) string { | ||
// Implement logic to determine the file name | ||
// For example, you might prompt the user for a file name or generate it based on the fileType | ||
return "" | ||
} |
Oops, something went wrong.