[Question]
  [
Given an input of a note, output an ASCII drawing of the corresponding major
key on the treble clef.
Here are all the major keys (that don't include double-sharps or double-flats)
and their corresponding key signatures:
![](https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Circle_of_fifths_deluxe_4.svg/600px-Circle_of_fifths_deluxe_4.svg.png)  
*Circle of fifths deluxe 4* by Wikipedia user Just plain Bill, copyright CC BY-SA 3.0
If the input is a key with sharps, draw the following ASCII art with the
appropriate number of sharps:
```
   #
-#-------
      #
----#----
  #
-------#-
     #
---------
---------
```
And if the input is a key with flats:
```
---------
  b
----b----
      b
-b-------
   b
-----b---
       b
---------
```
Here is a concise summary of the possible inputs as well as how many sharps or
flats they use:
```
   0   1   2   3   4   5   6   7
#  C   G   D   A   E   B   F#  C#
b  C   F   Bb  Eb  Ab  Db  Gb  Cb
```
All five lines of dashes must always be drawn, but there may be any number of
dashes per line such that the sharps or flats have at least one column of
padding on each side, as long as each line of dashes has the same length. For
example, these are all also acceptable outputs for the input `Ab`:
```
---------   ------   ---------   ----------------
  b           b           b          b
----b----   ----b-   -------b-   ------b---------
-b-------   -b----   ----b----   ---b------------
   b           b           b          b
---------   ------   ---------   ----------------
---------   ------   ---------   ----------------
```
In the case of C major, which has no sharps nor flats, any positive number of dashes per
line (even one) is acceptable.
Any amount of leading or trailing whitespace is fine, as are trailing spaces on
each line. Extra leading spaces are okay as long as there are the same number
on each line.
You may take input in lowercase or require that inputs without a sharp or flat contain a trailing space, if you so desire.
Since this is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), the shortest solution in bytes will win.
  
[Answer]
# [Python 2](https://docs.python.org/2/), ~~206 197 178 175 168~~ 161 bytes
*Thanks to Mr. Xcoder for -12 bytes!*
This is a function which returns the result as a 2D list of characters. If the input does not contain a sharp/flat, it should be padded with a space.
```
def f(k,r=range(10)):n="CGDAEB".find(k[0])+7*' #'.find(k[1]);return[[(' -'[i%2],'b#'[n>0])[0<j<=abs(n)and`i`==(n*"1403625"+"5263748")[j-1]]for j in r]for i in r]
```
**[Try it online!](https://tio.run/##RU/LboMwELzzFZZRtd6EREBeVRoqtSTtD/TmWgoUkxqiBTnkwNdTnLbKHmZnd2YO0/bdd0PxMBS6ZKWoA5vYjE5aRCHilhKevu9fDq98XhoqRC1DhdPNBJgP/59I4ZPV3dWSlALYDKR5iFUAuQ@Snke/DHfVLsnyiyDMqDiaY5IImvBoGS7W8YpP@SpeLzbLR46ymkVKlY1lFTPE7I2aXzr47ENfOkMn9tUU2vOcVuveqYbaaydw67FxWmuoc8r9EvBJMK8aQwL@9hld/uzSY23dI@LdP8CbDwGDlDnc3zDN4Qc "Python 2 – Try It Online")**
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 60 bytes
```
≔⁻⁺⊖﹪⊗℅§θ¬χ⁷×⁷№θ#×⁷№θbθ←ＵＯ⁹¦⁹-¶Ｆθ«Ｊι⊖﹪⊕×³ι⁷#»Ｆ±θ«Ｊι⊕﹪⁺³×⁴ι⁷b
```
[Try it online!](https://tio.run/##dZBNawJBDIbP7q8Y1ksWRrAoSOtJ9KK46sFjL/sRtwOzE3d2Rgqlv33MupaWoiEE8sGTNyk@MltQpkNYtK2qDKTK@BYOmsMKC4s1GoclpFR6TbAin2tO97ZUJtOwcGtT4ic0UuzIwcs4YZNi1oWjqrGFmRRL8sZ1I/EwTp508r7TJPMopQvC2xZPjpN9rslU8CoFezx6NzEXT2QFNIn4igYbX5@PBEqKB2rX5rfU75xIoe4CmTM4WMX7O1nz6LvH7rDKHDL9H/4v646/PWnyc870ITq/oUNYDsPooq8 "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
≔⁻⁺⊖﹪⊗℅§θ¬χ⁷×⁷№θ#×⁷№θbθ
```
Calculate the number of sharps in the key signature.
```
←ＵＯ⁹¦⁹-¶
```
Print the stave, but one square left of the origin so that the first sharp/flat is in column zero.
```
Ｆθ«
```
Loop over any sharps.
```
Ｊι⊖﹪⊕×³ι⁷#»
```
Calculate the row of the sharp and print it.
```
Ｆ±θ«
```
Loop over any flats.
```
Ｊι⊕﹪⁺³×⁴ι⁷b
```
Calculate the row of the flat and print it.
[Answer]
# Befunge, 139 bytes
```
~9%:4%7*+4/7%~6%1-2/7*+vv\`0:\!-g02+*2g00+1%7+g00*+3g00::<<0+55p02:_@
- b#09p01*-1*2p00:`0:-8<>10g`!**:00g2+*\!20g2%*+1g,1+:8`!#^_>$,:1-\^1
```
[Try it online!](http://befunge.tryitonline.net/#code=fjklOjQlNyorNC83JX42JTEtMi83Kit2dlxgMDpcIS1nMDIrKjJnMDArMSU3K2cwMCorM2cwMDo6PDwwKzU1cDAyOl9ACi0gYiMwOXAwMSotMSoycDAwOmAwOi04PD4xMGdgISoqOjAwZzIrKlwhMjBnMiUqKzFnLDErOjhgISNeXz4kLDoxLVxeMQ&input=QyMK)
Note that the input is expected to be terminated with a linefeed, as would typically occur when running the program interactively.
**Explanation**
We start by reading two characters from stdin - the main note, *n*, and an accidental, *a* (which can be a linefeed if there is no accidental). Using those values, we calculate the key signature number, *signum*, as follows:
```
signum = (n%9 + n%9%4*7)/4%7 + (a%6 - 1)/2*7 - 8
```
This returns a value in the range -7 to 7, where the sign tells us whether we need sharps or flats (positive or negative), and the absolute value gives us the number of sharps or flats required. So for later use, we extract the sign, *s*, and the the accidental count, *cnt*, with:
```
s   = (signum > 0)
cnt = abs(signum) 
```
Then we have two nested loops, iterating a row number, *r*, from 9 down to 0, and a column number, *c*, from 0 up to 8. For a particular row and column, we calculate whether an accidental should be visible at that point with:
```
accidental = (r == (c*(s+3) + s)%7 + 1 + s*2) and (c > 0) and (c <= cnt)
```
If it's not an accidental, we need to output a line or space depending on whether the row, *r*, is odd or even. And if it is an accidental, we need to output a sharp or flat depending on the sign, *s*. So we evaluate the following formula:
```
index = (!accidental * (r%2)) + (accidental * (s+2))
```
Which gives us an index in the range 0 to 3, representing either a line, a space, a flat or a sharp. We simply use that index to lookup the required output character from a table, which you can see embedded at the start of the second line of code.
]