Browse Source

Initial commit

master
Agatha 10 months ago
commit
b9c6b52198
Signed by: sorceress <agatharose@wantscuddl.es> GPG Key ID: 2DB18BA2E0A80BC3
6 changed files with 96 additions and 0 deletions
  1. +9
    -0
      .editorconfig
  2. +5
    -0
      .gitignore
  3. +5
    -0
      layouts/en_us.txt
  4. +13
    -0
      shard.yml
  5. +42
    -0
      src/keyboard.cr
  6. +22
    -0
      src/keysmash-analyzer.cr

+ 9
- 0
.editorconfig View File

@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

+ 5
- 0
layouts/en_us.txt View File

@@ -0,0 +1,5 @@
[`~] [1!][2@][3#][4$][5%][6^][7&][8*][9(][0)][-_][=+] |<-|
|tab| [q][w][e][r][t][y][u][i][o][p][\[{][\]}][\\|]
|caps| [a][s][d][f][g][h][j][k][l][;:]['"] |enter|
|shift| [z][x][c][v][b][n][m][,<][.>][/?] |shift|
|ctrl| |sys| |alt| [ ] |alt| |fn| |ctrl|

+ 13
- 0
shard.yml View File

@@ -0,0 +1,13 @@
name: keysmash-analyzer
version: 0.1.0

authors:
- Agatha Rose <agatharose@wantscuddl.es>

targets:
keysmash-analyzer:
main: src/keysmash-analyzer.cr

crystal: 0.35.1

license: MIT

+ 42
- 0
src/keyboard.cr View File

@@ -0,0 +1,42 @@
class Keyboard
property layout
property raw_layout : String
# path to layout file
setter name : String

def initialize(@name)
# character and frequency
@layout = {} of Char => Int32
@raw_layout = File.read(name)
i = 0
c = @raw_layout.chars
# iterates over characters in layout
while i < c.size
# outputs a hashmap of characters inside [] in the layout and sets each to 0 uses
if c[i] == '['
until c[i+1] == ']'
# character escape
if c[i+1] == '\\'
i += 1
end
@layout[c[i += 1]] = 0
end
end
i += 1
end
end

def to_s(io : IO)
# remove character escapes
formatted = @raw_layout.gsub(/\\(\S)/, &.[1])
# defines heatmap colors
colors = %i(default light_yellow yellow light_red red)
# add a keypress heatmap
max = @layout.to_a.sort_by { |k, v| v }.reverse.to_h.values[0]
formatted = formatted.gsub(/\[(.{1,2})\]/) {|m|
# calculate proportion of key usage to colors
m.colorize.fore(:white).back(colors[((@layout[m.chars[1]] * (colors.size - 1)) / max).to_i])
}
io << formatted
end
end

+ 22
- 0
src/keysmash-analyzer.cr View File

@@ -0,0 +1,22 @@
require "./keyboard.cr"
require "colorize"

module KeysmashAnalyzer
VERSION = "0.1.0"
# uses ANSI escape codes to suggest default layout
print "Please enter path to a layout: \x1b[s#{"layouts/en_us.txt".colorize(:dark_gray)}\x1b[u"
input = gets
# fall back to en_us.txt if no input is provided
input = "layouts/en_us.txt" if input.nil? || input.blank?
keyboard = Keyboard.new input
print "Please keysmash into your terminal: "
keysmash = gets
exit if keysmash.nil?
keysmash = keysmash.downcase
# set usage frequency for each char
keysmash.chars.each do |c|
keyboard.layout[c] += 1
end
# print the heatmap
puts keyboard
end

Loading…
Cancel
Save