Giter Site home page Giter Site logo

hyper-interactive's Introduction

Hyper-Interactive

Hyper-Interaction is a very small tiny library to help making a command line app become interactive. It allowes user to fill in qustionaire, making configuration or just a simple information input.

go get -u github.com/hyperjumptech/hyper-interactive

Simple text input

import (
	"fmt"
	interactive "github.com/hyperjumptech/hyper-interactive"
)

...
name := interactive.Ask("Whats your name ?", "Bruce Wayne", false)
fmt.Println("Hi " + name)
...

The command line will look like

$ interact
Whats your name ? [default "Bruce Wayne"] : Ferdinand
Hi Ferdinand

Default Answer

If you see [default __], simply hit enter will use the default value as your answer.

Confirming your Answer

All question type have a confirmation flag in their argument. For example

// the last argument is a confirmation flag
name := interactive.Ask("Whats your name ?", "Bruce Wayne", true) 
fmt.Println("Hi " + name)

This will result

$ interact
Whats your name ? [default "Bruce Wayne"] : Ferdinand
"Ferdinand", are you sure? (y/n/Y/N) [default : Y] ? Y
Hi Ferdinand

You will get result

The logic in all of those ask function will make sure the user fill in the correct answer.

  • User are ensured to choose valid option
  • User are ensured to specify valid number
  • User are ensured to specify valid time format
  • etc

Asking for Options

options := []string {
    "One","Two","Three",
    "Four","Five","Six",
    "Seven","Eight","Nine",
    "Ten",
}
choosen := interactive.Select("Please choose", options, 1,1, true)
fmt.Printf("You choose number %d\n", choosen)

This will be shown as

$ interact
Please choose :
(1) One    (3) Three  (5) Five   (7) Seven  (9) Nine                         
(2) Two    (4) Four   (6) Six    (8) Eight  (10) Ten                         
Choose from number above [default : (1) One] ? 5
(5) Five - Are you sure ?  (y/n/Y/N) [default : Y] ? Y
You choose number 5

Supported Questions

  • func Select(question string, options []string, startFrom, defaultOption int, confirm bool) int
  • func AskNumber(question string, from, to, def int, confirm bool) int
  • func AskTime(question string, def time.Time, confirm bool) time.Time. The answer must follow format 2006-01-02 15:04:05 -0700
  • func Ask(question, defaultAnswer string, confirm bool) string
  • func Confirm(question string, def bool) bool
  • more to come...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.