From 480645080138e9b515206588856037e24ce9a787 Mon Sep 17 00:00:00 2001 From: yassinebenaid Date: Sun, 1 Oct 2023 10:06:41 +0100 Subject: [PATCH 1/3] rename alerts file and updated doc block --- alert.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.go | 10 ++----- 2 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 alert.go diff --git a/alert.go b/alert.go new file mode 100644 index 0000000..f1216f9 --- /dev/null +++ b/alert.go @@ -0,0 +1,91 @@ +package prompts + +import ( + "fmt" + "regexp" + + "github.com/charmbracelet/lipgloss" +) + +// Prints the message "m" using the "INFO" theme for styling. +// Perfect for short messages +func Info(m string) { + Alert("INFO", "4", m) +} + +// Prints the message "m" using the "ERROR" theme for styling. +// Perfect for short messages +func Error(m string) { + Alert("ERROR", "1", m) +} + +// Prints m styled by SUCCESS theme +func Success(m string) { + Alert("SUCCESS", "2", m) +} + +// Prints the message "m" using the "WARNING" theme for styling. +// Perfect for short messages +func Warning(m string) { + Alert("WARNING", "#fca311", m) +} + +// Prints the alert "m" labeled by "label" with the specified color +// +// example : +// +// prompts.Alert("WARNING", "#fca311", "hello world") +func Alert(label string, color string, m string) { + label = style(). + Padding(0, 1, 0, 1). + Margin(0, 2, 0, 0). + Background(lipgloss.Color(color)). + Render(label) + + fmt.Println(style(). + Margin(1, 0, 1, 2). + Render(label + m)) +} + +// Prints the message "m" using the "INFO" theme for styling. +// Perfect for long messages +func InfoMessage(m string) { + Message(m, "4") +} + +// Prints the message "m" using the "ERROR" theme for styling. +// Perfect for long messages +func ErrorMessage(m string) { + Message(m, "2") +} + +// Prints the message "m" using the "SUCCESS" theme for styling. +// Perfect for long messages +func SuccessMessage(m string) { + Message(m, "1") +} + +// Prints the message "m" using the "WARNING" theme for styling. +// Perfect for long messages +func WarningMessage(m string) { + Message(m, "#fca311") +} + +// Prints the message "m" labeled by "label" with the specified color +// +// example : +// +// prompts.Message("hello world", "#fca311") +func Message(m string, color string) { + fmt.Println(style(). + Margin(1, 0, 1, 2). + Padding(1, 0, 1, 2). + Width(getTrmW() - 2). + Background(lipgloss.Color(color)). + Render(m)) +} + +func charWidth(m string) int { + r := regexp.MustCompile(`\x1b\[([^m]+)m`) + return len(r.ReplaceAll([]byte(m), []byte(""))) +} diff --git a/utils.go b/utils.go index 957cfd3..2058ba1 100644 --- a/utils.go +++ b/utils.go @@ -1,11 +1,5 @@ -// prompts helps you build cli applications by abstracting all terminal intractions in an easy to use -// apis and let you focus on the core of your application where it takes care of accepting the inputs and returning the output -// -// in other words , prompts is the connection between your application and the terminal -// -// it provides several components to format the output and give it custom styles -// , a router to structure the commands tree, components for accepting user input and a logger -// that you can use to log errors in a nice styles +// prompts offers several configurable, ituitive and intractive CLI components +// including inputs, selectbox, radio box and alerts formatter, just to name a few package prompts import ( From 9e760be8f49152c05cb9dbe076e20dce93221ef9 Mon Sep 17 00:00:00 2001 From: yassinebenaid Date: Sun, 1 Oct 2023 10:06:55 +0100 Subject: [PATCH 2/3] delete unused files --- stylesheet.go | 91 --------------------------------------------------- 1 file changed, 91 deletions(-) delete mode 100644 stylesheet.go diff --git a/stylesheet.go b/stylesheet.go deleted file mode 100644 index f1216f9..0000000 --- a/stylesheet.go +++ /dev/null @@ -1,91 +0,0 @@ -package prompts - -import ( - "fmt" - "regexp" - - "github.com/charmbracelet/lipgloss" -) - -// Prints the message "m" using the "INFO" theme for styling. -// Perfect for short messages -func Info(m string) { - Alert("INFO", "4", m) -} - -// Prints the message "m" using the "ERROR" theme for styling. -// Perfect for short messages -func Error(m string) { - Alert("ERROR", "1", m) -} - -// Prints m styled by SUCCESS theme -func Success(m string) { - Alert("SUCCESS", "2", m) -} - -// Prints the message "m" using the "WARNING" theme for styling. -// Perfect for short messages -func Warning(m string) { - Alert("WARNING", "#fca311", m) -} - -// Prints the alert "m" labeled by "label" with the specified color -// -// example : -// -// prompts.Alert("WARNING", "#fca311", "hello world") -func Alert(label string, color string, m string) { - label = style(). - Padding(0, 1, 0, 1). - Margin(0, 2, 0, 0). - Background(lipgloss.Color(color)). - Render(label) - - fmt.Println(style(). - Margin(1, 0, 1, 2). - Render(label + m)) -} - -// Prints the message "m" using the "INFO" theme for styling. -// Perfect for long messages -func InfoMessage(m string) { - Message(m, "4") -} - -// Prints the message "m" using the "ERROR" theme for styling. -// Perfect for long messages -func ErrorMessage(m string) { - Message(m, "2") -} - -// Prints the message "m" using the "SUCCESS" theme for styling. -// Perfect for long messages -func SuccessMessage(m string) { - Message(m, "1") -} - -// Prints the message "m" using the "WARNING" theme for styling. -// Perfect for long messages -func WarningMessage(m string) { - Message(m, "#fca311") -} - -// Prints the message "m" labeled by "label" with the specified color -// -// example : -// -// prompts.Message("hello world", "#fca311") -func Message(m string, color string) { - fmt.Println(style(). - Margin(1, 0, 1, 2). - Padding(1, 0, 1, 2). - Width(getTrmW() - 2). - Background(lipgloss.Color(color)). - Render(m)) -} - -func charWidth(m string) int { - r := regexp.MustCompile(`\x1b\[([^m]+)m`) - return len(r.ReplaceAll([]byte(m), []byte(""))) -} From dac5378ac494ae137c3e2525dac066bf3a5eac21 Mon Sep 17 00:00:00 2001 From: yassinebenaid Date: Sun, 1 Oct 2023 10:08:09 +0100 Subject: [PATCH 3/3] added installation instruction --- README.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index dc339af..c4dac75 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,13 @@ Build command line prompts with ease, prompts provides several apis to help you create intuitive TUI faster, - ![Screenshot from 2023-09-30 19-38-45](https://github.com/yassinebenaid/prompts/assets/101285507/7ef5edb3-b13c-4e64-b03d-74f627165982) +![Screenshot from 2023-09-30 19-38-45](https://github.com/yassinebenaid/prompts/assets/101285507/7ef5edb3-b13c-4e64-b03d-74f627165982) +## Installation + +```bash +go get github.com/yassinebenaid/prompts +``` ## API @@ -73,9 +78,8 @@ fmt.Println("password : " + value) ``` - **Result**: - - ![image](https://github.com/yassinebenaid/prompts/assets/101285507/825f5688-9d55-4e2d-acf2-c5b308dfc4a5) +![image](https://github.com/yassinebenaid/prompts/assets/101285507/825f5688-9d55-4e2d-acf2-c5b308dfc4a5) # @@ -102,7 +106,6 @@ fmt.Println("answer : ", value) ![image](https://github.com/yassinebenaid/prompts/assets/101285507/8a6c51cb-847d-415a-b11e-96cbf0a2beeb) - # ### Radio Input @@ -128,12 +131,11 @@ fmt.Println("gender : ", genders[value]) ![image](https://github.com/yassinebenaid/prompts/assets/101285507/52e82922-844c-4c3a-89b7-a9d8800cd5b0) - # ### Select Box -The select box api can be used to prompt the user to choose between several options , it returns a slice of selected indexes, +The select box api can be used to prompt the user to choose between several options , it returns a slice of selected indexes, - **Usage**: @@ -149,6 +151,7 @@ if err != nil { fmt.Println("gender : ", value) ``` + - **Result**: ![image](https://github.com/yassinebenaid/prompts/assets/101285507/192424db-c9fe-480d-9f9b-4f8d6cdff56b) @@ -156,7 +159,9 @@ fmt.Println("gender : ", value) # ### Alerts + these are helper apis you can use for better alerts and messages. + ```go prompts.Info("Info alert") prompts.Error("Error alert") @@ -165,12 +170,9 @@ prompts.Success("Success alert") ``` - **Result**: - -![Screenshot from 2023-09-29 18-18-21](https://github.com/yassinebenaid/prompts/assets/101285507/d380f45b-4db4-4a9d-b4a2-8edf5f518579) - +![Screenshot from 2023-09-29 18-18-21](https://github.com/yassinebenaid/prompts/assets/101285507/d380f45b-4db4-4a9d-b4a2-8edf5f518579) - ```go prompts.InfoMessage("Info message") prompts.ErrorMessage("Error message")