Git Product home page Git Product logo

nvim-codeshot's Introduction

Nvim Codeshot

Nvim Codeshot is a plugin that allows you to take a screenshot of the code in the file you are currently working in.

Requirements

Installation

Codeshot can be installed using any package manager. Here is an example of installation using lazy.nvim:

require("lazy").setup({
  {
    "Ofadiman/nvim-codeshot",
    -- Install latest chrome browser required for puppeteer.
    build = ":lua require('nvim-codeshot').install()"
  },
})

After installation, you can configure a keyboard shortcut that invokes the screenshot command.

vim.keymap.set("v", "<leader>ua", function()
  vim.cmd(":CodeshotScreenshot")
end, { noremap = true, silent = true, nowait = true })

Configuration

require("nvim-codeshot").setup({
  -- Setting this option to `true` will cause the screenshot to contain the path to the file where the screenshot was taken.
  breadcrumbs = true,
  padding = {
    -- Allows to configure the amount of horizontal padding.
    horizontal = 10,
    -- Allows to configure the amount of vertical padding.
    vertical = 10,
  },
  html = {
    -- Allows to completely override default html template used for generating screenshots.
    template = "",
    -- Allows to add a watermark to the page that renders the code.
    watermark = "",
    -- Allows to add additional styles to the page that renders the code.
    styles = "",
  },
  whitespace = {
    -- Setting this option to `false` will result in the extra whitespace not being removed from the code at screenshot time.
    trim = true,
    -- Allows to set the indentation width for code that uses tabs for indentations.
    tab_width = 2,
  },
  output = {
    -- Setting this option to `true` will cause the screenshot to be saved to the file system. Screenshot by default is only copied to clipboard.
    enable = false,
    -- Path to the folder with screenshots.
    directory = "~/.codeshot",
    -- Allows to format screenshot filename using lua function. Screenshot file names will by default follow `filename.extension` format (e.g. `readme.md.png`).
    -- For example, if you want to have `readme.md_2024_05_26_13:22:06.png` filename format, you have to add custom formatting function that would look like this:
    -- formatter = function(filename)
    --   return filename .. "_" .. os.date("%Y_%m_%d_%H:%M:%S")
    -- end,
    formatter = function(filename)
      return filename
    end,
  },
  clipboard = {
    -- Setting this option to `false` will cause the screenshot not being copied to the clipboard.
    enable = true,
    -- Allows to configure command which will be used to copy image to clipboard. The plugin currently supports the following clipboard managers:
    -- * xclip (command: `xclip -selection clipboard -t image/png -i %s`)
    -- If you are using unlisted clipboard manager you have to configure the script used to copy the image by yourself.
    command = nil,
  },
  -- Allows to configure theme used for making screenshots. The full list of themes is available here: https://shiki.style/themes
  theme = "catppuccin-mocha",
  -- Allows to configure screenshot extension. Currently supported extensions are `webp`, `jpeg` and `png`.
  extension = "png",
  -- Allows to customize screenshot scale.
  scale = 3,
  aliases = {
    -- Allows to set language aliases so that, for example, files with extension `.ofa` are treated as files with extension `.js` when taking a screenshot. Available language aliases: https://shiki.style/languages
    languages = nil,
    -- Allows to set file aliases so that, for example, files without extension (like Makefile) can be interpreted correctly when taking a screenshot. Available language aliases: https://shiki.style/languages
    files = nil,
  },
})

Similar projects

nvim-codeshot's People

Contributors

ofadiman avatar

Watchers

 avatar  avatar

nvim-codeshot's Issues

Handle whitespace characters

User Story

As a user, I want to be able to configure what happens if the code I'm taking a screenshot of has excessive amounts of whitespaces.

Technical Requirements and Implementation Details

Add a key whitespace to the plugin options, which will have fields trim and tab_width.

local M = {
  options = {
    whitespace = {
      trim = true,
      tab_width = 4,
    },
    ...
}
  • The trim field should take a value of type boolean, which should allow users to specify whether or not the redundant whitespace characters should be removed from the screenshot.
  • The tab_width field should take a value of type number, which should allow users to specify how much indentation to apply when the code contains tabs instead of spaces.

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • Setting the whitespace.trim option to true causes redundant whitespace characters to be removed from the screenshot.
  • Setting the whitespace.trim option to false causes redundant whitespace not to be removed from the screenshot.
  • Setting the whitespace.tab_width option to any number (e.g. 2, or 4) causes tab characters to be converted to the configured number of spaces.

Inform user when clipboard and output are disabled

User Story

As a user, I want to see the message in the console if I have the output and clipboard options disabled.

Technical Requirements and Implementation Details

The code should exit as early as possible.

A new debug option must be added to solve this problem. This option should allow you to display additional logs in the console about what is happening in the plugin.

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • A new plugin option called debug is added.
  • If the debug option is set to true and both clipboard.enable and output.enable options are set to false then the console displays a message about missconfigured plugin.
  • If the debug option is set to false and both clipboard.enable and output.enable options are set to false then the console does not display a message about missconfigured plugin.

Inject HTML watermark

User Story

As a user, I want to be able to add HTML watermark to code screenshots.

Technical Requirements and Implementation Details

Watermark configuration should be done using the html.watermark option.

local M = {
  options = {
    html = {
      watermark = [[
<span style="color: blue;">
  Ofadiman
</span>
]],
    },
  },
}

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • Adding the html.watermark option injects HTML into the template from which the code is generated.
  • The html.watermark option is validated as string.

Inject HTML template

User Story

As a user, I want to be able to inject custom HTML to override the plugin's default HTML.

Technical Requirements and Implementation Details

Template configuration should be done using the html.template option.

local M = {
  options = {
    html = {
      template = [[
<!doctype html>
<html>
  <head>
    <title>nvim-codeshot</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      pre {
        width: fit-content;
        padding: 20px;
      }
    </style>
  </head>
  <body>
    %s
  </body>
</html>
]],
    },
  },
}

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • Adding the html.template option allows to replace default HTML template used by plugin.
  • If the html.template option is present then the html.watermark and html.styles options are ignored.
  • The html.template option is validated as string.

Tools for abstracting clipboard interactions

Purpose

There is a clipboard tool on the market that allows for cross-platform interaction with the clipboard system. I would like to test if this tool can be used in a project instead of writing code to handle clipboard in lua/node.js, which will detect the operating system and the currently installed clipboard tool and perform actions based on that.

Technical Requirements and Implementation Details

  • The tool must run on macos and linux operating systems and support arm64 and x86_64 CPU architectures.
  • The tool must provide a binary that can be placed in the project folder and executed (e.g. sh ./bin/clipboard/x86_64 image.png).

Business Requirements

  • The user should not have to manually install an additional tool on their computer.

Resources

Acceptance Criteria

  • Conclusions of the tests conducted with the tool recorded in the issue comments.
  • Decision made whether to use the tool or not.

Initial documentation

Description

The first version of the project should contain documentation about what the project does, how to install and configure it.

Resources

Acceptance Criteria

  • The project has a readme.md file with documentation.
  • In the documentation there is a section on the purpose of the project.
  • In the documentation there is a section on the project installation.
  • In the documentation there is a section on the available options.
  • In the documentation there is a section on the similar libraries.

Custom language aliases

User Story

As a user, I want to be able to treat files with non-standard extensions as files with standard extensions. For example, I want to treat files with extension .ofa like files with extension .js when taking a screenshot.

Technical Requirements and Implementation Details

Shiki library allows to declare custom language aliases which is a feature that allows to map 1 file type into another file type.

Business Requirements

N/A

Resources

Acceptance Criteria

  • languages option allows to specify language aliases.

Rename `persist` option and its keys

Description

To configure the persistence of the image file I currently use the persist key in the plugin options. In the silicon.nvim plugin, this option is called output and I think this name is better because it is more generic.

Technical Requirements and Implementation Details

No response

Resources

Acceptance Criteria

  • persist option is renamed to output.
  • persist.path option is renamed to output.directory.

Custom file name formatter

User Story

As a user, I want to be able to customize the name of the file in which the generated image is saved.

Technical Requirements and Implementation Details

The user should be able to pass a formatting function to the plugin option that will be executed before the screenshot is taken.

The function should take the file name as a parameter and return the processed file name.

local M = {
  options = {
    output = {
      enable = false,
      path = "~/.codeshot",
      formatter = function(filename)
        return filename
      end,
    },
  },
}

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • The output option has the ability to configure the file name formatting function.
  • The value returned by the formatting function is used instead of the default file name while saving the image.

Breadcrumbs

User Story

As a user, I want to be able to configure whether the path to the file where the screenshot was taken will be shown in the screenshot or not.

Technical Requirements and Implementation Details

The option that allows users to configure whether the file path should be visible in the screenshot or not should be called breadcrumbs.

Business Requirements

No response

Resources

Acceptance Criteria

  • If the breadcrumbs option is set to true then the screenshot has the path to the file where the screenshot was taken.
  • If the breadcrumbs option is set to false then the screenshot does not have the path to the file where the screenshot was taken.

Investigate if quality option is needed

Description

I am using quality option to set deviceScaleFactor in page.setViewport method and I am using the same option again while taking a screenshot via page.screenshot method.

I added the quality option so that I can get a high-resolution screenshot but I'm not sure it is 100% required.

Technical Requirements and Implementation Details

  • Try to remove quality option while maintaining high quality screenshot.

Resources

Acceptance Criteria

  • quality option is removed from plugin API.

Change default generated image extension

Description

The images that are currently generated by the tool have webp extension. This has an advantage because such images are smaller than other formats. The problem is that I don't know if all programs support the webp format out of the box, so using png or jpeg format as the default should cause fewer problems for users.

Technical Requirements and Implementation Details

  • ChatGPT claims that for files containing screenshots of code, the png format should generate smaller files than the jpg format.

Resources

No response

Acceptance Criteria

  • Verify which file format produces smaller images.
  • Default extension for generated images is set to png or jpeg.

Configurable vertical and horizontal padding

User Story

As as user, I want to be able to configure vertical and horizontal padding independently.

Technical Requirements and Implementation Details

The current padding option, which takes one value, should be replaced with the following object:

local M = {
  options = {
    padding = {
      horizontal = 10,
      vertical = 20
    },
    ...
}

Business Requirements

No response

Resources

Acceptance Criteria

  • User has ability to configure horizontal padding.
  • User has ability to configure vertical padding.

Support `pbcopy` command

User Story

As a user, I want to be able to use pbcopy command to copy image to clipboard.

Technical Requirements and Implementation Details

No response

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • pbcopy command can be used to copy image to clipboard.

Line numbers support

User Story

As a user, I want to see line numbers in code screenshots.

Technical Requirements and Implementation Details

  • Line numbers must be displayed correctly regardless of the configured theme.

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • Line numbers are displayed on screenshots in colors of comments.

Support `xsel` command

User Story

As a user, I want to be able to use xsel command to copy image to clipboard.

Technical Requirements and Implementation Details

No response

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • xsel command can be used to copy image to clipboard.

Inject HTML styles

User Story

As a user, I want to be able to add HTML styles to customize code screenshots.

Technical Requirements and Implementation Details

Styles configuration should be done using the html.styles option.

local M = {
  options = {
    html = {
      styles = [[
<style>
h1 {
  color: red;
}

p {
  color: blue;
}
</style>
]],
    },
  },
}

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • Adding the html.styles option injects HTML into the template from which the code is generated.
  • The html.styles option is validated as string.

Clipboard program inference

User Story

As a user, I would like the clipboard program to be automatically set up without having to pass any options to clipboard.program.

Technical Requirements and Implementation Details

No response

Business Requirements

No response

Resources

No response

Acceptance Criteria

  • clipboard.program option is renamed to clipboard.command.
  • Copying an image to clipboard works without passing clipboard.command option if one of the supported clipboard managers is installed on the operating system.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.