10

I wondered if there were any tree libraries available for (n)curses.

I'm trying to write a component that shows a tree of folders & was curious if there was a prebuilt curses component that could do this.

I've checked 'core' curses as well as libraries like CDK - and I can't seem to find anything.

If none exists, I'm not averse to building my own - but I can't seem to locate any decent tutorials on doing this, so any help in this regard would also be much appreciated.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
phatmanace
  • 4,671
  • 3
  • 24
  • 29

4 Answers4

2

Urwid has a tree widget, FWIW.

UPDATE: 2020-10-01 - I wrote my answer back in 2010, when I wrote a tree widget for Urwid since I couldn't find anything. Urwid still has the tree widget, but these days, I would look at Python's prompt-toolkit or one of the alternatives.

robla
  • 133
  • 6
2

"I'm trying to write a component that shows a tree of folders"

CDK has the CDKFSELECT widget.

It displays a list of directories and files, that might work for you, or the source code of CDKFSELECT might be leveraged for your own custom written solution.

CDKFSELECT *fSelect = 0;

/*
Height of zero means to extent of xterm
Width of zero means to extent of xterm
*/
int HEIGHT = 0;
int WIDTH = 0;

char *title = new char[strlen("Pick a file to open") + 1];
strcpy(title, "Pick a file to open");

char *prompt = new char[strlen("==> ") + 1];
strcpy(prompt, "==> ");

char *directoryAttribute = new char[strlen("</B>") + 1]; /* bold */
strcpy(directoryAttribute, "</B>");

char *fileAttribute = new char[strlen("</N>") + 1]; /* normal */
strcpy(fileAttribute, "</N>");

char *linkAttribute = new char[strlen("</D>") + 1]; /* dim */
strcpy(linkAttribute, "</D>");

char *sockAttribute = new char[strlen("</D>") + 1]; /* dim */
strcpy(sockAttribute, "</D>");

boolean displayBox = TRUE;
boolean displayShadow = FALSE;

fSelect = newCDKFselect(pCdkScreen,
          TOP, LEFT, HEIGHT, WIDTH,
          title, prompt,
          A_NORMAL, '_', A_REVERSE,
          directoryAttribute, fileAttribute, linkAttribute, sockAttribute,
          displayBox, displayShadow);

char *filename = activateCDKFselect(fSelect, 0);
/*
2014-06-13, using DDD, filename being correctly populated
by CDK
*/

/* do other stuff... */

/*
 free the memory of any dynamically created objects
 that were created with new or malloc, or such
*/
destroyCDKFselect(fSelect);

delete [] title;
delete [] prompt;
delete [] directoryAttribute;
delete [] fileAttribute;
delete [] linkAttribute;
delete [] sockAttribute;
mrflash818
  • 930
  • 13
  • 24
2

The dialog program (which has a documented library interface) has a "tree" widget. The program works with (n)curses, and unlike CDK, is suitable for use with UTF-8.

dialog - tree view

It also has a file(/directory) selection widget.

dialog - file-selection

There's also wcd (though like mc, reusability of the library is uncertain). However, it is a good example of what OP probably wants:

screenshot converted from wcd page

Regarding urwid, that's debatable. Under the hood you may not actually have curses. For what it's worth, a screenshot of the treeview script:

urwid treeview

and on my Debian/testing system, the script doesn't use ncurses. It's hardcoded (i.e., using raw_display).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
1

Look at the Midnight commander source code (http://www.midnight-commander.org/) It has a tree store widget.

kalyan
  • 3,076
  • 1
  • 22
  • 29