Saturday, December 2, 2017

How to make answer buttons more fitted to touchscreens in Anki.


Anki is probably the best program to memorize things. It has so many settings which help you to streamline the process of learning and also it supports wonderful add-ons which make life simpler. For example, AwesomeTTS allows you to change text into mp3 which you can play in your cards. I could write about advantages of Anki for hours. But it is not the subject of this post. Anki has one really big drawback. The GUI in desktop version (Android version which I'm using seems ok for me). It's ugly, looks old, all buttons are minuscule and hard to use with a touchscreen. Unfortunately, there are no add-ons which solve the problem entirely. But I found the way to make answer buttons to be more comfortable to use on a touchscreen so after that they look like this:

If you are interested how to do this. This article is for you. I will try to explain it to people who don't have experience with programming so buckle up. 

DISCLAIMER:

This method worked on my Windows desktop version of Anki. It is not a professional solution! In fact, it is a very crude solution and it will only work properly with the English language. If you want it to work with other languages you will have to change some values. I'll show later in the article how to do that. There is also a chance that you will have to change some values regardless of using English to achieve the same result as me. Brute copy/paste of all the code below might not work. But if you copy/paste the code highlighted on the green it should work on all systems.

How to do this:

First, you need to install two add-ons from Anki-Web:

1. Bigger Show All Answer Buttons id: 2034935033

It makes buttons wider. And nothing more. I overwrote it to change the size of the buttons in all dimensions.

2. Button Colours (Good, Again) id: 2494384865

This add-on changes colors of a font in buttons. But I modified it to fill buttons with color.


To install add-ons in Anki open Tools/add-ons/Browse&Install and copy/paste aforementioned Id's:
Note that you can see all add-onsns installed in my Anki. If you want you can check them all.

3. Now you have to replace the code of aforementioned add-ons. First open Tools/add-ons/
Bigger_Show_All_Answer_Buttons/Edit:

Then just replace all the code with this bellow (in GEEN I marked lines changed by me from the original version. You can change only those lines wihout replacing the whole code!!!I highly recommend to do so) :

#################################################################################

from __future__ import division
import difflib, re, cgi
import unicodedata as ucd
import HTMLParser
from aqt.qt import *
from anki.utils import stripHTML, isMac, json
from anki.hooks import addHook, runHook
from anki.sound import playFromText, clearAudioQueue, play
from aqt.utils import mungeQA, getBase, openLink, tooltip
from aqt.sound import getAudio
import aqt
from aqt.reviewer import Reviewer

def myShowAnswerButton(self):
self._bottomReady = True
if not self.typeCorrect:
self.bottom.web.setFocus()
middle = '''
<span class=stattxt>%s</span><br>
<button title="%s" id=ansbut style="font-size : 20px; width:300px ; height:34px;" onclick='py.link(\"ans\");'>%s</button>''' % (
self._remaining(), _(""), _("Show Answer"))
# wrap it in a table so it has the same top margin as the ease buttons
middle = "<table cellpadding=0><tr><td class=stat2 align=center>%s</td></tr></table>" % middle
if self.card.shouldShowTimer():
maxTime = self.card.timeLimit() / 1000
else:
maxTime = 0
self.bottom.web.eval("showQuestion(%s,%d);" % (
json.dumps(middle), maxTime))


def myanswerButtons(self):
times = []
default = self._defaultEase()
def but(i, label):
if i == default:
extra = "id=defease"
else:
extra = ""
due = self._buttonTime(i)
return '''
<td align=center>%s<button %s title="%s" style="width:200px ; height:34px; padding: 0; border: none; background: none;" onclick='py.link("ease%d");'>\
%s</button></td>''' % (due, extra, _(""), i, label)
buf = "<center><table cellpading=0 cellspacing=0><tr>"
for ease, label in self._answerButtonList():
buf += but(ease, label)
buf += "</tr></table>"
script = """
<script>$(function () { $("#defease").focus(); });</script>"""
return buf + script

Reviewer._showAnswerButton = myShowAnswerButton
Reviewer._answerButtons = myanswerButtons

#################################################################################


Let's elaborate what I did especially in this line:

<td align=center>%s<button %s title="%s" style="width:200px ; height:34px; padding: 0; border: none; background: none;" onclick='py.link("ease%d");'>\

a. It changes with and the hight of all buttons: " style="width:200px ; height:34px;
b. It removes all settings of the buttons so the buttons are basically transparent: padding: 0; border: none; background: none;


4. Now you have to replace the code of the second add-on. Open Tools/add-ons/
Button_Colours_Good_Again/Edit

Then just replace all the code with this bellow (in GEEN I marked lines changed by me from the original version. You can change only those lines wihout replacing the whole code!!!I highly recommend to do so) :

#################################################################################
# Get reviewer class
from aqt.reviewer import Reviewer

# Replace _answerButtonList method
def answerButtonList(self):
l = ((1, "<font size='6' color='white' style='background-color: red; padding: 10px 75px 10px 75px; outline: inherit' >" + _("Again") + "</font>"),)
cnt = self.mw.col.sched.answerButtons(self.card)
if cnt == 2:
return l + ((2, "<font size='6' color='white' style='background-color: green; padding: 10px 77px 10px 77px; outline: inherit'>" + _("Good") + "</font>"),)
elif cnt == 3:
return l + ((2, "<font size='6' color='white' style='background-color: green; padding: 10px 77px 10px 77px; outline: inherit'>" + _("Good") + "</font>"), (3, "<font size='6' color='white' style='background-color: blue; padding: 10px 81px 10px 81px; outline: inherit'>" + _("Easy")+"</font>"))
else:
return l + ((2,"<font size='6' color='white' style='background-color: orange; padding: 20px 79px 20px 79px; outline: inherit'>" + _("Hard") +"</font>"), (3, "<font size='6' color='white' style='background-color: green; padding: 10px 77px 10px 77px; outline: inherit'>" + _("Good") + "</font>"), (4, "<font size='6' color='white' style='background-color: blue; padding: 20px 81px 20px 81px; outline: inherit'>" +_("Easy") +"</font>"))


Reviewer._answerButtonList = answerButtonList


#################################################################################


As you probably see changes in the code are modifying the look of every single button. Let's analyze one of them, in this case, the "Again" button:

l = ((1, "<font size='6' color='white' style='background-color: red; padding: 10px 75px 10px 75px; outline: inherit' >" + _("Again") + "</font>"),)

a. It sets font to size 6 and color of the font to white: font size='6' color='white' 
b. It sets a color of the background of the button to red because this is the button "Again": background-color: red;
c. It fills the button with color. Note that this value is different in other buttons. And this will probably require form you to change it to be fitted to your version. (If the values are too small the buttons will be asymmetric and if too big the text inside will not be in the center.) : padding: 10px 75px 10px 75px; 

And that is all. Congratulations. You have new beautiful buttons fitted to tablet mode. I hope this article was helpful for you. Just send a comment if you have any suggestions how to improve this article or just give me any other feedback.


Saturday, October 13, 2012

Useful commands in Vim


Vim is text editor based on Vi. It is very powerful program At first look it don't look invitingly but when you start deeper understanding of Vim you can see how siginficantly is speedup of writing. Vim have countless amount of settings which can wriete on ~/.vimrc file and which will be set during the opening as a default. Vim have a dozen work mode. A few the most principal are:
NORMAL - it is default mode to change for it press a <Esc>
INSERT - insert new tekst mode to strat press <i> to start writing in front of cursor and press <a> at the back of cursor. Also you can press <o><O> to start writing in the next line.
COMMAND LINE/Ex - command line mode. To start press a <:> key
VISUAL - visual mode where you can mark tekst. Press <v> to start this mode.
REPLACE - replace mode wehre some chars are replace by other chars. To start this mode press a <R>

Undermentioned commands are merely small piece that what Vim can. They serve of lern the usually using and the most useful. This list will be extend with common service of Vim. Tekst between [] means alternative nameof command. Tekst between <> means key from keyboard.

In NORMAL mode also in VISUAL mode:
<?> or </> searching a string in file. Start it at cusor position and search backwards or ahead.
<$> or <0> go to end or beginning of line.
<gg> or <G> go to begining or end of file.
<w> or <e> go to beginning or end of next word in file.
<b> or <ge> back to beginnig or end of previous word in file.
<number><G> go to linie given as a <nubmer>.
<x> delete char under the cursor.
<d$> delete chars to the ond of line.
<dd> delete entire line.
<de> or <dw> delete entire word without or with space.
<r><char> change char under cursor to given in <char>.
<J> merge to lines which will be separate by space.
<p> put the text from cache memory.
<ga> show ASCII value form char under the cursor.
<u> or <ctr+r> undo and redo changes.(default even last 1000 changes)
<.> redo last command.

In COMMAND LINE/EX mode:

Movement in Ex mode:
To search available commands we can write part of command and press <ctrl+d>
:set i<Ctrl+d>
icon         ignorecase   imsearch     includeexpr  indentexpr   infercase    isfname      iskeyword
iconstring   iminsert     include      incsearch    indentkeys   insertmode   isident      isprint
We can complete our command if is only one posible command and when we press <tab> and  it fill out autonaticly. But it is more fiting commands.Then when we press <tab> scoundly our command will be change.
:se[set] i<tab>
:set icon <tab>
:set ignorecase

To split screen on equal parts we can use:
:split name_of_file
We can also use word vertical to split teh screen vertically:
:vertical split
<ctrl+w><ctrl+w> serve to change a using window.
Except searching commands <tab> we can also searching file names from recent directory:
:split n<tab>
We have to files in recent directory:
name_of_file
name_of_file2
To complete the longest common string we can use:
:split n<ctrl+l>
To establish new screen with determined size :
:15split name_file determine a new screen with open name_file file as a contens with size on 15 lines.
We can also change size of screens already opened.
<number><ctrl+w><_> determine screen on size given in <number>
<ctrl+w><=> align all screens.
To open screen  with contens of recent directory:
:Sex[Sexplore]

It also posible to add tabs using command:
:tabnew path/file_name
To moving between tabs we can use:
:tabn, :tabp, :tabfir, :tablast, :tabn2
We can also open new tans from shell grade:
vim -p fiename1 filename2 it open Vim with two tabs for filename1 and filename2 files.

To simplify searching before using commands we can use:
:history this command show all our history of using commands.
<q:> and in this way you can open menu wehre you can chose command from history. YOu can move tehre like in NORMAL mode.

To make easier to  searching strings charakters we can set:
:set ic set ignore a case-sensitive during the searching.
:set hls[hlsearch] is[incsearch] hls command illumine finding strings and is command show up to date strings during it wiriting.
:set noic it end ignore a case-sensitive.
:nohlsearch it remove illumine finding strings.

To execute commands from shell we can use <:!>. To write it on open file:
:w[write] !{shell command} or !!{shell command} it execute command from shell and write on recent open file.
!! date it put our recent date.

Command responsible for sessions in Vim:
:mksession mysession.vim it write current session with all settings.
:source mysession.vim it read session from file
We can straightaway Vim with setting the session:
vim -S mysesion.vim

To set default settings we have to overwrite ~/.vimrc file:
:edit ~/.vimrc
We can also read example file:
:read $VIMRUNTIME/vimrc_example.vim

To replace one string with another string we use:
:s[substitute]/cow/pig/ it replace string "cow" with string "pig" in recent line and only one strings will be changed.
:s/cow/pig/g it will change all strings in line
:s/cow/pig/gc it will change all strins in line and every time it ask us to confirm removing.
:%s/cow/pig/g it will change all strings in entire file.
We can enforce it to change all strings only in particular place:
21s/cow/pig/g it wiil change all strings in 21 line.
5,21s/cow/pig/g it will change all strings between 5 and 21 line.

Friday, October 5, 2012

Linux and processes


Process is a instance of a computer program that is been executed. Each porcess have assign resources through operating system such as: processor, memory, access to intput-output devices. This resources are destined only for this process and it isn't share with other processes This is featue which distinguish between processes and threads. To manage the processes is responding a kernel. To create a new process is required to execute a system command fork or his derivative. Each processes have in linux:
- process identifier PID
- parent process identifier PPID
- process name CMD
- user name USER
- process piority queue PRI and NI
To display processes working on computer is command ps with:
-a display processes from all users
-x display list of demons porcess
-l display a additional information about porcess

user:~$ ps -ax
  PID          TTY     STAT      TIME COMMAND
    1 ?       Ss       0:01          /sbin/init
    2 ?       S        0:00          [kthreadd]
    3 ?       S        0:00          [ksoftirqd/0]
                   ...
 2459 ?       S        0:00          gnome-pty-helper
 2461 pts/0   Ss       0:00          bash
 2516 pts/0   R+       0:00          ps -ax

To display dinamic real-time view of runing processes is top command. To display ruining procesess as a tree is pstree command.
First  process who is open on linux is init process with PID=1. It is parent for all other procesess which we call them a child process. Scripts are open through init process and are locate in /etc/init.d and they are usually use to booting computer. Process init may by on varying levels of working wehre:
0 - halt the system
1 - single user mode
2-5- multi user modes
6 - reboot the sytem
To check levels of working we execute command who -r.

user:~$ who -r
run-level 2  2012-10-06 18:44

Init process is also demon process because it don't need interaction with human and run as a backgound peocess. Tradicionally demon names end with letter 'd' for example: ftpd, httpd and so on. To comunication with running process is command kill which send signals. To check what kinds of signals we can send we use kill -l. More ofen than not are ues:
-1  IGHUP read in again configuration file without terninate the process
-2  SIGTERM is default signal and it's sending requst to a process to request its termination and let the process to perform  appropriate termination.
-9  SIGKILL cause terninate immediately
-19 SIGSTOP stop a process for later resumption
-18 SIGCONT restart a process previously paused
To send signals to group of processes with the same name is killall command.

user:~$ sleep 100 &
[1] 2546
user:~$ sleep 200 &
[2] 2547
user :~$ killall sleep
[1]-  Zakończony             sleep 100
[2]+  Zakończony             sleep 200

Zombie proces is that which has compled execution but still has entry in the process table because its parent don't execute its child's exit status.
To change the  process priority we use renice command. To open the program with determining priority process we use nice comand. This commands change NI value which can impact on PRI value. When  NI have less value then have bigger priority.

nice -10 k-meleon

To open any process in the background we can add & on the end of command. Owing to & during working  process in the background we back immediately to the command line.To display process  in the background we use jobs command.

user@K52Je:~$ jobs
[1]-  Running                 sleep 200 &
[2]+  Running                 sleep 100 &

To bring the process back in the foreground we use fg command. To suspend a process we klick Ctrl+Z. We can move back in the bacground using bg command or in the  foregroud using fg command.

user@K52Je:~$ fg 2
sleep 100