Initial commit

This commit is contained in:
Agatha Lovelace 2020-07-05 12:06:59 +03:00
commit b9c6b52198
No known key found for this signature in database
GPG Key ID: 2DB18BA2E0A80BC3
6 changed files with 96 additions and 0 deletions

9
.editorconfig Normal file
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
.gitignore vendored Normal file
View File

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

5
layouts/en_us.txt Normal file
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
shard.yml Normal file
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
src/keyboard.cr Normal file
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
src/keysmash-analyzer.cr Normal file
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