Welcome to Vifm Q&A, where you can ask questions about using Vifm. Registration is optional, anonymous posts are moderated. E-mail and GitHub logins are enabled.
0 votes
in vifm by

I would like to show the used space percentage of mounted devices on the statusline, when are selected with the cursor, is that feasible? thank you in advance

2 Answers

0 votes
by
selected by
 
Best answer

So you want to show size of all selected files divided by capacity of the drive in which current directory is located? I assume that's the case below.

While Vifm has statusline macros for both, there isn't one for their ratio. It's possible to either use %{} syntax with a script or do it in Lua, however neither option will have access to size cache of Vifm.

by
edited by

as you pointed me out, my initial idea would encounter problems in a multiselection, so I would like to use %d and show this output from inside the path.

assuming that's the only output I want on statusline, I was trying something in the line of

set statusline="%{system('df -h %d | awk 'NR==2 {print $5}' ')}"

but, for what I can see, quotes are wrong and %d is not correctly escaped.

Is this semi-solution fixable?

by

The catch is that there is no way to escape } inside %{}, so I used sed instead:

set statusline="%{ system(expand('df -h %d') . '| sed -n ''2s/.* \\([0-9]\\+%\\).*/\\1/p''') }"
by

thank you for this working solution!
if I can't use awk, can this shell command works inside %{}?

df -h %d | sed 1d | cut -d ' ' -f 3,9

it returns me the total size too and it's more readable for me.
can I ask how expand expression comes to my help?

by

On its own cut won't produce reliable results on sequences of separators, but with tr, it should work fine:

set statusline="%{ system(expand(\"df -h %d | sed 1d | tr -s ' ' | cut -d ' ' -f 3,5\")) }"

expand() is needed to expand %d, but it dropped if %d is replaced with a dot:

set statusline="%{ system(\"df -h . | sed 1d | tr -s ' ' | cut -d ' ' -f 3,5\") }"
by

lovely, thank you <3

+1 vote
by
edited by

i just tried the lua way. the following code worked on my side with some hardcode, though i could not find a reliable way to invalidate the cache, gathered.

--the lua part
do
  local gathered = {}
  local function gather_used_pcent(dir)
    if gathered[dir] == nil then
      local file = assert(io.popen("df --output=pcent " .. dir, "r"))
      local output = file:read("*a")
      file:close()
      gathered[dir] = string.sub(output, #"Use%\n")
    end
    return gathered[dir]
  end

  assert(vifm.addhandler({
    name = "statusline",
    handler = function()
      local used_pcent = gather_used_pcent(vifm.currview().cwd)
      return { format = "%o %A %u:%g %= %s %d " .. used_pcent }
    end,
  }))
end
" the vifmrc part
set statusline=#thatplugin#statusline
...