All Projects → oxequa → Interact

oxequa / Interact

Licence: gpl-3.0
Question/answer and multiple choice by command line

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Interact

Plato
Plato helps software teams connect & stay engaged with users to gather feedback, provide support & deliver better software.
Stars: ✭ 293 (+581.4%)
Mutual labels:  questions-and-answers
Gowrap
GoWrap is a command line tool for generating decorators for Go interfaces
Stars: ✭ 464 (+979.07%)
Mutual labels:  golang-tools
Stegify
🔍 Go tool for LSB steganography, capable of hiding any file within an image.
Stars: ✭ 927 (+2055.81%)
Mutual labels:  golang-tools
Gotests
Automatically generate Go test boilerplate from your source code.
Stars: ✭ 3,597 (+8265.12%)
Mutual labels:  golang-tools
Interviewguide
计算机校招、社招面试八股文整理,也是《逆袭进大厂》唯一仓库,目前已收录 C/C++ 、操作系统、数据结构、计算机网络、MySQL、Redis等面试资料,未来打算继续收录Java、Python、Go等面试常见问题,坚持将此仓库维护下去。
Stars: ✭ 288 (+569.77%)
Mutual labels:  questions-and-answers
Richgo
Enrich `go test` outputs with text decorations.
Stars: ✭ 544 (+1165.12%)
Mutual labels:  golang-tools
Ios Swift Objc Questions And Answers
iOS (swift, objc)개발을 하면서 알게된 질문과 답변 모음.😀
Stars: ✭ 279 (+548.84%)
Mutual labels:  questions-and-answers
Diagonistician Reactjs Express Mongoose
Question - Answers demo SPA
Stars: ✭ 13 (-69.77%)
Mutual labels:  questions-and-answers
Iox
Tool for port forwarding & intranet proxy
Stars: ✭ 411 (+855.81%)
Mutual labels:  golang-tools
Dalfox
🌘🦊 DalFox(Finder Of XSS) / Parameter Analysis and XSS Scanning tool based on golang
Stars: ✭ 791 (+1739.53%)
Mutual labels:  golang-tools
Govalidate
Validates your Go installation and dependencies.
Stars: ✭ 334 (+676.74%)
Mutual labels:  golang-tools
Tsukae
🧑‍💻📊 Show off your most used shell commands
Stars: ✭ 345 (+702.33%)
Mutual labels:  golang-tools
Gobinaries
Golang binaries compiled on-demand for your system
Stars: ✭ 672 (+1462.79%)
Mutual labels:  golang-tools
Go Callvis
Visualize call graph of a Go program using Graphviz
Stars: ✭ 3,692 (+8486.05%)
Mutual labels:  golang-tools
Gocurrency
Simple currency converter. Insert an amount, what currency to convert from and what currency to convert to.
Stars: ✭ 26 (-39.53%)
Mutual labels:  golang-tools
Nlu sim
all kinds of baseline models for sentence similarity 句子对语义相似度模型
Stars: ✭ 286 (+565.12%)
Mutual labels:  questions-and-answers
Scoold
A Stack Overflow clone for teams (self-hosted)
Stars: ✭ 463 (+976.74%)
Mutual labels:  questions-and-answers
Go Benchmark App
Application for HTTP benchmarking via different rules and configs
Stars: ✭ 21 (-51.16%)
Mutual labels:  golang-tools
Qa
Virtual repository hosting our Questions & Answers system
Stars: ✭ 11 (-74.42%)
Mutual labels:  questions-and-answers
Goggles
🔭 Goggles is a cross-platform GUI for your $GOPATH!
Stars: ✭ 672 (+1462.79%)
Mutual labels:  golang-tools

Interact

An easy and fast Go library, without external imports, to handle questions and answers by command line

Features
Installation

To install interact:

$ go get github.com/tockins/interact
Single question

Run a simple question and manage the response. The response field is used to get the answer as a specific type.

package main

import (
	i "github.com/tockins/interact"
)

func main() {
	i.Run(&i.Interact{
		Questions: []*i.Question{
			{
				Quest: i.Quest{
					Msg:      "Would you like some coffee?",
				},
				Action: func(c i.Context) interface{} {
					val, err := c.Ans().Bool()
					if err != nil{
					    return err
					}
					fmt.Println(val)
					return nil
				},
			},
		},
	})
}
Questions list

Define a list of questions to be run in sequence. The Action func can be used for validate the answer and can return a custom error.

Question struct is only for single question whereas Interact struct supports multiple questions

package main

import (
	i "github.com/tockins/interact"
)

func main() {
	i.Run(&i.Interact{
		Questions: []*i.Question{
			{
				Quest: i.Quest{
					Msg:     "Would you like some coffee?",
				},
				Action: func(c i.Context) interface{} {
					val, err := c.Ans().Bool()
					if err != nil{
					    return err
					}
					fmt.Println(val)
					return nil
				},
			},
			{
				Quest: i.Quest{
					Msg:     "What's 2+2?",
				},
				Action: func(c i.Context) interface{} {
				    val, _ := c.Ans().Int()
					// get the answer as integer
					if val < 4 {
						// return a custom error and rerun the question
						return "INCREASE"
					}else if val > 4 {
						return "DECREASE"
					}
					return nil
				},
			},
		},
	})
}
Multiple choice

Define a multiple choice question

package main

import (
	i "github.com/tockins/interact"
)

func main() {
	i.Run(&i.Interact{
		Questions: []*i.Question{
			{
				Quest: i.Quest{
                    Msg:     "how much for a teacup?",
                    Choices: i.Choices{
                        Alternatives: []i.Choice{
                            {
                                Text: "Gyokuro teapcup",
                                Response: "20",
                            },
                            {
                                Text: "Sencha teacup",
                                Response: -10,
                            },
                            {
                                Text: "Matcha teacup",
                                Response: 15.50,
                            },
                        },
                    },
                },
                Action: func(c i.Context) interface{} {
                    val, _ := c.Ans().Int()
                    fmt.Println(val)
                    return nil
                },
			},
		},
	})
}
Sub questions

The sub questions list is managed by the "Resolve" func. Each sub question can access to the parent answer by the "Parent" method

package main

import (
	i "github.com/tockins/interact"
)

func main() {
    i.Run(&i.Interact{
        Questions: []*i.Question{
            {
                Quest: i.Quest{
                    Msg:     "Would you like some coffee?",
                    Resolve: func(c i.Context) bool {     
                        val, _ := c.Ans().Bool()
                        return val
                    },
                },
                Subs: []*i.Question{
                    {
                        Quest: i.Quest{
                            Msg:     "What Kind of Coffee?",
                            Choices: i.Choices{
                                Alternatives: []i.Choice{
                                    {
                                        Text: "Black coffee",
                                        Response: "black",
                                    },
                                    {
                                        Text: "With milk",
                                        Response: "milk",
                                    },
                                },
                            },
                        },
                        Action: func(c i.Context) interface{} {
                            // question (sub) answer
                            val, _ := c.Ans().String()
                            fmt.Println(val)
                            // parent answer
                            val, _ = c.Parent().Ans().String()
                            fmt.Println(val)
                            return nil
                        },
                    },
                },
                Action: func(c i.Context) interface{} {
                    // question answer   
                    val, _ := c.Ans().String()
                    fmt.Println(val)
                    // sub question answer
                    fmt.Println(c.Qns().Get(0).Ans().Raw())
                    return nil
                },
            },
        },
    })
}
Question prefix

Interact support a custom prefix for each question

You can define a global prefix for all questions but you can overwrite it in each question with ease

As the first param you can pass a custom io.writer instance

package main

import (
	i "github.com/tockins/interact"
)

func main() {
    i.Run(&i.Interact{
        Before: func(c i.Context) error{
            c.SetPrfx(nil,"GLOBAL PREFIX")
            return nil
        },
        Questions: []*i.Question{
            {
                Before: func(c i.Context) error{
                    c.SetPrfx(nil,"OVERWRITTEN PREFIX")
                    // print current prefix
                    fmt.Println(c.Prfx())
                    return nil
                },
                Quest: i.Quest{
                    Msg:     "Would you like some coffee?",
                },
                Action: func(c i.Context) interface{} {
                    return nil
                },
            },
            {
                Before: func(c i.Context) error{
                    fmt.Println(c.Prfx())
                    return nil
                },
                Quest: i.Quest{
                    Msg:     "What's 2+2?",
                },
                Action: func(c i.Context) interface{} {
                    return nil
                },
            },
        },
    })
}
Default values

You can define a default value for each question and get it in the action func as an answer

package main

import (
	i "github.com/tockins/interact"
)

func main() {
    i.Run(&i.Interact{
        Questions: []*i.Question{
            {
                Before: func(c i.Context) error{
                    c.SetDef("test",default val")
                    return nil
                },
                Quest: i.Quest{
                    Msg:     "Would you like some coffee?",
                },
                Action: func(c i.Context) interface{} {
                    val, _ := c.Ans().String()
                    fmt.Println(val)
                    return nil
                },
            },
            {
                Before: func(c i.Context) error{
                    c.SetDef("default","default val")
                    return nil
                },
                Quest: i.Quest{
                    Msg:     "Would you like some coffee?",
                },
                Action: func(c i.Context) interface{} {
                    val, _ := c.Ans().Bool()
                    fmt.Println(val)
                    return nil
                },
            },
        },
    })
}
Custom errors

You can define a default error for every question or you can set a default error message

package main

import (
	i "github.com/tockins/interact"
)

func main() {

	i.Run(&i.Interact{
		Before: func(c i.Context) error{
			c.SetErr("Default error")
			return nil
		},
		Questions: []*i.Question{
			{
				Quest: i.Quest{
					Msg: "Would you like some coffee?",
					Err: "Custom error fot this question",
				},
				Action: func(c i.Context) interface{} {
                    val, err := c.Ans().Bool()
					if err {
						return "Invalid answer"
					}
					return nil
				},
			},
			{
				Quest: i.Quest{
					Msg: "Would you like some coffee?",
				},
				Action: func(c i.Context) interface{} {
					val, err := c.Ans().Bool()
                    if err {
                        return c.Err()
                    }
                    return nil
				},
			},
		},
	})
}
After Before

For every question and for each list of questions you can define custom commands to be run before or after the relative instance

package main

import (
	i "github.com/tockins/interact"
)

function main(){
    i.Run(&i.Interact{
        Before: func(c i.Context) error{
            c.SetPrfx(nil, "TEST")
            return nil
        },
        Questions: []*i.Question{
            {
                Before: func(c i.Context) error{
                    c.SetPrfx(nil, "TEST A")
                    return nil
                },
                Quest: i.Quest{
                    Msg:     "How much coffee?",
                },
                Action: func(c i.Context) interface{} {
                    return nil
                },
                After: func(c i.Context) error{
                    val, _ := c.Ans().Int()
                    fmt.Println(val)
                    return nil
                },
            },
            {
                Quest: i.Quest{
                    Msg:     "How much coffee?",
                },
                Action: func(c i.Context) interface{} {
                    return nil
                },
            },
        },
        After: func(c i.Context) error{
            for _, v := range c.Qns().List(){
                fmt.Println(v.Quest(),v.Ans().Raw())
            }
            return nil
        },
    })
}
Skip a Question

With the skip func you can stop the execution of the current question or you can skip the next.

package main

import (
	i "github.com/tockins/interact"
)

function main(){
    i.Run(&i.Interact{
        Before: func(c i.Context) error{
            // skip all questions
            //c.Skip()
            return nil
        },
        Questions: []*i.Question{
            {
                Before: func(c i.Context) error{
                    // skip the current question
                    //c.Skip()
                    return nil
                },
                Quest: i.Quest{
                    Msg:     "How much coffee?",
                },
                Action: func(c i.Context) interface{} {
                    return nil
                },
                After: func(c i.Context) error{
                    // skip the next question
                    c.Skip()
                    return nil
                },
            },
            {
                Before: func(c i.Context) error{
                    return nil
                },
                Quest: i.Quest{
                    Msg:     "How much tea?",
                },
                Action: func(c i.Context) interface{} {
                    return nil
                },
                After: func(c i.Context) error{
                    return nil
                },
            },
        },
    })
}
Reload a Question

You can reload a question how many times as you want

package main

import (
	i "github.com/tockins/interact"
)

function main(){
    i.Run(&i.Interact{
        Questions: []*i.Question{
            {
                Quest: i.Quest{
                    Msg:     "Would you like Interact?",
                },
                Action: func(c i.Context) interface{}{
                    val, err := c.Ans().Bool()
                    if (err != nil || !val){
                        c.Reload()
                    }
                    return nil
                },
            },
        },
    })
}
End signal

End a group of questions or sub-questions with a specific character or string

package main

import (
	i "github.com/tockins/interact"
)

func main() {
	i.Run(&i.Interact{
		Before: func(c i.Context) error {
			c.SetEnd("!*")
			return nil
		},
		Questions: []*i.Question{
			{
				Before: func(c i.Context) error {
					c.SetEnd("*")
					return nil
				},
				Quest: i.Quest{
					Msg: "Would you like some coffee? (insert '*' to stop this question or the sub questions)",
					Resolve: func(c i.Context) bool {
						val, _ := c.Ans().Bool()
						return val
					},
				},
				Subs: []*i.Question{
					{
						Before: func(c i.Context) error {
							c.SetEnd("!")
							return nil
						},
						Quest: i.Quest{
							Msg: "What kind of Coffee? (insert '!' to stop)",
						},
						Action: func(c i.Context) interface{} {
							c.Reload()
							return nil
						},
					},
					{
						Quest: i.Quest{
							Msg: "What type of Coffee?",
						},
					},
				},
			},
			{
				Quest: i.Quest{
					Msg: "Would you like some tea?",
				},
				Action: func(c i.Context) interface{} {
					c.Reload()
					return nil
				},
			},
		},
	})
}
Colors support

Interact supports the color scheme defined by the package "fatih/color"

package main

import (
	"github.com/fatih/color"
	i "github.com/tockins/interact"
	"fmt"
)

func main() {

	b := color.New(color.FgHiWhite).Add(color.BgRed).SprintfFunc()
	y := color.New(color.FgYellow).SprintFunc()
	r := color.New(color.FgRed).SprintFunc()
	g := color.New(color.FgGreen).SprintFunc()
	prefix := y("[") + "INTERACT" + y("]")

	i.Run(&i.Interact{
		Before: func(c i.Context) error{
			c.SetPrfx(color.Output, prefix)
			return nil
		},
		Questions: []*i.Question{
			{
				Before: func(c i.Context) error{
					c.SetPrfx(nil,y("[") + "INTERACT QUEST" + y("]"))
					return nil
				},
				Quest: i.Quest{
					Msg:     "Would you like some coffee?",
					Options:  g("[yes/no]"),
					Err:      b("INVALID"),
					Resolve: func(c i.Context) bool{
						val, _ := c.Ans().Bool()
						return val
					},
				},
				Subs: []*i.Question{
					{
						Quest: i.Quest{
							Msg:     "What Kind of Coffee?",
							Err:      b("INVALID"),
							Choices: i.Choices{
								Color: g,
								Alternatives: []i.Choice{
									{
										Text: "Black coffee",
										Response: "black",
									},
									{
										Text: "With milk",
										Response: "milk",
									},
								},
							},
						},
						Action: func(c i.Context) interface{}{
						    val, _ := c.Ans().String()
							fmt.Println(val)
							val, _ := c.Parent().Ans().String()
							fmt.Println(val)
							return nil
						},
					},
				},
				Action: func(c i.Context) interface{} {
				    val, _ := c.Ans().Bool()
					if !val{
						return r("INVALID INPUT")
					}
					fmt.Println(c.Quest(), val)
					return nil
				},
			},
		},
	})
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].