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

feat: get icons by filetype #125

Merged
merged 1 commit into from
Apr 23, 2022
Merged

Conversation

b0o
Copy link
Contributor

@b0o b0o commented Apr 22, 2022

Adds the filetypes table, which is a map from vim filetypes to icon names.

local filetypes = {
  ["Brewfile"] = "Brewfile",
  ["COMMIT"] = "COMMIT_EDITMSG",
  ["COPYING"] = "COPYING",
  -- ...
  ["yaml"] = "yaml",
  ["zig"] = "zig",
  ["zsh"] = "zsh",
}

The map is not exhaustive. More filetypes should be added over time, but at least this is a start.

Also adds the following functions:

get_icon_by_filetype(filetype, opts)
get_icon_colors_by_filetype(filetype, opts)
get_icon_color_by_filetype(filetype, opts)
get_icon_cterm_color_by_filetype(filetype, opts)
get_icon_name_by_filetype(filetype)

Because some icons are not distinguishable by their filetype alone, the filetype table maps to the "most common" or "canonical" icon. E.g. the filetype cpp maps to the cpp icon, even though .hpp files also have the cpp filetype.

For this reason, users should prefer the standard get_icon function if the filename is known; the get_icon_name_by_filetype function should only be used if the filename is unknown.


P.s. if you're interested, I wrote this script to assist in generating the filetypes table, and then manually cleaned up the results.

#!/usr/bin/env bash
set -euo pipefail

function get_icon_names() {
  curl 'https://raw.githubusercontent.com/kyazdani42/nvim-web-devicons/master/lua/nvim-web-devicons.lua' |
    lua -e '
      pats={}
      for name in pairs(dofile().get_icons()) do
        table.insert(pats, name)
      end
      table.sort(pats)
      print(table.concat(pats, "\n"))
    '
}

function main() {
  local tmp
  tmp="$(mktemp -d)"
  # shellcheck disable=2064
  trap "rmdir '$tmp'" EXIT
  cd "$tmp"
  local file
  local -A filetypes=()
  local -a missing=()
  while read -r pat; do
    echo "$pat" >&2
    if [[ "$pat" =~ ^\. ]]; then
      file="$pat"
    else
      file="test.$pat"
    fi
    touch "./$file"
    local ft
    ft="$(/usr/bin/nvim -u NORC --noplugin --headless "./$file" +'lua
      local ok, err = pcall(vim.fn.writefile, { vim.bo.filetype }, "/dev/stdout")
      if not ok then
        print(err .. "\n")
        vim.cmd "cquit"
      end
      vim.cmd "quit"
    ')"
    rm "./$file"
    if [[ -n "$ft" ]]; then
      if [[ -v filetypes["$ft"] ]]; then
        filetypes["$ft"]="${filetypes["$ft"]}, '$pat'"
      else
        filetypes["$ft"]="'$pat'"
      fi
    else
      missing+=("$pat")
    fi
  done < <(get_icon_names)

  echo "local filetypes = {"
  for ft in "${!filetypes[@]}"; do
    echo "  ['$ft'] = { ${filetypes[$ft]} },"
  done
  echo "}"
  echo
  echo "local missing = {"
  printf "  '%s',\n" "${missing[@]}"
  echo "}"
}

main "$@"

Ref #29

@b0o b0o changed the title feat: get icons by filetype (#29) feat: get icons by filetype Apr 22, 2022
Copy link
Member

@kyazdani42 kyazdani42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice addition, thanks :)

@kyazdani42 kyazdani42 merged commit 7abc573 into nvim-tree:master Apr 23, 2022
@b0o
Copy link
Contributor Author

b0o commented Apr 23, 2022

Great! Should be able to close #29

@kyazdani42 kyazdani42 mentioned this pull request Apr 23, 2022
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Aug 22, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Aug 25, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Sep 8, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Sep 11, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Sep 14, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Oct 5, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Oct 5, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Oct 6, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Oct 19, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Oct 19, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
zappolowski added a commit to zappolowski/lualine.nvim that referenced this pull request Oct 22, 2022
With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
shadmansaleh pushed a commit to nvim-lualine/lualine.nvim that referenced this pull request Oct 23, 2022
* Refactor tests to use stubs for nvim-web-devicons

* Simplify getting icon

`get_icon` does not need the 2nd parameter (`ext`) as it will derive it
from the given `name` (1st argument) itself.

* fix component(filetype) proper icon based on filetype

With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes #578
craigmac pushed a commit to craigmac/lualine.nvim that referenced this pull request May 15, 2024
* Refactor tests to use stubs for nvim-web-devicons

* Simplify getting icon

`get_icon` does not need the 2nd parameter (`ext`) as it will derive it
from the given `name` (1st argument) itself.

* fix component(filetype) proper icon based on filetype

With nvim-tree/nvim-web-devicons#125 a filetype
based lookup was implemented upstream which can be used to get an icon
when a file doesn't have an extension.

fixes nvim-lualine#578
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

Successfully merging this pull request may close these issues.

None yet

2 participants