Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Elyx0 committed Sep 15, 2024
0 parents commit 6cbae7a
Show file tree
Hide file tree
Showing 14 changed files with 1,301 additions and 0 deletions.
122 changes: 122 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test
.env.production

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# End of https://mrkandreev.name/snippets/gitignore-generator/#Solidity
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Solidity Code to ANSI for Discord

Discord doesn't recognize the `solidity` syntax language.
But it does allow ansi: https://gist.github.com/kkrypt0nn/a02506f3712ff2d1c8ca7c9e0aed7c06


Solution is to parse the code to [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree), colorize, and output.

![Video](./readme_example/ex.mp4)

### Install and Usage

Node > v18.16.0.

1) `yarn install`

Depending on your os it's gonna use `pbcopy` or `xsel` to get it directly into the clipboard.
through https://github.com/sindresorhus/clipboardy

2) Copy the snippet you want to the clipboard
3) ./toDiscordAnsi.mjs
4) Paste into discord


Also supports passing a file input as 2nd param
```
node toDiscordAnsi.mjs snippet.sol
```

## Bonus
Add it as an alias and forget
```
alias toDiscordAnsi="node {FULL_PATH_TO}/toDiscordAnsi.mjs"
```

## Palette Configuration

Colors are defined in `highlighter.mjs`
```js
const colorsMap = {
keyword: colors.fg.blue,
type: colors.fg.cyan,
string: colors.fg.yellow,
identifier: colors.fg.white,
number: colors.fg.blue,
operator: colors.fg.white,
// ...
};
```

### TODO
- Global yarn package
- Finish definitions from `node_modules/@solidity-parser/parser/dist/src/ast-types.d.ts`

#### Disclaimer
Thanks to `solidity-parser-diligence` then continued by `@solidity-parser/parser`

### Other ways to solve it

Maybe using something like Vscode TextMate language from https://github.com/tintinweb/vscode-solidity-language/blob/master/src/syntaxes/solidity.tmLanguage.json + https://github.com/microsoft/vscode-textmate
35 changes: 35 additions & 0 deletions colors.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// colors.js
const colors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
dim: "\x1b[2m",
underscore: "\x1b[4m",
blink: "\x1b[5m",
reverse: "\x1b[7m",
hidden: "\x1b[8m",

fg: {
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m",
// Bright colors
brightBlack: "\x1b[90m",
brightRed: "\x1b[91m",
brightGreen: "\x1b[92m",
brightYellow: "\x1b[93m",
brightBlue: "\x1b[94m",
brightMagenta: "\x1b[95m",
brightCyan: "\x1b[96m",
brightWhite: "\x1b[97m",
},
bg: {
// Background colors if needed
}
};

export default colors;
45 changes: 45 additions & 0 deletions comments.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// comments.js
/**
* Extracts comments from the Solidity code.
* @param {string} code - The Solidity code.
* @returns {Array} - Array of comment objects with type, value, start, and end positions.
*/
function extractComments(code) {
const commentPattern = /\/\/.*$|\/\*[\s\S]*?\*\//gm;
const comments = [];
let match;

while ((match = commentPattern.exec(code)) !== null) {
const comment = match[0];
const startIndex = match.index;
const endIndex = match.index + comment.length;

const start = getLocation(code, startIndex);
const end = getLocation(code, endIndex);

comments.push({
type: 'comment',
value: comment,
start,
end
});
}

return comments;
}

/**
* Converts a character index to line and column numbers.
* @param {string} code - The original code.
* @param {number} index - The character index.
* @returns {Object} - Object with line and column numbers.
*/
function getLocation(code, index) {
const lines = code.slice(0, index).split('\n');
const line = lines.length;
const column = lines[lines.length - 1].length + 1; // 1-based column
return { line, column };
}

module.exports = extractComments;
export default { extractComments, getLocation };
68 changes: 68 additions & 0 deletions example/Solidity.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
pragma solidity ^0.8.20;

contract WETH9 {
string public name = "Wrapped Ether";
string public symbol = "WETH";
uint8 public decimals = 18;

event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
event Deposit(address indexed dst, uint wad);
event Withdrawal(address indexed src, uint wad);

mapping (address => uint) public balances;
mapping (address => mapping (address => uint)) public allowance;

receive() external payable {
deposit();
}

function deposit() public payable {
balances[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}

function withdraw(uint wad) public {
require(balances[msg.sender] >= wad);
balances[msg.sender] -= wad;
payable(msg.sender).transfer(wad);
emit Withdrawal(msg.sender, wad);
}

function balanceOf(address _owner) public view returns (uint balance) {
return balances[_owner];
}

function totalSupply() public view returns (uint) {
return address(this).balance;
}

function approve(address guy, uint wad) public returns (bool) {
allowance[msg.sender][guy] = wad;
emit Approval(msg.sender, guy, wad);
return true;
}

function transfer(address dst, uint wad) public returns (bool) {
return transferFrom(msg.sender, dst, wad);
}

function transferFrom(address src, address dst, uint wad)
public
returns (bool)
{
require(balances[src] >= wad);

if (src != msg.sender && allowance[src][msg.sender] != type(uint).max) {
require(allowance[src][msg.sender] >= wad);
allowance[src][msg.sender] -= wad;
}

balances[src] -= wad;
balances[dst] += wad;

emit Transfer(src, dst, wad);

return true;
}
}
Loading

0 comments on commit 6cbae7a

Please sign in to comment.