Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

fix #5 #6

Merged
merged 1 commit into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![kaktree](https://user-images.githubusercontent.com/19470159/59667890-2d397780-91c0-11e9-9214-e32b04539e7a.png)

This plugin displays the interactive filetree. It requires Tmux and Perl, as
well as `ls` command.
well as `ls` command that supports at least `-l`, `-F`, and `-a` flags.

## Installation
You need latest Kakoune build from master in order to use
Expand Down
24 changes: 19 additions & 5 deletions perl/kaktree.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use strict;
use warnings;

# This subroutine builds tree based on output of `ls' command. It recieves
# current indentation level, and constructs tree node based on it.
sub build_tree {
my $root = $_[0];
my $open_node = $ENV{"kak_opt_kaktree_dir_icon_open"};
Expand All @@ -17,10 +19,13 @@ sub build_tree {

chomp(my @input = <>);

# remove total ...
shift(@input);

if ($hidden eq "true") {
# remove `./' and `../' from tree
@input = grep {$_ ne "../"} @input;
@input = grep {$_ ne "./"} @input;
shift(@input);
shift(@input);
}

my $input_size = scalar @input;
Expand All @@ -32,21 +37,30 @@ sub build_tree {
my @files;

foreach my $item (@items) {
if ($item =~ /(.*)\/$/) {
print "$current_indent$indent_str$closed_node $1\n";
if ($item =~ /([^\s]+\s+){8}(.*)\/$/) {
if ($2 =~ /(.*)\s+->\s+.*/) {
print "$current_indent$indent_str$closed_node $1\n";
} else {
print "$current_indent$indent_str$closed_node $2\n";
}
} else {
push(@files, $item);
}
}

foreach my $file (@files) {
print "$current_indent$indent_str$file_node $file\n"
$file =~ /([^\s]+\s+){8}(.*)$/;
print "$current_indent$indent_str$file_node $2\n"
}
} else {
print "$current_indent$indent_str<empty>\n"
}
}

# Kaktree doesn't store paths of listed directories and path yet. So
# `make_path' subroutine works in opposite direction of `build_tree'.
# It builds path based on indentation in the tree buffer in order
# to get the location of currently expanded directory in the tree.
sub make_path {
my $indent = $ENV{"kak_opt_kaktree_indentation"};
my $current_indent = length($ENV{"kak_opt_kaktree__current_indent"});
Expand Down
20 changes: 6 additions & 14 deletions rc/kaktree.kak
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ bool kaktree_hlline true
declare-option -docstring "Amount of indentation for nested items. Must be greater than zero." \
int kaktree_indentation 2

declare-option -docstring "command to provide list of files. Strongly recommended to use analogs of -1 and -d if changed to another program.
Default value:
ls -1p" \
str kaktree_ls_command "ls -1p"

declare-option -hidden -docstring "Argument to pass to `kaktree_ls_command', to provide hidden files." \
str kaktree_hidden_arg "-a"

# Helper options
declare-option -hidden str kaktree__jumpclient
declare-option -hidden str kaktree__active 'false'
Expand Down Expand Up @@ -200,8 +192,8 @@ define-command -hidden kaktree-refresh %{ evaluate-commands %sh{
# $kak_opt_kaktree_show_hidden
kak_opt_kaktree__current_indent=""
kaktree_root="$(base_name $(pwd))"
[ "$kak_opt_kaktree_show_hidden" = "true" ] && hidden="$kak_opt_kaktree_hidden_arg"
command $kak_opt_kaktree_ls_command $hidden $(pwd) | perl -e "$kak_opt_kaktree__perl build_tree('$kaktree_root');" > ${tree}
[ "$kak_opt_kaktree_show_hidden" = "true" ] && hidden="-a"
command ls -lF $hidden $(pwd) | perl -e "$kak_opt_kaktree__perl build_tree('$kaktree_root');" > ${tree}

printf "%s\n" "evaluate-commands -client %opt{kaktreeclient} %{ try %{
edit! -debug -fifo ${fifo} *kaktree*
Expand Down Expand Up @@ -318,8 +310,8 @@ define-command -hidden kaktree-dir-unfold %{ evaluate-commands -save-regs 'abc"'
# build full path based on indentation to the currently expanded directory.
current_path=$(printf "%s\n" "$kak_quoted_reg_c" | perl -e "$kak_opt_kaktree__perl make_path();")

[ "$kak_opt_kaktree_show_hidden" = "true" ] && hidden="$kak_opt_kaktree_hidden_arg"
tree=$(command $kak_opt_kaktree_ls_command $hidden "./$current_path/$dir" | perl -e "$kak_opt_kaktree__perl build_tree('$kaktree_root');")
[ "$kak_opt_kaktree_show_hidden" = "true" ] && hidden="-a"
tree=$(command ls -lF $hidden "./$current_path/$dir/" | perl -e "$kak_opt_kaktree__perl build_tree('$kaktree_root');")

printf "%s\n" "set-register '\"' %{$tree}"
}
Expand Down Expand Up @@ -434,8 +426,8 @@ define-command -hidden kaktree-change-root -params ..1 %{ evaluate-commands -sav
escaped_path=$(printf "%s\n" "$current_path" | sed "s/#/##/g")
printf "%s\n" "change-directory %#$escaped_path#"
kak_opt_kaktree__current_indent=""
[ "$kak_opt_kaktree_show_hidden" = "true" ] && hidden="$kak_opt_kaktree_hidden_arg"
tree=$(command $kak_opt_kaktree_ls_command $hidden "$current_path" | perl -e "$kak_opt_kaktree__perl build_tree('$kaktree_root');" | sed "s/#/##/g")
[ "$kak_opt_kaktree_show_hidden" = "true" ] && hidden="-a"
tree=$(command ls -lF $hidden "$current_path" | perl -e "$kak_opt_kaktree__perl build_tree('$kaktree_root');" | sed "s/#/##/g")
printf "%s\n" "set-register '\"' %#$tree#; execute-keys '%Rgg'"
}
}}
Expand Down