Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/bash interface #90

Closed
alexmyczko opened this issue Oct 9, 2021 · 1 comment
Closed

cli/bash interface #90

alexmyczko opened this issue Oct 9, 2021 · 1 comment

Comments

@alexmyczko
Copy link

do you plan to make this usable like iseect? https://packages.debian.org/sid/iselect

@gansm
Copy link
Owner

gansm commented Oct 9, 2021

FINAL CUT is a widget toolkit for creating applications. It is not a standalone application. But with the FListBox widget you can easily create such an application.

Example: fiselect.cpp

#include <iostream>
#include <string>

#include <final/final.h>

using namespace finalcut;
using StringVector = std::vector<std::string>;

// Lazy conversion insert function
void stringToItem ( FListBoxItem& item
                  , FDataAccess* container
                  , std::size_t index )
{
  StringVector& str_vec = flistboxhelper::getContainer<StringVector>(container);
  auto iter = str_vec.begin();
  std::advance (iter, index);
  item.setText (FString(*iter));
  item.setData (*iter);
}

class SelectWidget final : public FWindow
{
  public:
    explicit SelectWidget (FWidget* parent = nullptr)
      : FWindow{parent}
    {
      init();
    }

    template <typename T>
    void insert (T&& list)
    {
      Listbox.insert(std::forward<T>(list), stringToItem);
    }

    void reserve (std::size_t capacity)
    {
      Listbox.reserve(capacity);
    }

    std::size_t getCurrentItem()
    {
      return Listbox.currentItem();
    }

  private:
    void init()
    {
      setActiveWindow(this);
      //Listbox.setMultiSelection();

      Listbox.addCallback
      (
        "clicked",
        this, &SelectWidget::cb_select
      );

      Listbox.addCallback
      (
        "row-changed",
        this, &SelectWidget::updateStatusbar
      );

      key_q.addCallback
      (
        "activate",
        finalcut::getFApplication(),
        &finalcut::FApplication::cb_exitApp,
        this
      );
    }

    void updateStatusbar()
    {
      FString msg = FString{} << Listbox.currentItem()
                              << L"/"
                              << Listbox.getCount();
      Listbox.setStatusbarMessage (msg);
      Statusbar.setMessage(msg);
      Statusbar.drawMessage();
    }

    void cb_select()          // Line was select with enter
    {
      FApplication::exit(1);  // Set the exit code to 1
      close();                // Close
    }

    void onShow (FShowEvent*) override
    {
      updateStatusbar();
    }

    void adjustSize() override
    {
      auto size = FSize{getDesktopWidth(), getDesktopHeight() - 1};
      setGeometry (FPoint{1, 1}, size, false);
      Listbox.setGeometry (FPoint{1, 1}, size, false);
      FWindow::adjustSize();
    }

    void resetColors() override
    {
      const auto& wc = getColorTheme();
      setForegroundColor (wc->dialog_fg);
      setBackgroundColor (wc->dialog_bg);
      FWidget::resetColors();
    }

    void draw() override
    {
      setColor();
      clearArea();
    }

    FListBox   Listbox{this};
    FStatusBar Statusbar{this};
    FStatusKey key_q{FKey::q, "Quit", &Statusbar};
};


int main (int argc, char* argv[])
{
  auto ret{0};
  std::size_t current_item{0};
  StringVector list{};
  list.reserve(100);

  for (std::string line; std::getline(std::cin, line);)
  {
    list.emplace_back(line);
  }

  // Close stdin filehandle and reconnect it to tty
  close(fileno(stdin));
  open("/dev/tty", O_RDONLY);

  // Save and close the stdin filehandle and reconnect it to tty
  int fd_stdout = dup(1);
  close(fileno(stdout));
  open("/dev/tty", O_RDWR);

  {
    FApplication app{argc, argv};
    SelectWidget select_widget(&app);
    FWidget::setMainWidget(&select_widget);
    select_widget.reserve(list.size());
    select_widget.insert (list);
    select_widget.show();
    ret = app.exec();
    current_item = select_widget.getCurrentItem();
  }

  if ( ret == 1 )
  {
    const auto& item = list[current_item - 1];
    write(fd_stdout, item.c_str(), item.length());
    write(fd_stdout, "\n", 1);
  }
  
  close(fd_stdout);
  return ret;
}

Compile with

g++ fiselect.cpp -o fiselect -O3 -lfinal -std=c++11

Test

grep -v '^#' ~/.bash_history | ./fiselect

or

ls * | ./fiselect | sed -e 's/^/your choice: /'

or

find /proc/ | fiselect

grafik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants