Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

ewpratten/inputlib

Repository files navigation

InputLib

Documentation Build library

InputLib is a small and easy-to-use Java library for working with text-based user inputs in a terminal. This library was originally built to drastically speed up my development time when working on those annoying "whats your name?" programs everyone seems to do in intro Computer Science courses. This repo contains "version 2", which is set up to handle more datatypes, has a faster parser (the old parser was ANTLR4-based), and provides a very simple interface.

Using in your project

Step 1. Add the RetryLife maven server to your build.gradle file:

repositories {
    maven { 
        name 'retrylife-release'
        url 'https://release.maven.retrylife.ca/' 
    }
}

Step 2. Add this library as a dependency:

dependencies {
    implementation 'ca.retrylife:inputlib:1.+'
    implementation 'ca.retrylife:inputlib:1.+:sources'
    implementation 'ca.retrylife:inputlib:1.+:javadoc'
}

Usage

With InputLib loaded into your project, usage is fairly simple. The library is centered around the ca.retrylife.inputlib.Prompt class.

import ca.retrylife.inputlib.Prompt;

Prompt myPrompt = new Prompt();

InputLib supports the following basic datatypes: String, char, int, double, float, boolean. Each of these have their respective "prompt function". To use these, simply call them, and pass in a message you want the user to see.

// Prompt a String
String s = myPrompt.promptString("message");
// Prompt a char
char c = myPrompt.promptCharacter("message");
// Prompt an int
int i = myPrompt.promptInteger("message");
// Prompt a double
double d = myPrompt.promptDouble("message");
// Prompt a float
float f = myPrompt.promptFloat("message");
// Prompt a boolean
boolean b = myPrompt.promptBoolean("message");

Some of these prompt functions support "fancy inputs". These are some quality-of-life additions to the InputLib parser I occasionally use:

  • promptInteger will accept binary and hex strings in the following format:
    • Binary: 0b0101101
    • Hexadecimal: 0xdeadbeef
  • promptInteger, promptDouble, and promptFloat all accept underscores as separators, just like Java does.
  • promptFloat allows the user to add the letter f as a suffix, just like Java does.
  • promptBoolean accepts the following inputs, and converts them to booleans:
    • yes
    • no
    • true
    • false
    • accept
    • deny

A few other functions exist for allowing a user to make a selection from a list:

// Make the user pick one of many characters
char selection = myPrompt.promptCharacterSelection("Pick one", 'a', 'b', 'c');
// Make the user pick a number between two other numbers
int selection = myPrompt.promptIntegerRangeSelection​("Pick between", 1, 10);
// Make the user pick one of many integers
int selection = myPrompt.promptIntegerSelection​("Pick one", 1, 10, 100, 120);
// Make the user pick an item from a list
String preference = myPrompt.promptList("Which is better", "cats", "dogs", "nerf guns");

Finally, the promptMultiLineString​ function can be used to get a string from the user that spans multiple lines of input.

How to push a release

Pushing a release is simple. Clone this repo, go to master, and run:

git tag -a <version> -m "<message>"
git push origin <version>