Giter VIP home page Giter VIP logo

swiftpicker's Introduction

SwiftPicker

License: MIT

SwiftPicker is a command-line picker tool written in Swift. It allows users to interactively select single or multiple items from a list using a text-based interface.

Features

  • Single and multiple item selection.
  • User-friendly prompts for input and permission.
  • ANSI terminal support for enhanced UI.
  • Customizable with any type conforming to DisplayablePickerItem.

Installation

To use SwiftPicker in your Swift project, add it as a dependency in your Package.swift file:

dependencies: [
    .package(url: "https://github.com/nikolainobadi/SwiftPicker.git", from: "0.8.0")
]

Then import it in your project:

import SwiftPicker

Usage

Single Selection

Single Selection Demo

import SwiftPicker

let picker = SwiftPicker()
let title = "Choose Your Favorite Programming Language"
let sampleList = ["Swift", "Python", "JavaScript", "C#", "Java", "Go", "Ruby", "Kotlin"]

if let selection = picker.singleSelection(title: title, items: sampleList) {
    // do what you want with the selction
}

Multi-Selection

To use custom items in SwiftPicker, conform your type to the DisplayablePickerItem protocol. And don't worry about long lists, SwiftPicker can handle scrolling!

Multiple Selection Demo

struct Movie {
    let title: String
    let director: String
}

// customize the 'displayName' 
extension Movie: DisplayablePickerItem {
    var displayName: String {
        return title
    }
}

let marvelMovies = Movie.marvelList
let title = "Select Your Favorite Marvel Movies."
let results = picker.multiSelection(title: title, items: marvelMovies)

Error Handling

SwiftPicker can throw errors which you may handle using do-catch:

do {
    let input = try picker.getRequiredInput("Please provide input:")
    print("Your input: \(input)")
} catch {
    print("Input is required.")
}

do {
    let selection = try picker.requiredSingleSelection(title: title, items: sampleList)
    print("You selected: \(selection.displayName)")
} catch SwiftPickerError.selectionCancelled {
    print("Selection was cancelled.")
} catch {
    print("An unknown error occurred.")
}

Backstory

I think programming is one of the few fields where 'specialized laziness' is actually a superpower. While building custom command line tools may seem like a daunting task to some, I see it as a way to never have to waste time on the boring portions of my workflow ever again. But I'm an iOS developer. When I write code, I prefer to do it in Swift. Unfortunately, there aren't many Swift libraries for command line tools. And I feel like it's a catch-22 because nobody wants to write libraries for the command line using Swift because there aren't many libraries out there to help them, and there aren't many libraries out there because nobody wants to write them, and round and round we go.

SwiftPicker is simply my contribution to the (hopefully growing) ecosystem of Swift command line tools. It's easy to use, relatively lightweight, and best of all, it helps me write more command line tools to feed my 'specialized laziness'.

Acknowledgements

This project was inspired by How to Make an Interactive Picker for a Swift Command-Line Tool by Pol Piella Abadia. Special thanks for the great tutorial.

Contributing

Any feedback or ideas to enhance SwiftPicker would be well received. Please feel free to open an issue if you'd like to help improve this swift package.

License

SwiftPicker is released under the MIT License. See LICENSE for details.

swiftpicker's People

Contributors

nikolainobadi avatar

Watchers

 avatar

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.